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