Merge remote-tracking branch 'github/pr/1082'
[collectd.git] / src / write_redis.c
1 /**
2  * collectd - src/write_redis.c
3  * Copyright (C) 2010       Florian Forster
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  **/
26
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
31
32 #include <pthread.h>
33 #include <sys/time.h>
34 #include <hiredis/hiredis.h>
35
36 struct wr_node_s
37 {
38   char name[DATA_MAX_NAME_LEN];
39
40   char *host;
41   int port;
42   struct timeval timeout;
43   char *prefix;
44
45   redisContext *conn;
46   pthread_mutex_t lock;
47 };
48 typedef struct wr_node_s wr_node_t;
49
50 /*
51  * Functions
52  */
53 static int wr_write (const data_set_t *ds, /* {{{ */
54     const value_list_t *vl,
55     user_data_t *ud)
56 {
57   wr_node_t *node = ud->data;
58   char ident[512];
59   char key[512];
60   char value[512];
61   char time[24];
62   size_t value_size;
63   char *value_ptr;
64   int status;
65   redisReply   *rr;
66   int i;
67
68   status = FORMAT_VL (ident, sizeof (ident), vl);
69   if (status != 0)
70     return (status);
71   if (node->prefix == NULL) {
72     ssnprintf (key, sizeof (key), "collectd/%s", ident);
73   }
74   else {
75     ssnprintf (key, sizeof (key), "%s/%s", node->prefix, ident);
76   }
77   ssnprintf (time, sizeof (time), "%.9f", CDTIME_T_TO_DOUBLE(vl->time));
78
79   memset (value, 0, sizeof (value));
80   value_size = sizeof (value);
81   value_ptr = &value[0];
82
83 #define APPEND(...) do {                                             \
84   status = snprintf (value_ptr, value_size, __VA_ARGS__);            \
85   if (((size_t) status) > value_size)                                \
86   {                                                                  \
87     value_ptr += value_size;                                         \
88     value_size = 0;                                                  \
89   }                                                                  \
90   else                                                               \
91   {                                                                  \
92     value_ptr += status;                                             \
93     value_size -= status;                                            \
94   }                                                                  \
95 } while (0)
96
97   APPEND ("%s:", time);
98
99   for (i = 0; i < ds->ds_num; i++)
100   {
101     if (ds->ds[i].type == DS_TYPE_COUNTER)
102       APPEND ("%llu", vl->values[i].counter);
103     else if (ds->ds[i].type == DS_TYPE_GAUGE)
104       APPEND (GAUGE_FORMAT, vl->values[i].gauge);
105     else if (ds->ds[i].type == DS_TYPE_DERIVE)
106       APPEND ("%"PRIi64, vl->values[i].derive);
107     else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
108       APPEND ("%"PRIu64, vl->values[i].absolute);
109     else
110       assert (23 == 42);
111   }
112
113 #undef APPEND
114
115   status = format_values (value_ptr, value_size, ds, vl, /* store rates = */ 0);
116   pthread_mutex_lock (&node->lock);
117   if (status != 0)
118     return (status);
119
120   if (node->conn == NULL)
121   {
122     node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
123     if (node->conn == NULL)
124     {
125       ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: Unkown reason",
126           (node->host != NULL) ? node->host : "localhost",
127           (node->port != 0) ? node->port : 6379);
128       pthread_mutex_unlock (&node->lock);
129       return (-1);
130     }
131     else if (node->conn->err)
132     {
133       ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: %s",
134           (node->host != NULL) ? node->host : "localhost",
135           (node->port != 0) ? node->port : 6379,
136           node->conn->errstr);
137       pthread_mutex_unlock (&node->lock);
138       return (-1);
139     }
140   }
141
142   rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
143   if (rr==NULL)
144     WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
145
146   rr = redisCommand (node->conn, "SADD collectd/values %s", ident);
147   if (rr==NULL)
148     WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
149
150   pthread_mutex_unlock (&node->lock);
151
152   return (0);
153 } /* }}} int wr_write */
154
155 static void wr_config_free (void *ptr) /* {{{ */
156 {
157   wr_node_t *node = ptr;
158
159   if (node == NULL)
160     return;
161
162   if (node->conn != NULL)
163   {
164     redisFree (node->conn);
165     node->conn = NULL;
166   }
167
168   sfree (node->host);
169   sfree (node);
170 } /* }}} void wr_config_free */
171
172 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
173 {
174   wr_node_t *node;
175   int timeout;
176   int status;
177   int i;
178
179   node = malloc (sizeof (*node));
180   if (node == NULL)
181     return (ENOMEM);
182   memset (node, 0, sizeof (*node));
183   node->host = NULL;
184   node->port = 0;
185   node->timeout.tv_sec = 0;
186   node->timeout.tv_usec = 1000;
187   node->conn = NULL;
188   node->prefix = NULL;
189   pthread_mutex_init (&node->lock, /* attr = */ NULL);
190
191   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
192   if (status != 0)
193   {
194     sfree (node);
195     return (status);
196   }
197
198   for (i = 0; i < ci->children_num; i++)
199   {
200     oconfig_item_t *child = ci->children + i;
201
202     if (strcasecmp ("Host", child->key) == 0)
203       status = cf_util_get_string (child, &node->host);
204     else if (strcasecmp ("Port", child->key) == 0)
205     {
206       status = cf_util_get_port_number (child);
207       if (status > 0)
208       {
209         node->port = status;
210         status = 0;
211       }
212     }
213     else if (strcasecmp ("Timeout", child->key) == 0) {
214       status = cf_util_get_int (child, &timeout);
215       if (status == 0) node->timeout.tv_usec = timeout;
216     }
217     else if (strcasecmp ("Prefix", child->key) == 0) {
218       status = cf_util_get_string (child, &node->prefix);
219     }
220     else
221       WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
222           child->key);
223
224     if (status != 0)
225       break;
226   } /* for (i = 0; i < ci->children_num; i++) */
227
228   if (status == 0)
229   {
230     char cb_name[DATA_MAX_NAME_LEN];
231     user_data_t ud;
232
233     ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
234
235     ud.data = node;
236     ud.free_func = wr_config_free;
237
238     status = plugin_register_write (cb_name, wr_write, &ud);
239   }
240
241   if (status != 0)
242     wr_config_free (node);
243
244   return (status);
245 } /* }}} int wr_config_node */
246
247 static int wr_config (oconfig_item_t *ci) /* {{{ */
248 {
249   int i;
250
251   for (i = 0; i < ci->children_num; i++)
252   {
253     oconfig_item_t *child = ci->children + i;
254
255     if (strcasecmp ("Node", child->key) == 0)
256       wr_config_node (child);
257     else
258       WARNING ("write_redis plugin: Ignoring unknown "
259           "configuration option \"%s\" at top level.", child->key);
260   }
261
262   return (0);
263 } /* }}} int wr_config */
264
265 void module_register (void)
266 {
267   plugin_register_complex_config ("write_redis", wr_config);
268 }
269
270 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */