f13cdc332b5155cbb9a0893cf87345d79921cacf
1 /**
2 * collectd - src/write_mongodb.c
3 * Copyright (C) 2010 Florian Forster, Akkarit Sangpetch
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian Forster <ff at octo.it>
25 * Akkarit Sangpetch <asangpet@andrew.cmu.edu>
26 **/
28 #include "collectd.h"
29 #include "plugin.h"
30 #include "common.h"
31 #include "configfile.h"
33 #include <pthread.h>
35 #if HAVE_STDINT_H
36 # define MONGO_HAVE_STDINT 1
37 #else
38 # define MONGO_USE_LONG_LONG_INT 1
39 #endif
40 #include <mongo.h>
42 struct wm_node_s
43 {
44 char name[DATA_MAX_NAME_LEN];
46 char *host;
47 int port;
48 int timeout;
50 int connected;
52 mongo conn[1];
53 pthread_mutex_t lock;
54 };
55 typedef struct wm_node_s wm_node_t;
57 /*
58 * Functions
59 */
60 static int wm_write (const data_set_t *ds, /* {{{ */
61 const value_list_t *vl,
62 user_data_t *ud)
63 {
64 wm_node_t *node = ud->data;
65 char collection_name[512];
66 int status;
67 int i;
68 bson record;
70 ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
72 bson_init(&record);
73 bson_append_time_t(&record,"ts",CDTIME_T_TO_TIME_T(vl->time));
74 bson_append_string(&record,"h",vl->host);
75 bson_append_string(&record,"i",vl->plugin_instance);
76 bson_append_string(&record,"t",vl->type);
77 bson_append_string(&record,"ti",vl->type_instance);
79 for (i = 0; i < ds->ds_num; i++)
80 {
81 if (ds->ds[i].type == DS_TYPE_COUNTER)
82 bson_append_long(&record, ds->ds[i].name, vl->values[i].counter);
83 else if (ds->ds[i].type == DS_TYPE_GAUGE)
84 bson_append_double(&record, ds->ds[i].name, vl->values[i].gauge);
85 else if (ds->ds[i].type == DS_TYPE_DERIVE)
86 bson_append_long(&record, ds->ds[i].name, vl->values[i].derive);
87 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
88 bson_append_long(&record, ds->ds[i].name, vl->values[i].absolute);
89 else
90 assert (23 == 42);
91 }
92 bson_finish(&record);
94 pthread_mutex_lock (&node->lock);
96 if (node->connected == 0)
97 {
98 /*
99 sstrncpy(node->opts->host, node->host,
100 sizeof (node->opts->host));
101 node->opts->port = node->port;
102 */
104 /* status = mongo_connect(node->conn,node->opts->host, node->opts->port);*/
105 status = mongo_connect(node->conn, node->host, node->port);
106 if (status != MONGO_OK) {
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 : MONGO_DEFAULT_PORT);
110 mongo_destroy(node->conn);
111 pthread_mutex_unlock (&node->lock);
112 return (-1);
113 } else {
114 node->connected = 1;
115 }
116 }
118 /* Assert if the connection has been established */
119 assert (node->connected == 1);
121 DEBUG ( "write_mongodb plugin: writing record");
122 /* bson_print(&record); */
124 status = mongo_insert(node->conn,collection_name,&record);
126 if(status != MONGO_OK)
127 {
128 ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
129 if(node->conn->err == MONGO_BSON_INVALID)
130 {
131 ERROR (node->conn->errstr);
132 } else if ( record.err)
133 {
134 ERROR (record.errstr);
135 }
137 }
140 pthread_mutex_unlock (&node->lock);
142 return (0);
143 } /* }}} int wm_write */
145 static void wm_config_free (void *ptr) /* {{{ */
146 {
147 wm_node_t *node = ptr;
149 if (node == NULL)
150 return;
152 if (node->connected != 0)
153 {
154 mongo_destroy(node->conn);
155 node->connected = 0;
156 }
158 sfree (node->host);
159 sfree (node);
160 } /* }}} void wm_config_free */
162 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
163 {
164 wm_node_t *node;
165 int status;
166 int i;
168 node = malloc (sizeof (*node));
169 if (node == NULL)
170 return (ENOMEM);
171 memset (node, 0, sizeof (*node));
172 node->host = NULL;
173 node->port = 0;
174 node->timeout = 1000;
175 node->connected = 0;
176 pthread_mutex_init (&node->lock, /* attr = */ NULL);
178 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
180 if (status != 0)
181 {
182 sfree (node);
183 return (status);
184 }
186 for (i = 0; i < ci->children_num; i++)
187 {
188 oconfig_item_t *child = ci->children + i;
190 if (strcasecmp ("Host", child->key) == 0)
191 status = cf_util_get_string (child, &node->host);
192 else if (strcasecmp ("Port", child->key) == 0)
193 {
194 status = cf_util_get_port_number (child);
195 if (status > 0)
196 {
197 node->port = status;
198 status = 0;
199 }
200 }
201 else if (strcasecmp ("Timeout", child->key) == 0)
202 status = cf_util_get_int (child, &node->timeout);
203 else
204 WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
205 child->key);
207 if (status != 0)
208 break;
209 } /* for (i = 0; i < ci->children_num; i++) */
211 if (status == 0)
212 {
213 char cb_name[DATA_MAX_NAME_LEN];
214 user_data_t ud;
216 ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name);
218 ud.data = node;
219 ud.free_func = wm_config_free;
221 status = plugin_register_write (cb_name, wm_write, &ud);
222 INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status);
223 }
225 if (status != 0)
226 wm_config_free (node);
228 return (status);
229 } /* }}} int wm_config_node */
231 static int wm_config (oconfig_item_t *ci) /* {{{ */
232 {
233 int i;
235 for (i = 0; i < ci->children_num; i++)
236 {
237 oconfig_item_t *child = ci->children + i;
239 if (strcasecmp ("Node", child->key) == 0)
240 wm_config_node (child);
241 else
242 WARNING ("write_mongodb plugin: Ignoring unknown "
243 "configuration option \"%s\" at top level.", child->key);
244 }
246 return (0);
247 } /* }}} int wm_config */
249 void module_register (void)
250 {
251 plugin_register_complex_config ("write_mongodb", wm_config);
252 }
254 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */