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 int port;
58 int timeout;
60 redis_node_t *next;
61 };
63 static redis_node_t *nodes_head = NULL;
65 static int redis_node_add (const redis_node_t *rn) /* {{{ */
66 {
67 redis_node_t *rn_copy;
68 redis_node_t *rn_ptr;
70 /* Check for duplicates first */
71 for (rn_ptr = nodes_head; rn_ptr != NULL; rn_ptr = rn_ptr->next)
72 if (strcmp (rn->name, rn_ptr->name) == 0)
73 break;
75 if (rn_ptr != NULL)
76 {
77 ERROR ("redis plugin: A node with the name `%s' already exists.",
78 rn->name);
79 return (-1);
80 }
82 rn_copy = malloc (sizeof (*rn_copy));
83 if (rn_copy == NULL)
84 {
85 ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
86 return (-1);
87 }
89 memcpy (rn_copy, rn, sizeof (*rn_copy));
90 rn_copy->next = NULL;
92 DEBUG ("redis plugin: Adding node \"%s\".", rn->name);
94 if (nodes_head == NULL)
95 nodes_head = rn_copy;
96 else
97 {
98 rn_ptr = nodes_head;
99 while (rn_ptr->next != NULL)
100 rn_ptr = rn_ptr->next;
101 rn_ptr->next = rn_copy;
102 }
104 return (0);
105 } /* }}} */
107 static int redis_config_node (oconfig_item_t *ci) /* {{{ */
108 {
109 redis_node_t rn;
110 int i;
111 int status;
113 memset (&rn, 0, sizeof (rn));
114 sstrncpy (rn.host, REDIS_DEF_HOST, sizeof (rn.host));
115 rn.port = REDIS_DEF_PORT;
116 rn.timeout = REDIS_DEF_TIMEOUT;
118 status = cf_util_get_string_buffer (ci, rn.name, sizeof (rn.name));
119 if (status != 0)
120 return (status);
122 for (i = 0; i < ci->children_num; i++)
123 {
124 oconfig_item_t *option = ci->children + i;
126 if (strcasecmp ("Host", option->key) == 0)
127 status = cf_util_get_string_buffer (option, rn.host, sizeof (rn.host));
128 else if (strcasecmp ("Port", option->key) == 0)
129 {
130 status = cf_util_get_port_number (option);
131 if (status > 0)
132 {
133 rn.port = status;
134 status = 0;
135 }
136 }
137 else if (strcasecmp ("Timeout", option->key) == 0)
138 status = cf_util_get_int (option, &rn.timeout);
139 else
140 WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
141 "block. I'll ignore this option.", option->key);
143 if (status != 0)
144 break;
145 }
147 if (status != 0)
148 return (status);
150 return (redis_node_add (&rn));
151 } /* }}} int redis_config_node */
153 static int redis_config (oconfig_item_t *ci) /* {{{ */
154 {
155 int i;
157 for (i = 0; i < ci->children_num; i++)
158 {
159 oconfig_item_t *option = ci->children + i;
161 if (strcasecmp ("Node", option->key) == 0)
162 redis_config_node (option);
163 else
164 WARNING ("redis plugin: Option `%s' not allowed in redis"
165 " configuration. It will be ignored.", option->key);
166 }
168 if (nodes_head == NULL)
169 {
170 ERROR ("redis plugin: No valid node configuration could be found.");
171 return (ENOENT);
172 }
174 return (0);
175 } /* }}} */
177 __attribute__ ((nonnull(2)))
178 static void redis_submit_g (char *plugin_instance,
179 const char *type, const char *type_instance,
180 gauge_t value) /* {{{ */
181 {
182 value_t values[1];
183 value_list_t vl = VALUE_LIST_INIT;
185 values[0].gauge = value;
187 vl.values = values;
188 vl.values_len = 1;
189 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
190 sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
191 if (plugin_instance != NULL)
192 sstrncpy (vl.plugin_instance, plugin_instance,
193 sizeof (vl.plugin_instance));
194 sstrncpy (vl.type, type, sizeof (vl.type));
195 if (type_instance != NULL)
196 sstrncpy (vl.type_instance, type_instance,
197 sizeof (vl.type_instance));
199 plugin_dispatch_values (&vl);
200 } /* }}} */
202 __attribute__ ((nonnull(2)))
203 static void redis_submit_d (char *plugin_instance,
204 const char *type, const char *type_instance,
205 derive_t value) /* {{{ */
206 {
207 value_t values[1];
208 value_list_t vl = VALUE_LIST_INIT;
210 values[0].derive = value;
212 vl.values = values;
213 vl.values_len = 1;
214 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
215 sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
216 if (plugin_instance != NULL)
217 sstrncpy (vl.plugin_instance, plugin_instance,
218 sizeof (vl.plugin_instance));
219 sstrncpy (vl.type, type, sizeof (vl.type));
220 if (type_instance != NULL)
221 sstrncpy (vl.type_instance, type_instance,
222 sizeof (vl.type_instance));
224 plugin_dispatch_values (&vl);
225 } /* }}} */
227 static int redis_init (void) /* {{{ */
228 {
229 redis_node_t rn = { "default", REDIS_DEF_HOST, REDIS_DEF_PORT,
230 REDIS_DEF_TIMEOUT, /* next = */ NULL };
232 if (nodes_head == NULL)
233 redis_node_add (&rn);
235 return (0);
236 } /* }}} int redis_init */
238 static int redis_read (void) /* {{{ */
239 {
240 redis_node_t *rn;
242 for (rn = nodes_head; rn != NULL; rn = rn->next)
243 {
244 REDIS rh;
245 REDIS_INFO info;
247 int status;
249 DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
251 rh = credis_connect (rn->host, rn->port, rn->timeout);
252 if (rh == NULL)
253 {
254 ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
255 continue;
256 }
258 memset (&info, 0, sizeof (info));
259 status = credis_info (rh, &info);
260 if (status != 0)
261 {
262 WARNING ("redis plugin: unable to get info from node `%s'.", rn->name);
263 credis_close (rh);
264 continue;
265 }
267 /* typedef struct _cr_info {
268 * char redis_version[CREDIS_VERSION_STRING_SIZE];
269 * int bgsave_in_progress;
270 * int connected_clients;
271 * int connected_slaves;
272 * unsigned int used_memory;
273 * long long changes_since_last_save;
274 * int last_save_time;
275 * long long total_connections_received;
276 * long long total_commands_processed;
277 * int uptime_in_seconds;
278 * int uptime_in_days;
279 * int role;
280 * } REDIS_INFO; */
282 DEBUG ("redis plugin: received info from node `%s': connected_clients = %d; "
283 "connected_slaves = %d; used_memory = %lu; changes_since_last_save = %lld; "
284 "bgsave_in_progress = %d; total_connections_received = %lld; "
285 "total_commands_processed = %lld; uptime_in_seconds = %ld", rn->name,
286 info.connected_clients, info.connected_slaves, info.used_memory,
287 info.changes_since_last_save, info.bgsave_in_progress,
288 info.total_connections_received, info.total_commands_processed,
289 info.uptime_in_seconds);
291 redis_submit_g (rn->name, "current_connections", "clients", info.connected_clients);
292 redis_submit_g (rn->name, "current_connections", "slaves", info.connected_slaves);
293 redis_submit_g (rn->name, "memory", "used", info.used_memory);
294 redis_submit_g (rn->name, "volatile_changes", NULL, info.changes_since_last_save);
295 redis_submit_d (rn->name, "total_connections", NULL, info.total_connections_received);
296 redis_submit_d (rn->name, "total_operations", NULL, info.total_commands_processed);
298 credis_close (rh);
299 }
301 return 0;
302 }
303 /* }}} */
305 void module_register (void) /* {{{ */
306 {
307 plugin_register_complex_config ("redis", redis_config);
308 plugin_register_init ("redis", redis_init);
309 plugin_register_read ("redis", redis_read);
310 /* TODO: plugin_register_write: one redis list per value id with
311 * X elements */
312 }
313 /* }}} */
315 /* vim: set sw=2 sts=2 et fdm=marker : */