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"
27 #include "utils_avltree.h"
32 #define REDIS_DEF_HOST "localhost"
33 #define REDIS_DEF_PORT 6379
34 #define REDIS_DEF_TIMEOUT 2000
35 #define MAX_REDIS_NODE_NAME 64
37 /* Redis plugin configuration example:
49 typedef struct redis_node_s redis_node_t;
52 char name[MAX_REDIS_NODE_NAME];
53 char host[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);
137 WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
138 "block. I'll ignore this option.", option->key);
147 return (redis_node_add (&rn));
148 } /* }}} int redis_config_node */
150 static int redis_config (oconfig_item_t *ci) /* {{{ */
154 for (i = 0; i < ci->children_num; i++)
156 oconfig_item_t *option = ci->children + i;
158 if (strcasecmp ("Node", option->key) == 0)
159 redis_config_node (option);
161 WARNING ("redis plugin: Option `%s' not allowed in redis"
162 " configuration. It will be ignored.", option->key);
165 if (nodes_head == NULL)
167 ERROR ("redis plugin: No valid node configuration could be found.");
174 __attribute__ ((nonnull(2)))
175 static void redis_submit_g (char *plugin_instance,
176 const char *type, const char *type_instance,
177 gauge_t value) /* {{{ */
180 value_list_t vl = VALUE_LIST_INIT;
182 values[0].gauge = value;
186 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
187 sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
188 if (plugin_instance != NULL)
189 sstrncpy (vl.plugin_instance, plugin_instance,
190 sizeof (vl.plugin_instance));
191 sstrncpy (vl.type, type, sizeof (vl.type));
192 if (type_instance != NULL)
193 sstrncpy (vl.type_instance, type_instance,
194 sizeof (vl.type_instance));
196 plugin_dispatch_values (&vl);
199 __attribute__ ((nonnull(2)))
200 static void redis_submit_c (char *plugin_instance,
201 const char *type, const char *type_instance,
202 counter_t value) /* {{{ */
205 value_list_t vl = VALUE_LIST_INIT;
207 values[0].counter = value;
211 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
212 sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
213 if (plugin_instance != NULL)
214 sstrncpy (vl.plugin_instance, plugin_instance,
215 sizeof (vl.plugin_instance));
216 sstrncpy (vl.type, type, sizeof (vl.type));
217 if (type_instance != NULL)
218 sstrncpy (vl.type_instance, type_instance,
219 sizeof (vl.type_instance));
221 plugin_dispatch_values (&vl);
224 static int redis_read (void) /* {{{ */
232 for (rn = nodes_head; rn != NULL; rn = rn->next)
234 DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
236 if ( (rh = credis_connect (rn->host, rn->port, rn->timeout)) == NULL )
238 ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
243 if ( (status = credis_info (rh, &info)) == -1 )
245 WARNING ("redis plugin: unable to get info from node `%s'.", rn->name);
250 /* typedef struct _cr_info {
251 * char redis_version[CREDIS_VERSION_STRING_SIZE];
252 * int bgsave_in_progress;
253 * int connected_clients;
254 * int connected_slaves;
255 * unsigned int used_memory;
256 * long long changes_since_last_save;
257 * int last_save_time;
258 * long long total_connections_received;
259 * long long total_commands_processed;
260 * int uptime_in_seconds;
261 * int uptime_in_days;
265 DEBUG ("redis plugin: received info from node `%s': connected_clients = %d; "
266 "connected_slaves = %d; used_memory = %lu; changes_since_last_save = %lld; "
267 "bgsave_in_progress = %d; total_connections_received = %lld; "
268 "total_commands_processed = %lld; uptime_in_seconds = %ld", rn->name,
269 info.connected_clients, info.connected_slaves, info.used_memory,
270 info.changes_since_last_save, info.bgsave_in_progress,
271 info.total_connections_received, info.total_commands_processed,
272 info.uptime_in_seconds);
274 redis_submit_g (rn->name, "connected_clients", NULL, info.connected_clients);
275 redis_submit_g (rn->name, "connected_slaves", NULL, info.connected_slaves);
276 redis_submit_g (rn->name, "used_memory", NULL, info.used_memory);
277 redis_submit_g (rn->name, "changes_since_last_save", NULL, info.changes_since_last_save);
278 redis_submit_g (rn->name, "bgsave_in_progress", NULL, info.bgsave_in_progress);
279 redis_submit_c (rn->name, "total_connections_received", NULL, info.total_connections_received);
280 redis_submit_c (rn->name, "total_commands_processed", NULL, info.total_commands_processed);
281 redis_submit_c (rn->name, "uptime_in_seconds", NULL, info.uptime_in_seconds);
291 void module_register (void) /* {{{ */
293 plugin_register_complex_config ("redis", redis_config);
294 plugin_register_read ("redis", redis_read);
295 /* TODO: plugin_register_write: one redis list per value id with
300 /* vim: set sw=2 sts=2 et fdm=marker : */