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