write_mongodb plugin: Actually store time as "date" type.
[collectd.git] / src / write_mongodb.c
1 /**
2  * collectd - src/write_mongodb.c
3  * Copyright (C) 2010-2012  Florian Forster
4  * Copyright (C) 2010       Akkarit Sangpetch
5  * Copyright (C) 2012       Chris Lundquist
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *   Florian Forster <ff at octo.it>
27  *   Akkarit Sangpetch <asangpet at andrew.cmu.edu>
28  *   Chris Lundquist <clundquist at bluebox.net>
29  **/
30
31 #include "collectd.h"
32 #include "plugin.h"
33 #include "common.h"
34 #include "configfile.h"
35 #include "utils_cache.h"
36
37 #include <pthread.h>
38
39 #if HAVE_STDINT_H
40 # define MONGO_HAVE_STDINT 1
41 #else
42 # define MONGO_USE_LONG_LONG_INT 1
43 #endif
44 #include <mongo.h>
45
46 struct wm_node_s
47 {
48   char name[DATA_MAX_NAME_LEN];
49
50   char *host;
51   int port;
52   int timeout;
53
54   int connected;
55
56   _Bool store_rates;
57
58   mongo conn[1];
59   pthread_mutex_t lock;
60 };
61 typedef struct wm_node_s wm_node_t;
62
63 /*
64  * Functions
65  */
66 static int wm_write (const data_set_t *ds, /* {{{ */
67     const value_list_t *vl,
68     user_data_t *ud)
69 {
70   wm_node_t *node = ud->data;
71   char collection_name[512];
72   int status;
73   int i;
74   bson record;
75
76   gauge_t *rates = NULL;
77   if (node->store_rates)
78   {
79     rates = uc_get_rate (ds, vl);
80     if (rates == NULL)
81     {
82       ERROR ("write_mongodb plugin: uc_get_rate() failed.");
83       return (-1);
84     }
85   }
86
87   ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
88
89   bson_init(&record);
90   bson_append_date (&record, "time", (bson_date_t) CDTIME_T_TO_MS (vl->time));
91   bson_append_string (&record, "host", vl->host);
92   bson_append_string (&record, "plugin_instance", vl->plugin_instance);
93   bson_append_string (&record, "type", vl->type);
94   bson_append_string (&record, "type_instance", vl->type_instance);
95
96   for (i = 0; i < ds->ds_num; i++)
97   {
98     if (ds->ds[i].type == DS_TYPE_GAUGE)
99       bson_append_double(&record, ds->ds[i].name, vl->values[i].gauge);
100     else if (node->store_rates)
101       bson_append_double(&record, ds->ds[i].name, (double) rates[i]);
102     else if (ds->ds[i].type == DS_TYPE_COUNTER)
103       bson_append_long(&record, ds->ds[i].name, vl->values[i].counter);
104     else if (ds->ds[i].type == DS_TYPE_DERIVE)
105       bson_append_long(&record, ds->ds[i].name, vl->values[i].derive);
106     else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
107       bson_append_long(&record, ds->ds[i].name, vl->values[i].absolute);
108     else
109       assert (23 == 42);
110   }
111   /* We must finish the record, other wise the insert will fail */
112   bson_finish(&record);
113
114   sfree (rates);
115
116   pthread_mutex_lock (&node->lock);
117
118   if (node->connected == 0)
119   {
120     status = mongo_connect(node->conn, node->host, node->port);
121     if (status != MONGO_OK) {
122       ERROR ("write_mongodb plugin: Connecting to host \"%s\" (port %i) failed.",
123           (node->host != NULL) ? node->host : "localhost",
124           (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
125       mongo_destroy(node->conn);
126       pthread_mutex_unlock (&node->lock);
127       return (-1);
128     }
129
130     if (node->timeout > 0) {
131       status = mongo_set_op_timeout (node->conn, node->timeout);
132       if (status != MONGO_OK) {
133         WARNING ("write_mongodb plugin: mongo_set_op_timeout(%i) failed: %s",
134             node->timeout, node->conn->errstr);
135       }
136     }
137
138     node->connected = 1;
139   }
140
141   /* Assert if the connection has been established */
142   assert (node->connected == 1);
143
144   DEBUG ( "write_mongodb plugin: writing record");
145   /* bson_print(&record); */
146
147   status = mongo_insert(node->conn,collection_name,&record);
148
149   if(status != MONGO_OK)
150   {
151     ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
152     if (node->conn->err == MONGO_BSON_INVALID)
153       ERROR ("write_mongodb plugin: %s", node->conn->errstr);
154     else if (record.err)
155       ERROR ("write_mongodb plugin: %s", record.errstr);
156   }
157
158   pthread_mutex_unlock (&node->lock);
159   /* free our resource as not to leak memory */
160   bson_destroy(&record);
161
162   return (0);
163 } /* }}} int wm_write */
164
165 static void wm_config_free (void *ptr) /* {{{ */
166 {
167   wm_node_t *node = ptr;
168
169   if (node == NULL)
170     return;
171
172   if (node->connected != 0)
173   {
174     mongo_destroy(node->conn);
175     node->connected = 0;
176   }
177
178   sfree (node->host);
179   sfree (node);
180 } /* }}} void wm_config_free */
181
182 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
183 {
184   wm_node_t *node;
185   int status;
186   int i;
187
188   node = malloc (sizeof (*node));
189   if (node == NULL)
190     return (ENOMEM);
191   memset (node, 0, sizeof (*node));
192   node->host = NULL;
193   node->store_rates = 1;
194   pthread_mutex_init (&node->lock, /* attr = */ NULL);
195
196   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
197
198   if (status != 0)
199   {
200     sfree (node);
201     return (status);
202   }
203
204   for (i = 0; i < ci->children_num; i++)
205   {
206     oconfig_item_t *child = ci->children + i;
207
208     if (strcasecmp ("Host", child->key) == 0)
209       status = cf_util_get_string (child, &node->host);
210     else if (strcasecmp ("Port", child->key) == 0)
211     {
212       status = cf_util_get_port_number (child);
213       if (status > 0)
214       {
215         node->port = status;
216         status = 0;
217       }
218     }
219     else if (strcasecmp ("Timeout", child->key) == 0)
220       status = cf_util_get_int (child, &node->timeout);
221     else if (strcasecmp ("StoreRates", child->key) == 0)
222       status = cf_util_get_boolean (child, &node->store_rates);
223     else
224       WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
225           child->key);
226
227     if (status != 0)
228       break;
229   } /* for (i = 0; i < ci->children_num; i++) */
230
231   if (status == 0)
232   {
233     char cb_name[DATA_MAX_NAME_LEN];
234     user_data_t ud;
235
236     ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name);
237
238     ud.data = node;
239     ud.free_func = wm_config_free;
240
241     status = plugin_register_write (cb_name, wm_write, &ud);
242     INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status);
243   }
244
245   if (status != 0)
246     wm_config_free (node);
247
248   return (status);
249 } /* }}} int wm_config_node */
250
251 static int wm_config (oconfig_item_t *ci) /* {{{ */
252 {
253   int i;
254
255   for (i = 0; i < ci->children_num; i++)
256   {
257     oconfig_item_t *child = ci->children + i;
258
259     if (strcasecmp ("Node", child->key) == 0)
260       wm_config_node (child);
261     else
262       WARNING ("write_mongodb plugin: Ignoring unknown "
263           "configuration option \"%s\" at top level.", child->key);
264   }
265
266   return (0);
267 } /* }}} int wm_config */
268
269 void module_register (void)
270 {
271   plugin_register_complex_config ("write_mongodb", wm_config);
272 }
273
274 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */