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_PORT 6379
37 #define REDIS_DEF_TIMEOUT 2000
38 #define MAX_REDIS_NODE_NAME 64
40 /* Redis plugin configuration example:
41 *
42 * <Plugin redis>
43 * <Node "mynode">
44 * Host "localhost"
45 * Port "6379"
46 * Timeout 2000
47 * </Node>
48 * </Plugin>
49 */
51 struct redis_node_s;
52 typedef struct redis_node_s redis_node_t;
53 struct redis_node_s
54 {
55 char name[MAX_REDIS_NODE_NAME];
56 char host[HOST_NAME_MAX];
57 char passwd[HOST_NAME_MAX];
58 int port;
59 int timeout;
61 redis_node_t *next;
62 };
64 static redis_node_t *nodes_head = NULL;
66 static int redis_node_add (const redis_node_t *rn) /* {{{ */
67 {
68 redis_node_t *rn_copy;
69 redis_node_t *rn_ptr;
71 /* Check for duplicates first */
72 for (rn_ptr = nodes_head; rn_ptr != NULL; rn_ptr = rn_ptr->next)
73 if (strcmp (rn->name, rn_ptr->name) == 0)
74 break;
76 if (rn_ptr != NULL)
77 {
78 ERROR ("redis plugin: A node with the name `%s' already exists.",
79 rn->name);
80 return (-1);
81 }
83 rn_copy = malloc (sizeof (*rn_copy));
84 if (rn_copy == NULL)
85 {
86 ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
87 return (-1);
88 }
90 memcpy (rn_copy, rn, sizeof (*rn_copy));
91 rn_copy->next = NULL;
93 DEBUG ("redis plugin: Adding node \"%s\".", rn->name);
95 if (nodes_head == NULL)
96 nodes_head = rn_copy;
97 else
98 {
99 rn_ptr = nodes_head;
100 while (rn_ptr->next != NULL)
101 rn_ptr = rn_ptr->next;
102 rn_ptr->next = rn_copy;
103 }
105 return (0);
106 } /* }}} */
108 static int redis_config_node (oconfig_item_t *ci) /* {{{ */
109 {
110 redis_node_t rn;
111 int i;
112 int status;
114 memset (&rn, 0, sizeof (rn));
115 sstrncpy (rn.host, REDIS_DEF_HOST, sizeof (rn.host));
116 rn.port = REDIS_DEF_PORT;
117 rn.timeout = REDIS_DEF_TIMEOUT;
119 status = cf_util_get_string_buffer (ci, rn.name, sizeof (rn.name));
120 if (status != 0)
121 return (status);
123 for (i = 0; i < ci->children_num; i++)
124 {
125 oconfig_item_t *option = ci->children + i;
127 if (strcasecmp ("Host", option->key) == 0)
128 status = cf_util_get_string_buffer (option, rn.host, sizeof (rn.host));
129 else if (strcasecmp ("Port", option->key) == 0)
130 {
131 status = cf_util_get_port_number (option);
132 if (status > 0)
133 {
134 rn.port = status;
135 status = 0;
136 }
137 }
138 else if (strcasecmp ("Timeout", option->key) == 0)
139 status = cf_util_get_int (option, &rn.timeout);
140 else if (strcasecmp ("Password", option->key) == 0)
141 status = cf_util_get_string_buffer (option, rn.passwd, sizeof (rn.passwd));
142 else
143 WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
144 "block. I'll ignore this option.", option->key);
146 if (status != 0)
147 break;
148 }
150 if (status != 0)
151 return (status);
153 return (redis_node_add (&rn));
154 } /* }}} int redis_config_node */
156 static int redis_config (oconfig_item_t *ci) /* {{{ */
157 {
158 int i;
160 for (i = 0; i < ci->children_num; i++)
161 {
162 oconfig_item_t *option = ci->children + i;
164 if (strcasecmp ("Node", option->key) == 0)
165 redis_config_node (option);
166 else
167 WARNING ("redis plugin: Option `%s' not allowed in redis"
168 " configuration. It will be ignored.", option->key);
169 }
171 if (nodes_head == NULL)
172 {
173 ERROR ("redis plugin: No valid node configuration could be found.");
174 return (ENOENT);
175 }
177 return (0);
178 } /* }}} */
180 __attribute__ ((nonnull(2)))
181 static void redis_submit_g (char *plugin_instance,
182 const char *type, const char *type_instance,
183 gauge_t value) /* {{{ */
184 {
185 value_t values[1];
186 value_list_t vl = VALUE_LIST_INIT;
188 values[0].gauge = value;
190 vl.values = values;
191 vl.values_len = 1;
192 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
193 sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
194 if (plugin_instance != NULL)
195 sstrncpy (vl.plugin_instance, plugin_instance,
196 sizeof (vl.plugin_instance));
197 sstrncpy (vl.type, type, sizeof (vl.type));
198 if (type_instance != NULL)
199 sstrncpy (vl.type_instance, type_instance,
200 sizeof (vl.type_instance));
202 plugin_dispatch_values (&vl);
203 } /* }}} */
205 __attribute__ ((nonnull(2)))
206 static void redis_submit_d (char *plugin_instance,
207 const char *type, const char *type_instance,
208 derive_t value) /* {{{ */
209 {
210 value_t values[1];
211 value_list_t vl = VALUE_LIST_INIT;
213 values[0].derive = value;
215 vl.values = values;
216 vl.values_len = 1;
217 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
218 sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
219 if (plugin_instance != NULL)
220 sstrncpy (vl.plugin_instance, plugin_instance,
221 sizeof (vl.plugin_instance));
222 sstrncpy (vl.type, type, sizeof (vl.type));
223 if (type_instance != NULL)
224 sstrncpy (vl.type_instance, type_instance,
225 sizeof (vl.type_instance));
227 plugin_dispatch_values (&vl);
228 } /* }}} */
230 static int redis_init (void) /* {{{ */
231 {
232 redis_node_t rn = { "default", REDIS_DEF_HOST, REDIS_DEF_PORT,
233 REDIS_DEF_TIMEOUT, /* next = */ NULL };
235 if (nodes_head == NULL)
236 redis_node_add (&rn);
238 return (0);
239 } /* }}} int redis_init */
241 static int redis_read (void) /* {{{ */
242 {
243 redis_node_t *rn;
245 for (rn = nodes_head; rn != NULL; rn = rn->next)
246 {
247 REDIS rh;
248 REDIS_INFO info;
250 int status;
252 DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
254 rh = credis_connect (rn->host, rn->port, rn->timeout);
255 if (rh == NULL)
256 {
257 ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
258 continue;
259 }
261 if (strlen (rn->passwd) > 0)
262 {
263 DEBUG ("redis plugin: authenticanting node `%s' passwd(%s).", rn->name, rn->passwd);
264 status = credis_auth(rh, rn->passwd);
265 if (status != 0)
266 {
267 WARNING ("redis plugin: unable to authenticate on node `%s'.", rn->name);
268 credis_close (rh);
269 continue;
270 }
271 }
273 memset (&info, 0, sizeof (info));
274 status = credis_info (rh, &info);
275 if (status != 0)
276 {
277 WARNING ("redis plugin: unable to get info from node `%s'.", rn->name);
278 credis_close (rh);
279 continue;
280 }
282 /* typedef struct _cr_info {
283 * char redis_version[CREDIS_VERSION_STRING_SIZE];
284 * int bgsave_in_progress;
285 * int connected_clients;
286 * int connected_slaves;
287 * unsigned int used_memory;
288 * long long changes_since_last_save;
289 * int last_save_time;
290 * long long total_connections_received;
291 * long long total_commands_processed;
292 * int uptime_in_seconds;
293 * int uptime_in_days;
294 * int role;
295 * } REDIS_INFO; */
297 DEBUG ("redis plugin: received info from node `%s': connected_clients = %d; "
298 "connected_slaves = %d; used_memory = %lu; changes_since_last_save = %lld; "
299 "bgsave_in_progress = %d; total_connections_received = %lld; "
300 "total_commands_processed = %lld; uptime_in_seconds = %ld", rn->name,
301 info.connected_clients, info.connected_slaves, info.used_memory,
302 info.changes_since_last_save, info.bgsave_in_progress,
303 info.total_connections_received, info.total_commands_processed,
304 info.uptime_in_seconds);
306 redis_submit_g (rn->name, "current_connections", "clients", info.connected_clients);
307 redis_submit_g (rn->name, "current_connections", "slaves", info.connected_slaves);
308 redis_submit_g (rn->name, "memory", "used", info.used_memory);
309 redis_submit_g (rn->name, "volatile_changes", NULL, info.changes_since_last_save);
310 redis_submit_d (rn->name, "total_connections", NULL, info.total_connections_received);
311 redis_submit_d (rn->name, "total_operations", NULL, info.total_commands_processed);
313 credis_close (rh);
314 }
316 return 0;
317 }
318 /* }}} */
320 void module_register (void) /* {{{ */
321 {
322 plugin_register_complex_config ("redis", redis_config);
323 plugin_register_init ("redis", redis_init);
324 plugin_register_read ("redis", redis_read);
325 /* TODO: plugin_register_write: one redis list per value id with
326 * X elements */
327 }
328 /* }}} */
330 /* vim: set sw=2 sts=2 et fdm=marker : */