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
44 char name[DATA_MAX_NAME_LEN];
52 mongo_connection conn[1];
53 mongo_connection_options opts[1];
56 typedef struct wm_node_s wm_node_t;
61 static int wm_write (const data_set_t *ds, /* {{{ */
62 const value_list_t *vl,
65 wm_node_t *node = ud->data;
66 char collection_name[512];
70 bson_buffer record_buf;
72 ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
74 bson_buffer_init(&record_buf);
75 bson_append_time_t(&record_buf,"ts",vl->time);
76 bson_append_string(&record_buf,"h",vl->host);
77 bson_append_string(&record_buf,"i",vl->plugin_instance);
78 bson_append_string(&record_buf,"t",vl->type);
79 bson_append_string(&record_buf,"ti",vl->type_instance);
81 for (i = 0; i < ds->ds_num; i++)
83 if (ds->ds[i].type == DS_TYPE_COUNTER)
84 bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].counter);
85 else if (ds->ds[i].type == DS_TYPE_GAUGE)
86 bson_append_double(&record_buf, ds->ds[i].name, vl->values[i].gauge);
87 else if (ds->ds[i].type == DS_TYPE_DERIVE)
88 bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].derive);
89 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
90 bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].absolute);
95 bson_from_buffer(&record,&record_buf);
97 pthread_mutex_lock (&node->lock);
99 if (node->connected == 0)
101 sstrncpy(node->opts->host, node->host,
102 sizeof (node->opts->host));
103 node->opts->port = node->port;
105 status = mongo_connect(node->conn,node->opts);
106 if (status!=mongo_conn_success) {
107 ERROR ("write_mongodb plugin: Connecting to host \"%s\" (port %i) failed.",
108 (node->host != NULL) ? node->host : "localhost",
109 (node->port != 0) ? node->port : 27017);
110 mongo_destroy(node->conn);
111 pthread_mutex_unlock (&node->lock);
118 /* Assert if the connection has been established */
119 assert (node->connected == 1);
121 mongo_insert(node->conn,collection_name,&record);
123 pthread_mutex_unlock (&node->lock);
125 bson_buffer_destroy(&record_buf);
128 } /* }}} int wm_write */
130 static void wm_config_free (void *ptr) /* {{{ */
132 wm_node_t *node = ptr;
137 if (node->connected != 0)
139 mongo_destroy(node->conn);
145 } /* }}} void wm_config_free */
147 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
153 node = malloc (sizeof (*node));
156 memset (node, 0, sizeof (*node));
159 node->timeout = 1000;
161 pthread_mutex_init (&node->lock, /* attr = */ NULL);
163 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
171 for (i = 0; i < ci->children_num; i++)
173 oconfig_item_t *child = ci->children + i;
175 if (strcasecmp ("Host", child->key) == 0)
176 status = cf_util_get_string (child, &node->host);
177 else if (strcasecmp ("Port", child->key) == 0)
179 status = cf_util_get_port_number (child);
186 else if (strcasecmp ("Timeout", child->key) == 0)
187 status = cf_util_get_int (child, &node->timeout);
189 WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
194 } /* for (i = 0; i < ci->children_num; i++) */
198 char cb_name[DATA_MAX_NAME_LEN];
201 ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name);
204 ud.free_func = wm_config_free;
206 status = plugin_register_write (cb_name, wm_write, &ud);
207 INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status);
211 wm_config_free (node);
214 } /* }}} int wm_config_node */
216 static int wm_config (oconfig_item_t *ci) /* {{{ */
220 for (i = 0; i < ci->children_num; i++)
222 oconfig_item_t *child = ci->children + i;
224 if (strcasecmp ("Node", child->key) == 0)
225 wm_config_node (child);
227 WARNING ("write_mongodb plugin: Ignoring unknown "
228 "configuration option \"%s\" at top level.", child->key);
232 } /* }}} int wm_config */
234 void module_register (void)
236 plugin_register_complex_config ("write_mongodb", wm_config);
239 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */