c085963890ef28df73bc86d44e18547c69fe9a6d
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"
27 #include "utils_avltree.h"
29 #include <pthread.h>
30 #include <credis.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:
38 *
39 * <Plugin redis>
40 * <Node "mynode">
41 * Host "localhost"
42 * Port "6379"
43 * Timeout 2000
44 * </Node>
45 * </Plugin>
46 */
48 struct redis_node_s;
49 typedef struct redis_node_s redis_node_t;
50 struct redis_node_s
51 {
52 char name[MAX_REDIS_NODE_NAME];
53 char host[HOST_NAME_MAX];
54 int port;
55 int timeout;
57 redis_node_t *next;
58 };
60 static redis_node_t *nodes_head = NULL;
62 static int redis_node_add (const redis_node_t *rn) /* {{{ */
63 {
64 redis_node_t *rn_copy;
65 redis_node_t *rn_ptr;
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)
70 break;
72 if (rn_ptr != NULL)
73 {
74 ERROR ("redis plugin: A node with the name `%s' already exists.",
75 rn->name);
76 return (-1);
77 }
79 rn_copy = malloc (sizeof (*rn_copy));
80 if (rn_copy == NULL)
81 {
82 ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
83 return (-1);
84 }
86 memcpy (rn_copy, rn, sizeof (*rn_copy));
87 rn_copy->next = NULL;
89 DEBUG ("redis plugin: Adding node \"%s\".", rn->name);
91 if (nodes_head == NULL)
92 nodes_head = rn_copy;
93 else
94 {
95 rn_ptr = nodes_head;
96 while (rn_ptr->next != NULL)
97 rn_ptr = rn_ptr->next;
98 rn_ptr->next = rn_copy;
99 }
101 return (0);
102 } /* }}} */
104 static int redis_config_node (oconfig_item_t *ci) /* {{{ */
105 {
106 redis_node_t rn;
107 int i;
108 int status;
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));
116 if (status != 0)
117 return (status);
119 for (i = 0; i < ci->children_num; i++)
120 {
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)
126 {
127 status = cf_util_get_port_number (option);
128 if (status > 0)
129 {
130 rn.port = status;
131 status = 0;
132 }
133 }
134 else if (strcasecmp ("Timeout", option->key) == 0)
135 status = cf_util_get_int (option, &rn.timeout);
136 else
137 WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
138 "block. I'll ignore this option.", option->key);
140 if (status != 0)
141 break;
142 }
144 if (status != 0)
145 return (status);
147 return (redis_node_add (&rn));
148 } /* }}} int redis_config_node */
150 static int redis_config (oconfig_item_t *ci) /* {{{ */
151 {
152 int i;
154 for (i = 0; i < ci->children_num; i++)
155 {
156 oconfig_item_t *option = ci->children + i;
158 if (strcasecmp ("Node", option->key) == 0)
159 redis_config_node (option);
160 else
161 WARNING ("redis plugin: Option `%s' not allowed in redis"
162 " configuration. It will be ignored.", option->key);
163 }
165 if (nodes_head == NULL)
166 {
167 ERROR ("redis plugin: No valid node configuration could be found.");
168 return (ENOENT);
169 }
171 return (0);
172 } /* }}} */
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) /* {{{ */
178 {
179 value_t values[1];
180 value_list_t vl = VALUE_LIST_INIT;
182 values[0].gauge = value;
184 vl.values = values;
185 vl.values_len = 1;
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);
197 } /* }}} */
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) /* {{{ */
203 {
204 value_t values[1];
205 value_list_t vl = VALUE_LIST_INIT;
207 values[0].counter = value;
209 vl.values = values;
210 vl.values_len = 1;
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);
222 } /* }}} */
224 static int redis_read (void) /* {{{ */
225 {
226 REDIS rh;
227 REDIS_INFO info;
229 int status;
230 redis_node_t *rn;
232 for (rn = nodes_head; rn != NULL; rn = rn->next)
233 {
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 )
237 {
238 ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
239 status = -1;
240 break;
241 }
243 if ( (status = credis_info (rh, &info)) == -1 )
244 {
245 WARNING ("redis plugin: unable to get info from node `%s'.", rn->name);
246 credis_close (rh);
247 break;
248 }
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;
262 * int role;
263 * } REDIS_INFO; */
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);
283 credis_close (rh);
284 status = 0;
285 }
287 return 0;
288 }
289 /* }}} */
291 void module_register (void) /* {{{ */
292 {
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
296 * X elements */
297 }
298 /* }}} */
300 /* vim: set sw=2 sts=2 et fdm=marker : */