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