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;
81 ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
83 bson_init(&record);
84 bson_append_time_t(&record,"ts",vl->time);
85 bson_append_string(&record,"h",vl->host);
86 bson_append_string(&record,"i",vl->plugin_instance);
87 bson_append_string(&record,"t",vl->type);
88 bson_append_string(&record,"ti",vl->type_instance);
90 for (i = 0; i < ds->ds_num; i++)
91 {
92 if (ds->ds[i].type == DS_TYPE_COUNTER)
93 bson_append_long(&record, ds->ds[i].name, vl->values[i].counter);
94 else if (ds->ds[i].type == DS_TYPE_GAUGE)
95 bson_append_double(&record, ds->ds[i].name, vl->values[i].gauge);
96 else if (ds->ds[i].type == DS_TYPE_DERIVE)
97 bson_append_long(&record, ds->ds[i].name, vl->values[i].derive);
98 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
99 bson_append_long(&record, ds->ds[i].name, vl->values[i].absolute);
100 else
101 assert (23 == 42);
102 }
103 bson_finish(&record);
105 pthread_mutex_lock (&node->lock);
107 if (node->connected == 0)
108 {
109 /*
110 sstrncpy(node->opts->host, node->host,
111 sizeof (node->opts->host));
112 node->opts->port = node->port;
113 */
115 /* status = mongo_connect(node->conn,node->opts->host, node->opts->port);*/
116 status = mongo_connect(node->conn, node->host, node->port);
117 if (status != MONGO_OK) {
118 ERROR ("write_mongodb plugin: Connecting to host \"%s\" (port %i) failed.",
119 (node->host != NULL) ? node->host : "localhost",
120 (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
121 mongo_destroy(node->conn);
122 pthread_mutex_unlock (&node->lock);
123 return (-1);
124 } else {
125 node->connected = 1;
126 }
127 }
129 /* Assert if the connection has been established */
130 assert (node->connected == 1);
132 DEBUG ( "write_mongodb plugin: writing record");
133 /* bson_print(&record); */
135 status = mongo_insert(node->conn,collection_name,&record);
137 if(status != MONGO_OK)
138 {
139 ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
140 if(node->conn->err == MONGO_BSON_INVALID)
141 {
142 ERROR (node->conn->errstr);
143 } else if ( record.err)
144 {
145 ERROR (record.errstr);
146 }
148 }
151 pthread_mutex_unlock (&node->lock);
153 return (0);
154 } /* }}} int wm_write */
156 static void wm_config_free (void *ptr) /* {{{ */
157 {
158 wm_node_t *node = ptr;
160 if (node == NULL)
161 return;
163 if (node->connected != 0)
164 {
165 mongo_destroy(node->conn);
166 node->connected = 0;
167 }
169 sfree (node->host);
170 sfree (node);
171 } /* }}} void wm_config_free */
173 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
174 {
175 wm_node_t *node;
176 int status;
177 int i;
179 node = malloc (sizeof (*node));
180 if (node == NULL)
181 return (ENOMEM);
182 memset (node, 0, sizeof (*node));
183 node->host = NULL;
184 node->port = 0;
185 node->timeout = 1000;
186 node->connected = 0;
187 pthread_mutex_init (&node->lock, /* attr = */ NULL);
189 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
191 if (status != 0)
192 {
193 sfree (node);
194 return (status);
195 }
197 for (i = 0; i < ci->children_num; i++)
198 {
199 oconfig_item_t *child = ci->children + i;
201 if (strcasecmp ("Host", child->key) == 0)
202 status = cf_util_get_string (child, &node->host);
203 else if (strcasecmp ("Port", child->key) == 0)
204 {
205 status = cf_util_get_port_number (child);
206 if (status > 0)
207 {
208 node->port = status;
209 status = 0;
210 }
211 }
212 else if (strcasecmp ("Timeout", child->key) == 0)
213 status = cf_util_get_int (child, &node->timeout);
214 else
215 WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
216 child->key);
218 if (status != 0)
219 break;
220 } /* for (i = 0; i < ci->children_num; i++) */
222 if (status == 0)
223 {
224 char cb_name[DATA_MAX_NAME_LEN];
225 user_data_t ud;
227 ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name);
229 ud.data = node;
230 ud.free_func = wm_config_free;
232 status = plugin_register_write (cb_name, wm_write, &ud);
233 INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status);
234 }
236 if (status != 0)
237 wm_config_free (node);
239 return (status);
240 } /* }}} int wm_config_node */
242 static int wm_config (oconfig_item_t *ci) /* {{{ */
243 {
244 int i;
246 for (i = 0; i < ci->children_num; i++)
247 {
248 oconfig_item_t *child = ci->children + i;
250 if (strcasecmp ("Node", child->key) == 0)
251 wm_config_node (child);
252 else
253 WARNING ("write_mongodb plugin: Ignoring unknown "
254 "configuration option \"%s\" at top level.", child->key);
255 }
257 return (0);
258 } /* }}} int wm_config */
260 void module_register (void)
261 {
262 plugin_register_complex_config ("write_mongodb", wm_config);
263 }
265 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */