8d76b608affa4c58d1a3c23cfbeda4f16cc8f5bc
[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   _Bool store_rates;
55
56   mongo conn[1];
57   pthread_mutex_t lock;
58 };
59 typedef struct wm_node_s wm_node_t;
60
61 /*
62  * Functions
63  */
64 static bson *wm_create_bson (const data_set_t *ds, /* {{{ */
65     const value_list_t *vl,
66     _Bool store_rates)
67 {
68   bson *ret;
69   gauge_t *rates;
70   int i;
71
72   ret = bson_create ();
73   if (ret == NULL)
74   {
75     ERROR ("write_mongodb plugin: bson_create failed.");
76     return (NULL);
77   }
78
79   if (store_rates)
80   {
81     rates = uc_get_rate (ds, vl);
82     if (rates == NULL)
83     {
84       ERROR ("write_mongodb plugin: uc_get_rate() failed.");
85       return (NULL);
86     }
87   }
88   else
89   {
90     rates = NULL;
91   }
92
93   bson_init (ret);
94   bson_append_date (ret, "time", (bson_date_t) CDTIME_T_TO_MS (vl->time));
95   bson_append_string (ret, "host", vl->host);
96   bson_append_string (ret, "plugin", vl->plugin);
97   bson_append_string (ret, "plugin_instance", vl->plugin_instance);
98   bson_append_string (ret, "type", vl->type);
99   bson_append_string (ret, "type_instance", vl->type_instance);
100
101   bson_append_start_array (ret, "values"); /* {{{ */
102   for (i = 0; i < ds->ds_num; i++)
103   {
104     char key[16];
105
106     ssnprintf (key, sizeof (key), "%i", i);
107
108     if (ds->ds[i].type == DS_TYPE_GAUGE)
109       bson_append_double(ret, key, vl->values[i].gauge);
110     else if (store_rates)
111       bson_append_double(ret, key, (double) rates[i]);
112     else if (ds->ds[i].type == DS_TYPE_COUNTER)
113       bson_append_long(ret, key, vl->values[i].counter);
114     else if (ds->ds[i].type == DS_TYPE_DERIVE)
115       bson_append_long(ret, key, vl->values[i].derive);
116     else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
117       bson_append_long(ret, key, vl->values[i].absolute);
118     else
119       assert (23 == 42);
120   }
121   bson_append_finish_array (ret); /* }}} values */
122
123   bson_append_start_array (ret, "dstypes"); /* {{{ */
124   for (i = 0; i < ds->ds_num; i++)
125   {
126     char key[16];
127
128     ssnprintf (key, sizeof (key), "%i", i);
129
130     if (store_rates)
131       bson_append_string (ret, key, "gauge");
132     else
133       bson_append_string (ret, key, DS_TYPE_TO_STRING (ds->ds[i].type));
134   }
135   bson_append_finish_array (ret); /* }}} dstypes */
136
137   bson_append_start_array (ret, "dsnames"); /* {{{ */
138   for (i = 0; i < ds->ds_num; i++)
139   {
140     char key[16];
141
142     ssnprintf (key, sizeof (key), "%i", i);
143     bson_append_string (ret, key, ds->ds[i].name);
144   }
145   bson_append_finish_array (ret); /* }}} dsnames */
146
147   bson_finish (ret);
148
149   sfree (rates);
150   return (ret);
151 } /* }}} bson *wm_create_bson */
152
153 static int wm_write (const data_set_t *ds, /* {{{ */
154     const value_list_t *vl,
155     user_data_t *ud)
156 {
157   wm_node_t *node = ud->data;
158   char collection_name[512];
159   bson *bson_record;
160   int status;
161
162   ssnprintf (collection_name, sizeof (collection_name), "collectd.%s",
163       vl->plugin);
164
165   bson_record = wm_create_bson (ds, vl, node->store_rates);
166   if (bson_record == NULL)
167     return (ENOMEM);
168
169   pthread_mutex_lock (&node->lock);
170
171   if (!mongo_is_connected (node->conn))
172   {
173     INFO ("write_mongodb plugin: Connecting to [%s]:%i",
174         (node->host != NULL) ? node->host : "localhost",
175         (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
176     status = mongo_connect (node->conn, node->host, node->port);
177     if (status != MONGO_OK) {
178       ERROR ("write_mongodb plugin: Connecting to [%s]:%i failed.",
179           (node->host != NULL) ? node->host : "localhost",
180           (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
181       mongo_destroy (node->conn);
182       pthread_mutex_unlock (&node->lock);
183       return (-1);
184     }
185
186     if (node->timeout > 0) {
187       status = mongo_set_op_timeout (node->conn, node->timeout);
188       if (status != MONGO_OK) {
189         WARNING ("write_mongodb plugin: mongo_set_op_timeout(%i) failed: %s",
190             node->timeout, node->conn->errstr);
191       }
192     }
193   }
194
195   /* Assert if the connection has been established */
196   assert (mongo_is_connected (node->conn));
197
198   status = mongo_insert (node->conn, collection_name, bson_record);
199   if(status != MONGO_OK)
200   {
201     ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
202     if (node->conn->err != MONGO_BSON_INVALID)
203       ERROR ("write_mongodb plugin: %s", node->conn->errstr);
204     else if (bson_record->err)
205       ERROR ("write_mongodb plugin: %s", bson_record->errstr);
206
207     /* Disconnect except on data errors. */
208     if ((node->conn->err != MONGO_BSON_INVALID)
209         && (node->conn->err != MONGO_BSON_NOT_FINISHED))
210       mongo_destroy (node->conn);
211   }
212
213   pthread_mutex_unlock (&node->lock);
214
215   /* free our resource as not to leak memory */
216   bson_dispose (bson_record);
217
218   return (0);
219 } /* }}} int wm_write */
220
221 static void wm_config_free (void *ptr) /* {{{ */
222 {
223   wm_node_t *node = ptr;
224
225   if (node == NULL)
226     return;
227
228   if (mongo_is_connected (node->conn))
229     mongo_destroy (node->conn);
230
231   sfree (node->host);
232   sfree (node);
233 } /* }}} void wm_config_free */
234
235 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
236 {
237   wm_node_t *node;
238   int status;
239   int i;
240
241   node = malloc (sizeof (*node));
242   if (node == NULL)
243     return (ENOMEM);
244   memset (node, 0, sizeof (*node));
245   mongo_init (node->conn);
246   node->host = NULL;
247   node->store_rates = 1;
248   pthread_mutex_init (&node->lock, /* attr = */ NULL);
249
250   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
251
252   if (status != 0)
253   {
254     sfree (node);
255     return (status);
256   }
257
258   for (i = 0; i < ci->children_num; i++)
259   {
260     oconfig_item_t *child = ci->children + i;
261
262     if (strcasecmp ("Host", child->key) == 0)
263       status = cf_util_get_string (child, &node->host);
264     else if (strcasecmp ("Port", child->key) == 0)
265     {
266       status = cf_util_get_port_number (child);
267       if (status > 0)
268       {
269         node->port = status;
270         status = 0;
271       }
272     }
273     else if (strcasecmp ("Timeout", child->key) == 0)
274       status = cf_util_get_int (child, &node->timeout);
275     else if (strcasecmp ("StoreRates", child->key) == 0)
276       status = cf_util_get_boolean (child, &node->store_rates);
277     else
278       WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
279           child->key);
280
281     if (status != 0)
282       break;
283   } /* for (i = 0; i < ci->children_num; i++) */
284
285   if (status == 0)
286   {
287     char cb_name[DATA_MAX_NAME_LEN];
288     user_data_t ud;
289
290     ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name);
291
292     ud.data = node;
293     ud.free_func = wm_config_free;
294
295     status = plugin_register_write (cb_name, wm_write, &ud);
296     INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status);
297   }
298
299   if (status != 0)
300     wm_config_free (node);
301
302   return (status);
303 } /* }}} int wm_config_node */
304
305 static int wm_config (oconfig_item_t *ci) /* {{{ */
306 {
307   int i;
308
309   for (i = 0; i < ci->children_num; i++)
310   {
311     oconfig_item_t *child = ci->children + i;
312
313     if (strcasecmp ("Node", child->key) == 0)
314       wm_config_node (child);
315     else
316       WARNING ("write_mongodb plugin: Ignoring unknown "
317           "configuration option \"%s\" at top level.", child->key);
318   }
319
320   return (0);
321 } /* }}} int wm_config */
322
323 void module_register (void)
324 {
325   plugin_register_complex_config ("write_mongodb", wm_config);
326 }
327
328 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */