2 * collectd - src/write_mongodb.c
3 * Copyright (C) 2010 Florian Forster, Akkarit Sangpetch
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian Forster <ff at octo.it>
25 * Akkarit Sangpetch <asangpet@andrew.cmu.edu>
31 #include "configfile.h"
36 # define MONGO_HAVE_STDINT 1
38 # define MONGO_USE_LONG_LONG_INT 1
49 typedef struct mongo_options mongo_options;
54 char name[DATA_MAX_NAME_LEN];
63 /* mongo_options opts[1]; */
66 typedef struct wm_node_s wm_node_t;
71 static int wm_write (const data_set_t *ds, /* {{{ */
72 const value_list_t *vl,
75 wm_node_t *node = ud->data;
76 char collection_name[512];
80 /*bson_data record_buf; */
82 ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
85 bson_append_time_t(&record,"ts",vl->time);
86 bson_append_string(&record,"h",vl->host);
87 bson_append_string(&record,"i",vl->plugin_instance);
88 bson_append_string(&record,"t",vl->type);
89 bson_append_string(&record,"ti",vl->type_instance);
91 for (i = 0; i < ds->ds_num; i++)
93 if (ds->ds[i].type == DS_TYPE_COUNTER)
94 bson_append_long(&record, ds->ds[i].name, vl->values[i].counter);
95 else if (ds->ds[i].type == DS_TYPE_GAUGE)
96 bson_append_double(&record, ds->ds[i].name, vl->values[i].gauge);
97 else if (ds->ds[i].type == DS_TYPE_DERIVE)
98 bson_append_long(&record, ds->ds[i].name, vl->values[i].derive);
99 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
100 bson_append_long(&record, ds->ds[i].name, vl->values[i].absolute);
106 pthread_mutex_lock (&node->lock);
108 if (node->connected == 0)
111 sstrncpy(node->opts->host, node->host,
112 sizeof (node->opts->host));
113 node->opts->port = node->port;
116 /* status = mongo_connect(node->conn,node->opts->host, node->opts->port);*/
117 status = mongo_connect(node->conn, node->host, node->port);
118 if (status != MONGO_OK) {
119 ERROR ("write_mongodb plugin: Connecting to host \"%s\" (port %i) failed.",
120 (node->host != NULL) ? node->host : "localhost",
121 (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
122 mongo_destroy(node->conn);
123 pthread_mutex_unlock (&node->lock);
130 /* Assert if the connection has been established */
131 assert (node->connected == 1);
133 DEBUG ( "write_mongodb plugin: writing record");
134 /* bson_print(&record); */
136 status = mongo_insert(node->conn,collection_name,&record);
138 if(status != MONGO_OK)
140 ERROR ( "write_mongodb plugin: error inserting record: ");
141 if(node->conn->err == MONGO_BSON_INVALID)
143 ERROR (record.errstr);
148 pthread_mutex_unlock (&node->lock);
151 } /* }}} int wm_write */
153 static void wm_config_free (void *ptr) /* {{{ */
155 wm_node_t *node = ptr;
160 if (node->connected != 0)
162 mongo_destroy(node->conn);
168 } /* }}} void wm_config_free */
170 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
176 node = malloc (sizeof (*node));
179 memset (node, 0, sizeof (*node));
182 node->timeout = 1000;
184 pthread_mutex_init (&node->lock, /* attr = */ NULL);
186 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
194 for (i = 0; i < ci->children_num; i++)
196 oconfig_item_t *child = ci->children + i;
198 if (strcasecmp ("Host", child->key) == 0)
199 status = cf_util_get_string (child, &node->host);
200 else if (strcasecmp ("Port", child->key) == 0)
202 status = cf_util_get_port_number (child);
209 else if (strcasecmp ("Timeout", child->key) == 0)
210 status = cf_util_get_int (child, &node->timeout);
212 WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
217 } /* for (i = 0; i < ci->children_num; i++) */
221 char cb_name[DATA_MAX_NAME_LEN];
224 ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name);
227 ud.free_func = wm_config_free;
229 status = plugin_register_write (cb_name, wm_write, &ud);
230 INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status);
234 wm_config_free (node);
237 } /* }}} int wm_config_node */
239 static int wm_config (oconfig_item_t *ci) /* {{{ */
243 for (i = 0; i < ci->children_num; i++)
245 oconfig_item_t *child = ci->children + i;
247 if (strcasecmp ("Node", child->key) == 0)
248 wm_config_node (child);
250 WARNING ("write_mongodb plugin: Ignoring unknown "
251 "configuration option \"%s\" at top level.", child->key);
255 } /* }}} int wm_config */
257 void module_register (void)
259 plugin_register_complex_config ("write_mongodb", wm_config);
262 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */