Code

Merge remote-tracking branch 'github/pr/387'
[collectd.git] / src / write_mongodb.c
1 /**
2  * collectd - src/write_mongodb.c
3  * Copyright (C) 2010-2013  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 <octo at collectd.org>
27  *   Akkarit Sangpetch <asangpet at andrew.cmu.edu>
28  *   Chris Lundquist <clundquist at bluebox.net>
29  **/
31 #include "collectd.h"
32 #include "plugin.h"
33 #include "common.h"
34 #include "configfile.h"
35 #include "utils_cache.h"
37 #include <pthread.h>
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>
46 struct wm_node_s
47 {
48   char name[DATA_MAX_NAME_LEN];
50   char *host;
51   int port;
52   int timeout;
54   /* Authentication information */
55   char *db;
56   char *user;
57   char *passwd;
59   _Bool store_rates;
61   mongo conn[1];
62   pthread_mutex_t lock;
63 };
64 typedef struct wm_node_s wm_node_t;
66 /*
67  * Functions
68  */
69 static bson *wm_create_bson (const data_set_t *ds, /* {{{ */
70     const value_list_t *vl,
71     _Bool store_rates)
72 {
73   bson *ret;
74   gauge_t *rates;
75   int i;
77   ret = bson_create ();
78   if (ret == NULL)
79   {
80     ERROR ("write_mongodb plugin: bson_create failed.");
81     return (NULL);
82   }
84   if (store_rates)
85   {
86     rates = uc_get_rate (ds, vl);
87     if (rates == NULL)
88     {
89       ERROR ("write_mongodb plugin: uc_get_rate() failed.");
90       return (NULL);
91     }
92   }
93   else
94   {
95     rates = NULL;
96   }
98   bson_init (ret);
99   bson_append_date (ret, "time", (bson_date_t) CDTIME_T_TO_MS (vl->time));
100   bson_append_string (ret, "host", vl->host);
101   bson_append_string (ret, "plugin", vl->plugin);
102   bson_append_string (ret, "plugin_instance", vl->plugin_instance);
103   bson_append_string (ret, "type", vl->type);
104   bson_append_string (ret, "type_instance", vl->type_instance);
106   bson_append_start_array (ret, "values"); /* {{{ */
107   for (i = 0; i < ds->ds_num; i++)
108   {
109     char key[16];
111     ssnprintf (key, sizeof (key), "%i", i);
113     if (ds->ds[i].type == DS_TYPE_GAUGE)
114       bson_append_double(ret, key, vl->values[i].gauge);
115     else if (store_rates)
116       bson_append_double(ret, key, (double) rates[i]);
117     else if (ds->ds[i].type == DS_TYPE_COUNTER)
118       bson_append_long(ret, key, vl->values[i].counter);
119     else if (ds->ds[i].type == DS_TYPE_DERIVE)
120       bson_append_long(ret, key, vl->values[i].derive);
121     else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
122       bson_append_long(ret, key, vl->values[i].absolute);
123     else
124       assert (23 == 42);
125   }
126   bson_append_finish_array (ret); /* }}} values */
128   bson_append_start_array (ret, "dstypes"); /* {{{ */
129   for (i = 0; i < ds->ds_num; i++)
130   {
131     char key[16];
133     ssnprintf (key, sizeof (key), "%i", i);
135     if (store_rates)
136       bson_append_string (ret, key, "gauge");
137     else
138       bson_append_string (ret, key, DS_TYPE_TO_STRING (ds->ds[i].type));
139   }
140   bson_append_finish_array (ret); /* }}} dstypes */
142   bson_append_start_array (ret, "dsnames"); /* {{{ */
143   for (i = 0; i < ds->ds_num; i++)
144   {
145     char key[16];
147     ssnprintf (key, sizeof (key), "%i", i);
148     bson_append_string (ret, key, ds->ds[i].name);
149   }
150   bson_append_finish_array (ret); /* }}} dsnames */
152   bson_finish (ret);
154   sfree (rates);
155   return (ret);
156 } /* }}} bson *wm_create_bson */
158 static int wm_write (const data_set_t *ds, /* {{{ */
159     const value_list_t *vl,
160     user_data_t *ud)
162   wm_node_t *node = ud->data;
163   char collection_name[512];
164   bson *bson_record;
165   int status;
167   ssnprintf (collection_name, sizeof (collection_name), "collectd.%s",
168       vl->plugin);
170   bson_record = wm_create_bson (ds, vl, node->store_rates);
171   if (bson_record == NULL)
172     return (ENOMEM);
174   pthread_mutex_lock (&node->lock);
176   if (!mongo_is_connected (node->conn))
177   {
178     INFO ("write_mongodb plugin: Connecting to [%s]:%i",
179         (node->host != NULL) ? node->host : "localhost",
180         (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
181     status = mongo_connect (node->conn, node->host, node->port);
182     if (status != MONGO_OK) {
183       ERROR ("write_mongodb plugin: Connecting to [%s]:%i failed.",
184           (node->host != NULL) ? node->host : "localhost",
185           (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
186       mongo_destroy (node->conn);
187       pthread_mutex_unlock (&node->lock);
188       return (-1);
189     }
191     if ((node->db != NULL) && (node->user != NULL) && (node->passwd != NULL))
192     {
193       status = mongo_cmd_authenticate (node->conn,
194           node->db, node->user, node->passwd);
195       if (status != MONGO_OK)
196       {
197         ERROR ("write_mongodb plugin: Authenticating to [%s]%i for database "
198             "\"%s\" as user \"%s\" failed.",
199           (node->host != NULL) ? node->host : "localhost",
200           (node->port != 0) ? node->port : MONGO_DEFAULT_PORT,
201           node->db, node->user);
202         mongo_destroy (node->conn);
203         pthread_mutex_unlock (&node->lock);
204         return (-1);
205       }
206     }
208     if (node->timeout > 0) {
209       status = mongo_set_op_timeout (node->conn, node->timeout);
210       if (status != MONGO_OK) {
211         WARNING ("write_mongodb plugin: mongo_set_op_timeout(%i) failed: %s",
212             node->timeout, node->conn->errstr);
213       }
214     }
215   }
217   /* Assert if the connection has been established */
218   assert (mongo_is_connected (node->conn));
220   #if MONGO_MINOR >= 6
221     /* There was an API change in 0.6.0 as linked below */
222     /* https://github.com/mongodb/mongo-c-driver/blob/master/HISTORY.md */
223     status = mongo_insert (node->conn, collection_name, bson_record, NULL);
224   #else
225     status = mongo_insert (node->conn, collection_name, bson_record);
226   #endif
228   if (status != MONGO_OK)
229   {
230     ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
231     if (node->conn->err != MONGO_BSON_INVALID)
232       ERROR ("write_mongodb plugin: %s", node->conn->errstr);
233     else
234       ERROR ("write_mongodb plugin: Invalid BSON structure, error = %#x",
235           (unsigned int) bson_record->err);
237     /* Disconnect except on data errors. */
238     if ((node->conn->err != MONGO_BSON_INVALID)
239         && (node->conn->err != MONGO_BSON_NOT_FINISHED))
240       mongo_destroy (node->conn);
241   }
243   pthread_mutex_unlock (&node->lock);
245   /* free our resource as not to leak memory */
246   bson_dispose (bson_record);
248   return (0);
249 } /* }}} int wm_write */
251 static void wm_config_free (void *ptr) /* {{{ */
253   wm_node_t *node = ptr;
255   if (node == NULL)
256     return;
258   if (mongo_is_connected (node->conn))
259     mongo_destroy (node->conn);
261   sfree (node->host);
262   sfree (node);
263 } /* }}} void wm_config_free */
265 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
267   wm_node_t *node;
268   int status;
269   int i;
271   node = malloc (sizeof (*node));
272   if (node == NULL)
273     return (ENOMEM);
274   memset (node, 0, sizeof (*node));
275   mongo_init (node->conn);
276   node->host = NULL;
277   node->store_rates = 1;
278   pthread_mutex_init (&node->lock, /* attr = */ NULL);
280   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
282   if (status != 0)
283   {
284     sfree (node);
285     return (status);
286   }
288   for (i = 0; i < ci->children_num; i++)
289   {
290     oconfig_item_t *child = ci->children + i;
292     if (strcasecmp ("Host", child->key) == 0)
293       status = cf_util_get_string (child, &node->host);
294     else if (strcasecmp ("Port", child->key) == 0)
295     {
296       status = cf_util_get_port_number (child);
297       if (status > 0)
298       {
299         node->port = status;
300         status = 0;
301       }
302     }
303     else if (strcasecmp ("Timeout", child->key) == 0)
304       status = cf_util_get_int (child, &node->timeout);
305     else if (strcasecmp ("StoreRates", child->key) == 0)
306       status = cf_util_get_boolean (child, &node->store_rates);
307     else if (strcasecmp ("Database", child->key) == 0)
308       status = cf_util_get_string (child, &node->db);
309     else if (strcasecmp ("User", child->key) == 0)
310       status = cf_util_get_string (child, &node->user);
311     else if (strcasecmp ("Password", child->key) == 0)
312       status = cf_util_get_string (child, &node->passwd);
313     else
314       WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
315           child->key);
317     if (status != 0)
318       break;
319   } /* for (i = 0; i < ci->children_num; i++) */
321   if ((node->db != NULL) || (node->user != NULL) || (node->passwd != NULL))
322   {
323     if ((node->db == NULL) || (node->user == NULL) || (node->passwd == NULL))
324     {
325       WARNING ("write_mongodb plugin: Authentication requires the "
326           "\"Database\", \"User\" and \"Password\" options to be specified, "
327           "but at last one of them is missing. Authentication will NOT be "
328           "used.");
329       sfree (node->db);
330       sfree (node->user);
331       sfree (node->passwd);
332     }
333   }
335   if (status == 0)
336   {
337     char cb_name[DATA_MAX_NAME_LEN];
338     user_data_t ud;
340     ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name);
342     ud.data = node;
343     ud.free_func = wm_config_free;
345     status = plugin_register_write (cb_name, wm_write, &ud);
346     INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status);
347   }
349   if (status != 0)
350     wm_config_free (node);
352   return (status);
353 } /* }}} int wm_config_node */
355 static int wm_config (oconfig_item_t *ci) /* {{{ */
357   int i;
359   for (i = 0; i < ci->children_num; i++)
360   {
361     oconfig_item_t *child = ci->children + i;
363     if (strcasecmp ("Node", child->key) == 0)
364       wm_config_node (child);
365     else
366       WARNING ("write_mongodb plugin: Ignoring unknown "
367           "configuration option \"%s\" at top level.", child->key);
368   }
370   return (0);
371 } /* }}} int wm_config */
373 void module_register (void)
375   plugin_register_complex_config ("write_mongodb", wm_config);
378 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */