redis plugin: Simplify configuration handling.
[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   "localhost"
33 #define REDIS_DEF_PORT    6379
34 #define REDIS_DEF_TIMEOUT 2000
35 #define MAX_REDIS_NODE_NAME 64
36
37 /* Redis plugin configuration example:
38  *
39  * <Plugin redis>
40  *   <Node "mynode">
41  *     Host "localhost"
42  *     Port "6379"
43  *     Timeout 2000
44  *   </Node>
45  * </Plugin>
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 redis_node_t *redis_node_get (const char *name, redis_node_t *rn) /* {{{ */
59 {
60   if (c_avl_get (redis_tree, name, (void *) rn) == 0)
61     return (rn);
62   else
63     return (NULL);
64 } /* }}} */
65
66 static int redis_node_add (const redis_node_t *rn) /* {{{ */
67 {
68   int status;
69   redis_node_t *rn_copy = NULL;
70   redis_node_t *rn_ptr;
71   redis_node_t  rn_get;
72
73   if (redis_tree == NULL)
74   {
75     redis_tree = c_avl_create ((void *) strcmp);
76     if (redis_tree == NULL)
77     {
78       ERROR ("redis plugin: c_avl_create failed.");
79       return (-1);
80     }
81   }
82
83   rn_copy = malloc (sizeof (*rn_copy));
84   if (rn_copy == NULL)
85   {
86     ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
87     return (-1);
88   }
89
90   memcpy (rn_copy, rn, sizeof (*rn_copy));
91   if (rn_copy->name[0] == 0)
92   {
93     /* in theory never fails */
94     (void) strncpy(rn_copy->name, "default", sizeof (rn_copy->name));
95   }
96
97   DEBUG ("redis plugin: adding entry `%s' to the tree.", rn_copy->name);
98
99   pthread_mutex_lock (&redis_lock);
100
101   rn_ptr = redis_node_get (rn_copy->name, &rn_get);
102   if (rn_ptr != NULL)
103   {
104     pthread_mutex_unlock (&redis_lock);
105     ERROR ("redis plugin: A node with the name `%s' already exists.",
106         rn_copy->name);
107     sfree (rn_copy);
108     return (-1);
109   }
110
111   status = c_avl_insert (redis_tree, rn_copy->name, rn_copy);
112   pthread_mutex_unlock (&redis_lock);
113
114   if (status != 0)
115   {
116     ERROR ("redis plugin: c_avl_insert (%s) failed adding new node.",
117         rn_copy->name);
118     sfree (rn_copy);
119     return (status);
120   }
121
122   return (0);
123 } /* }}} */
124
125 static int redis_config_node (oconfig_item_t *ci) /* {{{ */
126 {
127   redis_node_t rn;
128   int i;
129   int status;
130
131   memset (&rn, 0, sizeof (rn));
132   sstrncpy (rn.host, REDIS_DEF_HOST, sizeof (rn.host));
133   rn.port = REDIS_DEF_PORT;
134   rn.timeout = REDIS_DEF_TIMEOUT;
135
136   status = cf_util_get_string_buffer (ci, rn.name, sizeof (rn.name));
137   if (status != 0)
138     return (status);
139
140   for (i = 0; i < ci->children_num; i++)
141   {
142     oconfig_item_t *option = ci->children + i;
143
144     if (strcasecmp ("Host", option->key) == 0)
145       status = cf_util_get_string_buffer (option, rn.host, sizeof (rn.host));
146     else if (strcasecmp ("Port", option->key) == 0)
147     {
148       status = cf_util_get_port_number (option);
149       if (status > 0)
150       {
151         rn.port = status;
152         status = 0;
153       }
154     }
155     else if (strcasecmp ("Timeout", option->key) == 0)
156       status = cf_util_get_int (option, &rn.timeout);
157     else
158       WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
159           "block. I'll ignore this option.", option->key);
160
161     if (status != 0)
162       break;
163   }
164
165   if (status != 0)
166     return (status);
167
168   return (redis_node_add (&rn));
169 } /* }}} int redis_config_node */
170
171 static int redis_config (oconfig_item_t *ci) /* {{{ */
172 {
173   int i;
174
175   for (i = 0; i < ci->children_num; i++)
176   {
177     oconfig_item_t *option = ci->children + i;
178
179     if (strcasecmp ("Node", option->key) == 0)
180       redis_config_node (option);
181     else
182       WARNING ("redis plugin: Option `%s' not allowed in redis"
183           " configuration. It will be ignored.", option->key);
184   }
185
186   if (redis_tree == NULL)
187   {
188     ERROR ("redis plugin: No valid node configuration could be found.");
189     return (ENOENT);
190   }
191
192   return (0);
193 } /* }}} */
194
195   __attribute__ ((nonnull(2)))
196 static void redis_submit_g (char *plugin_instance,
197     const char *type, const char *type_instance,
198     gauge_t value) /* {{{ */
199 {
200   value_t values[1];
201   value_list_t vl = VALUE_LIST_INIT;
202
203   values[0].gauge = value;
204
205   vl.values = values;
206   vl.values_len = 1;
207   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
208   sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
209   if (plugin_instance != NULL)
210     sstrncpy (vl.plugin_instance, plugin_instance,
211         sizeof (vl.plugin_instance));
212   sstrncpy (vl.type, type, sizeof (vl.type));
213   if (type_instance != NULL)
214     sstrncpy (vl.type_instance, type_instance,
215         sizeof (vl.type_instance));
216
217   plugin_dispatch_values (&vl);
218 } /* }}} */
219
220   __attribute__ ((nonnull(2)))
221 static void redis_submit_c (char *plugin_instance,
222     const char *type, const char *type_instance,
223     counter_t value) /* {{{ */
224 {
225   value_t values[1];
226   value_list_t vl = VALUE_LIST_INIT;
227
228   values[0].counter = value;
229
230   vl.values = values;
231   vl.values_len = 1;
232   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
233   sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
234   if (plugin_instance != NULL)
235     sstrncpy (vl.plugin_instance, plugin_instance,
236         sizeof (vl.plugin_instance));
237   sstrncpy (vl.type, type, sizeof (vl.type));
238   if (type_instance != NULL)
239     sstrncpy (vl.type_instance, type_instance,
240         sizeof (vl.type_instance));
241
242   plugin_dispatch_values (&vl);
243 } /* }}} */
244
245 static int redis_read (void) /* {{{ */
246 {
247   REDIS rh;
248   REDIS_INFO info;
249
250   char key[64];
251   int status;
252   c_avl_iterator_t *iter;
253   redis_node_t *rn;
254
255   status = -1;
256   if ( (iter = c_avl_get_iterator (redis_tree)) == NULL )
257   {
258     ERROR ("redis plugin: unable to iterate redis tree.");
259     return (-1);
260   }
261
262   while (c_avl_iterator_next (iter, (void *) &key, (void *) &rn) == 0)
263   {
264     DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
265
266     if ( (rh = credis_connect (rn->host, rn->port, rn->timeout)) == NULL )
267     {
268       ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
269       status = -1;
270       break;
271     }
272
273     if ( (status = credis_info (rh, &info)) == -1 )
274     {
275       WARNING ("redis plugin: unable to get info from node `%s'.", rn->name);
276       credis_close (rh);
277       break;
278     }
279
280     /* typedef struct _cr_info {
281      *   char redis_version[CREDIS_VERSION_STRING_SIZE];
282      *   int bgsave_in_progress;
283      *   int connected_clients;
284      *   int connected_slaves;
285      *   unsigned int used_memory;
286      *   long long changes_since_last_save;
287      *   int last_save_time;
288      *   long long total_connections_received;
289      *   long long total_commands_processed;
290      *   int uptime_in_seconds;
291      *   int uptime_in_days;
292      *   int role;
293      * } REDIS_INFO; */
294
295     DEBUG ("redis plugin: received info from node `%s': connected_clients = %d; "
296         "connected_slaves = %d; used_memory = %lu; changes_since_last_save = %lld; "
297         "bgsave_in_progress = %d; total_connections_received = %lld; "
298         "total_commands_processed = %lld; uptime_in_seconds = %ld", rn->name,
299         info.connected_clients, info.connected_slaves, info.used_memory,
300         info.changes_since_last_save, info.bgsave_in_progress,
301         info.total_connections_received, info.total_commands_processed,
302         info.uptime_in_seconds);
303
304     redis_submit_g (rn->name, "connected_clients", NULL, info.connected_clients);
305     redis_submit_g (rn->name, "connected_slaves", NULL, info.connected_slaves);
306     redis_submit_g (rn->name, "used_memory", NULL, info.used_memory);
307     redis_submit_g (rn->name, "changes_since_last_save", NULL, info.changes_since_last_save);
308     redis_submit_g (rn->name, "bgsave_in_progress", NULL, info.bgsave_in_progress);
309     redis_submit_c (rn->name, "total_connections_received", NULL, info.total_connections_received);
310     redis_submit_c (rn->name, "total_commands_processed", NULL, info.total_commands_processed);
311     redis_submit_c (rn->name, "uptime_in_seconds", NULL, info.uptime_in_seconds);
312
313     credis_close (rh);
314     status = 0;
315   }
316
317   c_avl_iterator_destroy(iter);
318   if ( status != 0 )
319   {
320     return (-1);
321   }
322
323   return 0;
324 }
325 /* }}} */
326
327 void module_register (void) /* {{{ */
328 {
329   plugin_register_complex_config ("redis", redis_config);
330   plugin_register_read ("redis", redis_read);
331   /* TODO: plugin_register_write: one redis list per value id with
332    * X elements */
333 }
334 /* }}} */
335
336 /* vim: set sw=2 sts=2 et fdm=marker : */