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"
46 typedef struct rrd_cache_s rrd_cache_t;
51 struct rrd_queue_s *next;
53 typedef struct rrd_queue_s rrd_queue_t;
58 static int rra_timespans[] =
66 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
68 static int *rra_timespans_custom = NULL;
69 static int rra_timespans_custom_num = 0;
71 static char *rra_types[] =
77 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
79 static const char *config_keys[] =
90 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
92 static char *datadir = NULL;
93 static int stepsize = 0;
94 static int heartbeat = 0;
95 static int rrarows = 1200;
96 static double xff = 0.1;
98 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
99 * ALWAYS lock `cache_lock' first! */
100 static int cache_timeout = 0;
101 static int cache_flush_timeout = 0;
102 static time_t cache_flush_last;
103 static avl_tree_t *cache = NULL;
104 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
106 static rrd_queue_t *queue_head = NULL;
107 static rrd_queue_t *queue_tail = NULL;
108 static pthread_t queue_thread = 0;
109 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
110 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
112 static int do_shutdown = 0;
117 static int rra_get (char ***ret)
119 static char **rra_def = NULL;
120 static int rra_num = 0;
135 if ((rra_num != 0) && (rra_def != NULL))
141 /* Use the configured timespans or fall back to the built-in defaults */
142 if (rra_timespans_custom_num != 0)
144 rts = rra_timespans_custom;
145 rts_num = rra_timespans_custom_num;
150 rts_num = rra_timespans_num;
153 rra_max = rts_num * rra_types_num;
155 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
157 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
159 if ((stepsize <= 0) || (rrarows <= 0))
166 for (i = 0; i < rts_num; i++)
170 if ((span / stepsize) < rrarows)
176 cdp_len = (int) floor (((double) span)
177 / ((double) (rrarows * stepsize)));
179 cdp_num = (int) ceil (((double) span)
180 / ((double) (cdp_len * stepsize)));
182 for (j = 0; j < rra_types_num; j++)
184 if (rra_num >= rra_max)
187 if (snprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
189 cdp_len, cdp_num) >= sizeof (buffer))
191 ERROR ("rra_get: Buffer would have been truncated.");
195 rra_def[rra_num++] = sstrdup (buffer);
200 DEBUG ("rra_num = %i", rra_num);
201 for (i = 0; i < rra_num; i++)
202 DEBUG (" %s", rra_def[i]);
209 static void ds_free (int ds_num, char **ds_def)
213 for (i = 0; i < ds_num; i++)
214 if (ds_def[i] != NULL)
219 static int ds_get (char ***ret, const data_set_t *ds)
228 DEBUG ("ds->ds_num = %i", ds->ds_num);
230 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
234 ERROR ("rrdtool plugin: malloc failed: %s",
235 sstrerror (errno, errbuf, sizeof (errbuf)));
238 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
240 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
242 data_source_t *d = ds->ds + ds_num;
246 ds_def[ds_num] = NULL;
248 if (d->type == DS_TYPE_COUNTER)
250 else if (d->type == DS_TYPE_GAUGE)
254 ERROR ("rrdtool plugin: Unknown DS type: %i",
265 snprintf (min, sizeof (min), "%lf", d->min);
266 min[sizeof (min) - 1] = '\0';
275 snprintf (max, sizeof (max), "%lf", d->max);
276 max[sizeof (max) - 1] = '\0';
279 status = snprintf (buffer, sizeof (buffer),
281 d->name, type, heartbeat,
283 if ((status < 1) || (status >= sizeof (buffer)))
286 ds_def[ds_num] = sstrdup (buffer);
287 } /* for ds_num = 0 .. ds->ds_num */
292 DEBUG ("ds_num = %i", ds_num);
293 for (i = 0; i < ds_num; i++)
294 DEBUG (" %s", ds_def[i]);
298 if (ds_num != ds->ds_num)
300 ds_free (ds_num, ds_def);
308 static int rrd_create_file (char *filename, const data_set_t *ds)
317 char stepsize_str[16];
320 if (check_create_dir (filename))
323 if ((rra_num = rra_get (&rra_def)) < 1)
325 ERROR ("rrd_create_file failed: Could not calculate RRAs");
329 if ((ds_num = ds_get (&ds_def, ds)) < 1)
331 ERROR ("rrd_create_file failed: Could not calculate DSes");
335 argc = ds_num + rra_num + 4;
337 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
340 ERROR ("rrd_create failed: %s",
341 sstrerror (errno, errbuf, sizeof (errbuf)));
345 status = snprintf (stepsize_str, sizeof (stepsize_str),
347 if ((status < 1) || (status >= sizeof (stepsize_str)))
349 ERROR ("rrdtool plugin: snprintf failed.");
356 argv[3] = stepsize_str;
359 for (i = 0; i < ds_num; i++)
360 argv[j++] = ds_def[i];
361 for (i = 0; i < rra_num; i++)
362 argv[j++] = rra_def[i];
365 optind = 0; /* bug in librrd? */
367 if (rrd_create (argc, argv) == -1)
369 ERROR ("rrd_create failed: %s: %s", filename, rrd_get_error ());
374 ds_free (ds_num, ds_def);
379 static int value_list_to_string (char *buffer, int buffer_len,
380 const data_set_t *ds, const value_list_t *vl)
386 memset (buffer, '\0', sizeof (buffer_len));
388 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
389 if ((status < 1) || (status >= buffer_len))
393 for (i = 0; i < ds->ds_num; i++)
395 if ((ds->ds[i].type != DS_TYPE_COUNTER)
396 && (ds->ds[i].type != DS_TYPE_GAUGE))
399 if (ds->ds[i].type == DS_TYPE_COUNTER)
400 status = snprintf (buffer + offset, buffer_len - offset,
401 ":%llu", vl->values[i].counter);
403 status = snprintf (buffer + offset, buffer_len - offset,
404 ":%lf", vl->values[i].gauge);
406 if ((status < 1) || (status >= (buffer_len - offset)))
410 } /* for ds->ds_num */
413 } /* int value_list_to_string */
415 static int value_list_to_filename (char *buffer, int buffer_len,
416 const data_set_t *ds, const value_list_t *vl)
423 status = snprintf (buffer + offset, buffer_len - offset,
425 if ((status < 1) || (status >= buffer_len - offset))
430 status = snprintf (buffer + offset, buffer_len - offset,
432 if ((status < 1) || (status >= buffer_len - offset))
436 if (strlen (vl->plugin_instance) > 0)
437 status = snprintf (buffer + offset, buffer_len - offset,
438 "%s-%s/", vl->plugin, vl->plugin_instance);
440 status = snprintf (buffer + offset, buffer_len - offset,
442 if ((status < 1) || (status >= buffer_len - offset))
446 if (strlen (vl->type_instance) > 0)
447 status = snprintf (buffer + offset, buffer_len - offset,
448 "%s-%s.rrd", ds->type, vl->type_instance);
450 status = snprintf (buffer + offset, buffer_len - offset,
452 if ((status < 1) || (status >= buffer_len - offset))
457 } /* int value_list_to_filename */
459 static int rrd_write_to_file (char *filename, char **values, int values_num)
468 argc = values_num + 2;
469 argv = (char **) malloc ((argc + 1) * sizeof (char *));
475 memcpy (argv + 2, values, values_num * sizeof (char *));
478 DEBUG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
480 optind = 0; /* bug in librrd? */
482 status = rrd_update (argc, argv);
485 WARNING ("rrd_update failed: %s: %s",
486 filename, rrd_get_error ());
493 } /* int rrd_write_cache_entry */
495 static void *rrd_queue_thread (void *data)
499 rrd_queue_t *queue_entry;
500 rrd_cache_t *cache_entry;
505 /* XXX: If you need to lock both, cache_lock and queue_lock, at
506 * the same time, ALWAYS lock `cache_lock' first! */
508 /* wait until an entry is available */
509 pthread_mutex_lock (&queue_lock);
510 while ((queue_head == NULL) && (do_shutdown == 0))
511 pthread_cond_wait (&queue_cond, &queue_lock);
513 /* We're in the shutdown phase */
514 if (queue_head == NULL)
516 pthread_mutex_unlock (&queue_lock);
520 /* Dequeue the first entry */
521 queue_entry = queue_head;
522 if (queue_head == queue_tail)
523 queue_head = queue_tail = NULL;
525 queue_head = queue_head->next;
527 /* Unlock the queue again */
528 pthread_mutex_unlock (&queue_lock);
530 /* We now need the cache lock so the entry isn't updated while
531 * we make a copy of it's values */
532 pthread_mutex_lock (&cache_lock);
534 avl_get (cache, queue_entry->filename, (void *) &cache_entry);
536 values = cache_entry->values;
537 values_num = cache_entry->values_num;
539 cache_entry->values = NULL;
540 cache_entry->values_num = 0;
541 cache_entry->flags = FLAG_NONE;
543 pthread_mutex_unlock (&cache_lock);
545 /* Write the values to the RRD-file */
546 rrd_write_to_file (queue_entry->filename, values, values_num);
548 for (i = 0; i < values_num; i++)
553 sfree (queue_entry->filename);
557 pthread_mutex_lock (&cache_lock);
560 pthread_mutex_unlock (&cache_lock);
562 pthread_exit ((void *) 0);
564 } /* void *rrd_queue_thread */
566 static int rrd_queue_cache_entry (const char *filename)
568 rrd_queue_t *queue_entry;
570 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
571 if (queue_entry == NULL)
574 queue_entry->filename = strdup (filename);
575 if (queue_entry->filename == NULL)
581 queue_entry->next = NULL;
583 pthread_mutex_lock (&queue_lock);
584 if (queue_tail == NULL)
585 queue_head = queue_entry;
587 queue_tail->next = queue_entry;
588 queue_tail = queue_entry;
589 pthread_cond_signal (&queue_cond);
590 pthread_mutex_unlock (&queue_lock);
592 DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
595 } /* int rrd_queue_cache_entry */
597 static void rrd_cache_flush (int timeout)
606 avl_iterator_t *iter;
609 DEBUG ("Flushing cache, timeout = %i", timeout);
613 /* Build a list of entries to be flushed */
614 iter = avl_get_iterator (cache);
615 while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
617 DEBUG ("key = %s; age = %i;", key, now - rc->first_value);
619 if (rc->flags == FLAG_QUEUED)
621 else if ((now - rc->first_value) < timeout)
623 else if (rc->values_num > 0)
625 if (rrd_queue_cache_entry (key) == 0)
626 rc->flags = FLAG_QUEUED;
628 else /* ancient and no values -> waste of memory */
630 keys = (char **) realloc ((void *) keys,
631 (keys_num + 1) * sizeof (char *));
635 ERROR ("rrdtool plugin: "
636 "realloc failed: %s",
637 sstrerror (errno, errbuf,
639 avl_iterator_destroy (iter);
642 keys[keys_num] = key;
645 } /* while (avl_iterator_next) */
646 avl_iterator_destroy (iter);
648 for (i = 0; i < keys_num; i++)
650 if (avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
652 DEBUG ("avl_remove (%s) failed.", keys[i]);
656 assert (rc->values == NULL);
657 assert (rc->values_num == 0);
662 } /* for (i = 0..keys_num) */
665 DEBUG ("Flushed %i value(s)", keys_num);
667 cache_flush_last = now;
668 } /* void rrd_cache_flush */
670 static int rrd_cache_insert (const char *filename,
671 const char *value, time_t value_time)
673 rrd_cache_t *rc = NULL;
677 pthread_mutex_lock (&cache_lock);
679 avl_get (cache, filename, (void *) &rc);
683 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
690 rc->flags = FLAG_NONE;
694 if (rc->last_value >= value_time)
696 pthread_mutex_unlock (&cache_lock);
697 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
698 (unsigned int) rc->last_value,
699 (unsigned int) value_time);
703 values_new = (char **) realloc ((void *) rc->values,
704 (rc->values_num + 1) * sizeof (char *));
705 if (values_new == NULL)
708 void *cache_key = NULL;
710 sstrerror (errno, errbuf, sizeof (errbuf));
712 avl_remove (cache, filename, &cache_key, NULL);
713 pthread_mutex_unlock (&cache_lock);
715 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
722 rc->values = values_new;
724 rc->values[rc->values_num] = strdup (value);
725 if (rc->values[rc->values_num] != NULL)
728 if (rc->values_num == 1)
729 rc->first_value = value_time;
730 rc->last_value = value_time;
732 /* Insert if this is the first value */
735 void *cache_key = strdup (filename);
737 if (cache_key == NULL)
740 sstrerror (errno, errbuf, sizeof (errbuf));
742 pthread_mutex_unlock (&cache_lock);
744 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
746 sfree (rc->values[0]);
752 avl_insert (cache, cache_key, rc);
755 DEBUG ("rrd_cache_insert (%s, %s, %u) = %p", filename, value,
756 (unsigned int) value_time, (void *) rc);
758 if ((rc->last_value - rc->first_value) >= cache_timeout)
760 /* XXX: If you need to lock both, cache_lock and queue_lock, at
761 * the same time, ALWAYS lock `cache_lock' first! */
762 if (rc->flags != FLAG_QUEUED)
764 if (rrd_queue_cache_entry (filename) == 0)
765 rc->flags = FLAG_QUEUED;
769 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
773 if ((cache_timeout > 0) &&
774 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
775 rrd_cache_flush (cache_flush_timeout);
778 pthread_mutex_unlock (&cache_lock);
781 } /* int rrd_cache_insert */
783 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
790 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
793 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
796 if (stat (filename, &statbuf) == -1)
800 if (rrd_create_file (filename, ds))
806 ERROR ("stat(%s) failed: %s", filename,
807 sstrerror (errno, errbuf,
812 else if (!S_ISREG (statbuf.st_mode))
814 ERROR ("stat(%s): Not a regular file!",
819 status = rrd_cache_insert (filename, values, vl->time);
822 } /* int rrd_write */
824 static int rrd_config (const char *key, const char *value)
826 if (strcasecmp ("CacheTimeout", key) == 0)
828 int tmp = atoi (value);
831 fprintf (stderr, "rrdtool: `CacheTimeout' must "
832 "be greater than 0.\n");
837 else if (strcasecmp ("CacheFlush", key) == 0)
839 int tmp = atoi (value);
842 fprintf (stderr, "rrdtool: `CacheFlush' must "
843 "be greater than 0.\n");
846 cache_flush_timeout = tmp;
848 else if (strcasecmp ("DataDir", key) == 0)
852 datadir = strdup (value);
855 int len = strlen (datadir);
856 while ((len > 0) && (datadir[len - 1] == '/'))
868 else if (strcasecmp ("StepSize", key) == 0)
870 int tmp = atoi (value);
873 fprintf (stderr, "rrdtool: `StepSize' must "
874 "be greater than 0.\n");
879 else if (strcasecmp ("HeartBeat", key) == 0)
881 int tmp = atoi (value);
884 fprintf (stderr, "rrdtool: `HeartBeat' must "
885 "be greater than 0.\n");
890 else if (strcasecmp ("RRARows", key) == 0)
892 int tmp = atoi (value);
895 fprintf (stderr, "rrdtool: `RRARows' must "
896 "be greater than 0.\n");
901 else if (strcasecmp ("RRATimespan", key) == 0)
903 char *saveptr = NULL;
909 value_copy = strdup (value);
910 if (value_copy == NULL)
914 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
918 tmp_alloc = realloc (rra_timespans_custom,
919 sizeof (int) * (rra_timespans_custom_num + 1));
920 if (tmp_alloc == NULL)
922 fprintf (stderr, "rrdtool: realloc failed.\n");
926 rra_timespans_custom = tmp_alloc;
927 rra_timespans_custom[rra_timespans_custom_num] = atoi (ptr);
928 if (rra_timespans_custom[rra_timespans_custom_num] != 0)
929 rra_timespans_custom_num++;
930 } /* while (strtok_r) */
933 else if (strcasecmp ("XFF", key) == 0)
935 double tmp = atof (value);
936 if ((tmp < 0.0) || (tmp >= 1.0))
938 fprintf (stderr, "rrdtool: `XFF' must "
939 "be in the range 0 to 1 (exclusive).");
949 } /* int rrd_config */
951 static int rrd_shutdown (void)
953 pthread_mutex_lock (&cache_lock);
954 rrd_cache_flush (-1);
955 pthread_mutex_unlock (&cache_lock);
957 pthread_mutex_lock (&queue_lock);
959 pthread_cond_signal (&queue_cond);
960 pthread_mutex_unlock (&queue_lock);
963 } /* int rrd_shutdown */
965 static int rrd_init (void)
970 stepsize = interval_g;
972 heartbeat = 2 * interval_g;
974 if (heartbeat < interval_g)
975 WARNING ("rrdtool plugin: Your `heartbeat' is "
976 "smaller than your `interval'. This will "
977 "likely cause problems.");
978 else if (stepsize < interval_g)
979 WARNING ("rrdtool plugin: Your `stepsize' is "
980 "smaller than your `interval'. This will "
981 "create needlessly big RRD-files.");
983 /* Set the cache up */
984 pthread_mutex_lock (&cache_lock);
986 cache = avl_create ((int (*) (const void *, const void *)) strcmp);
989 ERROR ("rrdtool plugin: avl_create failed.");
993 cache_flush_last = time (NULL);
994 if (cache_timeout < 2)
997 cache_flush_timeout = 0;
999 else if (cache_flush_timeout < cache_timeout)
1000 cache_flush_timeout = 10 * cache_timeout;
1002 pthread_mutex_unlock (&cache_lock);
1004 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
1007 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1011 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1012 " heartbeat = %i; rrarows = %i; xff = %lf;",
1013 (datadir == NULL) ? "(null)" : datadir,
1014 stepsize, heartbeat, rrarows, xff);
1017 } /* int rrd_init */
1019 void module_register (void)
1021 plugin_register_config ("rrdtool", rrd_config,
1022 config_keys, config_keys_num);
1023 plugin_register_init ("rrdtool", rrd_init);
1024 plugin_register_write ("rrdtool", rrd_write);
1025 plugin_register_shutdown ("rrdtool", rrd_shutdown);