Merge branch 'collectd-5.0'
[collectd.git] / src / write_mongodb.c
1 /**
2  * collectd - src/write_mongodb.c
3  * Copyright (C) 2010  Florian Forster
4  * Copyright (C) 2010  Akkarit Sangpetch
5  * Copyright (C) 2012  Chris Lundquist
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *   Florian Forster <ff at octo.it>
27  *   Akkarit Sangpetch <asangpet at andrew.cmu.edu>
28  *   Chris Lundquist <clundquist at bluebox.net>
29  **/
30
31 #include "collectd.h"
32 #include "plugin.h"
33 #include "common.h"
34 #include "configfile.h"
35
36 #include <pthread.h>
37
38 #if HAVE_STDINT_H
39 # define MONGO_HAVE_STDINT 1
40 #else
41 # define MONGO_USE_LONG_LONG_INT 1
42 #endif
43 #include <mongo.h>
44
45 struct wm_node_s
46 {
47   char name[DATA_MAX_NAME_LEN];
48
49   char *host;
50   int port;
51   int timeout;
52
53   int connected;
54
55   mongo conn[1];
56   pthread_mutex_t lock;
57 };
58 typedef struct wm_node_s wm_node_t;
59
60 /*
61  * Functions
62  */
63 static int wm_write (const data_set_t *ds, /* {{{ */
64     const value_list_t *vl,
65     user_data_t *ud)
66 {
67   wm_node_t *node = ud->data;
68   char collection_name[512];
69   int status;
70   int i;
71   bson record;
72
73   ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
74
75   bson_init(&record);
76   bson_append_time_t(&record,"ts",CDTIME_T_TO_TIME_T(vl->time));
77   bson_append_string(&record,"h",vl->host);
78   bson_append_string(&record,"i",vl->plugin_instance);
79   bson_append_string(&record,"t",vl->type);
80   bson_append_string(&record,"ti",vl->type_instance);
81
82   for (i = 0; i < ds->ds_num; i++)
83   {
84     if (ds->ds[i].type == DS_TYPE_COUNTER)
85       bson_append_long(&record, ds->ds[i].name, vl->values[i].counter);
86     else if (ds->ds[i].type == DS_TYPE_GAUGE)
87       bson_append_double(&record, ds->ds[i].name, vl->values[i].gauge);
88     else if (ds->ds[i].type == DS_TYPE_DERIVE)
89       bson_append_long(&record, ds->ds[i].name, vl->values[i].derive);
90     else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
91       bson_append_long(&record, ds->ds[i].name, vl->values[i].absolute);
92     else
93       assert (23 == 42);
94   }
95   /* We must finish the record, other wise the insert will fail */
96   bson_finish(&record);
97
98   pthread_mutex_lock (&node->lock);
99
100   if (node->connected == 0)
101   {
102     status = mongo_connect(node->conn, node->host, node->port);
103     if (status != MONGO_OK) {
104       ERROR ("write_mongodb plugin: Connecting to host \"%s\" (port %i) failed.",
105           (node->host != NULL) ? node->host : "localhost",
106           (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
107       mongo_destroy(node->conn);
108       pthread_mutex_unlock (&node->lock);
109       return (-1);
110     } else {
111       node->connected = 1;
112     }
113   }
114
115   /* Assert if the connection has been established */
116   assert (node->connected == 1);
117
118   DEBUG ( "write_mongodb plugin: writing record");
119   /* bson_print(&record); */
120
121   status = mongo_insert(node->conn,collection_name,&record);
122
123   if(status != MONGO_OK)
124   {
125     ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
126     if (node->conn->err == MONGO_BSON_INVALID)
127       ERROR ("write_mongodb plugin: %s", node->conn->errstr);
128     else if (record.err)
129       ERROR ("write_mongodb plugin: %s", record.errstr);
130   }
131
132   pthread_mutex_unlock (&node->lock);
133   /* free our resource as not to leak memory */
134   bson_destroy(&record);
135
136   return (0);
137 } /* }}} int wm_write */
138
139 static void wm_config_free (void *ptr) /* {{{ */
140 {
141   wm_node_t *node = ptr;
142
143   if (node == NULL)
144     return;
145
146   if (node->connected != 0)
147   {
148     mongo_destroy(node->conn);
149     node->connected = 0;
150   }
151
152   sfree (node->host);
153   sfree (node);
154 } /* }}} void wm_config_free */
155
156 static int wm_config_node (oconfig_item_t *ci) /* {{{ */
157 {
158   wm_node_t *node;
159   int status;
160   int i;
161
162   node = malloc (sizeof (*node));
163   if (node == NULL)
164     return (ENOMEM);
165   memset (node, 0, sizeof (*node));
166   node->host = NULL;
167   node->port = 0;
168   node->timeout = 1000;
169   node->connected = 0;
170   pthread_mutex_init (&node->lock, /* attr = */ NULL);
171
172   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
173
174   if (status != 0)
175   {
176     sfree (node);
177     return (status);
178   }
179
180   for (i = 0; i < ci->children_num; i++)
181   {
182     oconfig_item_t *child = ci->children + i;
183
184     if (strcasecmp ("Host", child->key) == 0)
185       status = cf_util_get_string (child, &node->host);
186     else if (strcasecmp ("Port", child->key) == 0)
187     {
188       status = cf_util_get_port_number (child);
189       if (status > 0)
190       {
191         node->port = status;
192         status = 0;
193       }
194     }
195     else if (strcasecmp ("Timeout", child->key) == 0)
196       status = cf_util_get_int (child, &node->timeout);
197     else
198       WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
199           child->key);
200
201     if (status != 0)
202       break;
203   } /* for (i = 0; i < ci->children_num; i++) */
204
205   if (status == 0)
206   {
207     char cb_name[DATA_MAX_NAME_LEN];
208     user_data_t ud;
209
210     ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name);
211
212     ud.data = node;
213     ud.free_func = wm_config_free;
214
215     status = plugin_register_write (cb_name, wm_write, &ud);
216     INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status);
217   }
218
219   if (status != 0)
220     wm_config_free (node);
221
222   return (status);
223 } /* }}} int wm_config_node */
224
225 static int wm_config (oconfig_item_t *ci) /* {{{ */
226 {
227   int i;
228
229   for (i = 0; i < ci->children_num; i++)
230   {
231     oconfig_item_t *child = ci->children + i;
232
233     if (strcasecmp ("Node", child->key) == 0)
234       wm_config_node (child);
235     else
236       WARNING ("write_mongodb plugin: Ignoring unknown "
237           "configuration option \"%s\" at top level.", child->key);
238   }
239
240   return (0);
241 } /* }}} int wm_config */
242
243 void module_register (void)
244 {
245   plugin_register_complex_config ("write_mongodb", wm_config);
246 }
247
248 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */