Code

rrdcached plugin: Implement the "RRATimespan" option.
[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  **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "utils_rrdcreate.h"
27 #undef HAVE_CONFIG_H
28 #include <rrd.h>
29 #include <rrd_client.h>
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,
45         /* timespans = */ NULL,
46         /* timespans_num = */ 0,
48         /* consolidation_functions = */ NULL,
49         /* consolidation_functions_num = */ 0
50 };
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);
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;
68   assert (0 == strcmp (ds->type, vl->type));
70   memset (buffer, '\0', buffer_len);
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;
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);
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     }
106     if ((status < 1) || (status >= (buffer_len - offset)))
107       return (-1);
109     offset += status;
110   } /* for ds->ds_num */
112   return (0);
113 } /* int value_list_to_string */
115 static int value_list_to_filename (char *buffer, int buffer_len,
116     const data_set_t *ds, const value_list_t *vl)
118   int offset = 0;
119   int status;
121   assert (0 == strcmp (ds->type, vl->type));
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   }
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;
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;
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;
158   strncpy (buffer + offset, ".rrd", buffer_len - offset);
159   buffer[buffer_len - 1] = 0;
161   return (0);
162 } /* int value_list_to_filename */
164 static int rc_config_get_int_positive (oconfig_item_t const *ci, int *ret)
166   int status;
167   int tmp = 0;
169   status = cf_util_get_int (ci, &tmp);
170   if (status != 0)
171     return (status);
172   if (tmp < 0)
173     return (EINVAL);
175   *ret = tmp;
176   return (0);
177 } /* int rc_config_get_int_positive */
179 static int rc_config_add_timespan (int timespan)
181   int *tmp;
183   if (timespan <= 0)
184     return (EINVAL);
186   tmp = realloc (rrdcreate_config.timespans,
187       sizeof (*rrdcreate_config.timespans)
188       * (rrdcreate_config.timespans_num + 1));
189   if (tmp == NULL)
190     return (ENOMEM);
191   rrdcreate_config.timespans = tmp;
193   rrdcreate_config.timespans[rrdcreate_config.timespans_num] = timespan;
194   rrdcreate_config.timespans_num++;
196   return (0);
197 } /* int rc_config_add_timespan */
199 static int rc_config (oconfig_item_t *ci)
201   int i;
203   for (i = 0; i < ci->children_num; i++)
204   {
205     oconfig_item_t const *child = ci->children + i;
206     const char *key = child->key;
207     int status = 0;
209     if (strcasecmp ("DataDir", key) == 0)
210     {
211       status = cf_util_get_string (child, &datadir);
212       if (status == 0)
213       {
214         int len = strlen (datadir);
216         while ((len > 0) && (datadir[len - 1] == '/'))
217         {
218           len--;
219           datadir[len] = 0;
220         }
222         if (len <= 0)
223           sfree (datadir);
224       }
225     }
226     else if (strcasecmp ("DaemonAddress", key) == 0)
227       status = cf_util_get_string (child, &daemon_address);
228     else if (strcasecmp ("CreateFiles", key) == 0)
229       status = cf_util_get_boolean (child, &config_create_files);
230     else if (strcasecmp ("CollectStatistics", key) == 0)
231       status = cf_util_get_boolean (child, &config_collect_stats);
232     else if (strcasecmp ("StepSize", key) == 0)
233       status = rc_config_get_int_positive (child, &rrdcreate_config.stepsize);
234     else if (strcasecmp ("HeartBeat", key) == 0)
235       status = rc_config_get_int_positive (child, &rrdcreate_config.heartbeat);
236     else if (strcasecmp ("RRARows", key) == 0)
237       status = rc_config_get_int_positive (child, &rrdcreate_config.rrarows);
238     else if (strcasecmp ("RRATimespan", key) == 0)
239     {
240       int tmp = -1;
241       status = rc_config_get_int_positive (child, &tmp);
242       if (status == 0)
243         status = rc_config_add_timespan (tmp);
244     }
245     else
246     {
247       WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
248       continue;
249     }
251     if (status != 0)
252       WARNING ("rrdcached plugin: Handling the \"%s\" option failed.", key);
253   }
255   if (daemon_address != NULL)
256   {
257     plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
258     plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
259   }
260   return (0);
261 } /* int rc_config */
263 static int rc_read (void)
265   int status;
266   rrdc_stats_t *head;
267   rrdc_stats_t *ptr;
269   value_t values[1];
270   value_list_t vl = VALUE_LIST_INIT;
272   if (daemon_address == NULL)
273     return (-1);
275   if (!config_collect_stats)
276     return (-1);
278   vl.values = values;
279   vl.values_len = 1;
281   if ((strncmp ("unix:", daemon_address, strlen ("unix:")) == 0)
282       || (daemon_address[0] == '/'))
283     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
284   else
285     sstrncpy (vl.host, daemon_address, sizeof (vl.host));
286   sstrncpy (vl.plugin, "rrdcached", sizeof (vl.plugin));
288   head = NULL;
289   status = rrdc_stats_get (&head);
290   if (status != 0)
291   {
292     ERROR ("rrdcached plugin: rrdc_stats_get failed with status %i.", status);
293     return (-1);
294   }
296   for (ptr = head; ptr != NULL; ptr = ptr->next)
297   {
298     if (ptr->type == RRDC_STATS_TYPE_GAUGE)
299       values[0].gauge = (gauge_t) ptr->value.gauge;
300     else if (ptr->type == RRDC_STATS_TYPE_COUNTER)
301       values[0].counter = (counter_t) ptr->value.counter;
302     else
303       continue;
305     if (strcasecmp ("QueueLength", ptr->name) == 0)
306     {
307       sstrncpy (vl.type, "queue_length", sizeof (vl.type));
308       sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
309     }
310     else if (strcasecmp ("UpdatesWritten", ptr->name) == 0)
311     {
312       sstrncpy (vl.type, "operations", sizeof (vl.type));
313       sstrncpy (vl.type_instance, "write-updates", sizeof (vl.type_instance));
314     }
315     else if (strcasecmp ("DataSetsWritten", ptr->name) == 0)
316     {
317       sstrncpy (vl.type, "operations", sizeof (vl.type));
318       sstrncpy (vl.type_instance, "write-data_sets",
319           sizeof (vl.type_instance));
320     }
321     else if (strcasecmp ("TreeNodesNumber", ptr->name) == 0)
322     {
323       sstrncpy (vl.type, "gauge", sizeof (vl.type));
324       sstrncpy (vl.type_instance, "tree_nodes", sizeof (vl.type_instance));
325     }
326     else if (strcasecmp ("TreeDepth", ptr->name) == 0)
327     {
328       sstrncpy (vl.type, "gauge", sizeof (vl.type));
329       sstrncpy (vl.type_instance, "tree_depth", sizeof (vl.type_instance));
330     }
331     else if (strcasecmp ("FlushesReceived", ptr->name) == 0)
332     {
333       sstrncpy (vl.type, "operations", sizeof (vl.type));
334       sstrncpy (vl.type_instance, "receive-flush", sizeof (vl.type_instance));
335     }
336     else if (strcasecmp ("JournalBytes", ptr->name) == 0)
337     {
338       sstrncpy (vl.type, "counter", sizeof (vl.type));
339       sstrncpy (vl.type_instance, "journal-bytes", sizeof (vl.type_instance));
340     }
341     else if (strcasecmp ("JournalRotate", ptr->name) == 0)
342     {
343       sstrncpy (vl.type, "counter", sizeof (vl.type));
344       sstrncpy (vl.type_instance, "journal-rotates", sizeof (vl.type_instance));
345     }
346     else if (strcasecmp ("UpdatesReceived", ptr->name) == 0)
347     {
348       sstrncpy (vl.type, "operations", sizeof (vl.type));
349       sstrncpy (vl.type_instance, "receive-update", sizeof (vl.type_instance));
350     }
351     else
352     {
353       DEBUG ("rrdcached plugin: rc_read: Unknown statistic `%s'.", ptr->name);
354       continue;
355     }
357     plugin_dispatch_values (&vl);
358   } /* for (ptr = head; ptr != NULL; ptr = ptr->next) */
360   rrdc_stats_free (head);
362   return (0);
363 } /* int rc_read */
365 static int rc_init (void)
367   if (config_collect_stats)
368     plugin_register_read ("rrdcached", rc_read);
370   return (0);
371 } /* int rc_init */
373 static int rc_write (const data_set_t *ds, const value_list_t *vl,
374     user_data_t __attribute__((unused)) *user_data)
376   char filename[PATH_MAX];
377   char values[512];
378   char *values_array[2];
379   int status;
381   if (daemon_address == NULL)
382   {
383     ERROR ("rrdcached plugin: daemon_address == NULL.");
384     plugin_unregister_write ("rrdcached");
385     return (-1);
386   }
388   if (strcmp (ds->type, vl->type) != 0)
389   {
390     ERROR ("rrdcached plugin: DS type does not match value list type");
391     return (-1);
392   }
394   if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
395   {
396     ERROR ("rrdcached plugin: value_list_to_filename failed.");
397     return (-1);
398   }
400   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
401   {
402     ERROR ("rrdcached plugin: value_list_to_string failed.");
403     return (-1);
404   }
406   values_array[0] = values;
407   values_array[1] = NULL;
409   if (config_create_files)
410   {
411     struct stat statbuf;
413     status = stat (filename, &statbuf);
414     if (status != 0)
415     {
416       if (errno != ENOENT)
417       {
418         char errbuf[1024];
419         ERROR ("rrdcached plugin: stat (%s) failed: %s",
420             filename, sstrerror (errno, errbuf, sizeof (errbuf)));
421         return (-1);
422       }
424       status = cu_rrd_create_file (filename, ds, vl, &rrdcreate_config);
425       if (status != 0)
426       {
427         ERROR ("rrdcached plugin: cu_rrd_create_file (%s) failed.",
428             filename);
429         return (-1);
430       }
431     }
432   }
434   status = rrdc_connect (daemon_address);
435   if (status != 0)
436   {
437     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
438         daemon_address, status);
439     return (-1);
440   }
442   status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
443   if (status != 0)
444   {
445     ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
446         "status %i.",
447         filename, values_array[0], status);
448     return (-1);
449   }
451   return (0);
452 } /* int rc_write */
454 static int rc_flush (__attribute__((unused)) cdtime_t timeout, /* {{{ */
455     const char *identifier,
456     __attribute__((unused)) user_data_t *ud)
458   char filename[PATH_MAX + 1];
459   int status;
461   if (identifier == NULL)
462     return (EINVAL);
464   if (datadir != NULL)
465     ssnprintf (filename, sizeof (filename), "%s/%s.rrd", datadir, identifier);
466   else
467     ssnprintf (filename, sizeof (filename), "%s.rrd", identifier);
469   status = rrdc_connect (daemon_address);
470   if (status != 0)
471   {
472     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
473         daemon_address, status);
474     return (-1);
475   }
477   status = rrdc_flush (filename);
478   if (status != 0)
479   {
480     ERROR ("rrdcached plugin: rrdc_flush (%s) failed with status %i.",
481         filename, status);
482     return (-1);
483   }
484   DEBUG ("rrdcached plugin: rrdc_flush (%s): Success.", filename);
486   return (0);
487 } /* }}} int rc_flush */
489 static int rc_shutdown (void)
491   rrdc_disconnect ();
492   return (0);
493 } /* int rc_shutdown */
495 void module_register (void)
497   plugin_register_complex_config ("rrdcached", rc_config);
498   plugin_register_init ("rrdcached", rc_init);
499   plugin_register_shutdown ("rrdcached", rc_shutdown);
500 } /* void module_register */
502 /*
503  * vim: set sw=2 sts=2 et :
504  */