c17c35f10583eeb39ad78eda292d3ae80197293e
[collectd.git] / src / write_mongo.c
1 /**
2  * collectd - src/write_mongo.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  **/
27
28 #include "collectd.h"
29 #include "plugin.h"
30 #include "common.h"
31 #include "configfile.h"
32
33 #include <pthread.h>
34 #include "libmongo/bson.h"
35 #include "libmongo/mongo.h"
36
37 struct wm_node_s
38 {
39   char name[DATA_MAX_NAME_LEN];
40
41   char *host;
42   int port;
43   int timeout;
44
45   int connected;
46
47   mongo_connection conn[1];
48   mongo_connection_options opts[1];
49   pthread_mutex_t lock;
50 };
51 typedef struct wm_node_s wm_node_t;
52
53 /*
54  * Functions
55  */
56 static int wm_write (const data_set_t *ds, /* {{{ */
57     const value_list_t *vl,
58     user_data_t *ud)
59 {
60   wm_node_t *node = ud->data;
61   char collection_name[512];
62   int status;
63   int i;
64   bson record[1];
65   bson_buffer record_buf[1];
66
67   ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
68
69   bson_buffer_init(record_buf);
70   bson_append_time_t(record_buf,"ts",vl->time);
71   bson_append_string(record_buf,"h",vl->host);
72   bson_append_string(record_buf,"i",vl->plugin_instance);
73   bson_append_string(record_buf,"t",vl->plugin_instance);
74   bson_append_string(record_buf,"ti",vl->plugin_instance);
75
76   if (ds->ds_num == 1) {
77     if (ds->ds[0].type == DS_TYPE_COUNTER)
78       bson_append_long(record_buf, "v", vl->values[0].counter);
79     else if (ds->ds[0].type == DS_TYPE_GAUGE)
80       bson_append_double(record_buf, "v", vl->values[0].gauge);
81     else if (ds->ds[0].type == DS_TYPE_DERIVE)
82       bson_append_long(record_buf, "v", vl->values[0].derive);
83     else if (ds->ds[0].type == DS_TYPE_ABSOLUTE)
84       bson_append_long(record_buf, "v", vl->values[0].absolute);
85     else
86       assert (23 == 42);
87   } else {
88     bson_append_start_object(record_buf,"v");
89     for (i = 0; i < ds->ds_num; i++)
90     {
91       if (ds->ds[i].type == DS_TYPE_COUNTER)
92         bson_append_long(record_buf, ds->ds[i].name, vl->values[i].counter);
93       else if (ds->ds[i].type == DS_TYPE_GAUGE)
94         bson_append_double(record_buf, ds->ds[i].name, vl->values[i].gauge);
95       else if (ds->ds[i].type == DS_TYPE_DERIVE)
96         bson_append_long(record_buf, ds->ds[i].name, vl->values[i].derive);
97       else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
98         bson_append_long(record_buf, ds->ds[i].name, vl->values[i].absolute);
99       else
100         assert (23 == 42);
101     }
102     bson_append_finish_object(record_buf);
103   }
104   bson_from_buffer(record,record_buf);
105
106   pthread_mutex_lock (&node->lock);
107
108   if (node->connected == 0)
109   {
110     strcpy(node->opts->host, node->host);
111     node->opts->port = node->port;
112
113     status = mongo_connect(node->conn,node->opts);
114     if (status!=mongo_conn_success) {
115       ERROR ("write_mongo plugin: Connecting to host \"%s\" (port %i) failed.",
116           (node->host != NULL) ? node->host : "localhost",
117           (node->port != 0) ? node->port : 6379);
118       pthread_mutex_unlock (&node->lock);
119       return (-1);
120     } else {
121       node->connected = 1;
122     }
123   }
124
125   /* Assert if the connection has been established */
126   assert (node->connected == 1);
127
128   mongo_insert(node->conn,collection_name,record);
129
130   pthread_mutex_unlock (&node->lock);
131
132   return (0);
133 } /* }}} int wm_write */
134
135 static void wm_config_free (void *ptr) /* {{{ */
136 {
137   wm_node_t *node = ptr;
138
139   if (node == NULL)
140     return;
141
142   if (node->connected != 0)
143   {
144     mongo_destroy(node->conn);
145     node->connected = 0;
146   }
147
148   sfree (node->host);
149   sfree (node);
150 } /* }}} void wm_config_free */
151
152 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
153 {
154   wm_node_t *node;
155   int status;
156   int i;
157
158   node = malloc (sizeof (*node));
159   if (node == NULL)
160     return (ENOMEM);
161   memset (node, 0, sizeof (*node));
162   node->host = NULL;
163   node->port = 0;
164   node->timeout = 1000;
165   node->connected = 0;
166   pthread_mutex_init (&node->lock, /* attr = */ NULL);
167
168   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
169
170   if (status != 0)
171   {
172     sfree (node);
173     return (status);
174   }
175
176   for (i = 0; i < ci->children_num; i++)
177   {
178     oconfig_item_t *child = ci->children + i;
179
180     if (strcasecmp ("Host", child->key) == 0)
181       status = cf_util_get_string (child, &node->host);
182     else if (strcasecmp ("Port", child->key) == 0)
183     {
184       status = cf_util_get_port_number (child);
185       if (status > 0)
186       {
187         node->port = status;
188         status = 0;
189       }
190     }
191     else if (strcasecmp ("Timeout", child->key) == 0)
192       status = cf_util_get_int (child, &node->timeout);
193     else
194       WARNING ("write_mongo plugin: Ignoring unknown config option \"%s\".",
195           child->key);
196
197     if (status != 0)
198       break;
199   } /* for (i = 0; i < ci->children_num; i++) */
200
201   if (status == 0)
202   {
203     char cb_name[DATA_MAX_NAME_LEN];
204     user_data_t ud;
205
206     ssnprintf (cb_name, sizeof (cb_name), "write_mongo/%s", node->name);
207
208     ud.data = node;
209     ud.free_func = wm_config_free;
210
211     status = plugin_register_write (cb_name, wm_write, &ud);
212     INFO ("write_mongo plugin: registered write plugin %s %d",cb_name,status);
213   }
214
215   if (status != 0)
216     wm_config_free (node);
217
218   return (status);
219 } /* }}} int wm_config_node */
220
221 static int wm_config (oconfig_item_t *ci) /* {{{ */
222 {
223   int i;
224
225   for (i = 0; i < ci->children_num; i++)
226   {
227     oconfig_item_t *child = ci->children + i;
228
229     if (strcasecmp ("Node", child->key) == 0)
230       wm_config_node (child);
231     else
232       WARNING ("write_mongo plugin: Ignoring unknown "
233           "configuration option \"%s\" at top level.", child->key);
234   }
235
236   return (0);
237 } /* }}} int wm_config */
238
239 void module_register (void)
240 {
241   plugin_register_complex_config ("write_mongo", wm_config);
242 }
243
244 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */