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;
285 pthread_mutex_lock (&queue_lock);
286 /* Wait for values to arrive */
289 struct timespec ts_wait;
291 while ((flushq_head == NULL) && (queue_head == NULL)
292 && (do_shutdown == 0))
293 pthread_cond_wait (&queue_cond, &queue_lock);
295 if ((flushq_head == NULL) && (queue_head == NULL))
298 /* Don't delay if there's something to flush */
299 if (flushq_head != NULL)
302 /* Don't delay if we're shutting down */
303 if (do_shutdown != 0)
306 /* Don't delay if no delay was configured. */
307 if (write_rate <= 0.0)
310 gettimeofday (&tv_now, /* timezone = */ NULL);
311 status = timeval_cmp (tv_next_update, tv_now, NULL);
312 /* We're good to go */
316 /* We're supposed to wait a bit with this update, so we'll
317 * wait for the next addition to the queue or to the end of
318 * the wait period - whichever comes first. */
319 ts_wait.tv_sec = tv_next_update.tv_sec;
320 ts_wait.tv_nsec = 1000 * tv_next_update.tv_usec;
322 status = pthread_cond_timedwait (&queue_cond, &queue_lock,
324 if (status == ETIMEDOUT)
328 /* XXX: If you need to lock both, cache_lock and queue_lock, at
329 * the same time, ALWAYS lock `cache_lock' first! */
331 /* We're in the shutdown phase */
332 if ((flushq_head == NULL) && (queue_head == NULL))
334 pthread_mutex_unlock (&queue_lock);
338 if (flushq_head != NULL)
340 /* Dequeue the first flush entry */
341 queue_entry = flushq_head;
342 if (flushq_head == flushq_tail)
343 flushq_head = flushq_tail = NULL;
345 flushq_head = flushq_head->next;
347 else /* if (queue_head != NULL) */
349 /* Dequeue the first regular entry */
350 queue_entry = queue_head;
351 if (queue_head == queue_tail)
352 queue_head = queue_tail = NULL;
354 queue_head = queue_head->next;
357 /* Unlock the queue again */
358 pthread_mutex_unlock (&queue_lock);
360 /* We now need the cache lock so the entry isn't updated while
361 * we make a copy of it's values */
362 pthread_mutex_lock (&cache_lock);
364 status = c_avl_get (cache, queue_entry->filename,
365 (void *) &cache_entry);
369 values = cache_entry->values;
370 values_num = cache_entry->values_num;
372 cache_entry->values = NULL;
373 cache_entry->values_num = 0;
374 cache_entry->flags = FLAG_NONE;
377 pthread_mutex_unlock (&cache_lock);
381 sfree (queue_entry->filename);
386 /* Update `tv_next_update' */
387 if (write_rate > 0.0)
389 gettimeofday (&tv_now, /* timezone = */ NULL);
390 tv_next_update.tv_sec = tv_now.tv_sec;
391 tv_next_update.tv_usec = tv_now.tv_usec
392 + ((suseconds_t) (1000000 * write_rate));
393 while (tv_next_update.tv_usec > 1000000)
395 tv_next_update.tv_sec++;
396 tv_next_update.tv_usec -= 1000000;
400 /* Write the values to the RRD-file */
401 srrd_update (queue_entry->filename, NULL,
402 values_num, (const char **)values);
403 DEBUG ("rrdtool plugin: queue thread: Wrote %i values to %s",
404 values_num, queue_entry->filename);
406 for (i = 0; i < values_num; i++)
411 sfree (queue_entry->filename);
415 pthread_mutex_lock (&cache_lock);
416 c_avl_destroy (cache);
418 pthread_mutex_unlock (&cache_lock);
420 pthread_exit ((void *) 0);
422 } /* void *rrd_queue_thread */
424 static int rrd_queue_enqueue (const char *filename,
425 rrd_queue_t **head, rrd_queue_t **tail)
427 rrd_queue_t *queue_entry;
429 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
430 if (queue_entry == NULL)
433 queue_entry->filename = strdup (filename);
434 if (queue_entry->filename == NULL)
440 queue_entry->next = NULL;
442 pthread_mutex_lock (&queue_lock);
447 (*tail)->next = queue_entry;
450 pthread_cond_signal (&queue_cond);
451 pthread_mutex_unlock (&queue_lock);
454 } /* int rrd_queue_enqueue */
456 static int rrd_queue_dequeue (const char *filename,
457 rrd_queue_t **head, rrd_queue_t **tail)
462 pthread_mutex_lock (&queue_lock);
469 if (strcmp (this->filename, filename) == 0)
478 pthread_mutex_unlock (&queue_lock);
485 prev->next = this->next;
487 if (this->next == NULL)
490 pthread_mutex_unlock (&queue_lock);
492 sfree (this->filename);
496 } /* int rrd_queue_dequeue */
498 static void rrd_cache_flush (int timeout)
507 c_avl_iterator_t *iter;
510 DEBUG ("rrdtool plugin: Flushing cache, timeout = %i", timeout);
514 /* Build a list of entries to be flushed */
515 iter = c_avl_get_iterator (cache);
516 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
518 if (rc->flags != FLAG_NONE)
520 else if ((now - rc->first_value) < timeout)
522 else if (rc->values_num > 0)
526 status = rrd_queue_enqueue (key, &queue_head, &queue_tail);
528 rc->flags = FLAG_QUEUED;
530 else /* ancient and no values -> waste of memory */
532 char **tmp = (char **) realloc ((void *) keys,
533 (keys_num + 1) * sizeof (char *));
537 ERROR ("rrdtool plugin: "
538 "realloc failed: %s",
539 sstrerror (errno, errbuf,
541 c_avl_iterator_destroy (iter);
546 keys[keys_num] = key;
549 } /* while (c_avl_iterator_next) */
550 c_avl_iterator_destroy (iter);
552 for (i = 0; i < keys_num; i++)
554 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
556 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
560 assert (rc->values == NULL);
561 assert (rc->values_num == 0);
566 } /* for (i = 0..keys_num) */
570 cache_flush_last = now;
571 } /* void rrd_cache_flush */
573 static int rrd_cache_flush_identifier (int timeout, const char *identifier)
580 if (identifier == NULL)
582 rrd_cache_flush (timeout);
589 snprintf (key, sizeof (key), "%s.rrd",
592 snprintf (key, sizeof (key), "%s/%s.rrd",
593 datadir, identifier);
594 key[sizeof (key) - 1] = 0;
596 status = c_avl_get (cache, key, (void *) &rc);
599 WARNING ("rrdtool plugin: rrd_cache_flush_identifier: "
600 "c_avl_get (%s) failed. Does that file really exist?",
605 if (rc->flags == FLAG_FLUSHQ)
609 else if (rc->flags == FLAG_QUEUED)
611 rrd_queue_dequeue (key, &queue_head, &queue_tail);
612 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
614 rc->flags = FLAG_FLUSHQ;
616 else if ((now - rc->first_value) < timeout)
620 else if (rc->values_num > 0)
622 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
624 rc->flags = FLAG_FLUSHQ;
628 } /* int rrd_cache_flush_identifier */
630 static int rrd_cache_insert (const char *filename,
631 const char *value, time_t value_time)
633 rrd_cache_t *rc = NULL;
637 pthread_mutex_lock (&cache_lock);
639 /* This shouldn't happen, but it did happen at least once, so we'll be
643 pthread_mutex_unlock (&cache_lock);
644 WARNING ("rrdtool plugin: cache == NULL.");
648 c_avl_get (cache, filename, (void *) &rc);
652 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
659 rc->flags = FLAG_NONE;
663 if (rc->last_value >= value_time)
665 pthread_mutex_unlock (&cache_lock);
666 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
667 (unsigned int) rc->last_value,
668 (unsigned int) value_time);
672 values_new = (char **) realloc ((void *) rc->values,
673 (rc->values_num + 1) * sizeof (char *));
674 if (values_new == NULL)
677 void *cache_key = NULL;
679 sstrerror (errno, errbuf, sizeof (errbuf));
681 c_avl_remove (cache, filename, &cache_key, NULL);
682 pthread_mutex_unlock (&cache_lock);
684 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
691 rc->values = values_new;
693 rc->values[rc->values_num] = strdup (value);
694 if (rc->values[rc->values_num] != NULL)
697 if (rc->values_num == 1)
698 rc->first_value = value_time;
699 rc->last_value = value_time;
701 /* Insert if this is the first value */
704 void *cache_key = strdup (filename);
706 if (cache_key == NULL)
709 sstrerror (errno, errbuf, sizeof (errbuf));
711 pthread_mutex_unlock (&cache_lock);
713 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
715 sfree (rc->values[0]);
721 c_avl_insert (cache, cache_key, rc);
724 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
725 "values_num = %i; age = %lu;",
726 filename, rc->values_num,
727 (unsigned long)(rc->last_value - rc->first_value));
729 if ((rc->last_value - rc->first_value) >= cache_timeout)
731 /* XXX: If you need to lock both, cache_lock and queue_lock, at
732 * the same time, ALWAYS lock `cache_lock' first! */
733 if (rc->flags == FLAG_NONE)
737 status = rrd_queue_enqueue (filename, &queue_head, &queue_tail);
739 rc->flags = FLAG_QUEUED;
743 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
747 if ((cache_timeout > 0) &&
748 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
749 rrd_cache_flush (cache_flush_timeout);
751 pthread_mutex_unlock (&cache_lock);
754 } /* int rrd_cache_insert */
756 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
758 int a = *((int *) a_ptr);
759 int b = *((int *) b_ptr);
767 } /* int rrd_compare_numeric */
769 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
776 if (0 != strcmp (ds->type, vl->type)) {
777 ERROR ("rrdtool plugin: DS type does not match value list type");
781 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
784 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
787 if (stat (filename, &statbuf) == -1)
791 status = cu_rrd_create_file (filename,
792 ds, vl, &rrdcreate_config);
799 ERROR ("stat(%s) failed: %s", filename,
800 sstrerror (errno, errbuf,
805 else if (!S_ISREG (statbuf.st_mode))
807 ERROR ("stat(%s): Not a regular file!",
812 status = rrd_cache_insert (filename, values, vl->time);
815 } /* int rrd_write */
817 static int rrd_flush (int timeout, const char *identifier)
819 pthread_mutex_lock (&cache_lock);
822 pthread_mutex_unlock (&cache_lock);
826 rrd_cache_flush_identifier (timeout, identifier);
828 pthread_mutex_unlock (&cache_lock);
830 } /* int rrd_flush */
832 static int rrd_config (const char *key, const char *value)
834 if (strcasecmp ("CacheTimeout", key) == 0)
836 int tmp = atoi (value);
839 fprintf (stderr, "rrdtool: `CacheTimeout' must "
840 "be greater than 0.\n");
841 ERROR ("rrdtool: `CacheTimeout' must "
842 "be greater than 0.\n");
847 else if (strcasecmp ("CacheFlush", key) == 0)
849 int tmp = atoi (value);
852 fprintf (stderr, "rrdtool: `CacheFlush' must "
853 "be greater than 0.\n");
854 ERROR ("rrdtool: `CacheFlush' must "
855 "be greater than 0.\n");
858 cache_flush_timeout = tmp;
860 else if (strcasecmp ("DataDir", key) == 0)
864 datadir = strdup (value);
867 int len = strlen (datadir);
868 while ((len > 0) && (datadir[len - 1] == '/'))
880 else if (strcasecmp ("StepSize", key) == 0)
882 int temp = atoi (value);
884 rrdcreate_config.stepsize = temp;
886 else if (strcasecmp ("HeartBeat", key) == 0)
888 int temp = atoi (value);
890 rrdcreate_config.heartbeat = temp;
892 else if (strcasecmp ("RRARows", key) == 0)
894 int tmp = atoi (value);
897 fprintf (stderr, "rrdtool: `RRARows' must "
898 "be greater than 0.\n");
899 ERROR ("rrdtool: `RRARows' must "
900 "be greater than 0.\n");
903 rrdcreate_config.rrarows = tmp;
905 else if (strcasecmp ("RRATimespan", key) == 0)
907 char *saveptr = NULL;
913 value_copy = strdup (value);
914 if (value_copy == NULL)
918 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
922 tmp_alloc = realloc (rrdcreate_config.timespans,
923 sizeof (int) * (rrdcreate_config.timespans_num + 1));
924 if (tmp_alloc == NULL)
926 fprintf (stderr, "rrdtool: realloc failed.\n");
927 ERROR ("rrdtool: realloc failed.\n");
931 rrdcreate_config.timespans = tmp_alloc;
932 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = atoi (ptr);
933 if (rrdcreate_config.timespans[rrdcreate_config.timespans_num] != 0)
934 rrdcreate_config.timespans_num++;
935 } /* while (strtok_r) */
937 qsort (/* base = */ rrdcreate_config.timespans,
938 /* nmemb = */ rrdcreate_config.timespans_num,
939 /* size = */ sizeof (rrdcreate_config.timespans[0]),
940 /* compar = */ rrd_compare_numeric);
944 else if (strcasecmp ("XFF", key) == 0)
946 double tmp = atof (value);
947 if ((tmp < 0.0) || (tmp >= 1.0))
949 fprintf (stderr, "rrdtool: `XFF' must "
950 "be in the range 0 to 1 (exclusive).");
951 ERROR ("rrdtool: `XFF' must "
952 "be in the range 0 to 1 (exclusive).");
955 rrdcreate_config.xff = tmp;
957 else if (strcasecmp ("WritesPerSecond", key) == 0)
959 double wps = atof (value);
963 fprintf (stderr, "rrdtool: `WritesPerSecond' must be "
964 "greater than or equal to zero.");
973 write_rate = 1.0 / wps;
981 } /* int rrd_config */
983 static int rrd_shutdown (void)
985 pthread_mutex_lock (&cache_lock);
986 rrd_cache_flush (-1);
987 pthread_mutex_unlock (&cache_lock);
989 pthread_mutex_lock (&queue_lock);
991 pthread_cond_signal (&queue_cond);
992 pthread_mutex_unlock (&queue_lock);
994 /* Wait for all the values to be written to disk before returning. */
995 if (queue_thread != 0)
997 pthread_join (queue_thread, NULL);
999 DEBUG ("rrdtool plugin: queue_thread exited.");
1003 } /* int rrd_shutdown */
1005 static int rrd_init (void)
1009 if (rrdcreate_config.stepsize < 0)
1010 rrdcreate_config.stepsize = 0;
1011 if (rrdcreate_config.heartbeat <= 0)
1012 rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize;
1014 if ((rrdcreate_config.heartbeat > 0)
1015 && (rrdcreate_config.heartbeat < interval_g))
1016 WARNING ("rrdtool plugin: Your `heartbeat' is "
1017 "smaller than your `interval'. This will "
1018 "likely cause problems.");
1019 else if ((rrdcreate_config.stepsize > 0)
1020 && (rrdcreate_config.stepsize < interval_g))
1021 WARNING ("rrdtool plugin: Your `stepsize' is "
1022 "smaller than your `interval'. This will "
1023 "create needlessly big RRD-files.");
1025 /* Set the cache up */
1026 pthread_mutex_lock (&cache_lock);
1028 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1031 ERROR ("rrdtool plugin: c_avl_create failed.");
1035 cache_flush_last = time (NULL);
1036 if (cache_timeout < 2)
1039 cache_flush_timeout = 0;
1041 else if (cache_flush_timeout < cache_timeout)
1042 cache_flush_timeout = 10 * cache_timeout;
1044 pthread_mutex_unlock (&cache_lock);
1046 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
1049 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1053 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1054 " heartbeat = %i; rrarows = %i; xff = %lf;",
1055 (datadir == NULL) ? "(null)" : datadir,
1056 rrdcreate_config.stepsize,
1057 rrdcreate_config.heartbeat,
1058 rrdcreate_config.rrarows,
1059 rrdcreate_config.xff);
1062 } /* int rrd_init */
1064 void module_register (void)
1066 plugin_register_config ("rrdtool", rrd_config,
1067 config_keys, config_keys_num);
1068 plugin_register_init ("rrdtool", rrd_init);
1069 plugin_register_write ("rrdtool", rrd_write);
1070 plugin_register_flush ("rrdtool", rrd_flush);
1071 plugin_register_shutdown ("rrdtool", rrd_shutdown);