2 * collectd - src/rrdtool.c
3 * Copyright (C) 2006-2013 Florian octo Forster
4 * Copyright (C) 2008-2008 Sebastian Harl
5 * Copyright (C) 2009 Mariusz Gronczewski
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at collectd.org>
22 * Sebastian Harl <sh at tokkee.org>
23 * Mariusz Gronczewski <xani666 at gmail.com>
30 #include "utils_avltree.h"
31 #include "utils_random.h"
32 #include "utils_rrdcreate.h"
45 int64_t random_variation;
53 typedef struct rrd_cache_s rrd_cache_t;
60 typedef enum rrd_queue_dir_e rrd_queue_dir_t;
65 struct rrd_queue_s *next;
67 typedef struct rrd_queue_s rrd_queue_t;
72 static const char *config_keys[] =
86 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
88 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
89 * is zero a default, depending on the `interval' member of the value list is
91 static char *datadir = NULL;
92 static double write_rate = 0.0;
93 static rrdcreate_config_t rrdcreate_config =
100 /* timespans = */ NULL,
101 /* timespans_num = */ 0,
103 /* consolidation_functions = */ NULL,
104 /* consolidation_functions_num = */ 0,
109 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
110 * ALWAYS lock `cache_lock' first! */
111 static cdtime_t cache_timeout = 0;
112 static cdtime_t cache_flush_timeout = 0;
113 static cdtime_t random_timeout = TIME_T_TO_CDTIME_T (1);
114 static cdtime_t cache_flush_last;
115 static c_avl_tree_t *cache = NULL;
116 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
118 static rrd_queue_t *queue_head = NULL;
119 static rrd_queue_t *queue_tail = NULL;
120 static rrd_queue_t *flushq_head = NULL;
121 static rrd_queue_t *flushq_tail = NULL;
122 static pthread_t queue_thread;
123 static int queue_thread_running = 1;
124 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
125 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
127 #if !HAVE_THREADSAFE_LIBRRD
128 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
131 static int do_shutdown = 0;
133 #if HAVE_THREADSAFE_LIBRRD
134 static int srrd_update (char *filename, char *template,
135 int argc, const char **argv)
139 optind = 0; /* bug in librrd? */
142 status = rrd_update_r (filename, template, argc, (void *) argv);
146 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
147 filename, rrd_get_error ());
151 } /* int srrd_update */
152 /* #endif HAVE_THREADSAFE_LIBRRD */
154 #else /* !HAVE_THREADSAFE_LIBRRD */
155 static int srrd_update (char *filename, char *template,
156 int argc, const char **argv)
163 assert (template == NULL);
166 new_argv = malloc ((new_argc + 1) * sizeof (*new_argv));
167 if (new_argv == NULL)
169 ERROR ("rrdtool plugin: malloc failed.");
173 new_argv[0] = "update";
174 new_argv[1] = filename;
176 memcpy (new_argv + 2, argv, argc * sizeof (char *));
177 new_argv[new_argc] = NULL;
179 pthread_mutex_lock (&librrd_lock);
180 optind = 0; /* bug in librrd? */
183 status = rrd_update (new_argc, new_argv);
184 pthread_mutex_unlock (&librrd_lock);
188 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
189 filename, rrd_get_error ());
195 } /* int srrd_update */
196 #endif /* !HAVE_THREADSAFE_LIBRRD */
198 static int value_list_to_string_multiple (char *buffer, int buffer_len,
199 const data_set_t *ds, const value_list_t *vl)
205 memset (buffer, '\0', buffer_len);
207 tt = CDTIME_T_TO_TIME_T (vl->time);
208 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) tt);
209 if ((status < 1) || (status >= buffer_len))
213 for (size_t i = 0; i < ds->ds_num; i++)
215 if ((ds->ds[i].type != DS_TYPE_COUNTER)
216 && (ds->ds[i].type != DS_TYPE_GAUGE)
217 && (ds->ds[i].type != DS_TYPE_DERIVE)
218 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
221 if (ds->ds[i].type == DS_TYPE_COUNTER)
222 status = ssnprintf (buffer + offset, buffer_len - offset,
223 ":%llu", vl->values[i].counter);
224 else if (ds->ds[i].type == DS_TYPE_GAUGE)
225 status = ssnprintf (buffer + offset, buffer_len - offset,
226 ":"GAUGE_FORMAT, vl->values[i].gauge);
227 else if (ds->ds[i].type == DS_TYPE_DERIVE)
228 status = ssnprintf (buffer + offset, buffer_len - offset,
229 ":%"PRIi64, vl->values[i].derive);
230 else /*if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */
231 status = ssnprintf (buffer + offset, buffer_len - offset,
232 ":%"PRIu64, vl->values[i].absolute);
234 if ((status < 1) || (status >= (buffer_len - offset)))
238 } /* for ds->ds_num */
241 } /* int value_list_to_string_multiple */
243 static int value_list_to_string (char *buffer, int buffer_len,
244 const data_set_t *ds, const value_list_t *vl)
250 return (value_list_to_string_multiple (buffer, buffer_len,
253 tt = CDTIME_T_TO_TIME_T (vl->time);
254 switch (ds->ds[0].type)
257 status = ssnprintf (buffer, buffer_len, "%u:%"PRIi64,
258 (unsigned) tt, vl->values[0].derive);
261 status = ssnprintf (buffer, buffer_len, "%u:"GAUGE_FORMAT,
262 (unsigned) tt, vl->values[0].gauge);
264 case DS_TYPE_COUNTER:
265 status = ssnprintf (buffer, buffer_len, "%u:%llu",
266 (unsigned) tt, vl->values[0].counter);
268 case DS_TYPE_ABSOLUTE:
269 status = ssnprintf (buffer, buffer_len, "%u:%"PRIu64,
270 (unsigned) tt, vl->values[0].absolute);
276 if ((status < 1) || (status >= buffer_len))
280 } /* int value_list_to_string */
282 static int value_list_to_filename (char *buffer, size_t buffer_size,
283 value_list_t const *vl)
285 char const suffix[] = ".rrd";
291 size_t datadir_len = strlen (datadir) + 1;
293 if (datadir_len >= buffer_size)
296 sstrncpy (buffer, datadir, buffer_size);
297 buffer[datadir_len - 1] = '/';
298 buffer[datadir_len] = 0;
300 buffer += datadir_len;
301 buffer_size -= datadir_len;
304 status = FORMAT_VL (buffer, buffer_size, vl);
308 len = strlen (buffer);
309 assert (len < buffer_size);
313 if (buffer_size <= sizeof (suffix))
316 memcpy (buffer, suffix, sizeof (suffix));
318 } /* int value_list_to_filename */
320 static void *rrd_queue_thread (void __attribute__((unused)) *data)
322 struct timeval tv_next_update;
323 struct timeval tv_now;
325 gettimeofday (&tv_next_update, /* timezone = */ NULL);
329 rrd_queue_t *queue_entry;
330 rrd_cache_t *cache_entry;
338 pthread_mutex_lock (&queue_lock);
339 /* Wait for values to arrive */
342 struct timespec ts_wait;
344 while ((flushq_head == NULL) && (queue_head == NULL)
345 && (do_shutdown == 0))
346 pthread_cond_wait (&queue_cond, &queue_lock);
348 if ((flushq_head == NULL) && (queue_head == NULL))
351 /* Don't delay if there's something to flush */
352 if (flushq_head != NULL)
355 /* Don't delay if we're shutting down */
356 if (do_shutdown != 0)
359 /* Don't delay if no delay was configured. */
360 if (write_rate <= 0.0)
363 gettimeofday (&tv_now, /* timezone = */ NULL);
364 status = timeval_cmp (tv_next_update, tv_now, NULL);
365 /* We're good to go */
369 /* We're supposed to wait a bit with this update, so we'll
370 * wait for the next addition to the queue or to the end of
371 * the wait period - whichever comes first. */
372 ts_wait.tv_sec = tv_next_update.tv_sec;
373 ts_wait.tv_nsec = 1000 * tv_next_update.tv_usec;
375 status = pthread_cond_timedwait (&queue_cond, &queue_lock,
377 if (status == ETIMEDOUT)
381 /* XXX: If you need to lock both, cache_lock and queue_lock, at
382 * the same time, ALWAYS lock `cache_lock' first! */
384 /* We're in the shutdown phase */
385 if ((flushq_head == NULL) && (queue_head == NULL))
387 pthread_mutex_unlock (&queue_lock);
391 if (flushq_head != NULL)
393 /* Dequeue the first flush entry */
394 queue_entry = flushq_head;
395 if (flushq_head == flushq_tail)
396 flushq_head = flushq_tail = NULL;
398 flushq_head = flushq_head->next;
400 else /* if (queue_head != NULL) */
402 /* Dequeue the first regular entry */
403 queue_entry = queue_head;
404 if (queue_head == queue_tail)
405 queue_head = queue_tail = NULL;
407 queue_head = queue_head->next;
410 /* Unlock the queue again */
411 pthread_mutex_unlock (&queue_lock);
413 /* We now need the cache lock so the entry isn't updated while
414 * we make a copy of its values */
415 pthread_mutex_lock (&cache_lock);
417 status = c_avl_get (cache, queue_entry->filename,
418 (void *) &cache_entry);
422 values = cache_entry->values;
423 values_num = cache_entry->values_num;
425 cache_entry->values = NULL;
426 cache_entry->values_num = 0;
427 cache_entry->flags = FLAG_NONE;
430 pthread_mutex_unlock (&cache_lock);
434 sfree (queue_entry->filename);
439 /* Update `tv_next_update' */
440 if (write_rate > 0.0)
442 gettimeofday (&tv_now, /* timezone = */ NULL);
443 tv_next_update.tv_sec = tv_now.tv_sec;
444 tv_next_update.tv_usec = tv_now.tv_usec
445 + ((suseconds_t) (1000000 * write_rate));
446 while (tv_next_update.tv_usec > 1000000)
448 tv_next_update.tv_sec++;
449 tv_next_update.tv_usec -= 1000000;
453 /* Write the values to the RRD-file */
454 srrd_update (queue_entry->filename, NULL,
455 values_num, (const char **)values);
456 DEBUG ("rrdtool plugin: queue thread: Wrote %i value%s to %s",
457 values_num, (values_num == 1) ? "" : "s",
458 queue_entry->filename);
460 for (int i = 0; i < values_num; i++)
465 sfree (queue_entry->filename);
469 pthread_exit ((void *) 0);
471 } /* void *rrd_queue_thread */
473 static int rrd_queue_enqueue (const char *filename,
474 rrd_queue_t **head, rrd_queue_t **tail)
476 rrd_queue_t *queue_entry;
478 queue_entry = malloc (sizeof (*queue_entry));
479 if (queue_entry == NULL)
482 queue_entry->filename = strdup (filename);
483 if (queue_entry->filename == NULL)
489 queue_entry->next = NULL;
491 pthread_mutex_lock (&queue_lock);
496 (*tail)->next = queue_entry;
499 pthread_cond_signal (&queue_cond);
500 pthread_mutex_unlock (&queue_lock);
503 } /* int rrd_queue_enqueue */
505 static int rrd_queue_dequeue (const char *filename,
506 rrd_queue_t **head, rrd_queue_t **tail)
511 pthread_mutex_lock (&queue_lock);
518 if (strcmp (this->filename, filename) == 0)
527 pthread_mutex_unlock (&queue_lock);
534 prev->next = this->next;
536 if (this->next == NULL)
539 pthread_mutex_unlock (&queue_lock);
541 sfree (this->filename);
545 } /* int rrd_queue_dequeue */
547 /* XXX: You must hold "cache_lock" when calling this function! */
548 static void rrd_cache_flush (cdtime_t timeout)
557 c_avl_iterator_t *iter;
559 DEBUG ("rrdtool plugin: Flushing cache, timeout = %.3f",
560 CDTIME_T_TO_DOUBLE (timeout));
563 timeout = TIME_T_TO_CDTIME_T (timeout);
565 /* Build a list of entries to be flushed */
566 iter = c_avl_get_iterator (cache);
567 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
569 if (rc->flags != FLAG_NONE)
571 /* timeout == 0 => flush everything */
572 else if ((timeout != 0)
573 && ((now - rc->first_value) < timeout))
575 else if (rc->values_num > 0)
579 status = rrd_queue_enqueue (key, &queue_head, &queue_tail);
581 rc->flags = FLAG_QUEUED;
583 else /* ancient and no values -> waste of memory */
585 char **tmp = realloc (keys,
586 (keys_num + 1) * sizeof (char *));
590 ERROR ("rrdtool plugin: "
591 "realloc failed: %s",
592 sstrerror (errno, errbuf,
594 c_avl_iterator_destroy (iter);
599 keys[keys_num] = key;
602 } /* while (c_avl_iterator_next) */
603 c_avl_iterator_destroy (iter);
605 for (int i = 0; i < keys_num; i++)
607 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
609 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
613 assert (rc->values == NULL);
614 assert (rc->values_num == 0);
619 } /* for (i = 0..keys_num) */
623 cache_flush_last = now;
624 } /* void rrd_cache_flush */
626 static int rrd_cache_flush_identifier (cdtime_t timeout,
627 const char *identifier)
634 if (identifier == NULL)
636 rrd_cache_flush (timeout);
643 snprintf (key, sizeof (key), "%s.rrd",
646 snprintf (key, sizeof (key), "%s/%s.rrd",
647 datadir, identifier);
648 key[sizeof (key) - 1] = 0;
650 status = c_avl_get (cache, key, (void *) &rc);
653 INFO ("rrdtool plugin: rrd_cache_flush_identifier: "
654 "c_avl_get (%s) failed. Does that file really exist?",
659 if (rc->flags == FLAG_FLUSHQ)
663 else if (rc->flags == FLAG_QUEUED)
665 rrd_queue_dequeue (key, &queue_head, &queue_tail);
666 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
668 rc->flags = FLAG_FLUSHQ;
670 else if ((now - rc->first_value) < timeout)
674 else if (rc->values_num > 0)
676 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
678 rc->flags = FLAG_FLUSHQ;
682 } /* int rrd_cache_flush_identifier */
684 static int64_t rrd_get_random_variation (void)
689 if (random_timeout == 0)
692 /* Assure that "cache_timeout + random_variation" is never negative. */
693 if (random_timeout > cache_timeout)
695 INFO ("rrdtool plugin: Adjusting \"RandomTimeout\" to %.3f seconds.",
696 CDTIME_T_TO_DOUBLE (cache_timeout));
697 random_timeout = cache_timeout;
700 max = (long) (random_timeout / 2);
701 min = max - ((long) random_timeout);
703 return ((int64_t) cdrand_range (min, max));
704 } /* int64_t rrd_get_random_variation */
706 static int rrd_cache_insert (const char *filename,
707 const char *value, cdtime_t value_time)
709 rrd_cache_t *rc = NULL;
713 pthread_mutex_lock (&cache_lock);
715 /* This shouldn't happen, but it did happen at least once, so we'll be
719 pthread_mutex_unlock (&cache_lock);
720 WARNING ("rrdtool plugin: cache == NULL.");
724 c_avl_get (cache, filename, (void *) &rc);
728 rc = malloc (sizeof (*rc));
731 pthread_mutex_unlock (&cache_lock);
738 rc->random_variation = rrd_get_random_variation ();
739 rc->flags = FLAG_NONE;
743 assert (value_time > 0); /* plugin_dispatch() ensures this. */
744 if (rc->last_value >= value_time)
746 pthread_mutex_unlock (&cache_lock);
747 DEBUG ("rrdtool plugin: (rc->last_value = %"PRIu64") "
748 ">= (value_time = %"PRIu64")",
749 rc->last_value, value_time);
753 values_new = realloc ((void *) rc->values,
754 (rc->values_num + 1) * sizeof (char *));
755 if (values_new == NULL)
758 void *cache_key = NULL;
760 sstrerror (errno, errbuf, sizeof (errbuf));
762 c_avl_remove (cache, filename, &cache_key, NULL);
763 pthread_mutex_unlock (&cache_lock);
765 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
772 rc->values = values_new;
774 rc->values[rc->values_num] = strdup (value);
775 if (rc->values[rc->values_num] != NULL)
778 if (rc->values_num == 1)
779 rc->first_value = value_time;
780 rc->last_value = value_time;
782 /* Insert if this is the first value */
785 void *cache_key = strdup (filename);
787 if (cache_key == NULL)
790 sstrerror (errno, errbuf, sizeof (errbuf));
792 pthread_mutex_unlock (&cache_lock);
794 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
796 sfree (rc->values[0]);
802 c_avl_insert (cache, cache_key, rc);
805 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
806 "values_num = %i; age = %.3f;",
807 filename, rc->values_num,
808 CDTIME_T_TO_DOUBLE (rc->last_value - rc->first_value));
810 if ((rc->last_value - rc->first_value) >= (cache_timeout + rc->random_variation))
812 /* XXX: If you need to lock both, cache_lock and queue_lock, at
813 * the same time, ALWAYS lock `cache_lock' first! */
814 if (rc->flags == FLAG_NONE)
818 status = rrd_queue_enqueue (filename, &queue_head, &queue_tail);
820 rc->flags = FLAG_QUEUED;
822 rc->random_variation = rrd_get_random_variation ();
826 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
830 if ((cache_timeout > 0) &&
831 ((cdtime () - cache_flush_last) > cache_flush_timeout))
832 rrd_cache_flush (cache_flush_timeout);
834 pthread_mutex_unlock (&cache_lock);
837 } /* int rrd_cache_insert */
839 static int rrd_cache_destroy (void) /* {{{ */
846 pthread_mutex_lock (&cache_lock);
850 pthread_mutex_unlock (&cache_lock);
854 while (c_avl_pick (cache, &key, &value) == 0)
864 if (rc->values_num > 0)
867 for (int i = 0; i < rc->values_num; i++)
868 sfree (rc->values[i]);
873 c_avl_destroy (cache);
878 INFO ("rrdtool plugin: %i cache %s had values when destroying the cache.",
879 non_empty, (non_empty == 1) ? "entry" : "entries");
883 DEBUG ("rrdtool plugin: No values have been lost "
884 "when destroying the cache.");
887 pthread_mutex_unlock (&cache_lock);
889 } /* }}} int rrd_cache_destroy */
891 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
893 int a = *((int *) a_ptr);
894 int b = *((int *) b_ptr);
902 } /* int rrd_compare_numeric */
904 static int rrd_write (const data_set_t *ds, const value_list_t *vl,
905 user_data_t __attribute__((unused)) *user_data)
915 if (0 != strcmp (ds->type, vl->type)) {
916 ERROR ("rrdtool plugin: DS type does not match value list type");
920 if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
923 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
926 if (stat (filename, &statbuf) == -1)
930 status = cu_rrd_create_file (filename,
931 ds, vl, &rrdcreate_config);
934 else if (rrdcreate_config.async)
940 ERROR ("stat(%s) failed: %s", filename,
941 sstrerror (errno, errbuf,
946 else if (!S_ISREG (statbuf.st_mode))
948 ERROR ("stat(%s): Not a regular file!",
953 status = rrd_cache_insert (filename, values, vl->time);
956 } /* int rrd_write */
958 static int rrd_flush (cdtime_t timeout, const char *identifier,
959 __attribute__((unused)) user_data_t *user_data)
961 pthread_mutex_lock (&cache_lock);
964 pthread_mutex_unlock (&cache_lock);
968 rrd_cache_flush_identifier (timeout, identifier);
970 pthread_mutex_unlock (&cache_lock);
972 } /* int rrd_flush */
974 static int rrd_config (const char *key, const char *value)
976 if (strcasecmp ("CacheTimeout", key) == 0)
978 double tmp = atof (value);
981 fprintf (stderr, "rrdtool: `CacheTimeout' must "
982 "be greater than 0.\n");
983 ERROR ("rrdtool: `CacheTimeout' must "
984 "be greater than 0.\n");
987 cache_timeout = DOUBLE_TO_CDTIME_T (tmp);
989 else if (strcasecmp ("CacheFlush", key) == 0)
991 int tmp = atoi (value);
994 fprintf (stderr, "rrdtool: `CacheFlush' must "
995 "be greater than 0.\n");
996 ERROR ("rrdtool: `CacheFlush' must "
997 "be greater than 0.\n");
1000 cache_flush_timeout = tmp;
1002 else if (strcasecmp ("DataDir", key) == 0)
1007 tmp = strdup (value);
1010 ERROR ("rrdtool plugin: strdup failed.");
1015 while ((len > 0) && (tmp[len - 1] == '/'))
1023 ERROR ("rrdtool plugin: Invalid \"DataDir\" option.");
1028 if (datadir != NULL)
1035 else if (strcasecmp ("StepSize", key) == 0)
1037 unsigned long temp = strtoul (value, NULL, 0);
1039 rrdcreate_config.stepsize = temp;
1041 else if (strcasecmp ("HeartBeat", key) == 0)
1043 int temp = atoi (value);
1045 rrdcreate_config.heartbeat = temp;
1047 else if (strcasecmp ("CreateFilesAsync", key) == 0)
1049 if (IS_TRUE (value))
1050 rrdcreate_config.async = 1;
1052 rrdcreate_config.async = 0;
1054 else if (strcasecmp ("RRARows", key) == 0)
1056 int tmp = atoi (value);
1059 fprintf (stderr, "rrdtool: `RRARows' must "
1060 "be greater than 0.\n");
1061 ERROR ("rrdtool: `RRARows' must "
1062 "be greater than 0.\n");
1065 rrdcreate_config.rrarows = tmp;
1067 else if (strcasecmp ("RRATimespan", key) == 0)
1069 char *saveptr = NULL;
1075 value_copy = strdup (value);
1076 if (value_copy == NULL)
1080 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1084 tmp_alloc = realloc (rrdcreate_config.timespans,
1085 sizeof (int) * (rrdcreate_config.timespans_num + 1));
1086 if (tmp_alloc == NULL)
1088 fprintf (stderr, "rrdtool: realloc failed.\n");
1089 ERROR ("rrdtool: realloc failed.\n");
1093 rrdcreate_config.timespans = tmp_alloc;
1094 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = atoi (ptr);
1095 if (rrdcreate_config.timespans[rrdcreate_config.timespans_num] != 0)
1096 rrdcreate_config.timespans_num++;
1097 } /* while (strtok_r) */
1099 qsort (/* base = */ rrdcreate_config.timespans,
1100 /* nmemb = */ rrdcreate_config.timespans_num,
1101 /* size = */ sizeof (rrdcreate_config.timespans[0]),
1102 /* compar = */ rrd_compare_numeric);
1106 else if (strcasecmp ("XFF", key) == 0)
1108 double tmp = atof (value);
1109 if ((tmp < 0.0) || (tmp >= 1.0))
1111 fprintf (stderr, "rrdtool: `XFF' must "
1112 "be in the range 0 to 1 (exclusive).");
1113 ERROR ("rrdtool: `XFF' must "
1114 "be in the range 0 to 1 (exclusive).");
1117 rrdcreate_config.xff = tmp;
1119 else if (strcasecmp ("WritesPerSecond", key) == 0)
1121 double wps = atof (value);
1125 fprintf (stderr, "rrdtool: `WritesPerSecond' must be "
1126 "greater than or equal to zero.");
1129 else if (wps == 0.0)
1135 write_rate = 1.0 / wps;
1138 else if (strcasecmp ("RandomTimeout", key) == 0)
1145 fprintf (stderr, "rrdtool: `RandomTimeout' must "
1146 "be greater than or equal to zero.\n");
1147 ERROR ("rrdtool: `RandomTimeout' must "
1148 "be greater then or equal to zero.");
1152 random_timeout = DOUBLE_TO_CDTIME_T (tmp);
1160 } /* int rrd_config */
1162 static int rrd_shutdown (void)
1164 pthread_mutex_lock (&cache_lock);
1165 rrd_cache_flush (0);
1166 pthread_mutex_unlock (&cache_lock);
1168 pthread_mutex_lock (&queue_lock);
1170 pthread_cond_signal (&queue_cond);
1171 pthread_mutex_unlock (&queue_lock);
1173 if ((queue_thread_running != 0)
1174 && ((queue_head != NULL) || (flushq_head != NULL)))
1176 INFO ("rrdtool plugin: Shutting down the queue thread. "
1177 "This may take a while.");
1179 else if (queue_thread_running != 0)
1181 INFO ("rrdtool plugin: Shutting down the queue thread.");
1184 /* Wait for all the values to be written to disk before returning. */
1185 if (queue_thread_running != 0)
1187 pthread_join (queue_thread, NULL);
1188 memset (&queue_thread, 0, sizeof (queue_thread));
1189 queue_thread_running = 0;
1190 DEBUG ("rrdtool plugin: queue_thread exited.");
1193 rrd_cache_destroy ();
1196 } /* int rrd_shutdown */
1198 static int rrd_init (void)
1200 static int init_once = 0;
1207 if (rrdcreate_config.heartbeat <= 0)
1208 rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize;
1210 /* Set the cache up */
1211 pthread_mutex_lock (&cache_lock);
1213 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1216 pthread_mutex_unlock (&cache_lock);
1217 ERROR ("rrdtool plugin: c_avl_create failed.");
1221 cache_flush_last = cdtime ();
1222 if (cache_timeout == 0)
1224 cache_flush_timeout = 0;
1226 else if (cache_flush_timeout < cache_timeout)
1227 cache_flush_timeout = 10 * cache_timeout;
1229 pthread_mutex_unlock (&cache_lock);
1231 status = plugin_thread_create (&queue_thread, /* attr = */ NULL,
1232 rrd_queue_thread, /* args = */ NULL);
1235 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1238 queue_thread_running = 1;
1240 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %lu;"
1241 " heartbeat = %i; rrarows = %i; xff = %lf;",
1242 (datadir == NULL) ? "(null)" : datadir,
1243 rrdcreate_config.stepsize,
1244 rrdcreate_config.heartbeat,
1245 rrdcreate_config.rrarows,
1246 rrdcreate_config.xff);
1249 } /* int rrd_init */
1251 void module_register (void)
1253 plugin_register_config ("rrdtool", rrd_config,
1254 config_keys, config_keys_num);
1255 plugin_register_init ("rrdtool", rrd_init);
1256 plugin_register_write ("rrdtool", rrd_write, /* user_data = */ NULL);
1257 plugin_register_flush ("rrdtool", rrd_flush, /* user_data = */ NULL);
1258 plugin_register_shutdown ("rrdtool", rrd_shutdown);