2 * collectd - src/write_mongo.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"
34 #include "libmongo/bson.h"
35 #include "libmongo/mongo.h"
39 char name[DATA_MAX_NAME_LEN];
47 mongo_connection conn[1];
48 mongo_connection_options opts[1];
51 typedef struct wm_node_s wm_node_t;
56 static int wm_write (const data_set_t *ds, /* {{{ */
57 const value_list_t *vl,
60 wm_node_t *node = ud->data;
61 char collection_name[512];
65 bson_buffer record_buf;
67 ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
69 bson_buffer_init(&record_buf);
70 bson_append_time_t(&record_buf,"ts",vl->time);
71 bson_append_string(&record_buf,"h",vl->host);
72 bson_append_string(&record_buf,"i",vl->plugin_instance);
73 bson_append_string(&record_buf,"t",vl->type);
74 bson_append_string(&record_buf,"ti",vl->type_instance);
76 for (i = 0; i < ds->ds_num; i++)
78 if (ds->ds[i].type == DS_TYPE_COUNTER)
79 bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].counter);
80 else if (ds->ds[i].type == DS_TYPE_GAUGE)
81 bson_append_double(&record_buf, ds->ds[i].name, vl->values[i].gauge);
82 else if (ds->ds[i].type == DS_TYPE_DERIVE)
83 bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].derive);
84 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
85 bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].absolute);
90 bson_from_buffer(&record,&record_buf);
92 pthread_mutex_lock (&node->lock);
94 if (node->connected == 0)
96 strcpy(node->opts->host, node->host);
97 node->opts->port = node->port;
99 status = mongo_connect(node->conn,node->opts);
100 if (status!=mongo_conn_success) {
101 ERROR ("write_mongo plugin: Connecting to host \"%s\" (port %i) failed.",
102 (node->host != NULL) ? node->host : "localhost",
103 (node->port != 0) ? node->port : 27017);
104 mongo_destroy(node->conn);
105 pthread_mutex_unlock (&node->lock);
112 /* Assert if the connection has been established */
113 assert (node->connected == 1);
115 mongo_insert(node->conn,collection_name,&record);
117 pthread_mutex_unlock (&node->lock);
119 bson_buffer_destroy(&record_buf);
122 } /* }}} int wm_write */
124 static void wm_config_free (void *ptr) /* {{{ */
126 wm_node_t *node = ptr;
131 if (node->connected != 0)
133 mongo_destroy(node->conn);
139 } /* }}} void wm_config_free */
141 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
147 node = malloc (sizeof (*node));
150 memset (node, 0, sizeof (*node));
153 node->timeout = 1000;
155 pthread_mutex_init (&node->lock, /* attr = */ NULL);
157 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
165 for (i = 0; i < ci->children_num; i++)
167 oconfig_item_t *child = ci->children + i;
169 if (strcasecmp ("Host", child->key) == 0)
170 status = cf_util_get_string (child, &node->host);
171 else if (strcasecmp ("Port", child->key) == 0)
173 status = cf_util_get_port_number (child);
180 else if (strcasecmp ("Timeout", child->key) == 0)
181 status = cf_util_get_int (child, &node->timeout);
183 WARNING ("write_mongo plugin: Ignoring unknown config option \"%s\".",
188 } /* for (i = 0; i < ci->children_num; i++) */
192 char cb_name[DATA_MAX_NAME_LEN];
195 ssnprintf (cb_name, sizeof (cb_name), "write_mongo/%s", node->name);
198 ud.free_func = wm_config_free;
200 status = plugin_register_write (cb_name, wm_write, &ud);
201 INFO ("write_mongo plugin: registered write plugin %s %d",cb_name,status);
205 wm_config_free (node);
208 } /* }}} int wm_config_node */
210 static int wm_config (oconfig_item_t *ci) /* {{{ */
214 for (i = 0; i < ci->children_num; i++)
216 oconfig_item_t *child = ci->children + i;
218 if (strcasecmp ("Node", child->key) == 0)
219 wm_config_node (child);
221 WARNING ("write_mongo plugin: Ignoring unknown "
222 "configuration option \"%s\" at top level.", child->key);
226 } /* }}} int wm_config */
228 void module_register (void)
230 plugin_register_complex_config ("write_mongo", wm_config);
233 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */