5eed268abf6b494459183e6b6442719fa5d1eebf
[collectd.git] / src / redis.c
1 /**
2  * collectd - src/redis.c, based on src/memcached.c
3  * Copyright (C) 2010       Andrés J. Díaz <ajdiaz@connectical.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Andrés J. Díaz <ajdiaz@connectical.com>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "utils_avltree.h"
28
29 #include <pthread.h>
30 #include <credis.h>
31
32 #define REDIS_DEF_HOST "127.0.0.1"
33 #define REDIS_DEF_PORT 6379
34 #define MAX_REDIS_NODE_NAME 64
35
36 /* Redis plugin configuration example:
37  
38    <Plugin redis>
39     <Node mynode>
40         Host localhost
41         Port 6379
42         Timeout 2000
43     </Node>
44    </Plugin>
45
46 */
47
48 static c_avl_tree_t  *redis_tree = NULL;
49 static pthread_mutex_t redis_lock = PTHREAD_MUTEX_INITIALIZER;
50
51 typedef struct redis_node_s {
52     char name[MAX_REDIS_NODE_NAME];
53     char host[HOST_NAME_MAX];
54     int port;
55     int timeout;
56 } redis_node_t;
57
58 static int redis_config_node (redis_node_t *rn, oconfig_item_t *ci) /* {{{ */
59 {
60     int i;
61     int status = 0;
62
63     if ((ci->values_num != 1)
64             || (ci->values[0].type != OCONFIG_TYPE_STRING))
65     {
66         WARNING ("redis plugin: The `Node' block needs exactly one string "
67                 "argument.");
68         return (-1);
69     }
70
71     if (ci->children_num < 1)
72     {
73         WARNING ("redis plugin: The `Node' block needs at least one option.");
74         return (-1);
75     }
76
77     sstrncpy (rn->name, ci->values[0].value.string, sizeof (rn->name));
78
79     for (i = 0; i < ci->children_num; i++)
80     {
81         oconfig_item_t *option = ci->children + i;
82         status = 0;
83
84         if (strcasecmp ("Host", option->key) == 0)
85             status = cf_util_get_string_buffer (option, rn->host, HOST_NAME_MAX);
86         else if (strcasecmp ("Port", option->key) == 0)
87             status = rn->port = cf_util_get_port_number (option);
88         else if (strcasecmp ("Timeout", option->key) == 0)
89             status = cf_util_get_int (option, &rn->timeout);
90                 else
91                 {
92                         WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
93                                         "block.", option->key);
94                         status = -1;
95                 }
96
97                 if (status != 0)
98                         break;
99         }
100
101         return (status);
102
103 } /* }}} */
104
105 static redis_node_t *redis_node_get (const char *name, redis_node_t *rn) /* {{{ */
106 {
107         if (c_avl_get (redis_tree, name, (void *) rn) == 0)
108                 return (rn);
109         else
110                 return (NULL);
111 } /* }}} */
112
113 static int redis_node_add (const redis_node_t *rn) /* {{{ */
114 {
115         int status;
116         redis_node_t *rn_copy = NULL;
117         redis_node_t *rn_ptr;
118         redis_node_t  rn_get;
119
120         rn_copy = (redis_node_t *) malloc (sizeof (redis_node_t));
121         if (rn_copy == NULL)
122         {
123                 sfree (rn_copy);
124                 ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
125                 return (-1);
126         }
127         memcpy (rn_copy, rn, sizeof (redis_node_t));
128         if (*rn_copy->name == '\0')
129         {
130                 (void) strncpy(rn_copy->name, "default", MAX_REDIS_NODE_NAME); /* in theory never fails */
131         }
132
133         DEBUG ("redis plugin: adding entry `%s' to the tree.", rn_copy->name);
134
135         pthread_mutex_lock (&redis_lock);
136
137         if ( (rn_ptr = redis_node_get (rn_copy->name, &rn_get)) != NULL )
138         {
139                 WARNING ("redis plugin: the node `%s' override a previous node with same node.", rn_copy->name);
140         }
141
142         status = c_avl_insert (redis_tree, rn_copy->name, rn_copy);
143         pthread_mutex_unlock (&redis_lock);
144
145         if (status != 0)
146         {
147                 ERROR ("redis plugin: c_avl_insert (%s) failed adding noew node.", rn_copy->name);
148                 sfree (rn_copy);
149                 return (-1);
150         }
151
152         return (status);
153 } /* }}} */
154
155 static int redis_config (oconfig_item_t *ci) /* {{{ */
156 {
157     int status;
158     int i;
159
160     redis_node_t rn = {
161         .name = "",
162         .host = "",
163         .port = REDIS_DEF_PORT,
164         .timeout = 2000
165     };
166
167     if (redis_tree == NULL)
168     {
169         redis_tree = c_avl_create ((void *) strcmp);
170         if (redis_tree == NULL)
171         {
172             ERROR ("redis plugin: c_avl_create failed reading config.");
173             return (-1);
174         }
175     }
176
177     status = 0;
178     for (i = 0; i < ci->children_num; i++)
179     {
180         oconfig_item_t *option = ci->children + i;
181
182         if (strcasecmp ("Node", option->key) == 0)
183                 {
184                         if ( (status = redis_config_node (&rn, option)) == 0 )
185                                 status = redis_node_add (&rn);
186                 }
187                 else if (strcasecmp ("Host", option->key) == 0)
188             status = cf_util_get_string_buffer (option, rn.host, HOST_NAME_MAX);
189         else if (strcasecmp ("Port", option->key) == 0)
190             status = rn.port = cf_util_get_port_number (option);
191         else if (strcasecmp ("Timeout", option->key) == 0)
192             status = cf_util_get_int (option, &rn.timeout);
193                 else
194                 {
195                         WARNING ("redis plugin: Option `%s' not allowed in redis"
196                                         " configuration.", option->key);
197                         status = -1;
198                 }
199
200
201         if (status != 0)
202             break;
203     }
204
205     if ( status == 0 && *rn.name != '\0') {
206         status = redis_node_add (&rn);
207     }
208
209     return (status);
210 } /* }}} */
211
212 __attribute__ ((nonnull(2)))
213 static void redis_submit_g (char *plugin_instance,
214                 const char *type, const char *type_instance,
215                 gauge_t value) /* {{{ */
216 {
217         value_t values[1];
218         value_list_t vl = VALUE_LIST_INIT;
219
220         values[0].gauge = value;
221
222         vl.values = values;
223         vl.values_len = 1;
224         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
225         sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
226         if (plugin_instance != NULL)
227                 sstrncpy (vl.plugin_instance, plugin_instance,
228                                 sizeof (vl.plugin_instance));
229         sstrncpy (vl.type, type, sizeof (vl.type));
230         if (type_instance != NULL)
231                 sstrncpy (vl.type_instance, type_instance,
232                                 sizeof (vl.type_instance));
233
234         plugin_dispatch_values (&vl);
235 } /* }}} */
236
237 __attribute__ ((nonnull(2)))
238 static void redis_submit_c (char *plugin_instance,
239                 const char *type, const char *type_instance,
240                 counter_t value) /* {{{ */
241 {
242         value_t values[1];
243         value_list_t vl = VALUE_LIST_INIT;
244
245         values[0].counter = value;
246
247         vl.values = values;
248         vl.values_len = 1;
249         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
250         sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
251         if (plugin_instance != NULL)
252                 sstrncpy (vl.plugin_instance, plugin_instance,
253                                 sizeof (vl.plugin_instance));
254         sstrncpy (vl.type, type, sizeof (vl.type));
255         if (type_instance != NULL)
256                 sstrncpy (vl.type_instance, type_instance,
257                                 sizeof (vl.type_instance));
258
259         plugin_dispatch_values (&vl);
260 } /* }}} */
261
262 static int redis_read (void) /* {{{ */
263 {
264     REDIS rh;
265     REDIS_INFO info;
266
267     char key[64];
268     int status;
269     c_avl_iterator_t *iter;
270     redis_node_t *rn;
271
272         status = -1;
273         if ( (iter = c_avl_get_iterator (redis_tree)) == NULL )
274         {
275                 ERROR ("redis plugin: unable to iterate redis tree.");
276                 return (-1);
277         }
278
279         while (c_avl_iterator_next (iter, (void *) &key, (void *) &rn) == 0)
280         {
281                 DEBUG ("redis plugin: querying info from node `%s'.", rn->name);
282
283                 if ( (rh = credis_connect (rn->host, rn->port, rn->timeout)) == NULL )
284                 {
285                         ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
286                         status = -1;
287                         break;
288                 }
289
290                 if ( (status = credis_info (rh, &info)) == -1 )
291                 {
292                         WARNING ("redis plugin: unable to get info from node `%s'.", rn->name);
293                         credis_close (rh);
294                         break;
295                 }
296
297                 /* typedef struct _cr_info {
298                  *   char redis_version[CREDIS_VERSION_STRING_SIZE];
299                  *   int bgsave_in_progress;
300                  *   int connected_clients;
301                  *   int connected_slaves;
302                  *   unsigned int used_memory;
303                  *   long long changes_since_last_save;
304                  *   int last_save_time;
305                  *   long long total_connections_received;
306                  *   long long total_commands_processed;
307                  *   int uptime_in_seconds;
308                  *   int uptime_in_days;
309                  *   int role;
310                  * } REDIS_INFO; */
311
312                 DEBUG ("redis plugin: received info from node `%s': connected_clients = %d; "
313                                 "connected_slaves = %d; used_memory = %lu; changes_since_last_save = %lld; "
314                                 "bgsave_in_progress = %d; total_connections_received = %lld; "
315                                 "total_commands_processed = %lld; uptime_in_seconds = %ld", rn->name,
316                                 info.connected_clients, info.connected_slaves, info.used_memory,
317                                 info.changes_since_last_save, info.bgsave_in_progress,
318                                 info.total_connections_received, info.total_commands_processed,
319                                 info.uptime_in_seconds);
320
321                 redis_submit_g (rn->name, "connected_clients", NULL, info.connected_clients);
322                 redis_submit_g (rn->name, "connected_slaves", NULL, info.connected_slaves);
323                 redis_submit_g (rn->name, "used_memory", NULL, info.used_memory);
324                 redis_submit_g (rn->name, "changes_since_last_save", NULL, info.changes_since_last_save);
325                 redis_submit_g (rn->name, "bgsave_in_progress", NULL, info.bgsave_in_progress);
326                 redis_submit_c (rn->name, "total_connections_received", NULL, info.total_connections_received);
327                 redis_submit_c (rn->name, "total_commands_processed", NULL, info.total_commands_processed);
328                 redis_submit_c (rn->name, "uptime_in_seconds", NULL, info.uptime_in_seconds);
329
330                 credis_close (rh);
331                 status = 0;
332         }
333
334         c_avl_iterator_destroy(iter);
335         if ( status != 0 )
336         {
337                 return (-1);
338         }
339
340         return 0;
341 }
342 /* }}} */
343
344
345 void module_register (void) /* {{{ */
346 {
347         plugin_register_complex_config ("redis", redis_config);
348         plugin_register_read ("redis", redis_read);
349         /* TODO: plugin_register_write: one redis list per value id with
350          * X elements */
351 }
352 /* }}} */
353