Code

Merge remote-tracking branch 'github/pr/387'
[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 <credis.h>
31 #ifndef HOST_NAME_MAX
32 # define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
33 #endif
35 #define REDIS_DEF_HOST   "localhost"
36 #define REDIS_DEF_PASSWD ""
37 #define REDIS_DEF_PORT    6379
38 #define REDIS_DEF_TIMEOUT 2000
39 #define MAX_REDIS_NODE_NAME 64
41 /* Redis plugin configuration example:
42  *
43  * <Plugin redis>
44  *   <Node "mynode">
45  *     Host "localhost"
46  *     Port "6379"
47  *     Timeout 2000
48  *   </Node>
49  * </Plugin>
50  */
52 struct redis_node_s;
53 typedef struct redis_node_s redis_node_t;
54 struct redis_node_s
55 {
56   char name[MAX_REDIS_NODE_NAME];
57   char host[HOST_NAME_MAX];
58   char passwd[HOST_NAME_MAX];
59   int port;
60   int timeout;
62   redis_node_t *next;
63 };
65 static redis_node_t *nodes_head = NULL;
67 static int redis_node_add (const redis_node_t *rn) /* {{{ */
68 {
69   redis_node_t *rn_copy;
70   redis_node_t *rn_ptr;
72   /* Check for duplicates first */
73   for (rn_ptr = nodes_head; rn_ptr != NULL; rn_ptr = rn_ptr->next)
74     if (strcmp (rn->name, rn_ptr->name) == 0)
75       break;
77   if (rn_ptr != NULL)
78   {
79     ERROR ("redis plugin: A node with the name `%s' already exists.",
80         rn->name);
81     return (-1);
82   }
84   rn_copy = malloc (sizeof (*rn_copy));
85   if (rn_copy == NULL)
86   {
87     ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
88     return (-1);
89   }
91   memcpy (rn_copy, rn, sizeof (*rn_copy));
92   rn_copy->next = NULL;
94   DEBUG ("redis plugin: Adding node \"%s\".", rn->name);
96   if (nodes_head == NULL)
97     nodes_head = rn_copy;
98   else
99   {
100     rn_ptr = nodes_head;
101     while (rn_ptr->next != NULL)
102       rn_ptr = rn_ptr->next;
103     rn_ptr->next = rn_copy;
104   }
106   return (0);
107 } /* }}} */
109 static int redis_config_node (oconfig_item_t *ci) /* {{{ */
111   redis_node_t rn;
112   int i;
113   int status;
115   memset (&rn, 0, sizeof (rn));
116   sstrncpy (rn.host, REDIS_DEF_HOST, sizeof (rn.host));
117   rn.port = REDIS_DEF_PORT;
118   rn.timeout = REDIS_DEF_TIMEOUT;
120   status = cf_util_get_string_buffer (ci, rn.name, sizeof (rn.name));
121   if (status != 0)
122     return (status);
124   for (i = 0; i < ci->children_num; i++)
125   {
126     oconfig_item_t *option = ci->children + i;
128     if (strcasecmp ("Host", option->key) == 0)
129       status = cf_util_get_string_buffer (option, rn.host, sizeof (rn.host));
130     else if (strcasecmp ("Port", option->key) == 0)
131     {
132       status = cf_util_get_port_number (option);
133       if (status > 0)
134       {
135         rn.port = status;
136         status = 0;
137       }
138     }
139     else if (strcasecmp ("Timeout", option->key) == 0)
140       status = cf_util_get_int (option, &rn.timeout);
141     else if (strcasecmp ("Password", option->key) == 0)
142       status = cf_util_get_string_buffer (option, rn.passwd, sizeof (rn.passwd));
143     else
144       WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
145           "block. I'll ignore this option.", option->key);
147     if (status != 0)
148       break;
149   }
151   if (status != 0)
152     return (status);
154   return (redis_node_add (&rn));
155 } /* }}} int redis_config_node */
157 static int redis_config (oconfig_item_t *ci) /* {{{ */
159   int i;
161   for (i = 0; i < ci->children_num; i++)
162   {
163     oconfig_item_t *option = ci->children + i;
165     if (strcasecmp ("Node", option->key) == 0)
166       redis_config_node (option);
167     else
168       WARNING ("redis plugin: Option `%s' not allowed in redis"
169           " configuration. It will be ignored.", option->key);
170   }
172   if (nodes_head == NULL)
173   {
174     ERROR ("redis plugin: No valid node configuration could be found.");
175     return (ENOENT);
176   }
178   return (0);
179 } /* }}} */
181   __attribute__ ((nonnull(2)))
182 static void redis_submit_g (char *plugin_instance,
183     const char *type, const char *type_instance,
184     gauge_t value) /* {{{ */
186   value_t values[1];
187   value_list_t vl = VALUE_LIST_INIT;
189   values[0].gauge = value;
191   vl.values = values;
192   vl.values_len = 1;
193   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
194   sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
195   if (plugin_instance != NULL)
196     sstrncpy (vl.plugin_instance, plugin_instance,
197         sizeof (vl.plugin_instance));
198   sstrncpy (vl.type, type, sizeof (vl.type));
199   if (type_instance != NULL)
200     sstrncpy (vl.type_instance, type_instance,
201         sizeof (vl.type_instance));
203   plugin_dispatch_values (&vl);
204 } /* }}} */
206   __attribute__ ((nonnull(2)))
207 static void redis_submit_d (char *plugin_instance,
208     const char *type, const char *type_instance,
209     derive_t value) /* {{{ */
211   value_t values[1];
212   value_list_t vl = VALUE_LIST_INIT;
214   values[0].derive = value;
216   vl.values = values;
217   vl.values_len = 1;
218   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
219   sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
220   if (plugin_instance != NULL)
221     sstrncpy (vl.plugin_instance, plugin_instance,
222         sizeof (vl.plugin_instance));
223   sstrncpy (vl.type, type, sizeof (vl.type));
224   if (type_instance != NULL)
225     sstrncpy (vl.type_instance, type_instance,
226         sizeof (vl.type_instance));
228   plugin_dispatch_values (&vl);
229 } /* }}} */
231 static int redis_init (void) /* {{{ */
233   redis_node_t rn = { "default", REDIS_DEF_HOST, REDIS_DEF_PASSWD,
234     REDIS_DEF_PORT, REDIS_DEF_TIMEOUT, /* next = */ NULL };
236   if (nodes_head == NULL)
237     redis_node_add (&rn);
239   return (0);
240 } /* }}} int redis_init */
242 static int redis_read (void) /* {{{ */
244   redis_node_t *rn;
246   for (rn = nodes_head; rn != NULL; rn = rn->next)
247   {
248     REDIS rh;
249     REDIS_INFO info;
251     int status;
253     DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
255     rh = credis_connect (rn->host, rn->port, rn->timeout);
256     if (rh == NULL)
257     {
258       ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
259       continue;
260     }
262     if (strlen (rn->passwd) > 0)
263     {
264       DEBUG ("redis plugin: authenticanting node `%s' passwd(%s).", rn->name, rn->passwd);
265       status = credis_auth(rh, rn->passwd);
266       if (status != 0)
267       {
268         WARNING ("redis plugin: unable to authenticate on node `%s'.", rn->name);
269         credis_close (rh);
270         continue;
271       }
272     }
274     memset (&info, 0, sizeof (info));
275     status = credis_info (rh, &info);
276     if (status != 0)
277     {
278       WARNING ("redis plugin: unable to get info from node `%s'.", rn->name);
279       credis_close (rh);
280       continue;
281     }
283     /* typedef struct _cr_info {
284      *   char redis_version[CREDIS_VERSION_STRING_SIZE];
285      *   int bgsave_in_progress;
286      *   int connected_clients;
287      *   int connected_slaves;
288      *   unsigned int used_memory;
289      *   long long changes_since_last_save;
290      *   int last_save_time;
291      *   long long total_connections_received;
292      *   long long total_commands_processed;
293      *   int uptime_in_seconds;
294      *   int uptime_in_days;
295      *   int role;
296      * } REDIS_INFO; */
298     DEBUG ("redis plugin: received info from node `%s': connected_clients = %d; "
299         "connected_slaves = %d; used_memory = %lu; changes_since_last_save = %lld; "
300         "bgsave_in_progress = %d; total_connections_received = %lld; "
301         "total_commands_processed = %lld; uptime_in_seconds = %ld", rn->name,
302         info.connected_clients, info.connected_slaves, info.used_memory,
303         info.changes_since_last_save, info.bgsave_in_progress,
304         info.total_connections_received, info.total_commands_processed,
305         info.uptime_in_seconds);
307     redis_submit_g (rn->name, "current_connections", "clients", info.connected_clients);
308     redis_submit_g (rn->name, "current_connections", "slaves", info.connected_slaves);
309     redis_submit_g (rn->name, "memory", "used", info.used_memory);
310     redis_submit_g (rn->name, "volatile_changes", NULL, info.changes_since_last_save);
311     redis_submit_d (rn->name, "total_connections", NULL, info.total_connections_received);
312     redis_submit_d (rn->name, "total_operations", NULL, info.total_commands_processed);
314     credis_close (rh);
315   }
317   return 0;
319 /* }}} */
321 void module_register (void) /* {{{ */
323   plugin_register_complex_config ("redis", redis_config);
324   plugin_register_init ("redis", redis_init);
325   plugin_register_read ("redis", redis_read);
326   /* TODO: plugin_register_write: one redis list per value id with
327    * X elements */
329 /* }}} */
331 /* vim: set sw=2 sts=2 et fdm=marker : */