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