Fix plugin and type instance field name, also free bson object buffer
[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;
65   bson_buffer record_buf;
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->type);
74   bson_append_string(&record_buf,"ti",vl->type_instance);
75
76   for (i = 0; i < ds->ds_num; i++)
77   {
78     if (ds->ds[i].type == DS_TYPE_COUNTER)
79       bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].counter);
80     else if (ds->ds[i].type == DS_TYPE_GAUGE)
81       bson_append_double(&record_buf, ds->ds[i].name, vl->values[i].gauge);
82     else if (ds->ds[i].type == DS_TYPE_DERIVE)
83       bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].derive);
84     else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
85       bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].absolute);
86     else
87       assert (23 == 42);
88   }
89
90   bson_from_buffer(&record,&record_buf);
91
92   pthread_mutex_lock (&node->lock);
93
94   if (node->connected == 0)
95   {
96     strcpy(node->opts->host, node->host);
97     node->opts->port = node->port;
98
99     status = mongo_connect(node->conn,node->opts);
100     if (status!=mongo_conn_success) {
101       ERROR ("write_mongo plugin: Connecting to host \"%s\" (port %i) failed.",
102           (node->host != NULL) ? node->host : "localhost",
103           (node->port != 0) ? node->port : 27017);
104       mongo_destroy(node->conn);
105       pthread_mutex_unlock (&node->lock);
106       return (-1);
107     } else {
108       node->connected = 1;
109     }
110   }
111
112   /* Assert if the connection has been established */
113   assert (node->connected == 1);
114
115   mongo_insert(node->conn,collection_name,&record);
116
117   pthread_mutex_unlock (&node->lock);
118
119   bson_buffer_destroy(&record_buf);
120
121   return (0);
122 } /* }}} int wm_write */
123
124 static void wm_config_free (void *ptr) /* {{{ */
125 {
126   wm_node_t *node = ptr;
127
128   if (node == NULL)
129     return;
130
131   if (node->connected != 0)
132   {
133     mongo_destroy(node->conn);
134     node->connected = 0;
135   }
136
137   sfree (node->host);
138   sfree (node);
139 } /* }}} void wm_config_free */
140
141 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
142 {
143   wm_node_t *node;
144   int status;
145   int i;
146
147   node = malloc (sizeof (*node));
148   if (node == NULL)
149     return (ENOMEM);
150   memset (node, 0, sizeof (*node));
151   node->host = NULL;
152   node->port = 0;
153   node->timeout = 1000;
154   node->connected = 0;
155   pthread_mutex_init (&node->lock, /* attr = */ NULL);
156
157   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
158
159   if (status != 0)
160   {
161     sfree (node);
162     return (status);
163   }
164
165   for (i = 0; i < ci->children_num; i++)
166   {
167     oconfig_item_t *child = ci->children + i;
168
169     if (strcasecmp ("Host", child->key) == 0)
170       status = cf_util_get_string (child, &node->host);
171     else if (strcasecmp ("Port", child->key) == 0)
172     {
173       status = cf_util_get_port_number (child);
174       if (status > 0)
175       {
176         node->port = status;
177         status = 0;
178       }
179     }
180     else if (strcasecmp ("Timeout", child->key) == 0)
181       status = cf_util_get_int (child, &node->timeout);
182     else
183       WARNING ("write_mongo plugin: Ignoring unknown config option \"%s\".",
184           child->key);
185
186     if (status != 0)
187       break;
188   } /* for (i = 0; i < ci->children_num; i++) */
189
190   if (status == 0)
191   {
192     char cb_name[DATA_MAX_NAME_LEN];
193     user_data_t ud;
194
195     ssnprintf (cb_name, sizeof (cb_name), "write_mongo/%s", node->name);
196
197     ud.data = node;
198     ud.free_func = wm_config_free;
199
200     status = plugin_register_write (cb_name, wm_write, &ud);
201     INFO ("write_mongo plugin: registered write plugin %s %d",cb_name,status);
202   }
203
204   if (status != 0)
205     wm_config_free (node);
206
207   return (status);
208 } /* }}} int wm_config_node */
209
210 static int wm_config (oconfig_item_t *ci) /* {{{ */
211 {
212   int i;
213
214   for (i = 0; i < ci->children_num; i++)
215   {
216     oconfig_item_t *child = ci->children + i;
217
218     if (strcasecmp ("Node", child->key) == 0)
219       wm_config_node (child);
220     else
221       WARNING ("write_mongo plugin: Ignoring unknown "
222           "configuration option \"%s\" at top level.", child->key);
223   }
224
225   return (0);
226 } /* }}} int wm_config */
227
228 void module_register (void)
229 {
230   plugin_register_complex_config ("write_mongo", wm_config);
231 }
232
233 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */