5ee220e16503d825f33be25d6c2ad49dc85ae345
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 /*
43 struct mongo_options
44 {
45 char *host;
46 int port;
47 int timeout;
48 };
49 typedef struct mongo_options mongo_options;
50 */
52 struct wm_node_s
53 {
54 char name[DATA_MAX_NAME_LEN];
56 char *host;
57 int port;
58 int timeout;
60 int connected;
62 mongo conn[1];
63 /* mongo_options opts[1]; */
64 pthread_mutex_t lock;
65 };
66 typedef struct wm_node_s wm_node_t;
68 /*
69 * Functions
70 */
71 static int wm_write (const data_set_t *ds, /* {{{ */
72 const value_list_t *vl,
73 user_data_t *ud)
74 {
75 wm_node_t *node = ud->data;
76 char collection_name[512];
77 int status;
78 int i;
79 bson record;
80 /*bson_data record_buf; */
82 ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
84 bson_init(&record);
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++)
92 {
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);
101 else
102 assert (23 == 42);
103 }
104 bson_finish(&record);
107 pthread_mutex_lock (&node->lock);
109 if (node->connected == 0)
110 {
111 /*
112 sstrncpy(node->opts->host, node->host,
113 sizeof (node->opts->host));
114 node->opts->port = node->port;
115 */
117 /* status = mongo_connect(node->conn,node->opts->host, node->opts->port);*/
118 status = mongo_connect(node->conn, node->host, node->port);
119 if (status != MONGO_OK) {
120 ERROR ("write_mongodb plugin: Connecting to host \"%s\" (port %i) failed.",
121 (node->host != NULL) ? node->host : "localhost",
122 (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
123 mongo_destroy(node->conn);
124 pthread_mutex_unlock (&node->lock);
125 return (-1);
126 } else {
127 node->connected = 1;
128 }
129 }
131 /* Assert if the connection has been established */
132 assert (node->connected == 1);
134 DEBUG ( "write_mongodb plugin: writing record");
135 /* bson_print(&record); */
137 status = mongo_insert(node->conn,collection_name,&record);
139 if(status != MONGO_OK)
140 {
141 ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
142 if(node->conn->err == MONGO_BSON_INVALID)
143 {
144 ERROR (node->conn->errstr);
145 } else if ( record.err)
146 {
147 ERROR (record.errstr);
148 }
150 }
153 pthread_mutex_unlock (&node->lock);
155 return (0);
156 } /* }}} int wm_write */
158 static void wm_config_free (void *ptr) /* {{{ */
159 {
160 wm_node_t *node = ptr;
162 if (node == NULL)
163 return;
165 if (node->connected != 0)
166 {
167 mongo_destroy(node->conn);
168 node->connected = 0;
169 }
171 sfree (node->host);
172 sfree (node);
173 } /* }}} void wm_config_free */
175 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
176 {
177 wm_node_t *node;
178 int status;
179 int i;
181 node = malloc (sizeof (*node));
182 if (node == NULL)
183 return (ENOMEM);
184 memset (node, 0, sizeof (*node));
185 node->host = NULL;
186 node->port = 0;
187 node->timeout = 1000;
188 node->connected = 0;
189 pthread_mutex_init (&node->lock, /* attr = */ NULL);
191 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
193 if (status != 0)
194 {
195 sfree (node);
196 return (status);
197 }
199 for (i = 0; i < ci->children_num; i++)
200 {
201 oconfig_item_t *child = ci->children + i;
203 if (strcasecmp ("Host", child->key) == 0)
204 status = cf_util_get_string (child, &node->host);
205 else if (strcasecmp ("Port", child->key) == 0)
206 {
207 status = cf_util_get_port_number (child);
208 if (status > 0)
209 {
210 node->port = status;
211 status = 0;
212 }
213 }
214 else if (strcasecmp ("Timeout", child->key) == 0)
215 status = cf_util_get_int (child, &node->timeout);
216 else
217 WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
218 child->key);
220 if (status != 0)
221 break;
222 } /* for (i = 0; i < ci->children_num; i++) */
224 if (status == 0)
225 {
226 char cb_name[DATA_MAX_NAME_LEN];
227 user_data_t ud;
229 ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name);
231 ud.data = node;
232 ud.free_func = wm_config_free;
234 status = plugin_register_write (cb_name, wm_write, &ud);
235 INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status);
236 }
238 if (status != 0)
239 wm_config_free (node);
241 return (status);
242 } /* }}} int wm_config_node */
244 static int wm_config (oconfig_item_t *ci) /* {{{ */
245 {
246 int i;
248 for (i = 0; i < ci->children_num; i++)
249 {
250 oconfig_item_t *child = ci->children + i;
252 if (strcasecmp ("Node", child->key) == 0)
253 wm_config_node (child);
254 else
255 WARNING ("write_mongodb plugin: Ignoring unknown "
256 "configuration option \"%s\" at top level.", child->key);
257 }
259 return (0);
260 } /* }}} int wm_config */
262 void module_register (void)
263 {
264 plugin_register_complex_config ("write_mongodb", wm_config);
265 }
267 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */