rrdcached plugin: Refactor the configuration handling.
[collectd.git] / src / rrdcached.c
1 /**
2  * collectd - src/rrdcached.c
3  * Copyright (C) 2008-2012  Florian octo Forster
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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at collectd.org>
20  **/
21
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "utils_rrdcreate.h"
26
27 #undef HAVE_CONFIG_H
28 #include <rrd.h>
29 #include <rrd_client.h>
30
31 /*
32  * Private variables
33  */
34 static char *datadir = NULL;
35 static char *daemon_address = NULL;
36 static _Bool config_create_files = 1;
37 static _Bool config_collect_stats = 1;
38 static rrdcreate_config_t rrdcreate_config =
39 {
40         /* stepsize = */ 0,
41         /* heartbeat = */ 0,
42         /* rrarows = */ 1200,
43         /* xff = */ 0.1,
44
45         /* timespans = */ NULL,
46         /* timespans_num = */ 0,
47
48         /* consolidation_functions = */ NULL,
49         /* consolidation_functions_num = */ 0
50 };
51
52 /*
53  * Prototypes.
54  */
55 static int rc_write (const data_set_t *ds, const value_list_t *vl,
56     user_data_t __attribute__((unused)) *user_data);
57 static int rc_flush (__attribute__((unused)) cdtime_t timeout,
58     const char *identifier, __attribute__((unused)) user_data_t *ud);
59
60 static int value_list_to_string (char *buffer, int buffer_len,
61     const data_set_t *ds, const value_list_t *vl)
62 {
63   int offset;
64   int status;
65   int i;
66   time_t t;
67
68   assert (0 == strcmp (ds->type, vl->type));
69
70   memset (buffer, '\0', buffer_len);
71
72   t = CDTIME_T_TO_TIME_T (vl->time);
73   status = ssnprintf (buffer, buffer_len, "%lu", (unsigned long) t);
74   if ((status < 1) || (status >= buffer_len))
75     return (-1);
76   offset = status;
77
78   for (i = 0; i < ds->ds_num; i++)
79   {
80     if ((ds->ds[i].type != DS_TYPE_COUNTER)
81         && (ds->ds[i].type != DS_TYPE_GAUGE)
82         && (ds->ds[i].type != DS_TYPE_DERIVE)
83         && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
84       return (-1);
85
86     if (ds->ds[i].type == DS_TYPE_COUNTER)
87     {
88       status = ssnprintf (buffer + offset, buffer_len - offset,
89           ":%llu", vl->values[i].counter);
90     }
91     else if (ds->ds[i].type == DS_TYPE_GAUGE) 
92     {
93       status = ssnprintf (buffer + offset, buffer_len - offset,
94           ":%f", vl->values[i].gauge);
95     }
96     else if (ds->ds[i].type == DS_TYPE_DERIVE) {
97       status = ssnprintf (buffer + offset, buffer_len - offset,
98           ":%"PRIi64, vl->values[i].derive);
99     }
100     else /* if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */ {
101       status = ssnprintf (buffer + offset, buffer_len - offset,
102           ":%"PRIu64, vl->values[i].absolute);
103  
104     }
105
106     if ((status < 1) || (status >= (buffer_len - offset)))
107       return (-1);
108
109     offset += status;
110   } /* for ds->ds_num */
111
112   return (0);
113 } /* int value_list_to_string */
114
115 static int value_list_to_filename (char *buffer, int buffer_len,
116     const data_set_t *ds, const value_list_t *vl)
117 {
118   int offset = 0;
119   int status;
120
121   assert (0 == strcmp (ds->type, vl->type));
122
123   if (datadir != NULL)
124   {
125     status = ssnprintf (buffer + offset, buffer_len - offset,
126         "%s/", datadir);
127     if ((status < 1) || (status >= buffer_len - offset))
128       return (-1);
129     offset += status;
130   }
131
132   status = ssnprintf (buffer + offset, buffer_len - offset,
133       "%s/", vl->host);
134   if ((status < 1) || (status >= buffer_len - offset))
135     return (-1);
136   offset += status;
137
138   if (strlen (vl->plugin_instance) > 0)
139     status = ssnprintf (buffer + offset, buffer_len - offset,
140         "%s-%s/", vl->plugin, vl->plugin_instance);
141   else
142     status = ssnprintf (buffer + offset, buffer_len - offset,
143         "%s/", vl->plugin);
144   if ((status < 1) || (status >= buffer_len - offset))
145     return (-1);
146   offset += status;
147
148   if (strlen (vl->type_instance) > 0)
149     status = ssnprintf (buffer + offset, buffer_len - offset,
150         "%s-%s", vl->type, vl->type_instance);
151   else
152     status = ssnprintf (buffer + offset, buffer_len - offset,
153         "%s", vl->type);
154   if ((status < 1) || (status >= buffer_len - offset))
155     return (-1);
156   offset += status;
157
158   strncpy (buffer + offset, ".rrd", buffer_len - offset);
159   buffer[buffer_len - 1] = 0;
160
161   return (0);
162 } /* int value_list_to_filename */
163
164 static int rc_config (oconfig_item_t *ci)
165 {
166   int i;
167
168   for (i = 0; i < ci->children_num; i++)
169   {
170     oconfig_item_t const *child = ci->children + i;
171     const char *key = child->key;
172     int status = 0;
173
174     if (strcasecmp ("DataDir", key) == 0)
175     {
176       status = cf_util_get_string (child, &datadir);
177       if (status == 0)
178       {
179         int len = strlen (datadir);
180
181         while ((len > 0) && (datadir[len - 1] == '/'))
182         {
183           len--;
184           datadir[len] = 0;
185         }
186
187         if (len <= 0)
188           sfree (datadir);
189       }
190     }
191     else if (strcasecmp ("DaemonAddress", key) == 0)
192       status = cf_util_get_string (child, &daemon_address);
193     else if (strcasecmp ("CreateFiles", key) == 0)
194       status = cf_util_get_boolean (child, &config_create_files);
195     else if (strcasecmp ("CollectStatistics", key) == 0)
196       status = cf_util_get_boolean (child, &config_collect_stats);
197     else
198     {
199       WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
200       continue;
201     }
202
203     if (status != 0)
204       WARNING ("rrdcached plugin: Handling the \"%s\" option failed.", key);
205   }
206
207   if (daemon_address != NULL)
208   {
209     plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
210     plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
211   }
212   return (0);
213 } /* int rc_config */
214
215 static int rc_read (void)
216 {
217   int status;
218   rrdc_stats_t *head;
219   rrdc_stats_t *ptr;
220
221   value_t values[1];
222   value_list_t vl = VALUE_LIST_INIT;
223
224   if (daemon_address == NULL)
225     return (-1);
226
227   if (!config_collect_stats)
228     return (-1);
229
230   vl.values = values;
231   vl.values_len = 1;
232
233   if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
234       || (daemon_address[0] == '/'))
235     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
236   else
237     sstrncpy (vl.host, daemon_address, sizeof (vl.host));
238   sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
239
240   head = NULL;
241   status = rrdc_stats_get (&head);
242   if (status != 0)
243   {
244     ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
245     return (-1);
246   }
247
248   for (ptr = head; ptr != NULL; ptr = ptr->next)
249   {
250     if (ptr->type == RRDC_STATS_TYPE_GAUGE)
251       values[0].gauge = (gauge_t) ptr->value.gauge;
252     else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
253       values[0].counter = (counter_t) ptr->value.counter;
254     else
255       continue;
256
257     if (strcasecmp ("QueueLength", ptr->name) == 0)
258     {
259       sstrncpy (vl.type, "queue_length", sizeof (vl.type));
260       sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
261     }
262     else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
263     {
264       sstrncpy (vl.type, "operations", sizeof (vl.type));
265       sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
266     }
267     else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
268     {
269       sstrncpy (vl.type, "operations", sizeof (vl.type));
270       sstrncpy (vl.type_instance, "write-data_sets",
271           sizeof (vl.type_instance));
272     }
273     else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
274     {
275       sstrncpy (vl.type, "gauge", sizeof (vl.type));
276       sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
277     }
278     else if (strcasecmp ("TreeDepth", ptr->name) == 0)
279     {
280       sstrncpy (vl.type, "gauge", sizeof (vl.type));
281       sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
282     }
283     else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
284     {
285       sstrncpy (vl.type, "operations", sizeof (vl.type));
286       sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
287     }
288     else if (strcasecmp ("JournalBytes", ptr->name) == 0)
289     {
290       sstrncpy (vl.type, "counter", sizeof (vl.type));
291       sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
292     }
293     else if (strcasecmp ("JournalRotate", ptr->name) == 0)
294     {
295       sstrncpy (vl.type, "counter", sizeof (vl.type));
296       sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
297     }
298     else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
299     {
300       sstrncpy (vl.type, "operations", sizeof (vl.type));
301       sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
302     }
303     else
304     {
305       DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
306       continue;
307     }
308
309     plugin_dispatch_values (&vl);
310   } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
311
312   rrdc_stats_free (head);
313
314   return (0);
315 } /* int rc_read */
316
317 static int rc_init (void)
318 {
319   if (config_collect_stats)
320     plugin_register_read ("rrdcached", rc_read);
321
322   return (0);
323 } /* int rc_init */
324
325 static int rc_write (const data_set_t *ds, const value_list_t *vl,
326     user_data_t __attribute__((unused)) *user_data)
327 {
328   char filename[PATH_MAX];
329   char values[512];
330   char *values_array[2];
331   int status;
332
333   if (daemon_address == NULL)
334   {
335     ERROR ("rrdcached plugin: daemon_address == NULL.");
336     plugin_unregister_write ("rrdcached");
337     return (-1);
338   }
339
340   if (strcmp (ds->type, vl->type) != 0)
341   {
342     ERROR ("rrdcached plugin: DS type does not match value list type");
343     return (-1);
344   }
345
346   if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
347   {
348     ERROR ("rrdcached plugin: value_list_to_filename failed.");
349     return (-1);
350   }
351
352   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
353   {
354     ERROR ("rrdcached plugin: value_list_to_string failed.");
355     return (-1);
356   }
357
358   values_array[0] = values;
359   values_array[1] = NULL;
360
361   if (config_create_files)
362   {
363     struct stat statbuf;
364
365     status = stat (filename, &statbuf);
366     if (status != 0)
367     {
368       if (errno != ENOENT)
369       {
370         char errbuf[1024];
371         ERROR ("rrdcached plugin: stat (%s) failed: %s",
372             filename, sstrerror (errno, errbuf, sizeof (errbuf)));
373         return (-1);
374       }
375
376       status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
377       if (status != 0)
378       {
379         ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
380             filename);
381         return (-1);
382       }
383     }
384   }
385
386   status = rrdc_connect (daemon_address);
387   if (status != 0)
388   {
389     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
390         daemon_address, status);
391     return (-1);
392   }
393
394   status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
395   if (status != 0)
396   {
397     ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
398         "status %i.",
399         filename, values_array[0], status);
400     return (-1);
401   }
402
403   return (0);
404 } /* int rc_write */
405
406 static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
407     const char *identifier,
408     __attribute__((unused)) user_data_t *ud)
409 {
410   char filename[PATH_MAX + 1];
411   int status;
412
413   if (identifier == NULL)
414     return (EINVAL);
415
416   if (datadir != NULL)
417     ssnprintf (filename, sizeof (filename), "%s/%s.rrd", datadir, identifier);
418   else
419     ssnprintf (filename, sizeof (filename), "%s.rrd", identifier);
420
421   status = rrdc_connect (daemon_address);
422   if (status != 0)
423   {
424     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
425         daemon_address, status);
426     return (-1);
427   }
428
429   status = rrdc_flush (filename);
430   if (status != 0)
431   {
432     ERROR ("rrdcached plugin: rrdc_flush (%s) failed with status %i.",
433         filename, status);
434     return (-1);
435   }
436   DEBUG ("rrdcached plugin: rrdc_flush (%s): Success.", filename);
437
438   return (0);
439 } /* }}} int rc_flush */
440
441 static int rc_shutdown (void)
442 {
443   rrdc_disconnect ();
444   return (0);
445 } /* int rc_shutdown */
446
447 void module_register (void)
448 {
449   plugin_register_complex_config ("rrdcached", rc_config);
450   plugin_register_init ("rrdcached", rc_init);
451   plugin_register_shutdown ("rrdcached", rc_shutdown);
452 } /* void module_register */
453
454 /*
455  * vim: set sw=2 sts=2 et :
456  */