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