Code

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