2 * collectd - src/rrdtool.c
3 * Copyright (C) 2006,2007 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"
40 typedef struct rrd_cache_s rrd_cache_t;
45 static int rra_timespans[] =
53 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
55 static int *rra_timespans_custom = NULL;
56 static int rra_timespans_custom_num = 0;
58 static char *rra_types[] =
64 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
66 static const char *config_keys[] =
77 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
79 static char *datadir = NULL;
80 static int stepsize = 0;
81 static int heartbeat = 0;
82 static int rrarows = 1200;
83 static double xff = 0.1;
85 static int cache_timeout = 0;
86 static int cache_flush_timeout = 0;
87 static time_t cache_flush_last;
88 static avl_tree_t *cache = NULL;
89 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
94 static int rra_get (char ***ret)
96 static char **rra_def = NULL;
97 static int rra_num = 0;
112 if ((rra_num != 0) && (rra_def != NULL))
118 /* Use the configured timespans or fall back to the built-in defaults */
119 if (rra_timespans_custom_num != 0)
121 rts = rra_timespans_custom;
122 rts_num = rra_timespans_custom_num;
127 rts_num = rra_timespans_num;
130 rra_max = rts_num * rra_types_num;
132 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
134 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
136 if ((stepsize <= 0) || (rrarows <= 0))
143 for (i = 0; i < rts_num; i++)
147 if ((span / stepsize) < rrarows)
153 cdp_len = (int) floor (((double) span)
154 / ((double) (rrarows * stepsize)));
156 cdp_num = (int) ceil (((double) span)
157 / ((double) (cdp_len * stepsize)));
159 for (j = 0; j < rra_types_num; j++)
161 if (rra_num >= rra_max)
164 if (snprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
166 cdp_len, cdp_num) >= sizeof (buffer))
168 ERROR ("rra_get: Buffer would have been truncated.");
172 rra_def[rra_num++] = sstrdup (buffer);
177 DEBUG ("rra_num = %i", rra_num);
178 for (i = 0; i < rra_num; i++)
179 DEBUG (" %s", rra_def[i]);
186 static void ds_free (int ds_num, char **ds_def)
190 for (i = 0; i < ds_num; i++)
191 if (ds_def[i] != NULL)
196 static int ds_get (char ***ret, const data_set_t *ds)
205 DEBUG ("ds->ds_num = %i", ds->ds_num);
207 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
211 ERROR ("rrdtool plugin: malloc failed: %s",
212 sstrerror (errno, errbuf, sizeof (errbuf)));
215 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
217 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
219 data_source_t *d = ds->ds + ds_num;
223 ds_def[ds_num] = NULL;
225 if (d->type == DS_TYPE_COUNTER)
227 else if (d->type == DS_TYPE_GAUGE)
231 ERROR ("rrdtool plugin: Unknown DS type: %i",
242 snprintf (min, sizeof (min), "%lf", d->min);
243 min[sizeof (min) - 1] = '\0';
252 snprintf (max, sizeof (max), "%lf", d->max);
253 max[sizeof (max) - 1] = '\0';
256 status = snprintf (buffer, sizeof (buffer),
258 d->name, type, heartbeat,
260 if ((status < 1) || (status >= sizeof (buffer)))
263 ds_def[ds_num] = sstrdup (buffer);
264 } /* for ds_num = 0 .. ds->ds_num */
269 DEBUG ("ds_num = %i", ds_num);
270 for (i = 0; i < ds_num; i++)
271 DEBUG (" %s", ds_def[i]);
275 if (ds_num != ds->ds_num)
277 ds_free (ds_num, ds_def);
285 static int rrd_create_file (char *filename, const data_set_t *ds)
294 char stepsize_str[16];
297 if (check_create_dir (filename))
300 if ((rra_num = rra_get (&rra_def)) < 1)
302 ERROR ("rrd_create_file failed: Could not calculate RRAs");
306 if ((ds_num = ds_get (&ds_def, ds)) < 1)
308 ERROR ("rrd_create_file failed: Could not calculate DSes");
312 argc = ds_num + rra_num + 4;
314 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
317 ERROR ("rrd_create failed: %s",
318 sstrerror (errno, errbuf, sizeof (errbuf)));
322 status = snprintf (stepsize_str, sizeof (stepsize_str),
324 if ((status < 1) || (status >= sizeof (stepsize_str)))
326 ERROR ("rrdtool plugin: snprintf failed.");
333 argv[3] = stepsize_str;
336 for (i = 0; i < ds_num; i++)
337 argv[j++] = ds_def[i];
338 for (i = 0; i < rra_num; i++)
339 argv[j++] = rra_def[i];
342 optind = 0; /* bug in librrd? */
344 if (rrd_create (argc, argv) == -1)
346 ERROR ("rrd_create failed: %s: %s", filename, rrd_get_error ());
351 ds_free (ds_num, ds_def);
356 static int value_list_to_string (char *buffer, int buffer_len,
357 const data_set_t *ds, const value_list_t *vl)
363 memset (buffer, '\0', sizeof (buffer_len));
365 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
366 if ((status < 1) || (status >= buffer_len))
370 for (i = 0; i < ds->ds_num; i++)
372 if ((ds->ds[i].type != DS_TYPE_COUNTER)
373 && (ds->ds[i].type != DS_TYPE_GAUGE))
376 if (ds->ds[i].type == DS_TYPE_COUNTER)
377 status = snprintf (buffer + offset, buffer_len - offset,
378 ":%llu", vl->values[i].counter);
380 status = snprintf (buffer + offset, buffer_len - offset,
381 ":%lf", vl->values[i].gauge);
383 if ((status < 1) || (status >= (buffer_len - offset)))
387 } /* for ds->ds_num */
390 } /* int value_list_to_string */
392 static int value_list_to_filename (char *buffer, int buffer_len,
393 const data_set_t *ds, const value_list_t *vl)
400 status = snprintf (buffer + offset, buffer_len - offset,
402 if ((status < 1) || (status >= buffer_len - offset))
407 status = snprintf (buffer + offset, buffer_len - offset,
409 if ((status < 1) || (status >= buffer_len - offset))
413 if (strlen (vl->plugin_instance) > 0)
414 status = snprintf (buffer + offset, buffer_len - offset,
415 "%s-%s/", vl->plugin, vl->plugin_instance);
417 status = snprintf (buffer + offset, buffer_len - offset,
419 if ((status < 1) || (status >= buffer_len - offset))
423 if (strlen (vl->type_instance) > 0)
424 status = snprintf (buffer + offset, buffer_len - offset,
425 "%s-%s.rrd", ds->type, vl->type_instance);
427 status = snprintf (buffer + offset, buffer_len - offset,
429 if ((status < 1) || (status >= buffer_len - offset))
434 } /* int value_list_to_filename */
436 static rrd_cache_t *rrd_cache_insert (const char *filename,
439 rrd_cache_t *rc = NULL;
443 avl_get (cache, filename, (void *) &rc);
447 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
456 rc->values = (char **) realloc ((void *) rc->values,
457 (rc->values_num + 1) * sizeof (char *));
458 if (rc->values == NULL)
461 ERROR ("rrdtool plugin: realloc failed: %s",
462 sstrerror (errno, errbuf, sizeof (errbuf)));
465 void *cache_key = NULL;
466 avl_remove (cache, filename, &cache_key, NULL);
473 rc->values[rc->values_num] = strdup (value);
474 if (rc->values[rc->values_num] != NULL)
477 if (rc->values_num == 1)
478 rc->first_value = time (NULL);
480 /* Insert if this is the first value */
481 if ((cache != NULL) && (new_rc == 1))
483 void *cache_key = strdup (filename);
485 if (cache_key == NULL)
488 ERROR ("rrdtool plugin: strdup failed: %s",
489 sstrerror (errno, errbuf, sizeof (errbuf)));
490 sfree (rc->values[0]);
496 avl_insert (cache, cache_key, rc);
499 DEBUG ("rrd_cache_insert (%s, %s) = %p", filename, value, (void *) rc);
502 } /* rrd_cache_t *rrd_cache_insert */
504 static int rrd_write_cache_entry (const char *filename, rrd_cache_t *rc)
514 if (rc->values_num < 1)
517 argc = rc->values_num + 2;
518 argv = (char **) malloc ((argc + 1) * sizeof (char *));
522 fn = strdup (filename);
531 memcpy (argv + 2, rc->values, rc->values_num * sizeof (char *));
534 DEBUG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
536 optind = 0; /* bug in librrd? */
538 status = rrd_update (argc, argv);
541 WARNING ("rrd_update failed: %s: %s",
542 filename, rrd_get_error ());
548 /* Free the value list of `rc' */
549 for (i = 0; i < rc->values_num; i++)
550 free (rc->values[i]);
556 } /* int rrd_write_cache_entry */
558 static void rrd_cache_flush (int timeout)
567 avl_iterator_t *iter;
573 DEBUG ("Flushing cache, timeout = %i", timeout);
577 /* Build a list of entries to be flushed */
578 iter = avl_get_iterator (cache);
579 while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
581 DEBUG ("key = %s; age = %i;", key, now - rc->first_value);
582 if ((now - rc->first_value) >= timeout)
584 keys = (char **) realloc ((void *) keys,
585 (keys_num + 1) * sizeof (char *));
589 ERROR ("rrdtool plugin: "
590 "realloc failed: %s",
591 sstrerror (errno, errbuf,
593 avl_iterator_destroy (iter);
596 keys[keys_num] = key;
599 } /* while (avl_iterator_next) */
600 avl_iterator_destroy (iter);
602 for (i = 0; i < keys_num; i++)
604 if (avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
606 DEBUG ("avl_remove (%s) failed.", keys[i]);
610 rrd_write_cache_entry (keys[i], rc);
611 /* rc's value-list is free's by `rrd_write_cache_entry' */
615 } /* for (i = 0..keys_num) */
618 DEBUG ("Flushed %i value(s)", keys_num);
620 cache_flush_last = now;
621 } /* void rrd_cache_flush */
623 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
631 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
634 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
637 if (stat (filename, &statbuf) == -1)
641 if (rrd_create_file (filename, ds))
647 ERROR ("stat(%s) failed: %s", filename,
648 sstrerror (errno, errbuf,
653 else if (!S_ISREG (statbuf.st_mode))
655 ERROR ("stat(%s): Not a regular file!",
660 pthread_mutex_lock (&cache_lock);
661 rc = rrd_cache_insert (filename, values);
664 pthread_mutex_unlock (&cache_lock);
670 rrd_write_cache_entry (filename, rc);
671 /* rc's value-list is free's by `rrd_write_cache_entry' */
673 pthread_mutex_unlock (&cache_lock);
679 DEBUG ("age (%s) = %i", filename, now - rc->first_value);
681 /* `rc' is not free'd here, because we'll likely reuse it. If not, then
682 * the next flush will remove this entry. */
683 if ((now - rc->first_value) >= cache_timeout)
684 rrd_write_cache_entry (filename, rc);
686 if ((now - cache_flush_last) >= cache_flush_timeout)
687 rrd_cache_flush (cache_flush_timeout);
689 pthread_mutex_unlock (&cache_lock);
691 } /* int rrd_write */
693 static int rrd_config (const char *key, const char *value)
695 if (strcasecmp ("CacheTimeout", key) == 0)
697 int tmp = atoi (value);
700 fprintf (stderr, "rrdtool: `CacheTimeout' must "
701 "be greater than 0.\n");
706 else if (strcasecmp ("CacheFlush", key) == 0)
708 int tmp = atoi (value);
711 fprintf (stderr, "rrdtool: `CacheFlush' must "
712 "be greater than 0.\n");
715 cache_flush_timeout = tmp;
717 else if (strcasecmp ("DataDir", key) == 0)
721 datadir = strdup (value);
724 int len = strlen (datadir);
725 while ((len > 0) && (datadir[len - 1] == '/'))
737 else if (strcasecmp ("StepSize", key) == 0)
739 int tmp = atoi (value);
742 fprintf (stderr, "rrdtool: `StepSize' must "
743 "be greater than 0.\n");
748 else if (strcasecmp ("HeartBeat", key) == 0)
750 int tmp = atoi (value);
753 fprintf (stderr, "rrdtool: `HeartBeat' must "
754 "be greater than 0.\n");
759 else if (strcasecmp ("RRARows", key) == 0)
761 int tmp = atoi (value);
764 fprintf (stderr, "rrdtool: `RRARows' must "
765 "be greater than 0.\n");
770 else if (strcasecmp ("RRATimespan", key) == 0)
772 char *saveptr = NULL;
778 value_copy = strdup (value);
779 if (value_copy == NULL)
783 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
787 tmp_alloc = realloc (rra_timespans_custom,
788 sizeof (int) * (rra_timespans_custom_num + 1));
789 if (tmp_alloc == NULL)
791 fprintf (stderr, "rrdtool: realloc failed.\n");
795 rra_timespans_custom = tmp_alloc;
796 rra_timespans_custom[rra_timespans_custom_num] = atoi (ptr);
797 if (rra_timespans_custom[rra_timespans_custom_num] != 0)
798 rra_timespans_custom_num++;
799 } /* while (strtok_r) */
802 else if (strcasecmp ("XFF", key) == 0)
804 double tmp = atof (value);
805 if ((tmp < 0.0) || (tmp >= 1.0))
807 fprintf (stderr, "rrdtool: `XFF' must "
808 "be in the range 0 to 1 (exclusive).");
818 } /* int rrd_config */
820 static int rrd_shutdown (void)
822 pthread_mutex_lock (&cache_lock);
823 rrd_cache_flush (-1);
827 pthread_mutex_unlock (&cache_lock);
830 } /* int rrd_shutdown */
832 static int rrd_init (void)
835 stepsize = interval_g;
837 heartbeat = 2 * interval_g;
839 if (heartbeat < interval_g)
840 WARNING ("rrdtool plugin: Your `heartbeat' is "
841 "smaller than your `interval'. This will "
842 "likely cause problems.");
843 else if (stepsize < interval_g)
844 WARNING ("rrdtool plugin: Your `stepsize' is "
845 "smaller than your `interval'. This will "
846 "create needlessly big RRD-files.");
848 pthread_mutex_lock (&cache_lock);
849 if (cache_timeout < 2)
852 cache_flush_timeout = 0;
856 if (cache_flush_timeout < cache_timeout)
857 cache_flush_timeout = 10 * cache_timeout;
859 cache = avl_create ((int (*) (const void *, const void *)) strcmp);
860 cache_flush_last = time (NULL);
861 plugin_register_shutdown ("rrdtool", rrd_shutdown);
863 pthread_mutex_unlock (&cache_lock);
865 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
866 " heartbeat = %i; rrarows = %i; xff = %lf;",
867 (datadir == NULL) ? "(null)" : datadir,
868 stepsize, heartbeat, rrarows, xff);
873 void module_register (void)
875 plugin_register_config ("rrdtool", rrd_config,
876 config_keys, config_keys_num);
877 plugin_register_init ("rrdtool", rrd_init);
878 plugin_register_write ("rrdtool", rrd_write);