Code

Fix a number of minor bugs.
[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  **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "utils_avltree.h"
29 #include <pthread.h>
30 #include <credis.h>
32 #define REDIS_DEF_HOST "127.0.0.1"
33 #define REDIS_DEF_PORT 6379
34 #define MAX_REDIS_NODE_NAME 64
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  */
47 static c_avl_tree_t *redis_tree = NULL;
48 static pthread_mutex_t redis_lock = PTHREAD_MUTEX_INITIALIZER;
50 typedef struct redis_node_s {
51   char name[MAX_REDIS_NODE_NAME];
52   char host[HOST_NAME_MAX];
53   int port;
54   int timeout;
55 } redis_node_t;
57 static int redis_config_node (redis_node_t *rn, oconfig_item_t *ci) /* {{{ */
58 {
59   int i;
60   int status = 0;
62   if ((ci->values_num != 1)
63       || (ci->values[0].type != OCONFIG_TYPE_STRING))
64   {
65     WARNING ("redis plugin: The `Node' block needs exactly one string "
66         "argument.");
67     return (-1);
68   }
70   if (ci->children_num < 1)
71   {
72     WARNING ("redis plugin: The `Node' block needs at least one option.");
73     return (-1);
74   }
76   sstrncpy (rn->name, ci->values[0].value.string, sizeof (rn->name));
78   for (i = 0; i < ci->children_num; i++)
79   {
80     oconfig_item_t *option = ci->children + i;
81     status = 0;
83     if (strcasecmp ("Host", option->key) == 0)
84       status = cf_util_get_string_buffer (option, rn->host, sizeof (rn->host));
85     else if (strcasecmp ("Port", option->key) == 0)
86       rn->port = cf_util_get_port_number (option);
87     else if (strcasecmp ("Timeout", option->key) == 0)
88       status = cf_util_get_int (option, &rn->timeout);
89     else
90     {
91       WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
92           "block.", option->key);
93       status = -1;
94     }
96     if (status != 0)
97       break;
98   }
100   return (status);
101 } /* }}} */
103 static redis_node_t *redis_node_get (const char *name, redis_node_t *rn) /* {{{ */
105   if (c_avl_get (redis_tree, name, (void *) rn) == 0)
106     return (rn);
107   else
108     return (NULL);
109 } /* }}} */
111 static int redis_node_add (const redis_node_t *rn) /* {{{ */
113   int status;
114   redis_node_t *rn_copy = NULL;
115   redis_node_t *rn_ptr;
116   redis_node_t  rn_get;
118   rn_copy = (redis_node_t *) malloc (sizeof (redis_node_t));
119   if (rn_copy == NULL)
120   {
121     sfree (rn_copy);
122     ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
123     return (-1);
124   }
125   memcpy (rn_copy, rn, sizeof (redis_node_t));
126   if (*rn_copy->name == '\0')
127   {
128     (void) strncpy(rn_copy->name, "default", sizeof (rn_copy->name)); /* in theory never fails */
129   }
131   DEBUG ("redis plugin: adding entry `%s' to the tree.", rn_copy->name);
133   pthread_mutex_lock (&redis_lock);
135   if ( (rn_ptr = redis_node_get (rn_copy->name, &rn_get)) != NULL )
136   {
137     WARNING ("redis plugin: the node `%s' override a previous node with same node.", rn_copy->name);
138   }
140   status = c_avl_insert (redis_tree, rn_copy->name, rn_copy);
141   pthread_mutex_unlock (&redis_lock);
143   if (status != 0)
144   {
145     ERROR ("redis plugin: c_avl_insert (%s) failed adding noew node.", rn_copy->name);
146     sfree (rn_copy);
147     return (-1);
148   }
150   return (status);
151 } /* }}} */
153 static int redis_config (oconfig_item_t *ci) /* {{{ */
155   int status;
156   int i;
158   redis_node_t rn = {
159     .name = "",
160     .host = "",
161     .port = REDIS_DEF_PORT,
162     .timeout = 2000
163   };
165   if (redis_tree == NULL)
166   {
167     redis_tree = c_avl_create ((void *) strcmp);
168     if (redis_tree == NULL)
169     {
170       ERROR ("redis plugin: c_avl_create failed reading config.");
171       return (-1);
172     }
173   }
175   status = 0;
176   for (i = 0; i < ci->children_num; i++)
177   {
178     oconfig_item_t *option = ci->children + i;
180     if (strcasecmp ("Node", option->key) == 0)
181     {
182       if ( (status = redis_config_node (&rn, option)) == 0 )
183         status = redis_node_add (&rn);
184     }
185     else
186     {
187       WARNING ("redis plugin: Option `%s' not allowed in redis"
188           " configuration.", option->key);
189       status = -1;
190     }
193     if (status != 0)
194       break;
195   }
197   return (status);
198 } /* }}} */
200   __attribute__ ((nonnull(2)))
201 static void redis_submit_g (char *plugin_instance,
202     const char *type, const char *type_instance,
203     gauge_t value) /* {{{ */
205   value_t values[1];
206   value_list_t vl = VALUE_LIST_INIT;
208   values[0].gauge = value;
210   vl.values = values;
211   vl.values_len = 1;
212   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
213   sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
214   if (plugin_instance != NULL)
215     sstrncpy (vl.plugin_instance, plugin_instance,
216         sizeof (vl.plugin_instance));
217   sstrncpy (vl.type, type, sizeof (vl.type));
218   if (type_instance != NULL)
219     sstrncpy (vl.type_instance, type_instance,
220         sizeof (vl.type_instance));
222   plugin_dispatch_values (&vl);
223 } /* }}} */
225   __attribute__ ((nonnull(2)))
226 static void redis_submit_c (char *plugin_instance,
227     const char *type, const char *type_instance,
228     counter_t value) /* {{{ */
230   value_t values[1];
231   value_list_t vl = VALUE_LIST_INIT;
233   values[0].counter = value;
235   vl.values = values;
236   vl.values_len = 1;
237   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
238   sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
239   if (plugin_instance != NULL)
240     sstrncpy (vl.plugin_instance, plugin_instance,
241         sizeof (vl.plugin_instance));
242   sstrncpy (vl.type, type, sizeof (vl.type));
243   if (type_instance != NULL)
244     sstrncpy (vl.type_instance, type_instance,
245         sizeof (vl.type_instance));
247   plugin_dispatch_values (&vl);
248 } /* }}} */
250 static int redis_read (void) /* {{{ */
252   REDIS rh;
253   REDIS_INFO info;
255   char key[64];
256   int status;
257   c_avl_iterator_t *iter;
258   redis_node_t *rn;
260   status = -1;
261   if ( (iter = c_avl_get_iterator (redis_tree)) == NULL )
262   {
263     ERROR ("redis plugin: unable to iterate redis tree.");
264     return (-1);
265   }
267   while (c_avl_iterator_next (iter, (void *) &key, (void *) &rn) == 0)
268   {
269     DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
271     if ( (rh = credis_connect (rn->host, rn->port, rn->timeout)) == NULL )
272     {
273       ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
274       status = -1;
275       break;
276     }
278     if ( (status = credis_info (rh, &info)) == -1 )
279     {
280       WARNING ("redis plugin: unable to get info from node `%s'.", rn->name);
281       credis_close (rh);
282       break;
283     }
285     /* typedef struct _cr_info {
286      *   char redis_version[CREDIS_VERSION_STRING_SIZE];
287      *   int bgsave_in_progress;
288      *   int connected_clients;
289      *   int connected_slaves;
290      *   unsigned int used_memory;
291      *   long long changes_since_last_save;
292      *   int last_save_time;
293      *   long long total_connections_received;
294      *   long long total_commands_processed;
295      *   int uptime_in_seconds;
296      *   int uptime_in_days;
297      *   int role;
298      * } REDIS_INFO; */
300     DEBUG ("redis plugin: received info from node `%s': connected_clients = %d; "
301         "connected_slaves = %d; used_memory = %lu; changes_since_last_save = %lld; "
302         "bgsave_in_progress = %d; total_connections_received = %lld; "
303         "total_commands_processed = %lld; uptime_in_seconds = %ld", rn->name,
304         info.connected_clients, info.connected_slaves, info.used_memory,
305         info.changes_since_last_save, info.bgsave_in_progress,
306         info.total_connections_received, info.total_commands_processed,
307         info.uptime_in_seconds);
309     redis_submit_g (rn->name, "connected_clients", NULL, info.connected_clients);
310     redis_submit_g (rn->name, "connected_slaves", NULL, info.connected_slaves);
311     redis_submit_g (rn->name, "used_memory", NULL, info.used_memory);
312     redis_submit_g (rn->name, "changes_since_last_save", NULL, info.changes_since_last_save);
313     redis_submit_g (rn->name, "bgsave_in_progress", NULL, info.bgsave_in_progress);
314     redis_submit_c (rn->name, "total_connections_received", NULL, info.total_connections_received);
315     redis_submit_c (rn->name, "total_commands_processed", NULL, info.total_commands_processed);
316     redis_submit_c (rn->name, "uptime_in_seconds", NULL, info.uptime_in_seconds);
318     credis_close (rh);
319     status = 0;
320   }
322   c_avl_iterator_destroy(iter);
323   if ( status != 0 )
324   {
325     return (-1);
326   }
328   return 0;
330 /* }}} */
332 void module_register (void) /* {{{ */
334   plugin_register_complex_config ("redis", redis_config);
335   plugin_register_read ("redis", redis_read);
336   /* TODO: plugin_register_write: one redis list per value id with
337    * X elements */
339 /* }}} */
341 /* vim: set sw=2 sts=2 et fdm=marker : */