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"
36 typedef struct rrd_cache_s rrd_cache_t;
41 static int rra_timespans[] =
49 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
51 static char *rra_types[] =
57 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
59 static const char *config_keys[] =
69 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
71 static char *datadir = NULL;
72 static int stepsize = 0;
73 static int heartbeat = 0;
74 static int rrarows = 1200;
75 static double xff = 0.1;
77 static int cache_timeout = 0;
78 static int cache_flush_timeout = 0;
79 static time_t cache_flush_last;
80 static avl_tree_t *cache = NULL;
85 static int rra_get (char ***ret)
87 static char **rra_def = NULL;
88 static int rra_num = 0;
90 int rra_max = rra_timespans_num * rra_types_num;
100 if ((rra_num != 0) && (rra_def != NULL))
106 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
108 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
110 if ((stepsize <= 0) || (rrarows <= 0))
117 for (i = 0; i < rra_timespans_num; i++)
119 span = rra_timespans[i];
121 if ((span / stepsize) < rrarows)
127 cdp_len = (int) floor (((double) span)
128 / ((double) (rrarows * stepsize)));
130 cdp_num = (int) ceil (((double) span)
131 / ((double) (cdp_len * stepsize)));
133 for (j = 0; j < rra_types_num; j++)
135 if (rra_num >= rra_max)
138 if (snprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
140 cdp_len, cdp_num) >= sizeof (buffer))
142 ERROR ("rra_get: Buffer would have been truncated.");
146 rra_def[rra_num++] = sstrdup (buffer);
151 DEBUG ("rra_num = %i", rra_num);
152 for (i = 0; i < rra_num; i++)
153 DEBUG (" %s", rra_def[i]);
160 static void ds_free (int ds_num, char **ds_def)
164 for (i = 0; i < ds_num; i++)
165 if (ds_def[i] != NULL)
170 static int ds_get (char ***ret, const data_set_t *ds)
179 DEBUG ("ds->ds_num = %i", ds->ds_num);
181 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
185 ERROR ("rrdtool plugin: malloc failed: %s",
186 sstrerror (errno, errbuf, sizeof (errbuf)));
189 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
191 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
193 data_source_t *d = ds->ds + ds_num;
197 ds_def[ds_num] = NULL;
199 if (d->type == DS_TYPE_COUNTER)
201 else if (d->type == DS_TYPE_GAUGE)
205 ERROR ("rrdtool plugin: Unknown DS type: %i",
216 snprintf (min, sizeof (min), "%lf", d->min);
217 min[sizeof (min) - 1] = '\0';
226 snprintf (max, sizeof (max), "%lf", d->max);
227 max[sizeof (max) - 1] = '\0';
230 status = snprintf (buffer, sizeof (buffer),
232 d->name, type, heartbeat,
234 if ((status < 1) || (status >= sizeof (buffer)))
237 ds_def[ds_num] = sstrdup (buffer);
238 } /* for ds_num = 0 .. ds->ds_num */
243 DEBUG ("ds_num = %i", ds_num);
244 for (i = 0; i < ds_num; i++)
245 DEBUG (" %s", ds_def[i]);
249 if (ds_num != ds->ds_num)
251 ds_free (ds_num, ds_def);
259 static int rrd_create_file (char *filename, const data_set_t *ds)
268 char stepsize_str[16];
271 if (check_create_dir (filename))
274 if ((rra_num = rra_get (&rra_def)) < 1)
276 ERROR ("rrd_create_file failed: Could not calculate RRAs");
280 if ((ds_num = ds_get (&ds_def, ds)) < 1)
282 ERROR ("rrd_create_file failed: Could not calculate DSes");
286 argc = ds_num + rra_num + 4;
288 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
291 ERROR ("rrd_create failed: %s",
292 sstrerror (errno, errbuf, sizeof (errbuf)));
296 status = snprintf (stepsize_str, sizeof (stepsize_str),
298 if ((status < 1) || (status >= sizeof (stepsize_str)))
300 ERROR ("rrdtool plugin: snprintf failed.");
307 argv[3] = stepsize_str;
310 for (i = 0; i < ds_num; i++)
311 argv[j++] = ds_def[i];
312 for (i = 0; i < rra_num; i++)
313 argv[j++] = rra_def[i];
316 optind = 0; /* bug in librrd? */
318 if (rrd_create (argc, argv) == -1)
320 ERROR ("rrd_create failed: %s: %s", filename, rrd_get_error ());
325 ds_free (ds_num, ds_def);
330 static int value_list_to_string (char *buffer, int buffer_len,
331 const data_set_t *ds, const value_list_t *vl)
337 memset (buffer, '\0', sizeof (buffer_len));
339 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
340 if ((status < 1) || (status >= buffer_len))
344 for (i = 0; i < ds->ds_num; i++)
346 if ((ds->ds[i].type != DS_TYPE_COUNTER)
347 && (ds->ds[i].type != DS_TYPE_GAUGE))
350 if (ds->ds[i].type == DS_TYPE_COUNTER)
351 status = snprintf (buffer + offset, buffer_len - offset,
352 ":%llu", vl->values[i].counter);
354 status = snprintf (buffer + offset, buffer_len - offset,
355 ":%lf", vl->values[i].gauge);
357 if ((status < 1) || (status >= (buffer_len - offset)))
361 } /* for ds->ds_num */
364 } /* int value_list_to_string */
366 static int value_list_to_filename (char *buffer, int buffer_len,
367 const data_set_t *ds, const value_list_t *vl)
374 status = snprintf (buffer + offset, buffer_len - offset,
376 if ((status < 1) || (status >= buffer_len - offset))
381 status = snprintf (buffer + offset, buffer_len - offset,
383 if ((status < 1) || (status >= buffer_len - offset))
387 if (strlen (vl->plugin_instance) > 0)
388 status = snprintf (buffer + offset, buffer_len - offset,
389 "%s-%s/", vl->plugin, vl->plugin_instance);
391 status = snprintf (buffer + offset, buffer_len - offset,
393 if ((status < 1) || (status >= buffer_len - offset))
397 if (strlen (vl->type_instance) > 0)
398 status = snprintf (buffer + offset, buffer_len - offset,
399 "%s-%s.rrd", ds->type, vl->type_instance);
401 status = snprintf (buffer + offset, buffer_len - offset,
403 if ((status < 1) || (status >= buffer_len - offset))
408 } /* int value_list_to_filename */
410 static rrd_cache_t *rrd_cache_insert (const char *filename,
413 rrd_cache_t *rc = NULL;
417 avl_get (cache, filename, (void *) &rc);
421 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
430 rc->values = (char **) realloc ((void *) rc->values,
431 (rc->values_num + 1) * sizeof (char *));
432 if (rc->values == NULL)
435 ERROR ("rrdtool plugin: realloc failed: %s",
436 sstrerror (errno, errbuf, sizeof (errbuf)));
439 void *cache_key = NULL;
440 avl_remove (cache, filename, &cache_key, NULL);
447 rc->values[rc->values_num] = strdup (value);
448 if (rc->values[rc->values_num] != NULL)
451 if (rc->values_num == 1)
452 rc->first_value = time (NULL);
454 /* Insert if this is the first value */
455 if ((cache != NULL) && (new_rc == 1))
457 void *cache_key = strdup (filename);
459 if (cache_key == NULL)
462 ERROR ("rrdtool plugin: strdup failed: %s",
463 sstrerror (errno, errbuf, sizeof (errbuf)));
464 sfree (rc->values[0]);
470 avl_insert (cache, cache_key, rc);
473 DEBUG ("rrd_cache_insert (%s, %s) = %p", filename, value, (void *) rc);
476 } /* rrd_cache_t *rrd_cache_insert */
478 static int rrd_write_cache_entry (const char *filename, rrd_cache_t *rc)
488 argc = rc->values_num + 2;
489 argv = (char **) malloc ((argc + 1) * sizeof (char *));
493 fn = strdup (filename);
502 memcpy (argv + 2, rc->values, rc->values_num * sizeof (char *));
505 DEBUG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
507 optind = 0; /* bug in librrd? */
509 status = rrd_update (argc, argv);
514 /* Free the value list of `rc' */
515 for (i = 0; i < rc->values_num; i++)
516 free (rc->values[i]);
523 WARNING ("rrd_update failed: %s: %s",
524 filename, rrd_get_error ());
529 } /* int rrd_write_cache_entry */
531 static void rrd_cache_flush (int timeout)
540 avl_iterator_t *iter;
546 DEBUG ("Flushing cache, timeout = %i", timeout);
550 /* Build a list of entries to be flushed */
551 iter = avl_get_iterator (cache);
552 while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
554 DEBUG ("key = %s; age = %i;", key, now - rc->first_value);
555 if ((now - rc->first_value) >= timeout)
557 keys = (char **) realloc ((void *) keys,
558 (keys_num + 1) * sizeof (char *));
562 ERROR ("rrdtool plugin: "
563 "realloc failed: %s",
564 sstrerror (errno, errbuf,
566 avl_iterator_destroy (iter);
569 keys[keys_num] = key;
572 } /* while (avl_iterator_next) */
573 avl_iterator_destroy (iter);
575 for (i = 0; i < keys_num; i++)
577 if (avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
579 DEBUG ("avl_remove (%s) failed.", keys[i]);
583 rrd_write_cache_entry (keys[i], rc);
584 /* rc's value-list is free's by `rrd_write_cache_entry' */
588 } /* for (i = 0..keys_num) */
591 DEBUG ("Flushed %i value(s)", keys_num);
593 cache_flush_last = now;
594 } /* void rrd_cache_flush */
596 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
604 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
607 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
610 if (stat (filename, &statbuf) == -1)
614 if (rrd_create_file (filename, ds))
620 ERROR ("stat(%s) failed: %s", filename,
621 sstrerror (errno, errbuf,
626 else if (!S_ISREG (statbuf.st_mode))
628 ERROR ("stat(%s): Not a regular file!",
633 rc = rrd_cache_insert (filename, values);
639 rrd_write_cache_entry (filename, rc);
640 /* rc's value-list is free's by `rrd_write_cache_entry' */
647 DEBUG ("age (%s) = %i", filename, now - rc->first_value);
649 /* `rc' is not free'd here, because we'll likely reuse it. If not, then
650 * the next flush will remove this entry. */
651 if ((now - rc->first_value) >= cache_timeout)
652 rrd_write_cache_entry (filename, rc);
654 if ((now - cache_flush_last) >= cache_flush_timeout)
655 rrd_cache_flush (cache_flush_timeout);
658 } /* int rrd_write */
660 static int rrd_config (const char *key, const char *value)
662 if (strcasecmp ("CacheTimeout", key) == 0)
664 int tmp = atoi (value);
667 fprintf (stderr, "rrdtool: `CacheTimeout' must "
668 "be greater than 0.\n");
673 else if (strcasecmp ("CacheFlush", key) == 0)
675 int tmp = atoi (value);
678 fprintf (stderr, "rrdtool: `CacheFlush' must "
679 "be greater than 0.\n");
682 cache_flush_timeout = tmp;
684 else if (strcasecmp ("DataDir", key) == 0)
688 datadir = strdup (value);
691 int len = strlen (datadir);
692 while ((len > 0) && (datadir[len - 1] == '/'))
704 else if (strcasecmp ("StepSize", key) == 0)
706 int tmp = atoi (value);
709 fprintf (stderr, "rrdtool: `StepSize' must "
710 "be greater than 0.\n");
715 else if (strcasecmp ("HeartBeat", key) == 0)
717 int tmp = atoi (value);
720 fprintf (stderr, "rrdtool: `HeartBeat' must "
721 "be greater than 0.\n");
726 else if (strcasecmp ("RRARows", key) == 0)
728 int tmp = atoi (value);
731 fprintf (stderr, "rrdtool: `RRARows' must "
732 "be greater than 0.\n");
737 else if (strcasecmp ("XFF", key) == 0)
739 double tmp = atof (value);
740 if ((tmp < 0.0) || (tmp >= 1.0))
742 fprintf (stderr, "rrdtool: `XFF' must "
743 "be in the range 0 to 1 (exclusive).");
753 } /* int rrd_config */
755 static int rrd_shutdown (void)
757 rrd_cache_flush (-1);
763 } /* int rrd_shutdown */
765 static int rrd_init (void)
768 stepsize = interval_g;
770 heartbeat = 2 * interval_g;
772 if (heartbeat < interval_g)
773 WARNING ("rrdtool plugin: Your `heartbeat' is "
774 "smaller than your `interval'. This will "
775 "likely cause problems.");
776 else if (stepsize < interval_g)
777 WARNING ("rrdtool plugin: Your `stepsize' is "
778 "smaller than your `interval'. This will "
779 "create needlessly big RRD-files.");
781 if (cache_timeout < 2)
784 cache_flush_timeout = 0;
788 if (cache_flush_timeout < cache_timeout)
789 cache_flush_timeout = 10 * cache_timeout;
791 cache = avl_create ((int (*) (const void *, const void *)) strcmp);
792 cache_flush_last = time (NULL);
793 plugin_register_shutdown ("rrdtool", rrd_shutdown);
796 DEBUG ("datadir = %s; stepsize = %i; heartbeat = %i; rrarows = %i; xff = %lf;",
797 (datadir == NULL) ? "(null)" : datadir,
798 stepsize, heartbeat, rrarows, xff);
803 void module_register (void)
805 plugin_register_config ("rrdtool", rrd_config,
806 config_keys, config_keys_num);
807 plugin_register_init ("rrdtool", rrd_init);
808 plugin_register_write ("rrdtool", rrd_write);