write_redis: re-add colon dropped in b7984797
[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   size_t value_size;
61   char *value_ptr;
62   int status;
63   redisReply   *rr;
64   int i;
65
66   status = FORMAT_VL (ident, sizeof (ident), vl);
67   if (status != 0)
68     return (status);
69   ssnprintf (key, sizeof (key), "collectd/%s", ident);
70
71   memset (value, 0, sizeof (value));
72   value_size = sizeof (value);
73   value_ptr = &value[0];
74
75 #define APPEND(...) do {                                             \
76   status = snprintf (value_ptr, value_size, __VA_ARGS__);            \
77   if (((size_t) status) > value_size)                                \
78   {                                                                  \
79     value_ptr += value_size;                                         \
80     value_size = 0;                                                  \
81   }                                                                  \
82   else                                                               \
83   {                                                                  \
84     value_ptr += status;                                             \
85     value_size -= status;                                            \
86   }                                                                  \
87 } while (0)
88
89   APPEND ("%lu:", (unsigned long) vl->time);
90   for (i = 0; i < ds->ds_num; i++)
91   {
92     if (ds->ds[i].type == DS_TYPE_COUNTER)
93       APPEND ("%llu", vl->values[i].counter);
94     else if (ds->ds[i].type == DS_TYPE_GAUGE)
95       APPEND ("%g", vl->values[i].gauge);
96     else if (ds->ds[i].type == DS_TYPE_DERIVE)
97       APPEND ("%"PRIi64, vl->values[i].derive);
98     else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
99       APPEND ("%"PRIu64, vl->values[i].absolute);
100     else
101       assert (23 == 42);
102   }
103
104 #undef APPEND
105
106   pthread_mutex_lock (&node->lock);
107
108   if (node->conn == NULL)
109   {
110     node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
111     if (node->conn == NULL)
112     {
113       ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed.",
114           (node->host != NULL) ? node->host : "localhost",
115           (node->port != 0) ? node->port : 6379);
116       pthread_mutex_unlock (&node->lock);
117       return (-1);
118     }
119   }
120
121   assert (node->conn != NULL);
122   rr = redisCommand (node->conn, "ZADD %b %f %b", key, sizeof (key),
123     (double) vl->time, value, sizeof (value));
124   if (rr==NULL)
125     WARNING("ZADD command error. key:%s", key);
126
127   rr = redisCommand (node->conn, "SADD collectd/values %b", ident, sizeof(ident));
128   if (rr==NULL)
129     WARNING("SADD command error. ident:%s", ident);
130
131   pthread_mutex_unlock (&node->lock);
132
133   return (0);
134 } /* }}} int wr_write */
135
136 static void wr_config_free (void *ptr) /* {{{ */
137 {
138   wr_node_t *node = ptr;
139
140   if (node == NULL)
141     return;
142
143   if (node->conn != NULL)
144   {
145     redisFree (node->conn);
146     node->conn = NULL;
147   }
148
149   sfree (node->host);
150   sfree (node);
151 } /* }}} void wr_config_free */
152
153 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
154 {
155   wr_node_t *node;
156   int timeout;
157   int status;
158   int i;
159
160   node = malloc (sizeof (*node));
161   if (node == NULL)
162     return (ENOMEM);
163   memset (node, 0, sizeof (*node));
164   node->host = NULL;
165   node->port = 0;
166   node->timeout.tv_sec = 0;
167   node->timeout.tv_usec = 1000;
168   node->conn = NULL;
169   pthread_mutex_init (&node->lock, /* attr = */ NULL);
170
171   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
172   if (status != 0)
173   {
174     sfree (node);
175     return (status);
176   }
177
178   for (i = 0; i < ci->children_num; i++)
179   {
180     oconfig_item_t *child = ci->children + i;
181
182     if (strcasecmp ("Host", child->key) == 0)
183       status = cf_util_get_string (child, &node->host);
184     else if (strcasecmp ("Port", child->key) == 0)
185     {
186       status = cf_util_get_port_number (child);
187       if (status > 0)
188       {
189         node->port = status;
190         status = 0;
191       }
192     }
193     else if (strcasecmp ("Timeout", child->key) == 0) {
194       status = cf_util_get_int (child, &timeout);
195       if (status == 0) node->timeout.tv_usec = timeout;
196     }
197     else
198       WARNING ("write_redis 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_redis/%s", node->name);
211
212     ud.data = node;
213     ud.free_func = wr_config_free;
214
215     status = plugin_register_write (cb_name, wr_write, &ud);
216   }
217
218   if (status != 0)
219     wr_config_free (node);
220
221   return (status);
222 } /* }}} int wr_config_node */
223
224 static int wr_config (oconfig_item_t *ci) /* {{{ */
225 {
226   int i;
227
228   for (i = 0; i < ci->children_num; i++)
229   {
230     oconfig_item_t *child = ci->children + i;
231
232     if (strcasecmp ("Node", child->key) == 0)
233       wr_config_node (child);
234     else
235       WARNING ("write_redis plugin: Ignoring unknown "
236           "configuration option \"%s\" at top level.", child->key);
237   }
238
239   return (0);
240 } /* }}} int wr_config */
241
242 void module_register (void)
243 {
244   plugin_register_complex_config ("write_redis", wr_config);
245 }
246
247 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */