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 mongo_options
43 {
44 char *host;
45 int port;
46 int timeout;
47 };
48 typedef struct mongo_options mongo_options;
50 struct wm_node_s
51 {
52 char name[DATA_MAX_NAME_LEN];
54 char *host;
55 int port;
56 int timeout;
58 int connected;
60 mongo conn[1];
61 mongo_options opts[1];
62 pthread_mutex_t lock;
63 };
64 typedef struct wm_node_s wm_node_t;
66 /*
67 * Functions
68 */
69 static int wm_write (const data_set_t *ds, /* {{{ */
70 const value_list_t *vl,
71 user_data_t *ud)
72 {
73 wm_node_t *node = ud->data;
74 char collection_name[512];
75 int status;
76 int i;
77 bson record;
78 /*bson_data record_buf; */
80 ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
82 bson_init(&record);
83 bson_append_time_t(&record,"ts",vl->time);
84 bson_append_string(&record,"h",vl->host);
85 bson_append_string(&record,"i",vl->plugin_instance);
86 bson_append_string(&record,"t",vl->type);
87 bson_append_string(&record,"ti",vl->type_instance);
89 for (i = 0; i < ds->ds_num; i++)
90 {
91 if (ds->ds[i].type == DS_TYPE_COUNTER)
92 bson_append_long(&record, ds->ds[i].name, vl->values[i].counter);
93 else if (ds->ds[i].type == DS_TYPE_GAUGE)
94 bson_append_double(&record, ds->ds[i].name, vl->values[i].gauge);
95 else if (ds->ds[i].type == DS_TYPE_DERIVE)
96 bson_append_long(&record, ds->ds[i].name, vl->values[i].derive);
97 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
98 bson_append_long(&record, ds->ds[i].name, vl->values[i].absolute);
99 else
100 assert (23 == 42);
101 }
103 /* bson_from_buffer(&record,&record_buf); */
105 pthread_mutex_lock (&node->lock);
107 if (node->connected == 0)
108 {
109 sstrncpy(node->opts->host, node->host,
110 sizeof (node->opts->host));
111 node->opts->port = node->port;
113 status = mongo_connect(node->conn,node->opts->host, node->opts->port);
114 if (status!=MONGO_OK) {
115 ERROR ("write_mongodb plugin: Connecting to host \"%s\" (port %i) failed.",
116 (node->host != NULL) ? node->host : "localhost",
117 (node->port != 0) ? node->port : 27017);
118 mongo_destroy(node->conn);
119 pthread_mutex_unlock (&node->lock);
120 return (-1);
121 } else {
122 node->connected = 1;
123 }
124 }
126 /* Assert if the connection has been established */
127 assert (node->connected == 1);
129 mongo_insert(node->conn,collection_name,&record);
131 pthread_mutex_unlock (&node->lock);
133 /* bson_buffer_destroy(&record_buf); */
135 return (0);
136 } /* }}} int wm_write */
138 static void wm_config_free (void *ptr) /* {{{ */
139 {
140 wm_node_t *node = ptr;
142 if (node == NULL)
143 return;
145 if (node->connected != 0)
146 {
147 mongo_destroy(node->conn);
148 node->connected = 0;
149 }
151 sfree (node->host);
152 sfree (node);
153 } /* }}} void wm_config_free */
155 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
156 {
157 wm_node_t *node;
158 int status;
159 int i;
161 node = malloc (sizeof (*node));
162 if (node == NULL)
163 return (ENOMEM);
164 memset (node, 0, sizeof (*node));
165 node->host = NULL;
166 node->port = 0;
167 node->timeout = 1000;
168 node->connected = 0;
169 pthread_mutex_init (&node->lock, /* attr = */ NULL);
171 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
173 if (status != 0)
174 {
175 sfree (node);
176 return (status);
177 }
179 for (i = 0; i < ci->children_num; i++)
180 {
181 oconfig_item_t *child = ci->children + i;
183 if (strcasecmp ("Host", child->key) == 0)
184 status = cf_util_get_string (child, &node->host);
185 else if (strcasecmp ("Port", child->key) == 0)
186 {
187 status = cf_util_get_port_number (child);
188 if (status > 0)
189 {
190 node->port = status;
191 status = 0;
192 }
193 }
194 else if (strcasecmp ("Timeout", child->key) == 0)
195 status = cf_util_get_int (child, &node->timeout);
196 else
197 WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
198 child->key);
200 if (status != 0)
201 break;
202 } /* for (i = 0; i < ci->children_num; i++) */
204 if (status == 0)
205 {
206 char cb_name[DATA_MAX_NAME_LEN];
207 user_data_t ud;
209 ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name);
211 ud.data = node;
212 ud.free_func = wm_config_free;
214 status = plugin_register_write (cb_name, wm_write, &ud);
215 INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status);
216 }
218 if (status != 0)
219 wm_config_free (node);
221 return (status);
222 } /* }}} int wm_config_node */
224 static int wm_config (oconfig_item_t *ci) /* {{{ */
225 {
226 int i;
228 for (i = 0; i < ci->children_num; i++)
229 {
230 oconfig_item_t *child = ci->children + i;
232 if (strcasecmp ("Node", child->key) == 0)
233 wm_config_node (child);
234 else
235 WARNING ("write_mongodb plugin: Ignoring unknown "
236 "configuration option \"%s\" at top level.", child->key);
237 }
239 return (0);
240 } /* }}} int wm_config */
242 void module_register (void)
243 {
244 plugin_register_complex_config ("write_mongodb", wm_config);
245 }
247 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */