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