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