2 * collectd - src/rrdtool.c
3 * Copyright (C) 2006 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_avltree.h"
26 #include "utils_debug.h"
29 * This weird macro cascade forces the glibc to define `NAN'. I don't know
30 * another way to solve this, so more intelligent solutions are welcome. -octo
33 # define DISABLE__USE_ISOC99 1
34 # define __USE_ISOC99 1
37 #ifdef DISABLE__USE_ISOC99
38 # undef DISABLE__USE_ISOC99
51 typedef struct rrd_cache_s rrd_cache_t;
56 static int rra_timespans[] =
64 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
66 static char *rra_types[] =
72 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
74 static const char *config_keys[] =
84 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
86 static char *datadir = NULL;
87 static int stepsize = 0;
88 static int heartbeat = 0;
89 static int rrarows = 1200;
90 static double xff = 0.1;
92 static int cache_timeout = 0;
93 static int cache_flush_timeout = 0;
94 static time_t cache_flush_last;
95 static avl_tree_t *cache = NULL;
100 static int rra_get (char ***ret)
102 static char **rra_def = NULL;
103 static int rra_num = 0;
105 int rra_max = rra_timespans_num * rra_types_num;
115 if ((rra_num != 0) && (rra_def != NULL))
121 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
123 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
125 if ((stepsize <= 0) || (rrarows <= 0))
132 for (i = 0; i < rra_timespans_num; i++)
134 span = rra_timespans[i];
136 if ((span / stepsize) < rrarows)
142 cdp_len = (int) floor (((double) span)
143 / ((double) (rrarows * stepsize)));
145 cdp_num = (int) ceil (((double) span)
146 / ((double) (cdp_len * stepsize)));
148 for (j = 0; j < rra_types_num; j++)
150 if (rra_num >= rra_max)
153 if (snprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
155 cdp_len, cdp_num) >= sizeof (buffer))
157 syslog (LOG_ERR, "rra_get: Buffer would have been truncated.");
161 rra_def[rra_num++] = sstrdup (buffer);
166 DBG ("rra_num = %i", rra_num);
167 for (i = 0; i < rra_num; i++)
168 DBG (" %s", rra_def[i]);
175 static void ds_free (int ds_num, char **ds_def)
179 for (i = 0; i < ds_num; i++)
180 if (ds_def[i] != NULL)
185 static int ds_get (char ***ret, const data_set_t *ds)
194 DBG ("ds->ds_num = %i", ds->ds_num);
196 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
199 syslog (LOG_ERR, "rrdtool plugin: malloc failed: %s",
203 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
205 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
207 data_source_t *d = ds->ds + ds_num;
211 ds_def[ds_num] = NULL;
213 if (d->type == DS_TYPE_COUNTER)
215 else if (d->type == DS_TYPE_GAUGE)
219 syslog (LOG_ERR, "rrdtool plugin: Unknown DS type: %i",
230 snprintf (min, sizeof (min), "%lf", d->min);
231 min[sizeof (min) - 1] = '\0';
240 snprintf (max, sizeof (max), "%lf", d->max);
241 max[sizeof (max) - 1] = '\0';
244 status = snprintf (buffer, sizeof (buffer),
246 d->name, type, heartbeat,
248 if ((status < 1) || (status >= sizeof (buffer)))
251 ds_def[ds_num] = sstrdup (buffer);
252 } /* for ds_num = 0 .. ds->ds_num */
257 DBG ("ds_num = %i", ds_num);
258 for (i = 0; i < ds_num; i++)
259 DBG (" %s", ds_def[i]);
263 if (ds_num != ds->ds_num)
265 ds_free (ds_num, ds_def);
273 static int rrd_create_file (char *filename, const data_set_t *ds)
282 char stepsize_str[16];
285 if (check_create_dir (filename))
288 if ((rra_num = rra_get (&rra_def)) < 1)
290 syslog (LOG_ERR, "rrd_create_file failed: Could not calculate RRAs");
294 if ((ds_num = ds_get (&ds_def, ds)) < 1)
296 syslog (LOG_ERR, "rrd_create_file failed: Could not calculate DSes");
300 argc = ds_num + rra_num + 4;
302 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
304 syslog (LOG_ERR, "rrd_create failed: %s", strerror (errno));
308 status = snprintf (stepsize_str, sizeof (stepsize_str),
310 if ((status < 1) || (status >= sizeof (stepsize_str)))
312 syslog (LOG_ERR, "rrdtool plugin: snprintf failed.");
319 argv[3] = stepsize_str;
322 for (i = 0; i < ds_num; i++)
323 argv[j++] = ds_def[i];
324 for (i = 0; i < rra_num; i++)
325 argv[j++] = rra_def[i];
328 optind = 0; /* bug in librrd? */
330 if (rrd_create (argc, argv) == -1)
332 syslog (LOG_ERR, "rrd_create failed: %s: %s", filename, rrd_get_error ());
337 ds_free (ds_num, ds_def);
342 static int value_list_to_string (char *buffer, int buffer_len,
343 const data_set_t *ds, const value_list_t *vl)
349 memset (buffer, '\0', sizeof (buffer_len));
351 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
352 if ((status < 1) || (status >= buffer_len))
356 for (i = 0; i < ds->ds_num; i++)
358 if ((ds->ds[i].type != DS_TYPE_COUNTER)
359 && (ds->ds[i].type != DS_TYPE_GAUGE))
362 if (ds->ds[i].type == DS_TYPE_COUNTER)
363 status = snprintf (buffer + offset, buffer_len - offset,
364 ":%llu", vl->values[i].counter);
366 status = snprintf (buffer + offset, buffer_len - offset,
367 ":%lf", vl->values[i].gauge);
369 if ((status < 1) || (status >= (buffer_len - offset)))
373 } /* for ds->ds_num */
376 } /* int value_list_to_string */
378 static int value_list_to_filename (char *buffer, int buffer_len,
379 const data_set_t *ds, const value_list_t *vl)
386 status = snprintf (buffer + offset, buffer_len - offset,
388 if ((status < 1) || (status >= buffer_len - offset))
393 status = snprintf (buffer + offset, buffer_len - offset,
395 if ((status < 1) || (status >= buffer_len - offset))
399 if (strlen (vl->plugin_instance) > 0)
400 status = snprintf (buffer + offset, buffer_len - offset,
401 "%s-%s/", vl->plugin, vl->plugin_instance);
403 status = snprintf (buffer + offset, buffer_len - offset,
405 if ((status < 1) || (status >= buffer_len - offset))
409 if (strlen (vl->type_instance) > 0)
410 status = snprintf (buffer + offset, buffer_len - offset,
411 "%s-%s.rrd", ds->type, vl->type_instance);
413 status = snprintf (buffer + offset, buffer_len - offset,
415 if ((status < 1) || (status >= buffer_len - offset))
420 } /* int value_list_to_filename */
422 static rrd_cache_t *rrd_cache_insert (const char *filename,
425 rrd_cache_t *rc = NULL;
429 avl_get (cache, filename, (void *) &rc);
433 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
442 rc->values = (char **) realloc ((void *) rc->values,
443 (rc->values_num + 1) * sizeof (char *));
444 if (rc->values == NULL)
446 syslog (LOG_ERR, "rrdtool plugin: realloc failed: %s",
450 void *cache_key = NULL;
451 avl_remove (cache, filename, &cache_key, NULL);
458 rc->values[rc->values_num] = strdup (value);
459 if (rc->values[rc->values_num] != NULL)
462 if (rc->values_num == 1)
463 rc->first_value = time (NULL);
465 /* Insert if this is the first value */
466 if ((cache != NULL) && (new_rc == 1))
468 void *cache_key = strdup (filename);
470 if (cache_key == NULL)
472 syslog (LOG_ERR, "rrdtool plugin: strdup failed: %s",
474 sfree (rc->values[0]);
480 avl_insert (cache, cache_key, rc);
483 DBG ("rrd_cache_insert (%s, %s) = %p", filename, value, (void *) rc);
486 } /* rrd_cache_t *rrd_cache_insert */
488 static int rrd_write_cache_entry (const char *filename, rrd_cache_t *rc)
498 argc = rc->values_num + 2;
499 argv = (char **) malloc ((argc + 1) * sizeof (char *));
503 fn = strdup (filename);
512 memcpy (argv + 2, rc->values, rc->values_num * sizeof (char *));
515 DBG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
517 optind = 0; /* bug in librrd? */
519 status = rrd_update (argc, argv);
524 /* Free the value list of `rc' */
525 for (i = 0; i < rc->values_num; i++)
526 free (rc->values[i]);
533 syslog (LOG_WARNING, "rrd_update failed: %s: %s",
534 filename, rrd_get_error ());
539 } /* int rrd_write_cache_entry */
541 static void rrd_cache_flush (int timeout)
550 avl_iterator_t *iter;
556 DBG ("Flushing cache, timeout = %i", timeout);
560 /* Build a list of entries to be flushed */
561 iter = avl_get_iterator (cache);
562 while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
564 DBG ("key = %s; age = %i;", key, now - rc->first_value);
565 if ((now - rc->first_value) >= timeout)
567 keys = (char **) realloc ((void *) keys,
568 (keys_num + 1) * sizeof (char *));
571 DBG ("realloc failed: %s", strerror (errno));
572 syslog (LOG_ERR, "rrdtool plugin: "
573 "realloc failed: %s",
575 avl_iterator_destroy (iter);
578 keys[keys_num] = key;
581 } /* while (avl_iterator_next) */
582 avl_iterator_destroy (iter);
584 for (i = 0; i < keys_num; i++)
586 if (avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
588 DBG ("avl_remove (%s) failed.", keys[i]);
592 rrd_write_cache_entry (keys[i], rc);
593 /* rc's value-list is free's by `rrd_write_cache_entry' */
597 } /* for (i = 0..keys_num) */
600 DBG ("Flushed %i value(s)", keys_num);
602 cache_flush_last = now;
603 } /* void rrd_cache_flush */
605 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
613 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
616 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
619 if (stat (filename, &statbuf) == -1)
623 if (rrd_create_file (filename, ds))
628 syslog (LOG_ERR, "stat(%s) failed: %s",
629 filename, strerror (errno));
633 else if (!S_ISREG (statbuf.st_mode))
635 syslog (LOG_ERR, "stat(%s): Not a regular file!",
640 rc = rrd_cache_insert (filename, values);
646 rrd_write_cache_entry (filename, rc);
647 /* rc's value-list is free's by `rrd_write_cache_entry' */
654 DBG ("age (%s) = %i", filename, now - rc->first_value);
656 /* `rc' is not free'd here, because we'll likely reuse it. If not, then
657 * the next flush will remove this entry. */
658 if ((now - rc->first_value) >= cache_timeout)
659 rrd_write_cache_entry (filename, rc);
661 if ((now - cache_flush_last) >= cache_flush_timeout)
662 rrd_cache_flush (cache_flush_timeout);
665 } /* int rrd_write */
667 static int rrd_config (const char *key, const char *value)
669 if (strcasecmp ("CacheTimeout", key) == 0)
671 int tmp = atoi (value);
674 fprintf (stderr, "rrdtool: `CacheTimeout' must "
675 "be greater than 0.\n");
680 else if (strcasecmp ("CacheFlush", key) == 0)
682 int tmp = atoi (value);
685 fprintf (stderr, "rrdtool: `CacheFlush' must "
686 "be greater than 0.\n");
689 cache_flush_timeout = tmp;
691 else if (strcasecmp ("DataDir", key) == 0)
695 datadir = strdup (value);
698 int len = strlen (datadir);
699 while ((len > 0) && (datadir[len - 1] == '/'))
711 else if (strcasecmp ("StepSize", key) == 0)
713 int tmp = atoi (value);
716 fprintf (stderr, "rrdtool: `StepSize' must "
717 "be greater than 0.\n");
722 else if (strcasecmp ("HeartBeat", key) == 0)
724 int tmp = atoi (value);
727 fprintf (stderr, "rrdtool: `HeartBeat' must "
728 "be greater than 0.\n");
733 else if (strcasecmp ("RRARows", key) == 0)
735 int tmp = atoi (value);
738 fprintf (stderr, "rrdtool: `RRARows' must "
739 "be greater than 0.\n");
744 else if (strcasecmp ("XFF", key) == 0)
746 double tmp = atof (value);
747 if ((tmp < 0.0) || (tmp >= 1.0))
749 fprintf (stderr, "rrdtool: `XFF' must "
750 "be in the range 0 to 1 (exclusive).");
760 } /* int rrd_config */
762 static int rrd_shutdown (void)
764 rrd_cache_flush (-1);
770 } /* int rrd_shutdown */
772 static int rrd_init (void)
775 stepsize = interval_g;
777 heartbeat = 2 * interval_g;
779 if (heartbeat < interval_g)
780 syslog (LOG_WARNING, "rrdtool plugin: Your `heartbeat' is "
781 "smaller than your `interval'. This will "
782 "likely cause problems.");
783 else if (stepsize < interval_g)
784 syslog (LOG_WARNING, "rrdtool plugin: Your `stepsize' is "
785 "smaller than your `interval'. This will "
786 "create needlessly big RRD-files.");
788 if (cache_timeout < 2)
791 cache_flush_timeout = 0;
795 if (cache_flush_timeout < cache_timeout)
796 cache_flush_timeout = 10 * cache_timeout;
798 cache = avl_create ((int (*) (const void *, const void *)) strcmp);
799 cache_flush_last = time (NULL);
800 plugin_register_shutdown ("rrdtool", rrd_shutdown);
803 DBG ("datadir = %s; stepsize = %i; heartbeat = %i; rrarows = %i; xff = %lf;",
804 (datadir == NULL) ? "(null)" : datadir,
805 stepsize, heartbeat, rrarows, xff);
810 void module_register (void)
812 plugin_register_config ("rrdtool", rrd_config,
813 config_keys, config_keys_num);
814 plugin_register_init ("rrdtool", rrd_init);
815 plugin_register_write ("rrdtool", rrd_write);