2 * collectd - src/rrdtool.c
3 * Copyright (C) 2006-2008 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"
26 #include "utils_rrdcreate.h"
50 typedef struct rrd_cache_s rrd_cache_t;
57 typedef enum rrd_queue_dir_e rrd_queue_dir_t;
62 struct rrd_queue_s *next;
64 typedef struct rrd_queue_s rrd_queue_t;
69 static const char *config_keys[] =
81 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
83 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
84 * is zero a default, depending on the `interval' member of the value list is
86 static char *datadir = NULL;
87 static double write_rate = 0.0;
88 static rrdcreate_config_t rrdcreate_config =
95 /* timespans = */ NULL,
96 /* timespans_num = */ 0,
98 /* consolidation_functions = */ NULL,
99 /* consolidation_functions_num = */ 0
102 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
103 * ALWAYS lock `cache_lock' first! */
104 static int cache_timeout = 0;
105 static int cache_flush_timeout = 0;
106 static time_t cache_flush_last;
107 static c_avl_tree_t *cache = NULL;
108 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
110 static rrd_queue_t *queue_head = NULL;
111 static rrd_queue_t *queue_tail = NULL;
112 static rrd_queue_t *flushq_head = NULL;
113 static rrd_queue_t *flushq_tail = NULL;
114 static pthread_t queue_thread = 0;
115 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
116 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
118 #if !HAVE_THREADSAFE_LIBRRD
119 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
122 static int do_shutdown = 0;
124 #if HAVE_THREADSAFE_LIBRRD
125 static int srrd_update (char *filename, char *template,
126 int argc, const char **argv)
130 optind = 0; /* bug in librrd? */
133 status = rrd_update_r (filename, template, argc, (void *) argv);
137 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
138 filename, rrd_get_error ());
142 } /* int srrd_update */
143 /* #endif HAVE_THREADSAFE_LIBRRD */
145 #else /* !HAVE_THREADSAFE_LIBRRD */
146 static int srrd_update (char *filename, char *template,
147 int argc, const char **argv)
154 assert (template == NULL);
157 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
158 if (new_argv == NULL)
160 ERROR ("rrdtool plugin: malloc failed.");
164 new_argv[0] = "update";
165 new_argv[1] = filename;
167 memcpy (new_argv + 2, argv, argc * sizeof (char *));
168 new_argv[new_argc] = NULL;
170 pthread_mutex_lock (&librrd_lock);
171 optind = 0; /* bug in librrd? */
174 status = rrd_update (new_argc, new_argv);
175 pthread_mutex_unlock (&librrd_lock);
179 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
180 argv[1], rrd_get_error ());
186 } /* int srrd_update */
187 #endif /* !HAVE_THREADSAFE_LIBRRD */
189 static int value_list_to_string (char *buffer, int buffer_len,
190 const data_set_t *ds, const value_list_t *vl)
196 memset (buffer, '\0', buffer_len);
198 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
199 if ((status < 1) || (status >= buffer_len))
203 for (i = 0; i < ds->ds_num; i++)
205 if ((ds->ds[i].type != DS_TYPE_COUNTER)
206 && (ds->ds[i].type != DS_TYPE_GAUGE))
209 if (ds->ds[i].type == DS_TYPE_COUNTER)
210 status = ssnprintf (buffer + offset, buffer_len - offset,
211 ":%llu", vl->values[i].counter);
213 status = ssnprintf (buffer + offset, buffer_len - offset,
214 ":%lf", vl->values[i].gauge);
216 if ((status < 1) || (status >= (buffer_len - offset)))
220 } /* for ds->ds_num */
223 } /* int value_list_to_string */
225 static int value_list_to_filename (char *buffer, int buffer_len,
226 const data_set_t __attribute__((unused)) *ds, const value_list_t *vl)
233 status = ssnprintf (buffer + offset, buffer_len - offset,
235 if ((status < 1) || (status >= buffer_len - offset))
240 status = ssnprintf (buffer + offset, buffer_len - offset,
242 if ((status < 1) || (status >= buffer_len - offset))
246 if (strlen (vl->plugin_instance) > 0)
247 status = ssnprintf (buffer + offset, buffer_len - offset,
248 "%s-%s/", vl->plugin, vl->plugin_instance);
250 status = ssnprintf (buffer + offset, buffer_len - offset,
252 if ((status < 1) || (status >= buffer_len - offset))
256 if (strlen (vl->type_instance) > 0)
257 status = ssnprintf (buffer + offset, buffer_len - offset,
258 "%s-%s.rrd", vl->type, vl->type_instance);
260 status = ssnprintf (buffer + offset, buffer_len - offset,
262 if ((status < 1) || (status >= buffer_len - offset))
267 } /* int value_list_to_filename */
269 static void *rrd_queue_thread (void __attribute__((unused)) *data)
271 struct timeval tv_next_update;
272 struct timeval tv_now;
274 gettimeofday (&tv_next_update, /* timezone = */ NULL);
278 rrd_queue_t *queue_entry;
279 rrd_cache_t *cache_entry;
288 pthread_mutex_lock (&queue_lock);
289 /* Wait for values to arrive */
292 struct timespec ts_wait;
294 while ((flushq_head == NULL) && (queue_head == NULL)
295 && (do_shutdown == 0))
296 pthread_cond_wait (&queue_cond, &queue_lock);
298 if ((flushq_head == NULL) && (queue_head == NULL))
301 /* Don't delay if there's something to flush */
302 if (flushq_head != NULL)
305 /* Don't delay if we're shutting down */
306 if (do_shutdown != 0)
309 /* Don't delay if no delay was configured. */
310 if (write_rate <= 0.0)
313 gettimeofday (&tv_now, /* timezone = */ NULL);
314 status = timeval_cmp (tv_next_update, tv_now, NULL);
315 /* We're good to go */
319 /* We're supposed to wait a bit with this update, so we'll
320 * wait for the next addition to the queue or to the end of
321 * the wait period - whichever comes first. */
322 ts_wait.tv_sec = tv_next_update.tv_sec;
323 ts_wait.tv_nsec = 1000 * tv_next_update.tv_usec;
325 status = pthread_cond_timedwait (&queue_cond, &queue_lock,
327 if (status == ETIMEDOUT)
331 /* XXX: If you need to lock both, cache_lock and queue_lock, at
332 * the same time, ALWAYS lock `cache_lock' first! */
334 /* We're in the shutdown phase */
335 if ((flushq_head == NULL) && (queue_head == NULL))
337 pthread_mutex_unlock (&queue_lock);
341 if (flushq_head != NULL)
343 /* Dequeue the first flush entry */
344 queue_entry = flushq_head;
345 if (flushq_head == flushq_tail)
346 flushq_head = flushq_tail = NULL;
348 flushq_head = flushq_head->next;
350 else /* if (queue_head != NULL) */
352 /* Dequeue the first regular entry */
353 queue_entry = queue_head;
354 if (queue_head == queue_tail)
355 queue_head = queue_tail = NULL;
357 queue_head = queue_head->next;
360 /* Unlock the queue again */
361 pthread_mutex_unlock (&queue_lock);
363 /* We now need the cache lock so the entry isn't updated while
364 * we make a copy of it's values */
365 pthread_mutex_lock (&cache_lock);
367 status = c_avl_get (cache, queue_entry->filename,
368 (void *) &cache_entry);
372 values = cache_entry->values;
373 values_num = cache_entry->values_num;
375 cache_entry->values = NULL;
376 cache_entry->values_num = 0;
377 cache_entry->flags = FLAG_NONE;
380 pthread_mutex_unlock (&cache_lock);
384 sfree (queue_entry->filename);
389 /* Update `tv_next_update' */
390 if (write_rate > 0.0)
392 gettimeofday (&tv_now, /* timezone = */ NULL);
393 tv_next_update.tv_sec = tv_now.tv_sec;
394 tv_next_update.tv_usec = tv_now.tv_usec
395 + ((suseconds_t) (1000000 * write_rate));
396 while (tv_next_update.tv_usec > 1000000)
398 tv_next_update.tv_sec++;
399 tv_next_update.tv_usec -= 1000000;
403 /* Write the values to the RRD-file */
404 srrd_update (queue_entry->filename, NULL,
405 values_num, (const char **)values);
406 DEBUG ("rrdtool plugin: queue thread: Wrote %i values to %s",
407 values_num, queue_entry->filename);
409 for (i = 0; i < values_num; i++)
414 sfree (queue_entry->filename);
418 pthread_mutex_lock (&cache_lock);
419 c_avl_destroy (cache);
421 pthread_mutex_unlock (&cache_lock);
423 pthread_exit ((void *) 0);
425 } /* void *rrd_queue_thread */
427 static int rrd_queue_enqueue (const char *filename,
428 rrd_queue_t **head, rrd_queue_t **tail)
430 rrd_queue_t *queue_entry;
432 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
433 if (queue_entry == NULL)
436 queue_entry->filename = strdup (filename);
437 if (queue_entry->filename == NULL)
443 queue_entry->next = NULL;
445 pthread_mutex_lock (&queue_lock);
450 (*tail)->next = queue_entry;
453 pthread_cond_signal (&queue_cond);
454 pthread_mutex_unlock (&queue_lock);
457 } /* int rrd_queue_enqueue */
459 static int rrd_queue_dequeue (const char *filename,
460 rrd_queue_t **head, rrd_queue_t **tail)
465 pthread_mutex_lock (&queue_lock);
472 if (strcmp (this->filename, filename) == 0)
481 pthread_mutex_unlock (&queue_lock);
488 prev->next = this->next;
490 if (this->next == NULL)
493 pthread_mutex_unlock (&queue_lock);
495 sfree (this->filename);
499 } /* int rrd_queue_dequeue */
501 static void rrd_cache_flush (int timeout)
510 c_avl_iterator_t *iter;
513 DEBUG ("rrdtool plugin: Flushing cache, timeout = %i", timeout);
517 /* Build a list of entries to be flushed */
518 iter = c_avl_get_iterator (cache);
519 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
521 if (rc->flags != FLAG_NONE)
523 else if ((now - rc->first_value) < timeout)
525 else if (rc->values_num > 0)
529 status = rrd_queue_enqueue (key, &queue_head, &queue_tail);
531 rc->flags = FLAG_QUEUED;
533 else /* ancient and no values -> waste of memory */
535 char **tmp = (char **) realloc ((void *) keys,
536 (keys_num + 1) * sizeof (char *));
540 ERROR ("rrdtool plugin: "
541 "realloc failed: %s",
542 sstrerror (errno, errbuf,
544 c_avl_iterator_destroy (iter);
549 keys[keys_num] = key;
552 } /* while (c_avl_iterator_next) */
553 c_avl_iterator_destroy (iter);
555 for (i = 0; i < keys_num; i++)
557 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
559 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
563 assert (rc->values == NULL);
564 assert (rc->values_num == 0);
569 } /* for (i = 0..keys_num) */
573 cache_flush_last = now;
574 } /* void rrd_cache_flush */
576 static int rrd_cache_flush_identifier (int timeout, const char *identifier)
583 if (identifier == NULL)
585 rrd_cache_flush (timeout);
592 snprintf (key, sizeof (key), "%s.rrd",
595 snprintf (key, sizeof (key), "%s/%s.rrd",
596 datadir, identifier);
597 key[sizeof (key) - 1] = 0;
599 status = c_avl_get (cache, key, (void *) &rc);
602 WARNING ("rrdtool plugin: rrd_cache_flush_identifier: "
603 "c_avl_get (%s) failed. Does that file really exist?",
608 if (rc->flags == FLAG_FLUSHQ)
612 else if (rc->flags == FLAG_QUEUED)
614 rrd_queue_dequeue (key, &queue_head, &queue_tail);
615 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
617 rc->flags = FLAG_FLUSHQ;
619 else if ((now - rc->first_value) < timeout)
623 else if (rc->values_num > 0)
625 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
627 rc->flags = FLAG_FLUSHQ;
631 } /* int rrd_cache_flush_identifier */
633 static int rrd_cache_insert (const char *filename,
634 const char *value, time_t value_time)
636 rrd_cache_t *rc = NULL;
640 pthread_mutex_lock (&cache_lock);
642 /* This shouldn't happen, but it did happen at least once, so we'll be
646 pthread_mutex_unlock (&cache_lock);
647 WARNING ("rrdtool plugin: cache == NULL.");
651 c_avl_get (cache, filename, (void *) &rc);
655 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
662 rc->flags = FLAG_NONE;
666 if (rc->last_value >= value_time)
668 pthread_mutex_unlock (&cache_lock);
669 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
670 (unsigned int) rc->last_value,
671 (unsigned int) value_time);
675 values_new = (char **) realloc ((void *) rc->values,
676 (rc->values_num + 1) * sizeof (char *));
677 if (values_new == NULL)
680 void *cache_key = NULL;
682 sstrerror (errno, errbuf, sizeof (errbuf));
684 c_avl_remove (cache, filename, &cache_key, NULL);
685 pthread_mutex_unlock (&cache_lock);
687 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
694 rc->values = values_new;
696 rc->values[rc->values_num] = strdup (value);
697 if (rc->values[rc->values_num] != NULL)
700 if (rc->values_num == 1)
701 rc->first_value = value_time;
702 rc->last_value = value_time;
704 /* Insert if this is the first value */
707 void *cache_key = strdup (filename);
709 if (cache_key == NULL)
712 sstrerror (errno, errbuf, sizeof (errbuf));
714 pthread_mutex_unlock (&cache_lock);
716 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
718 sfree (rc->values[0]);
724 c_avl_insert (cache, cache_key, rc);
727 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
728 "values_num = %i; age = %lu;",
729 filename, rc->values_num,
730 (unsigned long)(rc->last_value - rc->first_value));
732 if ((rc->last_value - rc->first_value) >= cache_timeout)
734 /* XXX: If you need to lock both, cache_lock and queue_lock, at
735 * the same time, ALWAYS lock `cache_lock' first! */
736 if (rc->flags == FLAG_NONE)
740 status = rrd_queue_enqueue (filename, &queue_head, &queue_tail);
742 rc->flags = FLAG_QUEUED;
746 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
750 if ((cache_timeout > 0) &&
751 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
752 rrd_cache_flush (cache_flush_timeout);
754 pthread_mutex_unlock (&cache_lock);
757 } /* int rrd_cache_insert */
759 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
761 int a = *((int *) a_ptr);
762 int b = *((int *) b_ptr);
770 } /* int rrd_compare_numeric */
772 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
779 if (0 != strcmp (ds->type, vl->type)) {
780 ERROR ("rrdtool plugin: DS type does not match value list type");
784 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
787 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
790 if (stat (filename, &statbuf) == -1)
794 status = cu_rrd_create_file (filename,
795 ds, vl, &rrdcreate_config);
802 ERROR ("stat(%s) failed: %s", filename,
803 sstrerror (errno, errbuf,
808 else if (!S_ISREG (statbuf.st_mode))
810 ERROR ("stat(%s): Not a regular file!",
815 status = rrd_cache_insert (filename, values, vl->time);
818 } /* int rrd_write */
820 static int rrd_flush (int timeout, const char *identifier)
822 pthread_mutex_lock (&cache_lock);
825 pthread_mutex_unlock (&cache_lock);
829 rrd_cache_flush_identifier (timeout, identifier);
831 pthread_mutex_unlock (&cache_lock);
833 } /* int rrd_flush */
835 static int rrd_config (const char *key, const char *value)
837 if (strcasecmp ("CacheTimeout", key) == 0)
839 int tmp = atoi (value);
842 fprintf (stderr, "rrdtool: `CacheTimeout' must "
843 "be greater than 0.\n");
844 ERROR ("rrdtool: `CacheTimeout' must "
845 "be greater than 0.\n");
850 else if (strcasecmp ("CacheFlush", key) == 0)
852 int tmp = atoi (value);
855 fprintf (stderr, "rrdtool: `CacheFlush' must "
856 "be greater than 0.\n");
857 ERROR ("rrdtool: `CacheFlush' must "
858 "be greater than 0.\n");
861 cache_flush_timeout = tmp;
863 else if (strcasecmp ("DataDir", key) == 0)
867 datadir = strdup (value);
870 int len = strlen (datadir);
871 while ((len > 0) && (datadir[len - 1] == '/'))
883 else if (strcasecmp ("StepSize", key) == 0)
885 int temp = atoi (value);
887 rrdcreate_config.stepsize = temp;
889 else if (strcasecmp ("HeartBeat", key) == 0)
891 int temp = atoi (value);
893 rrdcreate_config.heartbeat = temp;
895 else if (strcasecmp ("RRARows", key) == 0)
897 int tmp = atoi (value);
900 fprintf (stderr, "rrdtool: `RRARows' must "
901 "be greater than 0.\n");
902 ERROR ("rrdtool: `RRARows' must "
903 "be greater than 0.\n");
906 rrdcreate_config.rrarows = tmp;
908 else if (strcasecmp ("RRATimespan", key) == 0)
910 char *saveptr = NULL;
916 value_copy = strdup (value);
917 if (value_copy == NULL)
921 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
925 tmp_alloc = realloc (rrdcreate_config.timespans,
926 sizeof (int) * (rrdcreate_config.timespans_num + 1));
927 if (tmp_alloc == NULL)
929 fprintf (stderr, "rrdtool: realloc failed.\n");
930 ERROR ("rrdtool: realloc failed.\n");
934 rrdcreate_config.timespans = tmp_alloc;
935 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = atoi (ptr);
936 if (rrdcreate_config.timespans[rrdcreate_config.timespans_num] != 0)
937 rrdcreate_config.timespans_num++;
938 } /* while (strtok_r) */
940 qsort (/* base = */ rrdcreate_config.timespans,
941 /* nmemb = */ rrdcreate_config.timespans_num,
942 /* size = */ sizeof (rrdcreate_config.timespans[0]),
943 /* compar = */ rrd_compare_numeric);
947 else if (strcasecmp ("XFF", key) == 0)
949 double tmp = atof (value);
950 if ((tmp < 0.0) || (tmp >= 1.0))
952 fprintf (stderr, "rrdtool: `XFF' must "
953 "be in the range 0 to 1 (exclusive).");
954 ERROR ("rrdtool: `XFF' must "
955 "be in the range 0 to 1 (exclusive).");
958 rrdcreate_config.xff = tmp;
960 else if (strcasecmp ("WritesPerSecond", key) == 0)
962 double wps = atof (value);
966 fprintf (stderr, "rrdtool: `WritesPerSecond' must be "
967 "greater than or equal to zero.");
976 write_rate = 1.0 / wps;
984 } /* int rrd_config */
986 static int rrd_shutdown (void)
988 pthread_mutex_lock (&cache_lock);
989 rrd_cache_flush (-1);
990 pthread_mutex_unlock (&cache_lock);
992 pthread_mutex_lock (&queue_lock);
994 pthread_cond_signal (&queue_cond);
995 pthread_mutex_unlock (&queue_lock);
997 /* Wait for all the values to be written to disk before returning. */
998 if (queue_thread != 0)
1000 pthread_join (queue_thread, NULL);
1002 DEBUG ("rrdtool plugin: queue_thread exited.");
1006 } /* int rrd_shutdown */
1008 static int rrd_init (void)
1010 static int init_once = 0;
1017 if (rrdcreate_config.stepsize < 0)
1018 rrdcreate_config.stepsize = 0;
1019 if (rrdcreate_config.heartbeat <= 0)
1020 rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize;
1022 if ((rrdcreate_config.heartbeat > 0)
1023 && (rrdcreate_config.heartbeat < interval_g))
1024 WARNING ("rrdtool plugin: Your `heartbeat' is "
1025 "smaller than your `interval'. This will "
1026 "likely cause problems.");
1027 else if ((rrdcreate_config.stepsize > 0)
1028 && (rrdcreate_config.stepsize < interval_g))
1029 WARNING ("rrdtool plugin: Your `stepsize' is "
1030 "smaller than your `interval'. This will "
1031 "create needlessly big RRD-files.");
1033 /* Set the cache up */
1034 pthread_mutex_lock (&cache_lock);
1036 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1039 ERROR ("rrdtool plugin: c_avl_create failed.");
1043 cache_flush_last = time (NULL);
1044 if (cache_timeout < 2)
1047 cache_flush_timeout = 0;
1049 else if (cache_flush_timeout < cache_timeout)
1050 cache_flush_timeout = 10 * cache_timeout;
1052 pthread_mutex_unlock (&cache_lock);
1054 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
1057 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1061 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1062 " heartbeat = %i; rrarows = %i; xff = %lf;",
1063 (datadir == NULL) ? "(null)" : datadir,
1064 rrdcreate_config.stepsize,
1065 rrdcreate_config.heartbeat,
1066 rrdcreate_config.rrarows,
1067 rrdcreate_config.xff);
1070 } /* int rrd_init */
1072 void module_register (void)
1074 plugin_register_config ("rrdtool", rrd_config,
1075 config_keys, config_keys_num);
1076 plugin_register_init ("rrdtool", rrd_init);
1077 plugin_register_write ("rrdtool", rrd_write);
1078 plugin_register_flush ("rrdtool", rrd_flush);
1079 plugin_register_shutdown ("rrdtool", rrd_shutdown);