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>
29 #include "utils_avltree.h"
30 #include "utils_random.h"
31 #include "utils_rrdcreate.h"
48 int64_t random_variation;
56 typedef struct rrd_cache_s rrd_cache_t;
63 typedef enum rrd_queue_dir_e rrd_queue_dir_t;
68 struct rrd_queue_s *next;
70 typedef struct rrd_queue_s rrd_queue_t;
75 static const char *config_keys[] =
89 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
91 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
92 * is zero a default, depending on the `interval' member of the value list is
94 static char *datadir = NULL;
95 static double write_rate = 0.0;
96 static rrdcreate_config_t rrdcreate_config =
100 /* rrarows = */ 1200,
103 /* timespans = */ NULL,
104 /* timespans_num = */ 0,
106 /* consolidation_functions = */ NULL,
107 /* consolidation_functions_num = */ 0,
112 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
113 * ALWAYS lock `cache_lock' first! */
114 static cdtime_t cache_timeout = 0;
115 static cdtime_t cache_flush_timeout = 0;
116 static cdtime_t random_timeout = TIME_T_TO_CDTIME_T (1);
117 static cdtime_t cache_flush_last;
118 static c_avl_tree_t *cache = NULL;
119 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
121 static rrd_queue_t *queue_head = NULL;
122 static rrd_queue_t *queue_tail = NULL;
123 static rrd_queue_t *flushq_head = NULL;
124 static rrd_queue_t *flushq_tail = NULL;
125 static pthread_t queue_thread;
126 static int queue_thread_running = 1;
127 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
128 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
130 #if !HAVE_THREADSAFE_LIBRRD
131 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
134 static int do_shutdown = 0;
136 #if HAVE_THREADSAFE_LIBRRD
137 static int srrd_update (char *filename, char *template,
138 int argc, const char **argv)
142 optind = 0; /* bug in librrd? */
145 status = rrd_update_r (filename, template, argc, (void *) argv);
149 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
150 filename, rrd_get_error ());
154 } /* int srrd_update */
155 /* #endif HAVE_THREADSAFE_LIBRRD */
157 #else /* !HAVE_THREADSAFE_LIBRRD */
158 static int srrd_update (char *filename, char *template,
159 int argc, const char **argv)
166 assert (template == NULL);
169 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
170 if (new_argv == NULL)
172 ERROR ("rrdtool plugin: malloc failed.");
176 new_argv[0] = "update";
177 new_argv[1] = filename;
179 memcpy (new_argv + 2, argv, argc * sizeof (char *));
180 new_argv[new_argc] = NULL;
182 pthread_mutex_lock (&librrd_lock);
183 optind = 0; /* bug in librrd? */
186 status = rrd_update (new_argc, new_argv);
187 pthread_mutex_unlock (&librrd_lock);
191 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
192 filename, rrd_get_error ());
198 } /* int srrd_update */
199 #endif /* !HAVE_THREADSAFE_LIBRRD */
201 static int value_list_to_string_multiple (char *buffer, int buffer_len,
202 const data_set_t *ds, const value_list_t *vl)
209 memset (buffer, '\0', buffer_len);
211 tt = CDTIME_T_TO_TIME_T (vl->time);
212 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) tt);
213 if ((status < 1) || (status >= buffer_len))
217 for (i = 0; i < ds->ds_num; i++)
219 if ((ds->ds[i].type != DS_TYPE_COUNTER)
220 && (ds->ds[i].type != DS_TYPE_GAUGE)
221 && (ds->ds[i].type != DS_TYPE_DERIVE)
222 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
225 if (ds->ds[i].type == DS_TYPE_COUNTER)
226 status = ssnprintf (buffer + offset, buffer_len - offset,
227 ":%llu", vl->values[i].counter);
228 else if (ds->ds[i].type == DS_TYPE_GAUGE)
229 status = ssnprintf (buffer + offset, buffer_len - offset,
230 ":"GAUGE_FORMAT, vl->values[i].gauge);
231 else if (ds->ds[i].type == DS_TYPE_DERIVE)
232 status = ssnprintf (buffer + offset, buffer_len - offset,
233 ":%"PRIi64, vl->values[i].derive);
234 else /*if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */
235 status = ssnprintf (buffer + offset, buffer_len - offset,
236 ":%"PRIu64, vl->values[i].absolute);
238 if ((status < 1) || (status >= (buffer_len - offset)))
242 } /* for ds->ds_num */
245 } /* int value_list_to_string_multiple */
247 static int value_list_to_string (char *buffer, int buffer_len,
248 const data_set_t *ds, const value_list_t *vl)
254 return (value_list_to_string_multiple (buffer, buffer_len,
257 tt = CDTIME_T_TO_TIME_T (vl->time);
258 switch (ds->ds[0].type)
261 status = ssnprintf (buffer, buffer_len, "%u:%"PRIi64,
262 (unsigned) tt, vl->values[0].derive);
265 status = ssnprintf (buffer, buffer_len, "%u:"GAUGE_FORMAT,
266 (unsigned) tt, vl->values[0].gauge);
268 case DS_TYPE_COUNTER:
269 status = ssnprintf (buffer, buffer_len, "%u:%llu",
270 (unsigned) tt, vl->values[0].counter);
272 case DS_TYPE_ABSOLUTE:
273 status = ssnprintf (buffer, buffer_len, "%u:%"PRIu64,
274 (unsigned) tt, vl->values[0].absolute);
280 if ((status < 1) || (status >= buffer_len))
284 } /* int value_list_to_string */
286 static int value_list_to_filename (char *buffer, size_t buffer_size,
287 value_list_t const *vl)
289 char const suffix[] = ".rrd";
295 size_t datadir_len = strlen (datadir) + 1;
297 if (datadir_len >= buffer_size)
300 sstrncpy (buffer, datadir, buffer_size);
301 buffer[datadir_len - 1] = '/';
302 buffer[datadir_len] = 0;
304 buffer += datadir_len;
305 buffer_size -= datadir_len;
308 status = FORMAT_VL (buffer, buffer_size, vl);
312 len = strlen (buffer);
313 assert (len < buffer_size);
317 if (buffer_size <= sizeof (suffix))
320 memcpy (buffer, suffix, sizeof (suffix));
322 } /* int value_list_to_filename */
324 static void *rrd_queue_thread (void __attribute__((unused)) *data)
326 struct timeval tv_next_update;
327 struct timeval tv_now;
329 gettimeofday (&tv_next_update, /* timezone = */ NULL);
333 rrd_queue_t *queue_entry;
334 rrd_cache_t *cache_entry;
343 pthread_mutex_lock (&queue_lock);
344 /* Wait for values to arrive */
347 struct timespec ts_wait;
349 while ((flushq_head == NULL) && (queue_head == NULL)
350 && (do_shutdown == 0))
351 pthread_cond_wait (&queue_cond, &queue_lock);
353 if ((flushq_head == NULL) && (queue_head == NULL))
356 /* Don't delay if there's something to flush */
357 if (flushq_head != NULL)
360 /* Don't delay if we're shutting down */
361 if (do_shutdown != 0)
364 /* Don't delay if no delay was configured. */
365 if (write_rate <= 0.0)
368 gettimeofday (&tv_now, /* timezone = */ NULL);
369 status = timeval_cmp (tv_next_update, tv_now, NULL);
370 /* We're good to go */
374 /* We're supposed to wait a bit with this update, so we'll
375 * wait for the next addition to the queue or to the end of
376 * the wait period - whichever comes first. */
377 ts_wait.tv_sec = tv_next_update.tv_sec;
378 ts_wait.tv_nsec = 1000 * tv_next_update.tv_usec;
380 status = pthread_cond_timedwait (&queue_cond, &queue_lock,
382 if (status == ETIMEDOUT)
386 /* XXX: If you need to lock both, cache_lock and queue_lock, at
387 * the same time, ALWAYS lock `cache_lock' first! */
389 /* We're in the shutdown phase */
390 if ((flushq_head == NULL) && (queue_head == NULL))
392 pthread_mutex_unlock (&queue_lock);
396 if (flushq_head != NULL)
398 /* Dequeue the first flush entry */
399 queue_entry = flushq_head;
400 if (flushq_head == flushq_tail)
401 flushq_head = flushq_tail = NULL;
403 flushq_head = flushq_head->next;
405 else /* if (queue_head != NULL) */
407 /* Dequeue the first regular entry */
408 queue_entry = queue_head;
409 if (queue_head == queue_tail)
410 queue_head = queue_tail = NULL;
412 queue_head = queue_head->next;
415 /* Unlock the queue again */
416 pthread_mutex_unlock (&queue_lock);
418 /* We now need the cache lock so the entry isn't updated while
419 * we make a copy of it's values */
420 pthread_mutex_lock (&cache_lock);
422 status = c_avl_get (cache, queue_entry->filename,
423 (void *) &cache_entry);
427 values = cache_entry->values;
428 values_num = cache_entry->values_num;
430 cache_entry->values = NULL;
431 cache_entry->values_num = 0;
432 cache_entry->flags = FLAG_NONE;
435 pthread_mutex_unlock (&cache_lock);
439 sfree (queue_entry->filename);
444 /* Update `tv_next_update' */
445 if (write_rate > 0.0)
447 gettimeofday (&tv_now, /* timezone = */ NULL);
448 tv_next_update.tv_sec = tv_now.tv_sec;
449 tv_next_update.tv_usec = tv_now.tv_usec
450 + ((suseconds_t) (1000000 * write_rate));
451 while (tv_next_update.tv_usec > 1000000)
453 tv_next_update.tv_sec++;
454 tv_next_update.tv_usec -= 1000000;
458 /* Write the values to the RRD-file */
459 srrd_update (queue_entry->filename, NULL,
460 values_num, (const char **)values);
461 DEBUG ("rrdtool plugin: queue thread: Wrote %i value%s to %s",
462 values_num, (values_num == 1) ? "" : "s",
463 queue_entry->filename);
465 for (i = 0; i < values_num; i++)
470 sfree (queue_entry->filename);
474 pthread_exit ((void *) 0);
476 } /* void *rrd_queue_thread */
478 static int rrd_queue_enqueue (const char *filename,
479 rrd_queue_t **head, rrd_queue_t **tail)
481 rrd_queue_t *queue_entry;
483 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
484 if (queue_entry == NULL)
487 queue_entry->filename = strdup (filename);
488 if (queue_entry->filename == NULL)
494 queue_entry->next = NULL;
496 pthread_mutex_lock (&queue_lock);
501 (*tail)->next = queue_entry;
504 pthread_cond_signal (&queue_cond);
505 pthread_mutex_unlock (&queue_lock);
508 } /* int rrd_queue_enqueue */
510 static int rrd_queue_dequeue (const char *filename,
511 rrd_queue_t **head, rrd_queue_t **tail)
516 pthread_mutex_lock (&queue_lock);
523 if (strcmp (this->filename, filename) == 0)
532 pthread_mutex_unlock (&queue_lock);
539 prev->next = this->next;
541 if (this->next == NULL)
544 pthread_mutex_unlock (&queue_lock);
546 sfree (this->filename);
550 } /* int rrd_queue_dequeue */
552 /* XXX: You must hold "cache_lock" when calling this function! */
553 static void rrd_cache_flush (cdtime_t timeout)
562 c_avl_iterator_t *iter;
565 DEBUG ("rrdtool plugin: Flushing cache, timeout = %.3f",
566 CDTIME_T_TO_DOUBLE (timeout));
569 timeout = TIME_T_TO_CDTIME_T (timeout);
571 /* Build a list of entries to be flushed */
572 iter = c_avl_get_iterator (cache);
573 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
575 if (rc->flags != FLAG_NONE)
577 /* timeout == 0 => flush everything */
578 else if ((timeout != 0)
579 && ((now - rc->first_value) < timeout))
581 else if (rc->values_num > 0)
585 status = rrd_queue_enqueue (key, &queue_head, &queue_tail);
587 rc->flags = FLAG_QUEUED;
589 else /* ancient and no values -> waste of memory */
591 char **tmp = (char **) realloc ((void *) keys,
592 (keys_num + 1) * sizeof (char *));
596 ERROR ("rrdtool plugin: "
597 "realloc failed: %s",
598 sstrerror (errno, errbuf,
600 c_avl_iterator_destroy (iter);
605 keys[keys_num] = key;
608 } /* while (c_avl_iterator_next) */
609 c_avl_iterator_destroy (iter);
611 for (i = 0; i < keys_num; i++)
613 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
615 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
619 assert (rc->values == NULL);
620 assert (rc->values_num == 0);
625 } /* for (i = 0..keys_num) */
629 cache_flush_last = now;
630 } /* void rrd_cache_flush */
632 static int rrd_cache_flush_identifier (cdtime_t timeout,
633 const char *identifier)
640 if (identifier == NULL)
642 rrd_cache_flush (timeout);
649 snprintf (key, sizeof (key), "%s.rrd",
652 snprintf (key, sizeof (key), "%s/%s.rrd",
653 datadir, identifier);
654 key[sizeof (key) - 1] = 0;
656 status = c_avl_get (cache, key, (void *) &rc);
659 INFO ("rrdtool plugin: rrd_cache_flush_identifier: "
660 "c_avl_get (%s) failed. Does that file really exist?",
665 if (rc->flags == FLAG_FLUSHQ)
669 else if (rc->flags == FLAG_QUEUED)
671 rrd_queue_dequeue (key, &queue_head, &queue_tail);
672 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
674 rc->flags = FLAG_FLUSHQ;
676 else if ((now - rc->first_value) < timeout)
680 else if (rc->values_num > 0)
682 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
684 rc->flags = FLAG_FLUSHQ;
688 } /* int rrd_cache_flush_identifier */
690 static int64_t rrd_get_random_variation (void)
695 if (random_timeout <= 0)
698 /* Assure that "cache_timeout + random_variation" is never negative. */
699 if (random_timeout > cache_timeout)
701 INFO ("rrdtool plugin: Adjusting \"RandomTimeout\" to %.3f seconds.",
702 CDTIME_T_TO_DOUBLE (cache_timeout));
703 random_timeout = cache_timeout;
706 max = (long) (random_timeout / 2);
707 min = max - ((long) random_timeout);
709 return ((int64_t) cdrand_range (min, max));
710 } /* int64_t rrd_get_random_variation */
712 static int rrd_cache_insert (const char *filename,
713 const char *value, cdtime_t value_time)
715 rrd_cache_t *rc = NULL;
719 pthread_mutex_lock (&cache_lock);
721 /* This shouldn't happen, but it did happen at least once, so we'll be
725 pthread_mutex_unlock (&cache_lock);
726 WARNING ("rrdtool plugin: cache == NULL.");
730 c_avl_get (cache, filename, (void *) &rc);
734 rc = malloc (sizeof (*rc));
737 pthread_mutex_unlock (&cache_lock);
744 rc->random_variation = rrd_get_random_variation ();
745 rc->flags = FLAG_NONE;
749 assert (value_time > 0); /* plugin_dispatch() ensures this. */
750 if (rc->last_value >= value_time)
752 pthread_mutex_unlock (&cache_lock);
753 DEBUG ("rrdtool plugin: (rc->last_value = %"PRIu64") "
754 ">= (value_time = %"PRIu64")",
755 rc->last_value, value_time);
759 values_new = (char **) realloc ((void *) rc->values,
760 (rc->values_num + 1) * sizeof (char *));
761 if (values_new == NULL)
764 void *cache_key = NULL;
766 sstrerror (errno, errbuf, sizeof (errbuf));
768 c_avl_remove (cache, filename, &cache_key, NULL);
769 pthread_mutex_unlock (&cache_lock);
771 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
778 rc->values = values_new;
780 rc->values[rc->values_num] = strdup (value);
781 if (rc->values[rc->values_num] != NULL)
784 if (rc->values_num == 1)
785 rc->first_value = value_time;
786 rc->last_value = value_time;
788 /* Insert if this is the first value */
791 void *cache_key = strdup (filename);
793 if (cache_key == NULL)
796 sstrerror (errno, errbuf, sizeof (errbuf));
798 pthread_mutex_unlock (&cache_lock);
800 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
802 sfree (rc->values[0]);
808 c_avl_insert (cache, cache_key, rc);
811 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
812 "values_num = %i; age = %.3f;",
813 filename, rc->values_num,
814 CDTIME_T_TO_DOUBLE (rc->last_value - rc->first_value));
816 if ((rc->last_value - rc->first_value) >= (cache_timeout + rc->random_variation))
818 /* XXX: If you need to lock both, cache_lock and queue_lock, at
819 * the same time, ALWAYS lock `cache_lock' first! */
820 if (rc->flags == FLAG_NONE)
824 status = rrd_queue_enqueue (filename, &queue_head, &queue_tail);
826 rc->flags = FLAG_QUEUED;
828 rc->random_variation = rrd_get_random_variation ();
832 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
836 if ((cache_timeout > 0) &&
837 ((cdtime () - cache_flush_last) > cache_flush_timeout))
838 rrd_cache_flush (cache_flush_timeout);
840 pthread_mutex_unlock (&cache_lock);
843 } /* int rrd_cache_insert */
845 static int rrd_cache_destroy (void) /* {{{ */
852 pthread_mutex_lock (&cache_lock);
856 pthread_mutex_unlock (&cache_lock);
860 while (c_avl_pick (cache, &key, &value) == 0)
871 if (rc->values_num > 0)
874 for (i = 0; i < rc->values_num; i++)
875 sfree (rc->values[i]);
880 c_avl_destroy (cache);
885 INFO ("rrdtool plugin: %i cache %s had values when destroying the cache.",
886 non_empty, (non_empty == 1) ? "entry" : "entries");
890 DEBUG ("rrdtool plugin: No values have been lost "
891 "when destroying the cache.");
894 pthread_mutex_unlock (&cache_lock);
896 } /* }}} int rrd_cache_destroy */
898 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
900 int a = *((int *) a_ptr);
901 int b = *((int *) b_ptr);
909 } /* int rrd_compare_numeric */
911 static int rrd_write (const data_set_t *ds, const value_list_t *vl,
912 user_data_t __attribute__((unused)) *user_data)
922 if (0 != strcmp (ds->type, vl->type)) {
923 ERROR ("rrdtool plugin: DS type does not match value list type");
927 if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
930 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
933 if (stat (filename, &statbuf) == -1)
937 status = cu_rrd_create_file (filename,
938 ds, vl, &rrdcreate_config);
941 else if (rrdcreate_config.async)
947 ERROR ("stat(%s) failed: %s", filename,
948 sstrerror (errno, errbuf,
953 else if (!S_ISREG (statbuf.st_mode))
955 ERROR ("stat(%s): Not a regular file!",
960 status = rrd_cache_insert (filename, values, vl->time);
963 } /* int rrd_write */
965 static int rrd_flush (cdtime_t timeout, const char *identifier,
966 __attribute__((unused)) user_data_t *user_data)
968 pthread_mutex_lock (&cache_lock);
971 pthread_mutex_unlock (&cache_lock);
975 rrd_cache_flush_identifier (timeout, identifier);
977 pthread_mutex_unlock (&cache_lock);
979 } /* int rrd_flush */
981 static int rrd_config (const char *key, const char *value)
983 if (strcasecmp ("CacheTimeout", key) == 0)
985 double tmp = atof (value);
988 fprintf (stderr, "rrdtool: `CacheTimeout' must "
989 "be greater than 0.\n");
990 ERROR ("rrdtool: `CacheTimeout' must "
991 "be greater than 0.\n");
994 cache_timeout = DOUBLE_TO_CDTIME_T (tmp);
996 else if (strcasecmp ("CacheFlush", key) == 0)
998 int tmp = atoi (value);
1001 fprintf (stderr, "rrdtool: `CacheFlush' must "
1002 "be greater than 0.\n");
1003 ERROR ("rrdtool: `CacheFlush' must "
1004 "be greater than 0.\n");
1007 cache_flush_timeout = tmp;
1009 else if (strcasecmp ("DataDir", key) == 0)
1014 tmp = strdup (value);
1017 ERROR ("rrdtool plugin: strdup failed.");
1022 while ((len > 0) && (tmp[len - 1] == '/'))
1030 ERROR ("rrdtool plugin: Invalid \"DataDir\" option.");
1035 if (datadir != NULL)
1042 else if (strcasecmp ("StepSize", key) == 0)
1044 unsigned long temp = strtoul (value, NULL, 0);
1046 rrdcreate_config.stepsize = temp;
1048 else if (strcasecmp ("HeartBeat", key) == 0)
1050 int temp = atoi (value);
1052 rrdcreate_config.heartbeat = temp;
1054 else if (strcasecmp ("CreateFilesAsync", key) == 0)
1056 if (IS_TRUE (value))
1057 rrdcreate_config.async = 1;
1059 rrdcreate_config.async = 0;
1061 else if (strcasecmp ("RRARows", key) == 0)
1063 int tmp = atoi (value);
1066 fprintf (stderr, "rrdtool: `RRARows' must "
1067 "be greater than 0.\n");
1068 ERROR ("rrdtool: `RRARows' must "
1069 "be greater than 0.\n");
1072 rrdcreate_config.rrarows = tmp;
1074 else if (strcasecmp ("RRATimespan", key) == 0)
1076 char *saveptr = NULL;
1082 value_copy = strdup (value);
1083 if (value_copy == NULL)
1087 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1091 tmp_alloc = realloc (rrdcreate_config.timespans,
1092 sizeof (int) * (rrdcreate_config.timespans_num + 1));
1093 if (tmp_alloc == NULL)
1095 fprintf (stderr, "rrdtool: realloc failed.\n");
1096 ERROR ("rrdtool: realloc failed.\n");
1100 rrdcreate_config.timespans = tmp_alloc;
1101 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = atoi (ptr);
1102 if (rrdcreate_config.timespans[rrdcreate_config.timespans_num] != 0)
1103 rrdcreate_config.timespans_num++;
1104 } /* while (strtok_r) */
1106 qsort (/* base = */ rrdcreate_config.timespans,
1107 /* nmemb = */ rrdcreate_config.timespans_num,
1108 /* size = */ sizeof (rrdcreate_config.timespans[0]),
1109 /* compar = */ rrd_compare_numeric);
1113 else if (strcasecmp ("XFF", key) == 0)
1115 double tmp = atof (value);
1116 if ((tmp < 0.0) || (tmp >= 1.0))
1118 fprintf (stderr, "rrdtool: `XFF' must "
1119 "be in the range 0 to 1 (exclusive).");
1120 ERROR ("rrdtool: `XFF' must "
1121 "be in the range 0 to 1 (exclusive).");
1124 rrdcreate_config.xff = tmp;
1126 else if (strcasecmp ("WritesPerSecond", key) == 0)
1128 double wps = atof (value);
1132 fprintf (stderr, "rrdtool: `WritesPerSecond' must be "
1133 "greater than or equal to zero.");
1136 else if (wps == 0.0)
1142 write_rate = 1.0 / wps;
1145 else if (strcasecmp ("RandomTimeout", key) == 0)
1152 fprintf (stderr, "rrdtool: `RandomTimeout' must "
1153 "be greater than or equal to zero.\n");
1154 ERROR ("rrdtool: `RandomTimeout' must "
1155 "be greater then or equal to zero.");
1159 random_timeout = DOUBLE_TO_CDTIME_T (tmp);
1167 } /* int rrd_config */
1169 static int rrd_shutdown (void)
1171 pthread_mutex_lock (&cache_lock);
1172 rrd_cache_flush (0);
1173 pthread_mutex_unlock (&cache_lock);
1175 pthread_mutex_lock (&queue_lock);
1177 pthread_cond_signal (&queue_cond);
1178 pthread_mutex_unlock (&queue_lock);
1180 if ((queue_thread_running != 0)
1181 && ((queue_head != NULL) || (flushq_head != NULL)))
1183 INFO ("rrdtool plugin: Shutting down the queue thread. "
1184 "This may take a while.");
1186 else if (queue_thread_running != 0)
1188 INFO ("rrdtool plugin: Shutting down the queue thread.");
1191 /* Wait for all the values to be written to disk before returning. */
1192 if (queue_thread_running != 0)
1194 pthread_join (queue_thread, NULL);
1195 memset (&queue_thread, 0, sizeof (queue_thread));
1196 queue_thread_running = 0;
1197 DEBUG ("rrdtool plugin: queue_thread exited.");
1200 rrd_cache_destroy ();
1203 } /* int rrd_shutdown */
1205 static int rrd_init (void)
1207 static int init_once = 0;
1214 if (rrdcreate_config.heartbeat <= 0)
1215 rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize;
1217 /* Set the cache up */
1218 pthread_mutex_lock (&cache_lock);
1220 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1223 pthread_mutex_unlock (&cache_lock);
1224 ERROR ("rrdtool plugin: c_avl_create failed.");
1228 cache_flush_last = cdtime ();
1229 if (cache_timeout == 0)
1231 cache_flush_timeout = 0;
1233 else if (cache_flush_timeout < cache_timeout)
1234 cache_flush_timeout = 10 * cache_timeout;
1236 pthread_mutex_unlock (&cache_lock);
1238 status = plugin_thread_create (&queue_thread, /* attr = */ NULL,
1239 rrd_queue_thread, /* args = */ NULL);
1242 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1245 queue_thread_running = 1;
1247 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %lu;"
1248 " heartbeat = %i; rrarows = %i; xff = %lf;",
1249 (datadir == NULL) ? "(null)" : datadir,
1250 rrdcreate_config.stepsize,
1251 rrdcreate_config.heartbeat,
1252 rrdcreate_config.rrarows,
1253 rrdcreate_config.xff);
1256 } /* int rrd_init */
1258 void module_register (void)
1260 plugin_register_config ("rrdtool", rrd_config,
1261 config_keys, config_keys_num);
1262 plugin_register_init ("rrdtool", rrd_init);
1263 plugin_register_write ("rrdtool", rrd_write, /* user_data = */ NULL);
1264 plugin_register_flush ("rrdtool", rrd_flush, /* user_data = */ NULL);
1265 plugin_register_shutdown ("rrdtool", rrd_shutdown);