rrdcached, rrdtool plugins: Honor the "DataDir" setting.
[collectd.git] / src / rrdcached.c
1 /**
2  * collectd - src/rrdcached.c
3  * Copyright (C) 2008-2013  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, size_t buffer_size,
116     value_list_t const *vl)
117 {
118   char const suffix[] = ".rrd";
119   int status;
120   size_t len;
121
122   if (datadir != NULL)
123   {
124     size_t datadir_len = strlen (datadir);
125
126     if (datadir_len >= buffer_size)
127       return (ENOMEM);
128
129     sstrncpy (buffer, datadir, buffer_size);
130     buffer += datadir_len;
131     buffer_size -= datadir_len;
132   }
133
134   status = FORMAT_VL (buffer, buffer_size, vl);
135   if (status != 0)
136     return (status);
137
138   len = strlen (buffer);
139   assert (len < buffer_size);
140   buffer += len;
141   buffer_size -= len;
142
143   if (buffer_size <= sizeof (suffix))
144     return (ENOMEM);
145
146   memcpy (buffer, suffix, sizeof (suffix));
147   return (0);
148 } /* int value_list_to_filename */
149
150 static int rc_config_get_int_positive (oconfig_item_t const *ci, int *ret)
151 {
152   int status;
153   int tmp = 0;
154
155   status = cf_util_get_int (ci, &tmp);
156   if (status != 0)
157     return (status);
158   if (tmp < 0)
159     return (EINVAL);
160
161   *ret = tmp;
162   return (0);
163 } /* int rc_config_get_int_positive */
164
165 static int rc_config_get_xff (oconfig_item_t const *ci, double *ret)
166 {
167   double value;
168
169   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
170   {
171     ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
172         "in the range [0.0, 1.0)", ci->key);
173     return (EINVAL);
174   }
175
176   value = ci->values[0].value.number;
177   if ((value >= 0.0) && (value < 1.0))
178   {
179     *ret = value;
180     return (0);
181   }
182
183   ERROR ("rrdcached plugin: The \"%s\" needs exactly one numeric argument "
184       "in the range [0.0, 1.0)", ci->key);
185   return (EINVAL);
186 } /* int rc_config_get_xff */
187
188 static int rc_config_add_timespan (int timespan)
189 {
190   int *tmp;
191
192   if (timespan <= 0)
193     return (EINVAL);
194
195   tmp = realloc (rrdcreate_config.timespans,
196       sizeof (*rrdcreate_config.timespans)
197       * (rrdcreate_config.timespans_num + 1));
198   if (tmp == NULL)
199     return (ENOMEM);
200   rrdcreate_config.timespans = tmp;
201
202   rrdcreate_config.timespans[rrdcreate_config.timespans_num] = timespan;
203   rrdcreate_config.timespans_num++;
204
205   return (0);
206 } /* int rc_config_add_timespan */
207
208 static int rc_config (oconfig_item_t *ci)
209 {
210   int i;
211
212   for (i = 0; i < ci->children_num; i++)
213   {
214     oconfig_item_t const *child = ci->children + i;
215     const char *key = child->key;
216     int status = 0;
217
218     if (strcasecmp ("DataDir", key) == 0)
219     {
220       status = cf_util_get_string (child, &datadir);
221       if (status == 0)
222       {
223         int len = strlen (datadir);
224
225         while ((len > 0) && (datadir[len - 1] == '/'))
226         {
227           len--;
228           datadir[len] = 0;
229         }
230
231         if (len <= 0)
232           sfree (datadir);
233       }
234     }
235     else if (strcasecmp ("DaemonAddress", key) == 0)
236       status = cf_util_get_string (child, &daemon_address);
237     else if (strcasecmp ("CreateFiles", key) == 0)
238       status = cf_util_get_boolean (child, &config_create_files);
239     else if (strcasecmp ("CollectStatistics", key) == 0)
240       status = cf_util_get_boolean (child, &config_collect_stats);
241     else if (strcasecmp ("StepSize", key) == 0)
242     {
243       int tmp = -1;
244
245       status = rc_config_get_int_positive (child, &tmp);
246       if (status == 0)
247         rrdcreate_config.stepsize = (unsigned long) tmp;
248     }
249     else if (strcasecmp ("HeartBeat", key) == 0)
250       status = rc_config_get_int_positive (child, &rrdcreate_config.heartbeat);
251     else if (strcasecmp ("RRARows", key) == 0)
252       status = rc_config_get_int_positive (child, &rrdcreate_config.rrarows);
253     else if (strcasecmp ("RRATimespan", key) == 0)
254     {
255       int tmp = -1;
256       status = rc_config_get_int_positive (child, &tmp);
257       if (status == 0)
258         status = rc_config_add_timespan (tmp);
259     }
260     else if (strcasecmp ("XFF", key) == 0)
261       status = rc_config_get_xff (child, &rrdcreate_config.xff);
262     else
263     {
264       WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
265       continue;
266     }
267
268     if (status != 0)
269       WARNING ("rrdcached plugin: Handling the \"%s\" option failed.", key);
270   }
271
272   if (daemon_address != NULL)
273   {
274     plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
275     plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
276   }
277   return (0);
278 } /* int rc_config */
279
280 static int rc_read (void)
281 {
282   int status;
283   rrdc_stats_t *head;
284   rrdc_stats_t *ptr;
285
286   value_t values[1];
287   value_list_t vl = VALUE_LIST_INIT;
288
289   if (daemon_address == NULL)
290     return (-1);
291
292   if (!config_collect_stats)
293     return (-1);
294
295   vl.values = values;
296   vl.values_len = 1;
297
298   if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
299       || (daemon_address[0] == '/'))
300     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
301   else
302     sstrncpy (vl.host, daemon_address, sizeof (vl.host));
303   sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
304
305   status = rrdc_connect (daemon_address);
306   if (status != 0)
307   {
308     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
309         daemon_address, status);
310     return (-1);
311   }
312
313   head = NULL;
314   status = rrdc_stats_get (&head);
315   if (status != 0)
316   {
317     ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
318     return (-1);
319   }
320
321   for (ptr = head; ptr != NULL; ptr = ptr->next)
322   {
323     if (ptr->type == RRDC_STATS_TYPE_GAUGE)
324       values[0].gauge = (gauge_t) ptr->value.gauge;
325     else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
326       values[0].counter = (counter_t) ptr->value.counter;
327     else
328       continue;
329
330     if (strcasecmp ("QueueLength", ptr->name) == 0)
331     {
332       sstrncpy (vl.type, "queue_length", sizeof (vl.type));
333       sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
334     }
335     else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
336     {
337       sstrncpy (vl.type, "operations", sizeof (vl.type));
338       sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
339     }
340     else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
341     {
342       sstrncpy (vl.type, "operations", sizeof (vl.type));
343       sstrncpy (vl.type_instance, "write-data_sets",
344           sizeof (vl.type_instance));
345     }
346     else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
347     {
348       sstrncpy (vl.type, "gauge", sizeof (vl.type));
349       sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
350     }
351     else if (strcasecmp ("TreeDepth", ptr->name) == 0)
352     {
353       sstrncpy (vl.type, "gauge", sizeof (vl.type));
354       sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
355     }
356     else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
357     {
358       sstrncpy (vl.type, "operations", sizeof (vl.type));
359       sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
360     }
361     else if (strcasecmp ("JournalBytes", ptr->name) == 0)
362     {
363       sstrncpy (vl.type, "counter", sizeof (vl.type));
364       sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
365     }
366     else if (strcasecmp ("JournalRotate", ptr->name) == 0)
367     {
368       sstrncpy (vl.type, "counter", sizeof (vl.type));
369       sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
370     }
371     else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
372     {
373       sstrncpy (vl.type, "operations", sizeof (vl.type));
374       sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
375     }
376     else
377     {
378       DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
379       continue;
380     }
381
382     plugin_dispatch_values (&vl);
383   } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
384
385   rrdc_stats_free (head);
386
387   return (0);
388 } /* int rc_read */
389
390 static int rc_init (void)
391 {
392   if (config_collect_stats)
393     plugin_register_read ("rrdcached", rc_read);
394
395   return (0);
396 } /* int rc_init */
397
398 static int rc_write (const data_set_t *ds, const value_list_t *vl,
399     user_data_t __attribute__((unused)) *user_data)
400 {
401   char filename[PATH_MAX];
402   char values[512];
403   char *values_array[2];
404   int status;
405
406   if (daemon_address == NULL)
407   {
408     ERROR ("rrdcached plugin: daemon_address == NULL.");
409     plugin_unregister_write ("rrdcached");
410     return (-1);
411   }
412
413   if (strcmp (ds->type, vl->type) != 0)
414   {
415     ERROR ("rrdcached plugin: DS type does not match value list type");
416     return (-1);
417   }
418
419   if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
420   {
421     ERROR ("rrdcached plugin: value_list_to_filename failed.");
422     return (-1);
423   }
424
425   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
426   {
427     ERROR ("rrdcached plugin: value_list_to_string failed.");
428     return (-1);
429   }
430
431   values_array[0] = values;
432   values_array[1] = NULL;
433
434   if (config_create_files)
435   {
436     struct stat statbuf;
437
438     status = stat (filename, &statbuf);
439     if (status != 0)
440     {
441       if (errno != ENOENT)
442       {
443         char errbuf[1024];
444         ERROR ("rrdcached plugin: stat (%s) failed: %s",
445             filename, sstrerror (errno, errbuf, sizeof (errbuf)));
446         return (-1);
447       }
448
449       status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
450       if (status != 0)
451       {
452         ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
453             filename);
454         return (-1);
455       }
456     }
457   }
458
459   status = rrdc_connect (daemon_address);
460   if (status != 0)
461   {
462     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
463         daemon_address, status);
464     return (-1);
465   }
466
467   status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
468   if (status != 0)
469   {
470     ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
471         "status %i.",
472         filename, values_array[0], status);
473     return (-1);
474   }
475
476   return (0);
477 } /* int rc_write */
478
479 static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
480     const char *identifier,
481     __attribute__((unused)) user_data_t *ud)
482 {
483   char filename[PATH_MAX + 1];
484   int status;
485
486   if (identifier == NULL)
487     return (EINVAL);
488
489   if (datadir != NULL)
490     ssnprintf (filename, sizeof (filename), "%s/%s.rrd", datadir, identifier);
491   else
492     ssnprintf (filename, sizeof (filename), "%s.rrd", identifier);
493
494   status = rrdc_connect (daemon_address);
495   if (status != 0)
496   {
497     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
498         daemon_address, status);
499     return (-1);
500   }
501
502   status = rrdc_flush (filename);
503   if (status != 0)
504   {
505     ERROR ("rrdcached plugin: rrdc_flush (%s) failed with status %i.",
506         filename, status);
507     return (-1);
508   }
509   DEBUG ("rrdcached plugin: rrdc_flush (%s): Success.", filename);
510
511   return (0);
512 } /* }}} int rc_flush */
513
514 static int rc_shutdown (void)
515 {
516   rrdc_disconnect ();
517   return (0);
518 } /* int rc_shutdown */
519
520 void module_register (void)
521 {
522   plugin_register_complex_config ("rrdcached", rc_config);
523   plugin_register_init ("rrdcached", rc_init);
524   plugin_register_shutdown ("rrdcached", rc_shutdown);
525 } /* void module_register */
526
527 /*
528  * vim: set sw=2 sts=2 et :
529  */