dcede59983c00e38d281481b689c0b35d894e364
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 <sys/time.h>
30 #include <hiredis/hiredis.h>
32 #ifndef HOST_NAME_MAX
33 # define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
34 #endif
36 #define REDIS_DEF_HOST "localhost"
37 #define REDIS_DEF_PASSWD ""
38 #define REDIS_DEF_PORT 6379
39 #define REDIS_DEF_TIMEOUT 2000
40 #define MAX_REDIS_NODE_NAME 64
41 #define MAX_REDIS_PASSWD_LENGTH 512
43 /* Redis plugin configuration example:
44 *
45 * <Plugin redis>
46 * <Node "mynode">
47 * Host "localhost"
48 * Port "6379"
49 * Timeout 2
50 * Password "foobar"
51 * </Node>
52 * </Plugin>
53 */
55 struct redis_node_s;
56 typedef struct redis_node_s redis_node_t;
57 struct redis_node_s
58 {
59 char name[MAX_REDIS_NODE_NAME];
60 char host[HOST_NAME_MAX];
61 char passwd[MAX_REDIS_PASSWD_LENGTH];
62 int port;
63 int timeout;
65 redis_node_t *next;
66 };
68 static redis_node_t *nodes_head = NULL;
70 static int redis_node_add (const redis_node_t *rn) /* {{{ */
71 {
72 redis_node_t *rn_copy;
73 redis_node_t *rn_ptr;
75 /* Check for duplicates first */
76 for (rn_ptr = nodes_head; rn_ptr != NULL; rn_ptr = rn_ptr->next)
77 if (strcmp (rn->name, rn_ptr->name) == 0)
78 break;
80 if (rn_ptr != NULL)
81 {
82 ERROR ("redis plugin: A node with the name `%s' already exists.",
83 rn->name);
84 return (-1);
85 }
87 rn_copy = malloc (sizeof (*rn_copy));
88 if (rn_copy == NULL)
89 {
90 ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
91 return (-1);
92 }
94 memcpy (rn_copy, rn, sizeof (*rn_copy));
95 rn_copy->next = NULL;
97 DEBUG ("redis plugin: Adding node \"%s\".", rn->name);
99 if (nodes_head == NULL)
100 nodes_head = rn_copy;
101 else
102 {
103 rn_ptr = nodes_head;
104 while (rn_ptr->next != NULL)
105 rn_ptr = rn_ptr->next;
106 rn_ptr->next = rn_copy;
107 }
109 return (0);
110 } /* }}} */
113 static int redis_config_node (oconfig_item_t *ci) /* {{{ */
114 {
115 redis_node_t rn;
116 int i;
117 int status;
119 memset (&rn, 0, sizeof (rn));
120 sstrncpy (rn.host, REDIS_DEF_HOST, sizeof (rn.host));
121 rn.port = REDIS_DEF_PORT;
122 rn.timeout = REDIS_DEF_TIMEOUT;
124 status = cf_util_get_string_buffer (ci, rn.name, sizeof (rn.name));
125 if (status != 0)
126 return (status);
128 for (i = 0; i < ci->children_num; i++)
129 {
130 oconfig_item_t *option = ci->children + i;
132 if (strcasecmp ("Host", option->key) == 0)
133 status = cf_util_get_string_buffer (option, rn.host, sizeof (rn.host));
134 else if (strcasecmp ("Port", option->key) == 0)
135 {
136 status = cf_util_get_port_number (option);
137 if (status > 0)
138 {
139 rn.port = status;
140 status = 0;
141 }
142 }
143 else if (strcasecmp ("Timeout", option->key) == 0)
144 status = cf_util_get_int (option, &rn.timeout);
145 else if (strcasecmp ("Password", option->key) == 0)
146 status = cf_util_get_string_buffer (option, rn.passwd, sizeof (rn.passwd));
147 else
148 WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
149 "block. I'll ignore this option.", option->key);
151 if (status != 0)
152 break;
153 }
155 if (status != 0)
156 return (status);
158 return (redis_node_add (&rn));
159 } /* }}} int redis_config_node */
161 static int redis_config (oconfig_item_t *ci) /* {{{ */
162 {
163 int i;
165 for (i = 0; i < ci->children_num; i++)
166 {
167 oconfig_item_t *option = ci->children + i;
169 if (strcasecmp ("Node", option->key) == 0)
170 redis_config_node (option);
171 else
172 WARNING ("redis plugin: Option `%s' not allowed in redis"
173 " configuration. It will be ignored.", option->key);
174 }
176 if (nodes_head == NULL)
177 {
178 ERROR ("redis plugin: No valid node configuration could be found.");
179 return (ENOENT);
180 }
182 return (0);
183 } /* }}} */
185 __attribute__ ((nonnull(2)))
186 static void redis_submit (char *plugin_instance,
187 const char *type, const char *type_instance,
188 value_t value) /* {{{ */
189 {
190 value_t values[1];
191 value_list_t vl = VALUE_LIST_INIT;
193 values[0] = value;
195 vl.values = values;
196 vl.values_len = 1;
197 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
198 sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
199 if (plugin_instance != NULL)
200 sstrncpy (vl.plugin_instance, plugin_instance,
201 sizeof (vl.plugin_instance));
202 sstrncpy (vl.type, type, sizeof (vl.type));
203 if (type_instance != NULL)
204 sstrncpy (vl.type_instance, type_instance,
205 sizeof (vl.type_instance));
207 plugin_dispatch_values (&vl);
208 } /* }}} */
210 static int redis_init (void) /* {{{ */
211 {
212 redis_node_t rn = {
213 .name = "default",
214 .host = REDIS_DEF_HOST,
215 .port = REDIS_DEF_PORT,
216 .timeout = REDIS_DEF_TIMEOUT,
217 .next = NULL
218 };
220 if (nodes_head == NULL)
221 redis_node_add (&rn);
223 return (0);
224 } /* }}} int redis_init */
226 int redis_handle_info (char *node, char const *info_line, char const *type, char const *type_instance, char const *field_name, int ds_type) /* {{{ */
227 {
228 char *str = strstr (info_line, field_name);
229 static char buf[MAX_REDIS_VAL_SIZE];
230 value_t val;
231 if (str)
232 {
233 int i;
235 str += strlen (field_name) + 1; /* also skip the ':' */
236 for(i=0;(*str && (isdigit(*str) || *str == '.'));i++,str++)
237 buf[i] = *str;
238 buf[i] ='\0';
240 if(parse_value (buf, &val, ds_type) == -1)
241 {
242 WARNING ("redis plugin: Unable to parse field `%s'.", field_name);
243 return (-1);
244 }
246 redis_submit (node, type, type_instance, val);
247 return (0);
248 }
249 return (-1);
251 } /* }}} int redis_handle_info */
253 static int redis_read (void) /* {{{ */
254 {
255 redis_node_t *rn;
257 for (rn = nodes_head; rn != NULL; rn = rn->next)
258 {
259 redisContext *rh;
260 redisReply *rr;
262 struct timeval tmout;
264 tmout.tv_sec = rn->timeout;
265 tmout.tv_usec = 0;
267 DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
269 rh = redisConnectWithTimeout ((char *)rn->host, rn->port, tmout);
270 if (rh == NULL)
271 {
272 ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
273 continue;
274 }
276 if (strlen (rn->passwd) > 0)
277 {
278 DEBUG ("redis plugin: authenticanting node `%s' passwd(%s).", rn->name, rn->passwd);
279 rr = redisCommand (rh, "AUTH %s", rn->passwd);
281 if (rr == NULL || rr->type != 5)
282 {
283 WARNING ("redis plugin: unable to authenticate on node `%s'.", rn->name);
284 redisFree (rh);
285 continue;
286 }
287 }
289 if ((rr = redisCommand(rh, "INFO")) == NULL)
290 {
291 WARNING ("redis plugin: unable to connect to node `%s'.", rn->name);
292 redisFree (rh);
293 continue;
294 }
296 redis_handle_info (rn->name, rr->str, "uptime", NULL, "uptime_in_seconds", DS_TYPE_GAUGE);
297 redis_handle_info (rn->name, rr->str, "connections", "clients", "connected_clients", DS_TYPE_GAUGE);
298 redis_handle_info (rn->name, rr->str, "connections", "slaves", "connected_slaves", DS_TYPE_GAUGE);
299 redis_handle_info (rn->name, rr->str, "blocked_clients", NULL, "blocked_clients", DS_TYPE_GAUGE);
300 redis_handle_info (rn->name, rr->str, "memory", NULL, "used_memory", DS_TYPE_GAUGE);
301 redis_handle_info (rn->name, rr->str, "changes_since_last_save", NULL, "changes_since_last_save", DS_TYPE_GAUGE);
302 redis_handle_info (rn->name, rr->str, "operations", NULL, "total_commands_processed", DS_TYPE_DERIVE);
303 redis_handle_info (rn->name, rr->str, "expired_keys", NULL, "expired_keys", DS_TYPE_GAUGE);
304 redis_handle_info (rn->name, rr->str, "pubsub", "patterns", "pubsub_patterns", DS_TYPE_GAUGE);
305 redis_handle_info (rn->name, rr->str, "pubsub", "channels", "pubsub_channels", DS_TYPE_GAUGE);
307 redisFree (rh);
308 }
310 return 0;
311 }
312 /* }}} */
314 void module_register (void) /* {{{ */
315 {
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
320 * X elements */
321 }
322 /* }}} */
324 /* vim: set sw=2 sts=2 et fdm=marker : */