2 * collectd - src/redis.c, based on src/memcached.c
3 * Copyright (C) 2010 Andrés J. Díaz <ajdiaz@connectical.com>
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.
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.
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
20 * Andrés J. Díaz <ajdiaz@connectical.com>
26 #include "configfile.h"
31 #define REDIS_DEF_HOST "localhost"
32 #define REDIS_DEF_PORT 6379
33 #define REDIS_DEF_TIMEOUT 2000
34 #define MAX_REDIS_NODE_NAME 64
36 /* Redis plugin configuration example:
48 typedef struct redis_node_s redis_node_t;
51 char name[MAX_REDIS_NODE_NAME];
52 char host[HOST_NAME_MAX];
53 char passwd[HOST_NAME_MAX];
60 static redis_node_t *nodes_head = NULL;
62 static int redis_node_add (const redis_node_t *rn) /* {{{ */
64 redis_node_t *rn_copy;
67 /* Check for duplicates first */
68 for (rn_ptr = nodes_head; rn_ptr != NULL; rn_ptr = rn_ptr->next)
69 if (strcmp (rn->name, rn_ptr->name) == 0)
74 ERROR ("redis plugin: A node with the name `%s' already exists.",
79 rn_copy = malloc (sizeof (*rn_copy));
82 ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
86 memcpy (rn_copy, rn, sizeof (*rn_copy));
89 DEBUG ("redis plugin: Adding node \"%s\".", rn->name);
91 if (nodes_head == NULL)
96 while (rn_ptr->next != NULL)
97 rn_ptr = rn_ptr->next;
98 rn_ptr->next = rn_copy;
104 static int redis_config_node (oconfig_item_t *ci) /* {{{ */
110 memset (&rn, 0, sizeof (rn));
111 sstrncpy (rn.host, REDIS_DEF_HOST, sizeof (rn.host));
112 rn.port = REDIS_DEF_PORT;
113 rn.timeout = REDIS_DEF_TIMEOUT;
115 status = cf_util_get_string_buffer (ci, rn.name, sizeof (rn.name));
119 for (i = 0; i < ci->children_num; i++)
121 oconfig_item_t *option = ci->children + i;
123 if (strcasecmp ("Host", option->key) == 0)
124 status = cf_util_get_string_buffer (option, rn.host, sizeof (rn.host));
125 else if (strcasecmp ("Port", option->key) == 0)
127 status = cf_util_get_port_number (option);
134 else if (strcasecmp ("Timeout", option->key) == 0)
135 status = cf_util_get_int (option, &rn.timeout);
136 else if (strcasecmp ("Passwd", option->key) == 0)
137 status = cf_util_get_string_buffer (option, rn.passwd, sizeof (rn.passwd));
139 WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
140 "block. I'll ignore this option.", option->key);
149 return (redis_node_add (&rn));
150 } /* }}} int redis_config_node */
152 static int redis_config (oconfig_item_t *ci) /* {{{ */
156 for (i = 0; i < ci->children_num; i++)
158 oconfig_item_t *option = ci->children + i;
160 if (strcasecmp ("Node", option->key) == 0)
161 redis_config_node (option);
163 WARNING ("redis plugin: Option `%s' not allowed in redis"
164 " configuration. It will be ignored.", option->key);
167 if (nodes_head == NULL)
169 ERROR ("redis plugin: No valid node configuration could be found.");
176 __attribute__ ((nonnull(2)))
177 static void redis_submit_g (char *plugin_instance,
178 const char *type, const char *type_instance,
179 gauge_t value) /* {{{ */
182 value_list_t vl = VALUE_LIST_INIT;
184 values[0].gauge = value;
188 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
189 sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
190 if (plugin_instance != NULL)
191 sstrncpy (vl.plugin_instance, plugin_instance,
192 sizeof (vl.plugin_instance));
193 sstrncpy (vl.type, type, sizeof (vl.type));
194 if (type_instance != NULL)
195 sstrncpy (vl.type_instance, type_instance,
196 sizeof (vl.type_instance));
198 plugin_dispatch_values (&vl);
201 __attribute__ ((nonnull(2)))
202 static void redis_submit_d (char *plugin_instance,
203 const char *type, const char *type_instance,
204 derive_t value) /* {{{ */
207 value_list_t vl = VALUE_LIST_INIT;
209 values[0].derive = value;
213 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
214 sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
215 if (plugin_instance != NULL)
216 sstrncpy (vl.plugin_instance, plugin_instance,
217 sizeof (vl.plugin_instance));
218 sstrncpy (vl.type, type, sizeof (vl.type));
219 if (type_instance != NULL)
220 sstrncpy (vl.type_instance, type_instance,
221 sizeof (vl.type_instance));
223 plugin_dispatch_values (&vl);
226 static int redis_init (void) /* {{{ */
228 redis_node_t rn = { "default", REDIS_DEF_HOST, REDIS_DEF_PORT,
229 REDIS_DEF_TIMEOUT, /* next = */ NULL };
231 if (nodes_head == NULL)
232 redis_node_add (&rn);
235 } /* }}} int redis_init */
237 static int redis_read (void) /* {{{ */
241 for (rn = nodes_head; rn != NULL; rn = rn->next)
248 DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
250 rh = credis_connect (rn->host, rn->port, rn->timeout);
253 ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
257 if ( strlen(rn->passwd) > 0 ) {
258 DEBUG ("redis plugin: authenticanting node `%s' passwd(%s).", rn->name, rn->passwd);
259 if ( credis_auth(rh, rn->passwd) != 0 )
261 WARNING ("redis plugin: unable to authenticate on node `%s'.", rn->name);
267 memset (&info, 0, sizeof (info));
268 status = credis_info (rh, &info);
271 WARNING ("redis plugin: unable to get info from node `%s'.", rn->name);
276 /* typedef struct _cr_info {
277 * char redis_version[CREDIS_VERSION_STRING_SIZE];
278 * int bgsave_in_progress;
279 * int connected_clients;
280 * int connected_slaves;
281 * unsigned int used_memory;
282 * long long changes_since_last_save;
283 * int last_save_time;
284 * long long total_connections_received;
285 * long long total_commands_processed;
286 * int uptime_in_seconds;
287 * int uptime_in_days;
291 DEBUG ("redis plugin: received info from node `%s': connected_clients = %d; "
292 "connected_slaves = %d; used_memory = %lu; changes_since_last_save = %lld; "
293 "bgsave_in_progress = %d; total_connections_received = %lld; "
294 "total_commands_processed = %lld; uptime_in_seconds = %ld", rn->name,
295 info.connected_clients, info.connected_slaves, info.used_memory,
296 info.changes_since_last_save, info.bgsave_in_progress,
297 info.total_connections_received, info.total_commands_processed,
298 info.uptime_in_seconds);
300 redis_submit_g (rn->name, "current_connections", "clients", info.connected_clients);
301 redis_submit_g (rn->name, "current_connections", "slaves", info.connected_slaves);
302 redis_submit_g (rn->name, "memory", "used", info.used_memory);
303 redis_submit_g (rn->name, "volatile_changes", NULL, info.changes_since_last_save);
304 redis_submit_d (rn->name, "total_connections", NULL, info.total_connections_received);
305 redis_submit_d (rn->name, "total_operations", NULL, info.total_commands_processed);
314 void module_register (void) /* {{{ */
316 plugin_register_complex_config ("redis", redis_config);
317 plugin_register_init ("redis", redis_init);
318 plugin_register_read ("redis", redis_read);
319 /* TODO: plugin_register_write: one redis list per value id with
324 /* vim: set sw=2 sts=2 et fdm=marker : */