Code

Merge pull request #728 from mbethke/test-and-clean
[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"
28 #include <pthread.h>
29 #include <sys/time.h>
30 #include <hiredis/hiredis.h>
32 #ifndef HOST_NAME_MAX
33 # define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
34 #endif
36 #define REDIS_DEF_HOST   "localhost"
37 #define REDIS_DEF_PASSWD ""
38 #define REDIS_DEF_PORT    6379
39 #define REDIS_DEF_TIMEOUT 2000
40 #define MAX_REDIS_NODE_NAME 64
41 #define MAX_REDIS_PASSWD_LENGTH 512
42 #define MAX_REDIS_VAL_SIZE 256
44 /* Redis plugin configuration example:
45  *
46  * <Plugin redis>
47  *   <Node "mynode">
48  *     Host "localhost"
49  *     Port "6379"
50  *     Timeout 2
51  *     Password "foobar"
52  *   </Node>
53  * </Plugin>
54  */
56 struct redis_node_s;
57 typedef struct redis_node_s redis_node_t;
58 struct redis_node_s
59 {
60   char name[MAX_REDIS_NODE_NAME];
61   char host[HOST_NAME_MAX];
62   char passwd[MAX_REDIS_PASSWD_LENGTH];
63   int port;
64   struct timeval timeout;
66   redis_node_t *next;
67 };
69 static redis_node_t *nodes_head = NULL;
71 static int redis_node_add (const redis_node_t *rn) /* {{{ */
72 {
73   redis_node_t *rn_copy;
74   redis_node_t *rn_ptr;
76   /* Check for duplicates first */
77   for (rn_ptr = nodes_head; rn_ptr != NULL; rn_ptr = rn_ptr->next)
78     if (strcmp (rn->name, rn_ptr->name) == 0)
79       break;
81   if (rn_ptr != NULL)
82   {
83     ERROR ("redis plugin: A node with the name `%s' already exists.",
84         rn->name);
85     return (-1);
86   }
88   rn_copy = malloc (sizeof (*rn_copy));
89   if (rn_copy == NULL)
90   {
91     ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
92     return (-1);
93   }
95   memcpy (rn_copy, rn, sizeof (*rn_copy));
96   rn_copy->next = NULL;
98   DEBUG ("redis plugin: Adding node \"%s\".", rn->name);
100   if (nodes_head == NULL)
101     nodes_head = rn_copy;
102   else
103   {
104     rn_ptr = nodes_head;
105     while (rn_ptr->next != NULL)
106       rn_ptr = rn_ptr->next;
107     rn_ptr->next = rn_copy;
108   }
110   return (0);
111 } /* }}} */
114 static int redis_config_node (oconfig_item_t *ci) /* {{{ */
116   redis_node_t rn;
117   int i;
118   int status;
119   int timeout;
121   memset (&rn, 0, sizeof (rn));
122   sstrncpy (rn.host, REDIS_DEF_HOST, sizeof (rn.host));
123   rn.port = REDIS_DEF_PORT;
124   rn.timeout.tv_usec = REDIS_DEF_TIMEOUT;
126   status = cf_util_get_string_buffer (ci, rn.name, sizeof (rn.name));
127   if (status != 0)
128     return (status);
130   for (i = 0; i < ci->children_num; i++)
131   {
132     oconfig_item_t *option = ci->children + i;
134     if (strcasecmp ("Host", option->key) == 0)
135       status = cf_util_get_string_buffer (option, rn.host, sizeof (rn.host));
136     else if (strcasecmp ("Port", option->key) == 0)
137     {
138       status = cf_util_get_port_number (option);
139       if (status > 0)
140       {
141         rn.port = status;
142         status = 0;
143       }
144     }
145     else if (strcasecmp ("Timeout", option->key) == 0)
146     {
147       status = cf_util_get_int (option, &timeout);
148       if (status == 0) rn.timeout.tv_usec = timeout;
149     }
150     else if (strcasecmp ("Password", option->key) == 0)
151       status = cf_util_get_string_buffer (option, rn.passwd, sizeof (rn.passwd));
152     else
153       WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
154           "block. I'll ignore this option.", option->key);
156     if (status != 0)
157       break;
158   }
160   if (status != 0)
161     return (status);
163   return (redis_node_add (&rn));
164 } /* }}} int redis_config_node */
166 static int redis_config (oconfig_item_t *ci) /* {{{ */
168   int i;
170   for (i = 0; i < ci->children_num; i++)
171   {
172     oconfig_item_t *option = ci->children + i;
174     if (strcasecmp ("Node", option->key) == 0)
175       redis_config_node (option);
176     else
177       WARNING ("redis plugin: Option `%s' not allowed in redis"
178           " configuration. It will be ignored.", option->key);
179   }
181   if (nodes_head == NULL)
182   {
183     ERROR ("redis plugin: No valid node configuration could be found.");
184     return (ENOENT);
185   }
187   return (0);
188 } /* }}} */
190   __attribute__ ((nonnull(2)))
191 static void redis_submit (char *plugin_instance,
192     const char *type, const char *type_instance,
193     value_t value) /* {{{ */
195   value_t values[1];
196   value_list_t vl = VALUE_LIST_INIT;
198   values[0] = value;
200   vl.values = values;
201   vl.values_len = 1;
202   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
203   sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
204   if (plugin_instance != NULL)
205     sstrncpy (vl.plugin_instance, plugin_instance,
206         sizeof (vl.plugin_instance));
207   sstrncpy (vl.type, type, sizeof (vl.type));
208   if (type_instance != NULL)
209     sstrncpy (vl.type_instance, type_instance,
210         sizeof (vl.type_instance));
212   plugin_dispatch_values (&vl);
213 } /* }}} */
215 static int redis_init (void) /* {{{ */
217   redis_node_t rn = {
218     .name = "default",
219     .host = REDIS_DEF_HOST,
220     .port = REDIS_DEF_PORT,
221     .timeout.tv_sec = 0,
222     .timeout.tv_usec = REDIS_DEF_TIMEOUT,
223     .next = NULL
224 };
226   if (nodes_head == NULL)
227     redis_node_add (&rn);
229   return (0);
230 } /* }}} int redis_init */
232 int redis_handle_info (char *node, char const *info_line, char const *type, char const *type_instance, char const *field_name, int ds_type) /* {{{ */
234   char *str = strstr (info_line, field_name);
235   static char buf[MAX_REDIS_VAL_SIZE];
236   value_t val;
237   if (str)
238   {
239     int i;
241     str += strlen (field_name) + 1; /* also skip the ':' */
242     for(i=0;(*str && (isdigit(*str) || *str == '.'));i++,str++)
243       buf[i] = *str;
244     buf[i] ='\0';
246     if(parse_value (buf, &val, ds_type) == -1)
247     {
248       WARNING ("redis plugin: Unable to parse field `%s'.", field_name);
249       return (-1);
250     }
252     redis_submit (node, type, type_instance, val);
253     return (0);
254   }
255   return (-1);
257 } /* }}} int redis_handle_info */
259 static int redis_read (void) /* {{{ */
261   redis_node_t *rn;
263   for (rn = nodes_head; rn != NULL; rn = rn->next)
264   {
265     redisContext *rh;
266     redisReply   *rr;
268     DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
270     rh = redisConnectWithTimeout ((char *)rn->host, rn->port, rn->timeout);
271     if (rh == NULL)
272     {
273       ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
274       continue;
275     }
277     if (strlen (rn->passwd) > 0)
278     {
279       DEBUG ("redis plugin: authenticanting node `%s' passwd(%s).", rn->name, rn->passwd);
280       rr = redisCommand (rh, "AUTH %s", rn->passwd);
282       if (rr == NULL || rr->type != REDIS_REPLY_STATUS)
283       {
284         WARNING ("redis plugin: unable to authenticate on node `%s'.", rn->name);
285         if (rr != NULL)
286           freeReplyObject (rr);
288         redisFree (rh);
289         continue;
290       }
291     }
293     if ((rr = redisCommand(rh, "INFO")) == NULL)
294     {
295       WARNING ("redis plugin: unable to connect to node `%s'.", rn->name);
296       redisFree (rh);
297       continue;
298     }
300     redis_handle_info (rn->name, rr->str, "uptime", NULL, "uptime_in_seconds", DS_TYPE_GAUGE);
301     redis_handle_info (rn->name, rr->str, "current_connections", "clients", "connected_clients", DS_TYPE_GAUGE);
302     redis_handle_info (rn->name, rr->str, "blocked_clients", NULL, "blocked_clients", DS_TYPE_GAUGE);
303     redis_handle_info (rn->name, rr->str, "memory", NULL, "used_memory", DS_TYPE_GAUGE);
304     redis_handle_info (rn->name, rr->str, "memory_lua", NULL, "used_memory_lua", DS_TYPE_GAUGE);
305     /* changes_since_last_save: Deprecated in redis version 2.6 and above */
306     redis_handle_info (rn->name, rr->str, "volatile_changes", NULL, "changes_since_last_save", DS_TYPE_GAUGE);
307     redis_handle_info (rn->name, rr->str, "total_connections", NULL, "total_connections_received", DS_TYPE_DERIVE);
308     redis_handle_info (rn->name, rr->str, "total_operations", NULL, "total_commands_processed", DS_TYPE_DERIVE);
309     redis_handle_info (rn->name, rr->str, "expired_keys", NULL, "expired_keys", DS_TYPE_GAUGE);
310     redis_handle_info (rn->name, rr->str, "pubsub", "channels", "pubsub_channels", DS_TYPE_GAUGE);
311     redis_handle_info (rn->name, rr->str, "pubsub", "patterns", "pubsub_patterns", DS_TYPE_GAUGE);
312     redis_handle_info (rn->name, rr->str, "current_connections", "slaves", "connected_slaves", DS_TYPE_GAUGE);
314     freeReplyObject (rr);
315     redisFree (rh);
316   }
318   return 0;
320 /* }}} */
322 void module_register (void) /* {{{ */
324   plugin_register_complex_config ("redis", redis_config);
325   plugin_register_init ("redis", redis_init);
326   plugin_register_read ("redis", redis_read);
327   /* TODO: plugin_register_write: one redis list per value id with
328    * X elements */
330 /* }}} */
332 /* vim: set sw=2 sts=2 et fdm=marker : */