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"
48 typedef struct rrd_cache_s rrd_cache_t;
53 struct rrd_queue_s *next;
55 typedef struct rrd_queue_s rrd_queue_t;
60 static int rra_timespans[] =
68 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
70 static int *rra_timespans_custom = NULL;
71 static int rra_timespans_custom_num = 0;
73 static char *rra_types[] =
79 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
81 static const char *config_keys[] =
92 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
94 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
95 * is zero a default, depending on the `interval' member of the value list is
97 static char *datadir = NULL;
98 static int stepsize = 0;
99 static int heartbeat = 0;
100 static int rrarows = 1200;
101 static double xff = 0.1;
103 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
104 * ALWAYS lock `cache_lock' first! */
105 static int cache_timeout = 0;
106 static int cache_flush_timeout = 0;
107 static time_t cache_flush_last;
108 static c_avl_tree_t *cache = NULL;
109 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
111 static rrd_queue_t *queue_head = NULL;
112 static rrd_queue_t *queue_tail = NULL;
113 static pthread_t queue_thread = 0;
114 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
115 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
117 #if !HAVE_THREADSAFE_LIBRRD
118 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
121 static int do_shutdown = 0;
127 static void rra_free (int rra_num, char **rra_def)
131 for (i = 0; i < rra_num; i++)
136 } /* void rra_free */
138 static int rra_get (char ***ret, const value_list_t *vl)
156 /* The stepsize we use here: If it is user-set, use it. If not, use the
157 * interval of the value-list. */
166 ss = (stepsize > 0) ? stepsize : vl->interval;
173 /* Use the configured timespans or fall back to the built-in defaults */
174 if (rra_timespans_custom_num != 0)
176 rts = rra_timespans_custom;
177 rts_num = rra_timespans_custom_num;
182 rts_num = rra_timespans_num;
185 rra_max = rts_num * rra_types_num;
187 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
189 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
193 for (i = 0; i < rts_num; i++)
197 if ((span / ss) < rrarows)
203 cdp_len = (int) floor (((double) span)
204 / ((double) (rrarows * ss)));
206 cdp_num = (int) ceil (((double) span)
207 / ((double) (cdp_len * ss)));
209 for (j = 0; j < rra_types_num; j++)
211 if (rra_num >= rra_max)
214 if (snprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
216 cdp_len, cdp_num) >= sizeof (buffer))
218 ERROR ("rra_get: Buffer would have been truncated.");
222 rra_def[rra_num++] = sstrdup (buffer);
227 DEBUG ("rra_num = %i", rra_num);
228 for (i = 0; i < rra_num; i++)
229 DEBUG (" %s", rra_def[i]);
236 static void ds_free (int ds_num, char **ds_def)
240 for (i = 0; i < ds_num; i++)
241 if (ds_def[i] != NULL)
246 static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
255 DEBUG ("ds->ds_num = %i", ds->ds_num);
257 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
261 ERROR ("rrdtool plugin: malloc failed: %s",
262 sstrerror (errno, errbuf, sizeof (errbuf)));
265 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
267 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
269 data_source_t *d = ds->ds + ds_num;
273 ds_def[ds_num] = NULL;
275 if (d->type == DS_TYPE_COUNTER)
277 else if (d->type == DS_TYPE_GAUGE)
281 ERROR ("rrdtool plugin: Unknown DS type: %i",
292 snprintf (min, sizeof (min), "%lf", d->min);
293 min[sizeof (min) - 1] = '\0';
302 snprintf (max, sizeof (max), "%lf", d->max);
303 max[sizeof (max) - 1] = '\0';
306 status = snprintf (buffer, sizeof (buffer),
309 (heartbeat > 0) ? heartbeat : (2 * vl->interval),
311 if ((status < 1) || (status >= sizeof (buffer)))
314 ds_def[ds_num] = sstrdup (buffer);
315 } /* for ds_num = 0 .. ds->ds_num */
320 DEBUG ("ds_num = %i", ds_num);
321 for (i = 0; i < ds_num; i++)
322 DEBUG (" %s", ds_def[i]);
326 if (ds_num != ds->ds_num)
328 ds_free (ds_num, ds_def);
336 #if HAVE_THREADSAFE_LIBRRD
337 static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
338 int argc, char **argv)
342 optind = 0; /* bug in librrd? */
345 status = rrd_create_r (filename, pdp_step, last_up, argc, argv);
349 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
350 filename, rrd_get_error ());
354 } /* int srrd_create */
356 static int srrd_update (char *filename, char *template, int argc, char **argv)
360 optind = 0; /* bug in librrd? */
363 status = rrd_update_r (filename, template, argc, argv);
367 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
368 filename, rrd_get_error ());
372 } /* int srrd_update */
373 /* #endif HAVE_THREADSAFE_LIBRRD */
375 #else /* !HAVE_THREADSAFE_LIBRRD */
376 static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
377 int argc, char **argv)
384 char pdp_step_str[16];
385 char last_up_str[16];
388 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
389 if (new_argv == NULL)
391 ERROR ("rrdtool plugin: malloc failed.");
396 last_up = time (NULL) - 10;
398 snprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
399 pdp_step_str[sizeof (pdp_step_str) - 1] = '\0';
400 snprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);
401 last_up_str[sizeof (last_up_str) - 1] = '\0';
403 new_argv[0] = "create";
404 new_argv[1] = filename;
406 new_argv[3] = pdp_step_str;
408 new_argv[5] = last_up_str;
410 memcpy (new_argv + 6, argv, argc * sizeof (char *));
411 new_argv[new_argc] = NULL;
413 pthread_mutex_lock (&librrd_lock);
414 optind = 0; /* bug in librrd? */
417 status = rrd_create (new_argc, new_argv);
418 pthread_mutex_unlock (&librrd_lock);
422 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
423 filename, rrd_get_error ());
429 } /* int srrd_create */
431 static int srrd_update (char *filename, char *template, int argc, char **argv)
438 assert (template == NULL);
441 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
442 if (new_argv == NULL)
444 ERROR ("rrdtool plugin: malloc failed.");
448 new_argv[0] = "update";
449 new_argv[1] = filename;
451 memcpy (new_argv + 2, argv, argc * sizeof (char *));
452 new_argv[new_argc] = NULL;
454 pthread_mutex_lock (&librrd_lock);
455 optind = 0; /* bug in librrd? */
458 status = rrd_update (new_argc, new_argv);
459 pthread_mutex_unlock (&librrd_lock);
463 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
464 argv[1], rrd_get_error ());
470 } /* int srrd_update */
471 #endif /* !HAVE_THREADSAFE_LIBRRD */
473 static int rrd_create_file (char *filename, const data_set_t *ds, const value_list_t *vl)
483 if (check_create_dir (filename))
486 if ((rra_num = rra_get (&rra_def, vl)) < 1)
488 ERROR ("rrd_create_file failed: Could not calculate RRAs");
492 if ((ds_num = ds_get (&ds_def, ds, vl)) < 1)
494 ERROR ("rrd_create_file failed: Could not calculate DSes");
498 argc = ds_num + rra_num;
500 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
503 ERROR ("rrd_create failed: %s",
504 sstrerror (errno, errbuf, sizeof (errbuf)));
508 memcpy (argv, ds_def, ds_num * sizeof (char *));
509 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
510 argv[ds_num + rra_num] = NULL;
512 assert (vl->time > 10);
513 status = srrd_create (filename,
514 (stepsize > 0) ? stepsize : vl->interval,
519 ds_free (ds_num, ds_def);
520 rra_free (rra_num, rra_def);
525 static int value_list_to_string (char *buffer, int buffer_len,
526 const data_set_t *ds, const value_list_t *vl)
532 memset (buffer, '\0', buffer_len);
534 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
535 if ((status < 1) || (status >= buffer_len))
539 for (i = 0; i < ds->ds_num; i++)
541 if ((ds->ds[i].type != DS_TYPE_COUNTER)
542 && (ds->ds[i].type != DS_TYPE_GAUGE))
545 if (ds->ds[i].type == DS_TYPE_COUNTER)
546 status = snprintf (buffer + offset, buffer_len - offset,
547 ":%llu", vl->values[i].counter);
549 status = snprintf (buffer + offset, buffer_len - offset,
550 ":%lf", vl->values[i].gauge);
552 if ((status < 1) || (status >= (buffer_len - offset)))
556 } /* for ds->ds_num */
559 } /* int value_list_to_string */
561 static int value_list_to_filename (char *buffer, int buffer_len,
562 const data_set_t *ds, const value_list_t *vl)
569 status = snprintf (buffer + offset, buffer_len - offset,
571 if ((status < 1) || (status >= buffer_len - offset))
576 status = snprintf (buffer + offset, buffer_len - offset,
578 if ((status < 1) || (status >= buffer_len - offset))
582 if (strlen (vl->plugin_instance) > 0)
583 status = snprintf (buffer + offset, buffer_len - offset,
584 "%s-%s/", vl->plugin, vl->plugin_instance);
586 status = snprintf (buffer + offset, buffer_len - offset,
588 if ((status < 1) || (status >= buffer_len - offset))
592 if (strlen (vl->type_instance) > 0)
593 status = snprintf (buffer + offset, buffer_len - offset,
594 "%s-%s.rrd", ds->type, vl->type_instance);
596 status = snprintf (buffer + offset, buffer_len - offset,
598 if ((status < 1) || (status >= buffer_len - offset))
603 } /* int value_list_to_filename */
605 static void *rrd_queue_thread (void *data)
609 rrd_queue_t *queue_entry;
610 rrd_cache_t *cache_entry;
615 /* XXX: If you need to lock both, cache_lock and queue_lock, at
616 * the same time, ALWAYS lock `cache_lock' first! */
618 /* wait until an entry is available */
619 pthread_mutex_lock (&queue_lock);
620 while ((queue_head == NULL) && (do_shutdown == 0))
621 pthread_cond_wait (&queue_cond, &queue_lock);
623 /* We're in the shutdown phase */
624 if (queue_head == NULL)
626 pthread_mutex_unlock (&queue_lock);
630 /* Dequeue the first entry */
631 queue_entry = queue_head;
632 if (queue_head == queue_tail)
633 queue_head = queue_tail = NULL;
635 queue_head = queue_head->next;
637 /* Unlock the queue again */
638 pthread_mutex_unlock (&queue_lock);
640 /* We now need the cache lock so the entry isn't updated while
641 * we make a copy of it's values */
642 pthread_mutex_lock (&cache_lock);
644 c_avl_get (cache, queue_entry->filename, (void *) &cache_entry);
646 values = cache_entry->values;
647 values_num = cache_entry->values_num;
649 cache_entry->values = NULL;
650 cache_entry->values_num = 0;
651 cache_entry->flags = FLAG_NONE;
653 pthread_mutex_unlock (&cache_lock);
655 /* Write the values to the RRD-file */
656 srrd_update (queue_entry->filename, NULL, values_num, values);
657 DEBUG ("rrdtool plugin: queue thread: Wrote %i values to %s",
658 values_num, queue_entry->filename);
660 for (i = 0; i < values_num; i++)
665 sfree (queue_entry->filename);
669 pthread_mutex_lock (&cache_lock);
670 c_avl_destroy (cache);
672 pthread_mutex_unlock (&cache_lock);
674 pthread_exit ((void *) 0);
676 } /* void *rrd_queue_thread */
678 static int rrd_queue_cache_entry (const char *filename)
680 rrd_queue_t *queue_entry;
682 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
683 if (queue_entry == NULL)
686 queue_entry->filename = strdup (filename);
687 if (queue_entry->filename == NULL)
693 queue_entry->next = NULL;
695 pthread_mutex_lock (&queue_lock);
696 if (queue_tail == NULL)
697 queue_head = queue_entry;
699 queue_tail->next = queue_entry;
700 queue_tail = queue_entry;
701 pthread_cond_signal (&queue_cond);
702 pthread_mutex_unlock (&queue_lock);
704 DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
707 } /* int rrd_queue_cache_entry */
709 static void rrd_cache_flush (int timeout)
718 c_avl_iterator_t *iter;
721 DEBUG ("rrdtool plugin: Flushing cache, timeout = %i", timeout);
725 /* Build a list of entries to be flushed */
726 iter = c_avl_get_iterator (cache);
727 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
729 if (rc->flags == FLAG_QUEUED)
731 else if ((now - rc->first_value) < timeout)
733 else if (rc->values_num > 0)
735 if (rrd_queue_cache_entry (key) == 0)
736 rc->flags = FLAG_QUEUED;
738 else /* ancient and no values -> waste of memory */
740 char **tmp = (char **) realloc ((void *) keys,
741 (keys_num + 1) * sizeof (char *));
745 ERROR ("rrdtool plugin: "
746 "realloc failed: %s",
747 sstrerror (errno, errbuf,
749 c_avl_iterator_destroy (iter);
754 keys[keys_num] = key;
757 } /* while (c_avl_iterator_next) */
758 c_avl_iterator_destroy (iter);
760 for (i = 0; i < keys_num; i++)
762 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
764 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
768 assert (rc->values == NULL);
769 assert (rc->values_num == 0);
774 } /* for (i = 0..keys_num) */
778 cache_flush_last = now;
779 } /* void rrd_cache_flush */
781 static int rrd_cache_insert (const char *filename,
782 const char *value, time_t value_time)
784 rrd_cache_t *rc = NULL;
788 pthread_mutex_lock (&cache_lock);
790 c_avl_get (cache, filename, (void *) &rc);
794 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
801 rc->flags = FLAG_NONE;
805 if (rc->last_value >= value_time)
807 pthread_mutex_unlock (&cache_lock);
808 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
809 (unsigned int) rc->last_value,
810 (unsigned int) value_time);
814 values_new = (char **) realloc ((void *) rc->values,
815 (rc->values_num + 1) * sizeof (char *));
816 if (values_new == NULL)
819 void *cache_key = NULL;
821 sstrerror (errno, errbuf, sizeof (errbuf));
823 c_avl_remove (cache, filename, &cache_key, NULL);
824 pthread_mutex_unlock (&cache_lock);
826 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
833 rc->values = values_new;
835 rc->values[rc->values_num] = strdup (value);
836 if (rc->values[rc->values_num] != NULL)
839 if (rc->values_num == 1)
840 rc->first_value = value_time;
841 rc->last_value = value_time;
843 /* Insert if this is the first value */
846 void *cache_key = strdup (filename);
848 if (cache_key == NULL)
851 sstrerror (errno, errbuf, sizeof (errbuf));
853 pthread_mutex_unlock (&cache_lock);
855 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
857 sfree (rc->values[0]);
863 c_avl_insert (cache, cache_key, rc);
866 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
867 "values_num = %i; age = %u;",
868 filename, rc->values_num,
869 rc->last_value - rc->first_value);
871 if ((rc->last_value - rc->first_value) >= cache_timeout)
873 /* XXX: If you need to lock both, cache_lock and queue_lock, at
874 * the same time, ALWAYS lock `cache_lock' first! */
875 if (rc->flags != FLAG_QUEUED)
877 if (rrd_queue_cache_entry (filename) == 0)
878 rc->flags = FLAG_QUEUED;
882 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
886 if ((cache_timeout > 0) &&
887 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
888 rrd_cache_flush (cache_flush_timeout);
891 pthread_mutex_unlock (&cache_lock);
894 } /* int rrd_cache_insert */
896 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
903 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
906 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
909 if (stat (filename, &statbuf) == -1)
913 if (rrd_create_file (filename, ds, vl))
919 ERROR ("stat(%s) failed: %s", filename,
920 sstrerror (errno, errbuf,
925 else if (!S_ISREG (statbuf.st_mode))
927 ERROR ("stat(%s): Not a regular file!",
932 status = rrd_cache_insert (filename, values, vl->time);
935 } /* int rrd_write */
937 static int rrd_config (const char *key, const char *value)
939 if (strcasecmp ("CacheTimeout", key) == 0)
941 int tmp = atoi (value);
944 fprintf (stderr, "rrdtool: `CacheTimeout' must "
945 "be greater than 0.\n");
950 else if (strcasecmp ("CacheFlush", key) == 0)
952 int tmp = atoi (value);
955 fprintf (stderr, "rrdtool: `CacheFlush' must "
956 "be greater than 0.\n");
959 cache_flush_timeout = tmp;
961 else if (strcasecmp ("DataDir", key) == 0)
965 datadir = strdup (value);
968 int len = strlen (datadir);
969 while ((len > 0) && (datadir[len - 1] == '/'))
981 else if (strcasecmp ("StepSize", key) == 0)
983 stepsize = atoi (value);
987 else if (strcasecmp ("HeartBeat", key) == 0)
989 heartbeat = atoi (value);
993 else if (strcasecmp ("RRARows", key) == 0)
995 int tmp = atoi (value);
998 fprintf (stderr, "rrdtool: `RRARows' must "
999 "be greater than 0.\n");
1004 else if (strcasecmp ("RRATimespan", key) == 0)
1006 char *saveptr = NULL;
1012 value_copy = strdup (value);
1013 if (value_copy == NULL)
1017 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1021 tmp_alloc = realloc (rra_timespans_custom,
1022 sizeof (int) * (rra_timespans_custom_num + 1));
1023 if (tmp_alloc == NULL)
1025 fprintf (stderr, "rrdtool: realloc failed.\n");
1029 rra_timespans_custom = tmp_alloc;
1030 rra_timespans_custom[rra_timespans_custom_num] = atoi (ptr);
1031 if (rra_timespans_custom[rra_timespans_custom_num] != 0)
1032 rra_timespans_custom_num++;
1033 } /* while (strtok_r) */
1036 else if (strcasecmp ("XFF", key) == 0)
1038 double tmp = atof (value);
1039 if ((tmp < 0.0) || (tmp >= 1.0))
1041 fprintf (stderr, "rrdtool: `XFF' must "
1042 "be in the range 0 to 1 (exclusive).");
1052 } /* int rrd_config */
1054 static int rrd_shutdown (void)
1056 pthread_mutex_lock (&cache_lock);
1057 rrd_cache_flush (-1);
1058 pthread_mutex_unlock (&cache_lock);
1060 pthread_mutex_lock (&queue_lock);
1062 pthread_cond_signal (&queue_cond);
1063 pthread_mutex_unlock (&queue_lock);
1065 /* Wait for all the values to be written to disk before returning. */
1066 if (queue_thread != 0)
1068 pthread_join (queue_thread, NULL);
1070 DEBUG ("rrdtool plugin: queue_thread exited.");
1074 } /* int rrd_shutdown */
1076 static int rrd_init (void)
1085 heartbeat = 2 * stepsize;
1090 if ((heartbeat > 0) && (heartbeat < interval_g))
1091 WARNING ("rrdtool plugin: Your `heartbeat' is "
1092 "smaller than your `interval'. This will "
1093 "likely cause problems.");
1094 else if ((stepsize > 0) && (stepsize < interval_g))
1095 WARNING ("rrdtool plugin: Your `stepsize' is "
1096 "smaller than your `interval'. This will "
1097 "create needlessly big RRD-files.");
1099 /* Set the cache up */
1100 pthread_mutex_lock (&cache_lock);
1102 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1105 ERROR ("rrdtool plugin: c_avl_create failed.");
1109 cache_flush_last = time (NULL);
1110 if (cache_timeout < 2)
1113 cache_flush_timeout = 0;
1115 else if (cache_flush_timeout < cache_timeout)
1116 cache_flush_timeout = 10 * cache_timeout;
1118 pthread_mutex_unlock (&cache_lock);
1120 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
1123 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1127 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1128 " heartbeat = %i; rrarows = %i; xff = %lf;",
1129 (datadir == NULL) ? "(null)" : datadir,
1130 stepsize, heartbeat, rrarows, xff);
1133 } /* int rrd_init */
1135 void module_register (void)
1137 plugin_register_config ("rrdtool", rrd_config,
1138 config_keys, config_keys_num);
1139 plugin_register_init ("rrdtool", rrd_init);
1140 plugin_register_write ("rrdtool", rrd_write);
1141 plugin_register_shutdown ("rrdtool", rrd_shutdown);