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 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
93 * is zero a default, depending on the `interval' member of the value list is
95 static char *datadir = NULL;
96 static int stepsize = 0;
97 static int heartbeat = 0;
98 static int rrarows = 1200;
99 static double xff = 0.1;
101 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
102 * ALWAYS lock `cache_lock' first! */
103 static int cache_timeout = 0;
104 static int cache_flush_timeout = 0;
105 static time_t cache_flush_last;
106 static avl_tree_t *cache = NULL;
107 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
109 static rrd_queue_t *queue_head = NULL;
110 static rrd_queue_t *queue_tail = NULL;
111 static pthread_t queue_thread = 0;
112 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
113 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
115 #if !HAVE_THREADSAFE_LIBRRD
116 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
119 static int do_shutdown = 0;
125 static void rra_free (int rra_num, char **rra_def)
129 for (i = 0; i < rra_num; i++)
134 } /* void rra_free */
136 static int rra_get (char ***ret, const value_list_t *vl)
154 /* The stepsize we use here: If it is user-set, use it. If not, use the
155 * interval of the value-list. */
164 ss = (stepsize > 0) ? stepsize : vl->interval;
171 /* Use the configured timespans or fall back to the built-in defaults */
172 if (rra_timespans_custom_num != 0)
174 rts = rra_timespans_custom;
175 rts_num = rra_timespans_custom_num;
180 rts_num = rra_timespans_num;
183 rra_max = rts_num * rra_types_num;
185 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
187 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
191 for (i = 0; i < rts_num; i++)
195 if ((span / ss) < rrarows)
201 cdp_len = (int) floor (((double) span)
202 / ((double) (rrarows * ss)));
204 cdp_num = (int) ceil (((double) span)
205 / ((double) (cdp_len * ss)));
207 for (j = 0; j < rra_types_num; j++)
209 if (rra_num >= rra_max)
212 if (snprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
214 cdp_len, cdp_num) >= sizeof (buffer))
216 ERROR ("rra_get: Buffer would have been truncated.");
220 rra_def[rra_num++] = sstrdup (buffer);
225 DEBUG ("rra_num = %i", rra_num);
226 for (i = 0; i < rra_num; i++)
227 DEBUG (" %s", rra_def[i]);
234 static void ds_free (int ds_num, char **ds_def)
238 for (i = 0; i < ds_num; i++)
239 if (ds_def[i] != NULL)
244 static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
253 DEBUG ("ds->ds_num = %i", ds->ds_num);
255 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
259 ERROR ("rrdtool plugin: malloc failed: %s",
260 sstrerror (errno, errbuf, sizeof (errbuf)));
263 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
265 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
267 data_source_t *d = ds->ds + ds_num;
271 ds_def[ds_num] = NULL;
273 if (d->type == DS_TYPE_COUNTER)
275 else if (d->type == DS_TYPE_GAUGE)
279 ERROR ("rrdtool plugin: Unknown DS type: %i",
290 snprintf (min, sizeof (min), "%lf", d->min);
291 min[sizeof (min) - 1] = '\0';
300 snprintf (max, sizeof (max), "%lf", d->max);
301 max[sizeof (max) - 1] = '\0';
304 status = snprintf (buffer, sizeof (buffer),
307 (heartbeat > 0) ? heartbeat : (2 * vl->interval),
309 if ((status < 1) || (status >= sizeof (buffer)))
312 ds_def[ds_num] = sstrdup (buffer);
313 } /* for ds_num = 0 .. ds->ds_num */
318 DEBUG ("ds_num = %i", ds_num);
319 for (i = 0; i < ds_num; i++)
320 DEBUG (" %s", ds_def[i]);
324 if (ds_num != ds->ds_num)
326 ds_free (ds_num, ds_def);
334 #if HAVE_THREADSAFE_LIBRRD
335 static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
336 int argc, char **argv)
340 optind = 0; /* bug in librrd? */
343 status = rrd_create_r (filename, pdp_step, last_up, argc, argv);
347 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
348 filename, rrd_get_error ());
352 } /* int srrd_create */
354 static int srrd_update (char *filename, char *template, int argc, char **argv)
358 optind = 0; /* bug in librrd? */
361 status = rrd_update_r (filename, template, argc, argv);
365 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
366 filename, rrd_get_error ());
370 } /* int srrd_update */
371 /* #endif HAVE_THREADSAFE_LIBRRD */
373 #else /* !HAVE_THREADSAFE_LIBRRD */
374 static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
375 int argc, char **argv)
382 char pdp_step_str[16];
383 char last_up_str[16];
386 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
387 if (new_argv == NULL)
389 ERROR ("rrdtool plugin: malloc failed.");
394 last_up = time (NULL) - 10;
396 snprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
397 pdp_step_str[sizeof (pdp_step_str) - 1] = '\0';
398 snprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);
399 last_up_str[sizeof (last_up_str) - 1] = '\0';
401 new_argv[0] = "create";
402 new_argv[1] = filename;
404 new_argv[3] = pdp_step_str;
406 new_argv[5] = last_up_str;
408 memcpy (new_argv + 6, argv, argc * sizeof (char *));
409 new_argv[new_argc] = NULL;
411 pthread_mutex_lock (&librrd_lock);
412 optind = 0; /* bug in librrd? */
415 status = rrd_create (new_argc, new_argv);
416 pthread_mutex_unlock (&librrd_lock);
420 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
421 filename, rrd_get_error ());
427 } /* int srrd_create */
429 static int srrd_update (char *filename, char *template, int argc, char **argv)
436 assert (template == NULL);
439 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
440 if (new_argv == NULL)
442 ERROR ("rrdtool plugin: malloc failed.");
446 new_argv[0] = "update";
447 new_argv[1] = filename;
449 memcpy (new_argv + 2, argv, argc * sizeof (char *));
450 new_argv[new_argc] = NULL;
452 pthread_mutex_lock (&librrd_lock);
453 optind = 0; /* bug in librrd? */
456 status = rrd_update (new_argc, new_argv);
457 pthread_mutex_unlock (&librrd_lock);
461 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
462 argv[1], rrd_get_error ());
468 } /* int srrd_update */
469 #endif /* !HAVE_THREADSAFE_LIBRRD */
471 static int rrd_create_file (char *filename, const data_set_t *ds, const value_list_t *vl)
481 if (check_create_dir (filename))
484 if ((rra_num = rra_get (&rra_def, vl)) < 1)
486 ERROR ("rrd_create_file failed: Could not calculate RRAs");
490 if ((ds_num = ds_get (&ds_def, ds, vl)) < 1)
492 ERROR ("rrd_create_file failed: Could not calculate DSes");
496 argc = ds_num + rra_num;
498 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
501 ERROR ("rrd_create failed: %s",
502 sstrerror (errno, errbuf, sizeof (errbuf)));
506 memcpy (argv, ds_def, ds_num * sizeof (char *));
507 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
508 argv[ds_num + rra_num] = NULL;
510 assert (vl->time > 10);
511 status = srrd_create (filename,
512 (stepsize > 0) ? stepsize : vl->interval,
517 ds_free (ds_num, ds_def);
518 rra_free (rra_num, rra_def);
523 static int value_list_to_string (char *buffer, int buffer_len,
524 const data_set_t *ds, const value_list_t *vl)
530 memset (buffer, '\0', buffer_len);
532 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
533 if ((status < 1) || (status >= buffer_len))
537 for (i = 0; i < ds->ds_num; i++)
539 if ((ds->ds[i].type != DS_TYPE_COUNTER)
540 && (ds->ds[i].type != DS_TYPE_GAUGE))
543 if (ds->ds[i].type == DS_TYPE_COUNTER)
544 status = snprintf (buffer + offset, buffer_len - offset,
545 ":%llu", vl->values[i].counter);
547 status = snprintf (buffer + offset, buffer_len - offset,
548 ":%lf", vl->values[i].gauge);
550 if ((status < 1) || (status >= (buffer_len - offset)))
554 } /* for ds->ds_num */
557 } /* int value_list_to_string */
559 static int value_list_to_filename (char *buffer, int buffer_len,
560 const data_set_t *ds, const value_list_t *vl)
567 status = snprintf (buffer + offset, buffer_len - offset,
569 if ((status < 1) || (status >= buffer_len - offset))
574 status = snprintf (buffer + offset, buffer_len - offset,
576 if ((status < 1) || (status >= buffer_len - offset))
580 if (strlen (vl->plugin_instance) > 0)
581 status = snprintf (buffer + offset, buffer_len - offset,
582 "%s-%s/", vl->plugin, vl->plugin_instance);
584 status = snprintf (buffer + offset, buffer_len - offset,
586 if ((status < 1) || (status >= buffer_len - offset))
590 if (strlen (vl->type_instance) > 0)
591 status = snprintf (buffer + offset, buffer_len - offset,
592 "%s-%s.rrd", ds->type, vl->type_instance);
594 status = snprintf (buffer + offset, buffer_len - offset,
596 if ((status < 1) || (status >= buffer_len - offset))
601 } /* int value_list_to_filename */
603 static void *rrd_queue_thread (void *data)
607 rrd_queue_t *queue_entry;
608 rrd_cache_t *cache_entry;
613 /* XXX: If you need to lock both, cache_lock and queue_lock, at
614 * the same time, ALWAYS lock `cache_lock' first! */
616 /* wait until an entry is available */
617 pthread_mutex_lock (&queue_lock);
618 while ((queue_head == NULL) && (do_shutdown == 0))
619 pthread_cond_wait (&queue_cond, &queue_lock);
621 /* We're in the shutdown phase */
622 if (queue_head == NULL)
624 pthread_mutex_unlock (&queue_lock);
628 /* Dequeue the first entry */
629 queue_entry = queue_head;
630 if (queue_head == queue_tail)
631 queue_head = queue_tail = NULL;
633 queue_head = queue_head->next;
635 /* Unlock the queue again */
636 pthread_mutex_unlock (&queue_lock);
638 /* We now need the cache lock so the entry isn't updated while
639 * we make a copy of it's values */
640 pthread_mutex_lock (&cache_lock);
642 avl_get (cache, queue_entry->filename, (void *) &cache_entry);
644 values = cache_entry->values;
645 values_num = cache_entry->values_num;
647 cache_entry->values = NULL;
648 cache_entry->values_num = 0;
649 cache_entry->flags = FLAG_NONE;
651 pthread_mutex_unlock (&cache_lock);
653 /* Write the values to the RRD-file */
654 srrd_update (queue_entry->filename, NULL, values_num, values);
656 for (i = 0; i < values_num; i++)
661 sfree (queue_entry->filename);
665 pthread_mutex_lock (&cache_lock);
668 pthread_mutex_unlock (&cache_lock);
670 pthread_exit ((void *) 0);
672 } /* void *rrd_queue_thread */
674 static int rrd_queue_cache_entry (const char *filename)
676 rrd_queue_t *queue_entry;
678 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
679 if (queue_entry == NULL)
682 queue_entry->filename = strdup (filename);
683 if (queue_entry->filename == NULL)
689 queue_entry->next = NULL;
691 pthread_mutex_lock (&queue_lock);
692 if (queue_tail == NULL)
693 queue_head = queue_entry;
695 queue_tail->next = queue_entry;
696 queue_tail = queue_entry;
697 pthread_cond_signal (&queue_cond);
698 pthread_mutex_unlock (&queue_lock);
700 DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
703 } /* int rrd_queue_cache_entry */
705 static void rrd_cache_flush (int timeout)
714 avl_iterator_t *iter;
717 DEBUG ("rrdtool plugin: Flushing cache, timeout = %i", timeout);
721 /* Build a list of entries to be flushed */
722 iter = avl_get_iterator (cache);
723 while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
725 if (rc->flags == FLAG_QUEUED)
727 else if ((now - rc->first_value) < timeout)
729 else if (rc->values_num > 0)
731 if (rrd_queue_cache_entry (key) == 0)
732 rc->flags = FLAG_QUEUED;
734 else /* ancient and no values -> waste of memory */
736 keys = (char **) realloc ((void *) keys,
737 (keys_num + 1) * sizeof (char *));
741 ERROR ("rrdtool plugin: "
742 "realloc failed: %s",
743 sstrerror (errno, errbuf,
745 avl_iterator_destroy (iter);
748 keys[keys_num] = key;
751 } /* while (avl_iterator_next) */
752 avl_iterator_destroy (iter);
754 for (i = 0; i < keys_num; i++)
756 if (avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
758 DEBUG ("rrdtool plugin: avl_remove (%s) failed.", keys[i]);
762 assert (rc->values == NULL);
763 assert (rc->values_num == 0);
768 } /* for (i = 0..keys_num) */
772 cache_flush_last = now;
773 } /* void rrd_cache_flush */
775 static int rrd_cache_insert (const char *filename,
776 const char *value, time_t value_time)
778 rrd_cache_t *rc = NULL;
782 pthread_mutex_lock (&cache_lock);
784 avl_get (cache, filename, (void *) &rc);
788 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
795 rc->flags = FLAG_NONE;
799 if (rc->last_value >= value_time)
801 pthread_mutex_unlock (&cache_lock);
802 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
803 (unsigned int) rc->last_value,
804 (unsigned int) value_time);
808 values_new = (char **) realloc ((void *) rc->values,
809 (rc->values_num + 1) * sizeof (char *));
810 if (values_new == NULL)
813 void *cache_key = NULL;
815 sstrerror (errno, errbuf, sizeof (errbuf));
817 avl_remove (cache, filename, &cache_key, NULL);
818 pthread_mutex_unlock (&cache_lock);
820 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
827 rc->values = values_new;
829 rc->values[rc->values_num] = strdup (value);
830 if (rc->values[rc->values_num] != NULL)
833 if (rc->values_num == 1)
834 rc->first_value = value_time;
835 rc->last_value = value_time;
837 /* Insert if this is the first value */
840 void *cache_key = strdup (filename);
842 if (cache_key == NULL)
845 sstrerror (errno, errbuf, sizeof (errbuf));
847 pthread_mutex_unlock (&cache_lock);
849 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
851 sfree (rc->values[0]);
857 avl_insert (cache, cache_key, rc);
860 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
861 "values_num = %i; age = %u;",
862 filename, rc->values_num,
863 rc->last_value - rc->first_value);
865 if ((rc->last_value - rc->first_value) >= cache_timeout)
867 /* XXX: If you need to lock both, cache_lock and queue_lock, at
868 * the same time, ALWAYS lock `cache_lock' first! */
869 if (rc->flags != FLAG_QUEUED)
871 if (rrd_queue_cache_entry (filename) == 0)
872 rc->flags = FLAG_QUEUED;
876 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
880 if ((cache_timeout > 0) &&
881 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
882 rrd_cache_flush (cache_flush_timeout);
885 pthread_mutex_unlock (&cache_lock);
888 } /* int rrd_cache_insert */
890 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
897 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
900 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
903 if (stat (filename, &statbuf) == -1)
907 if (rrd_create_file (filename, ds, vl))
913 ERROR ("stat(%s) failed: %s", filename,
914 sstrerror (errno, errbuf,
919 else if (!S_ISREG (statbuf.st_mode))
921 ERROR ("stat(%s): Not a regular file!",
926 status = rrd_cache_insert (filename, values, vl->time);
929 } /* int rrd_write */
931 static int rrd_config (const char *key, const char *value)
933 if (strcasecmp ("CacheTimeout", key) == 0)
935 int tmp = atoi (value);
938 fprintf (stderr, "rrdtool: `CacheTimeout' must "
939 "be greater than 0.\n");
944 else if (strcasecmp ("CacheFlush", key) == 0)
946 int tmp = atoi (value);
949 fprintf (stderr, "rrdtool: `CacheFlush' must "
950 "be greater than 0.\n");
953 cache_flush_timeout = tmp;
955 else if (strcasecmp ("DataDir", key) == 0)
959 datadir = strdup (value);
962 int len = strlen (datadir);
963 while ((len > 0) && (datadir[len - 1] == '/'))
975 else if (strcasecmp ("StepSize", key) == 0)
977 stepsize = atoi (value);
981 else if (strcasecmp ("HeartBeat", key) == 0)
983 heartbeat = atoi (value);
987 else if (strcasecmp ("RRARows", key) == 0)
989 int tmp = atoi (value);
992 fprintf (stderr, "rrdtool: `RRARows' must "
993 "be greater than 0.\n");
998 else if (strcasecmp ("RRATimespan", key) == 0)
1000 char *saveptr = NULL;
1006 value_copy = strdup (value);
1007 if (value_copy == NULL)
1011 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1015 tmp_alloc = realloc (rra_timespans_custom,
1016 sizeof (int) * (rra_timespans_custom_num + 1));
1017 if (tmp_alloc == NULL)
1019 fprintf (stderr, "rrdtool: realloc failed.\n");
1023 rra_timespans_custom = tmp_alloc;
1024 rra_timespans_custom[rra_timespans_custom_num] = atoi (ptr);
1025 if (rra_timespans_custom[rra_timespans_custom_num] != 0)
1026 rra_timespans_custom_num++;
1027 } /* while (strtok_r) */
1030 else if (strcasecmp ("XFF", key) == 0)
1032 double tmp = atof (value);
1033 if ((tmp < 0.0) || (tmp >= 1.0))
1035 fprintf (stderr, "rrdtool: `XFF' must "
1036 "be in the range 0 to 1 (exclusive).");
1046 } /* int rrd_config */
1048 static int rrd_shutdown (void)
1050 pthread_mutex_lock (&cache_lock);
1051 rrd_cache_flush (-1);
1052 pthread_mutex_unlock (&cache_lock);
1054 pthread_mutex_lock (&queue_lock);
1056 pthread_cond_signal (&queue_cond);
1057 pthread_mutex_unlock (&queue_lock);
1059 /* Wait for all the values to be written to disk before returning. */
1060 if (queue_thread != 0)
1062 pthread_join (queue_thread, NULL);
1064 DEBUG ("rrdtool plugin: queue_thread exited.");
1068 } /* int rrd_shutdown */
1070 static int rrd_init (void)
1079 heartbeat = 2 * stepsize;
1084 if ((heartbeat > 0) && (heartbeat < interval_g))
1085 WARNING ("rrdtool plugin: Your `heartbeat' is "
1086 "smaller than your `interval'. This will "
1087 "likely cause problems.");
1088 else if ((stepsize > 0) && (stepsize < interval_g))
1089 WARNING ("rrdtool plugin: Your `stepsize' is "
1090 "smaller than your `interval'. This will "
1091 "create needlessly big RRD-files.");
1093 /* Set the cache up */
1094 pthread_mutex_lock (&cache_lock);
1096 cache = avl_create ((int (*) (const void *, const void *)) strcmp);
1099 ERROR ("rrdtool plugin: avl_create failed.");
1103 cache_flush_last = time (NULL);
1104 if (cache_timeout < 2)
1107 cache_flush_timeout = 0;
1109 else if (cache_flush_timeout < cache_timeout)
1110 cache_flush_timeout = 10 * cache_timeout;
1112 pthread_mutex_unlock (&cache_lock);
1114 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
1117 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1121 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1122 " heartbeat = %i; rrarows = %i; xff = %lf;",
1123 (datadir == NULL) ? "(null)" : datadir,
1124 stepsize, heartbeat, rrarows, xff);
1127 } /* int rrd_init */
1129 void module_register (void)
1131 plugin_register_config ("rrdtool", rrd_config,
1132 config_keys, config_keys_num);
1133 plugin_register_init ("rrdtool", rrd_init);
1134 plugin_register_write ("rrdtool", rrd_write);
1135 plugin_register_shutdown ("rrdtool", rrd_shutdown);