redis plugin: Coding style fixes.
[collectd.git] / src / redis.c
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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27
28 #include <pthread.h>
29 #include <credis.h>
30
31 #define REDIS_DEF_HOST   "localhost"
32 #define REDIS_DEF_PORT    6379
33 #define REDIS_DEF_TIMEOUT 2000
34 #define MAX_REDIS_NODE_NAME 64
35
36 /* Redis plugin configuration example:
37  *
38  * <Plugin redis>
39  *   <Node "mynode">
40  *     Host "localhost"
41  *     Port "6379"
42  *     Timeout 2000
43  *   </Node>
44  * </Plugin>
45  */
46
47 struct redis_node_s;
48 typedef struct redis_node_s redis_node_t;
49 struct redis_node_s
50 {
51   char name[MAX_REDIS_NODE_NAME];
52   char host[HOST_NAME_MAX];
53   char passwd[HOST_NAME_MAX];
54   int port;
55   int timeout;
56
57   redis_node_t *next;
58 };
59
60 static redis_node_t *nodes_head = NULL;
61
62 static int redis_node_add (const redis_node_t *rn) /* {{{ */
63 {
64   redis_node_t *rn_copy;
65   redis_node_t *rn_ptr;
66
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;
71
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   }
78
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   }
85
86   memcpy (rn_copy, rn, sizeof (*rn_copy));
87   rn_copy->next = NULL;
88
89   DEBUG ("redis plugin: Adding node \"%s\".", rn->name);
90
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   }
100
101   return (0);
102 } /* }}} */
103
104 static int redis_config_node (oconfig_item_t *ci) /* {{{ */
105 {
106   redis_node_t rn;
107   int i;
108   int status;
109
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;
114
115   status = cf_util_get_string_buffer (ci, rn.name, sizeof (rn.name));
116   if (status != 0)
117     return (status);
118
119   for (i = 0; i < ci->children_num; i++)
120   {
121     oconfig_item_t *option = ci->children + i;
122
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 if (strcasecmp ("Password", option->key) == 0)
137       status = cf_util_get_string_buffer (option, rn.passwd, sizeof (rn.passwd));
138     else
139       WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
140           "block. I'll ignore this option.", option->key);
141
142     if (status != 0)
143       break;
144   }
145
146   if (status != 0)
147     return (status);
148
149   return (redis_node_add (&rn));
150 } /* }}} int redis_config_node */
151
152 static int redis_config (oconfig_item_t *ci) /* {{{ */
153 {
154   int i;
155
156   for (i = 0; i < ci->children_num; i++)
157   {
158     oconfig_item_t *option = ci->children + i;
159
160     if (strcasecmp ("Node", option->key) == 0)
161       redis_config_node (option);
162     else
163       WARNING ("redis plugin: Option `%s' not allowed in redis"
164           " configuration. It will be ignored.", option->key);
165   }
166
167   if (nodes_head == NULL)
168   {
169     ERROR ("redis plugin: No valid node configuration could be found.");
170     return (ENOENT);
171   }
172
173   return (0);
174 } /* }}} */
175
176   __attribute__ ((nonnull(2)))
177 static void redis_submit_g (char *plugin_instance,
178     const char *type, const char *type_instance,
179     gauge_t value) /* {{{ */
180 {
181   value_t values[1];
182   value_list_t vl = VALUE_LIST_INIT;
183
184   values[0].gauge = value;
185
186   vl.values = values;
187   vl.values_len = 1;
188   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
189   sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
190   if (plugin_instance != NULL)
191     sstrncpy (vl.plugin_instance, plugin_instance,
192         sizeof (vl.plugin_instance));
193   sstrncpy (vl.type, type, sizeof (vl.type));
194   if (type_instance != NULL)
195     sstrncpy (vl.type_instance, type_instance,
196         sizeof (vl.type_instance));
197
198   plugin_dispatch_values (&vl);
199 } /* }}} */
200
201   __attribute__ ((nonnull(2)))
202 static void redis_submit_d (char *plugin_instance,
203     const char *type, const char *type_instance,
204     derive_t value) /* {{{ */
205 {
206   value_t values[1];
207   value_list_t vl = VALUE_LIST_INIT;
208
209   values[0].derive = value;
210
211   vl.values = values;
212   vl.values_len = 1;
213   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
214   sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
215   if (plugin_instance != NULL)
216     sstrncpy (vl.plugin_instance, plugin_instance,
217         sizeof (vl.plugin_instance));
218   sstrncpy (vl.type, type, sizeof (vl.type));
219   if (type_instance != NULL)
220     sstrncpy (vl.type_instance, type_instance,
221         sizeof (vl.type_instance));
222
223   plugin_dispatch_values (&vl);
224 } /* }}} */
225
226 static int redis_init (void) /* {{{ */
227 {
228   redis_node_t rn = { "default", REDIS_DEF_HOST, REDIS_DEF_PORT,
229     REDIS_DEF_TIMEOUT, /* next = */ NULL };
230
231   if (nodes_head == NULL)
232     redis_node_add (&rn);
233
234   return (0);
235 } /* }}} int redis_init */
236
237 static int redis_read (void) /* {{{ */
238 {
239   redis_node_t *rn;
240
241   for (rn = nodes_head; rn != NULL; rn = rn->next)
242   {
243     REDIS rh;
244     REDIS_INFO info;
245
246     int status;
247
248     DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
249
250     rh = credis_connect (rn->host, rn->port, rn->timeout);
251     if (rh == NULL)
252     {
253       ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
254       continue;
255     }
256
257     if (strlen (rn->passwd) > 0)
258     {
259       DEBUG ("redis plugin: authenticanting node `%s' passwd(%s).", rn->name, rn->passwd);
260       status = credis_auth(rh, rn->passwd);
261       if (status != 0)
262       {
263         WARNING ("redis plugin: unable to authenticate on node `%s'.", rn->name);
264         credis_close (rh);
265         continue;
266       }
267     }
268
269     memset (&info, 0, sizeof (info));
270     status = credis_info (rh, &info);
271     if (status != 0)
272     {
273       WARNING ("redis plugin: unable to get info from node `%s'.", rn->name);
274       credis_close (rh);
275       continue;
276     }
277
278     /* typedef struct _cr_info {
279      *   char redis_version[CREDIS_VERSION_STRING_SIZE];
280      *   int bgsave_in_progress;
281      *   int connected_clients;
282      *   int connected_slaves;
283      *   unsigned int used_memory;
284      *   long long changes_since_last_save;
285      *   int last_save_time;
286      *   long long total_connections_received;
287      *   long long total_commands_processed;
288      *   int uptime_in_seconds;
289      *   int uptime_in_days;
290      *   int role;
291      * } REDIS_INFO; */
292
293     DEBUG ("redis plugin: received info from node `%s': connected_clients = %d; "
294         "connected_slaves = %d; used_memory = %lu; changes_since_last_save = %lld; "
295         "bgsave_in_progress = %d; total_connections_received = %lld; "
296         "total_commands_processed = %lld; uptime_in_seconds = %ld", rn->name,
297         info.connected_clients, info.connected_slaves, info.used_memory,
298         info.changes_since_last_save, info.bgsave_in_progress,
299         info.total_connections_received, info.total_commands_processed,
300         info.uptime_in_seconds);
301
302     redis_submit_g (rn->name, "current_connections", "clients", info.connected_clients);
303     redis_submit_g (rn->name, "current_connections", "slaves", info.connected_slaves);
304     redis_submit_g (rn->name, "memory", "used", info.used_memory);
305     redis_submit_g (rn->name, "volatile_changes", NULL, info.changes_since_last_save);
306     redis_submit_d (rn->name, "total_connections", NULL, info.total_connections_received);
307     redis_submit_d (rn->name, "total_operations", NULL, info.total_commands_processed);
308
309     credis_close (rh);
310   }
311
312   return 0;
313 }
314 /* }}} */
315
316 void module_register (void) /* {{{ */
317 {
318   plugin_register_complex_config ("redis", redis_config);
319   plugin_register_init ("redis", redis_init);
320   plugin_register_read ("redis", redis_read);
321   /* TODO: plugin_register_write: one redis list per value id with
322    * X elements */
323 }
324 /* }}} */
325
326 /* vim: set sw=2 sts=2 et fdm=marker : */