2 * collectd - src/rrdtool.c
3 * Copyright (C) 2006-2008 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 verplant.org>
22 * Sebastian Harl <sh at tokkee.org>
23 * Mariusz Gronczewski <xani666 at gmail.com>
29 #include "utils_avltree.h"
30 #include "utils_rrdcreate.h"
47 int64_t random_variation;
55 typedef struct rrd_cache_s rrd_cache_t;
62 typedef enum rrd_queue_dir_e rrd_queue_dir_t;
67 struct rrd_queue_s *next;
69 typedef struct rrd_queue_s rrd_queue_t;
74 static const char *config_keys[] =
87 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
89 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
90 * is zero a default, depending on the `interval' member of the value list is
92 static char *datadir = NULL;
93 static double write_rate = 0.0;
94 static rrdcreate_config_t rrdcreate_config =
101 /* timespans = */ NULL,
102 /* timespans_num = */ 0,
104 /* consolidation_functions = */ NULL,
105 /* consolidation_functions_num = */ 0
108 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
109 * ALWAYS lock `cache_lock' first! */
110 static cdtime_t cache_timeout = 0;
111 static cdtime_t cache_flush_timeout = 0;
112 static cdtime_t random_timeout = TIME_T_TO_CDTIME_T (1);
113 static cdtime_t cache_flush_last;
114 static c_avl_tree_t *cache = NULL;
115 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
117 static rrd_queue_t *queue_head = NULL;
118 static rrd_queue_t *queue_tail = NULL;
119 static rrd_queue_t *flushq_head = NULL;
120 static rrd_queue_t *flushq_tail = NULL;
121 static pthread_t queue_thread;
122 static int queue_thread_running = 1;
123 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
124 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
126 #if !HAVE_THREADSAFE_LIBRRD
127 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
130 static int do_shutdown = 0;
132 #if HAVE_THREADSAFE_LIBRRD
133 static int srrd_update (char *filename, char *template,
134 int argc, const char **argv)
138 optind = 0; /* bug in librrd? */
141 status = rrd_update_r (filename, template, argc, (void *) argv);
145 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
146 filename, rrd_get_error ());
150 } /* int srrd_update */
151 /* #endif HAVE_THREADSAFE_LIBRRD */
153 #else /* !HAVE_THREADSAFE_LIBRRD */
154 static int srrd_update (char *filename, char *template,
155 int argc, const char **argv)
162 assert (template == NULL);
165 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
166 if (new_argv == NULL)
168 ERROR ("rrdtool plugin: malloc failed.");
172 new_argv[0] = "update";
173 new_argv[1] = filename;
175 memcpy (new_argv + 2, argv, argc * sizeof (char *));
176 new_argv[new_argc] = NULL;
178 pthread_mutex_lock (&librrd_lock);
179 optind = 0; /* bug in librrd? */
182 status = rrd_update (new_argc, new_argv);
183 pthread_mutex_unlock (&librrd_lock);
187 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
188 filename, rrd_get_error ());
194 } /* int srrd_update */
195 #endif /* !HAVE_THREADSAFE_LIBRRD */
197 static int value_list_to_string (char *buffer, int buffer_len,
198 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 (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 ":%lf", 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 */
243 static int value_list_to_filename (char *buffer, int buffer_len,
244 const data_set_t __attribute__((unused)) *ds, const value_list_t *vl)
251 status = ssnprintf (buffer + offset, buffer_len - offset,
253 if ((status < 1) || (status >= buffer_len - offset))
258 status = ssnprintf (buffer + offset, buffer_len - offset,
260 if ((status < 1) || (status >= buffer_len - offset))
264 if (strlen (vl->plugin_instance) > 0)
265 status = ssnprintf (buffer + offset, buffer_len - offset,
266 "%s-%s/", vl->plugin, vl->plugin_instance);
268 status = ssnprintf (buffer + offset, buffer_len - offset,
270 if ((status < 1) || (status >= buffer_len - offset))
274 if (strlen (vl->type_instance) > 0)
275 status = ssnprintf (buffer + offset, buffer_len - offset,
276 "%s-%s.rrd", vl->type, vl->type_instance);
278 status = ssnprintf (buffer + offset, buffer_len - offset,
280 if ((status < 1) || (status >= buffer_len - offset))
285 } /* int value_list_to_filename */
287 static void *rrd_queue_thread (void __attribute__((unused)) *data)
289 struct timeval tv_next_update;
290 struct timeval tv_now;
292 gettimeofday (&tv_next_update, /* timezone = */ NULL);
296 rrd_queue_t *queue_entry;
297 rrd_cache_t *cache_entry;
306 pthread_mutex_lock (&queue_lock);
307 /* Wait for values to arrive */
310 struct timespec ts_wait;
312 while ((flushq_head == NULL) && (queue_head == NULL)
313 && (do_shutdown == 0))
314 pthread_cond_wait (&queue_cond, &queue_lock);
316 if ((flushq_head == NULL) && (queue_head == NULL))
319 /* Don't delay if there's something to flush */
320 if (flushq_head != NULL)
323 /* Don't delay if we're shutting down */
324 if (do_shutdown != 0)
327 /* Don't delay if no delay was configured. */
328 if (write_rate <= 0.0)
331 gettimeofday (&tv_now, /* timezone = */ NULL);
332 status = timeval_cmp (tv_next_update, tv_now, NULL);
333 /* We're good to go */
337 /* We're supposed to wait a bit with this update, so we'll
338 * wait for the next addition to the queue or to the end of
339 * the wait period - whichever comes first. */
340 ts_wait.tv_sec = tv_next_update.tv_sec;
341 ts_wait.tv_nsec = 1000 * tv_next_update.tv_usec;
343 status = pthread_cond_timedwait (&queue_cond, &queue_lock,
345 if (status == ETIMEDOUT)
349 /* XXX: If you need to lock both, cache_lock and queue_lock, at
350 * the same time, ALWAYS lock `cache_lock' first! */
352 /* We're in the shutdown phase */
353 if ((flushq_head == NULL) && (queue_head == NULL))
355 pthread_mutex_unlock (&queue_lock);
359 if (flushq_head != NULL)
361 /* Dequeue the first flush entry */
362 queue_entry = flushq_head;
363 if (flushq_head == flushq_tail)
364 flushq_head = flushq_tail = NULL;
366 flushq_head = flushq_head->next;
368 else /* if (queue_head != NULL) */
370 /* Dequeue the first regular entry */
371 queue_entry = queue_head;
372 if (queue_head == queue_tail)
373 queue_head = queue_tail = NULL;
375 queue_head = queue_head->next;
378 /* Unlock the queue again */
379 pthread_mutex_unlock (&queue_lock);
381 /* We now need the cache lock so the entry isn't updated while
382 * we make a copy of it's values */
383 pthread_mutex_lock (&cache_lock);
385 status = c_avl_get (cache, queue_entry->filename,
386 (void *) &cache_entry);
390 values = cache_entry->values;
391 values_num = cache_entry->values_num;
393 cache_entry->values = NULL;
394 cache_entry->values_num = 0;
395 cache_entry->flags = FLAG_NONE;
398 pthread_mutex_unlock (&cache_lock);
402 sfree (queue_entry->filename);
407 /* Update `tv_next_update' */
408 if (write_rate > 0.0)
410 gettimeofday (&tv_now, /* timezone = */ NULL);
411 tv_next_update.tv_sec = tv_now.tv_sec;
412 tv_next_update.tv_usec = tv_now.tv_usec
413 + ((suseconds_t) (1000000 * write_rate));
414 while (tv_next_update.tv_usec > 1000000)
416 tv_next_update.tv_sec++;
417 tv_next_update.tv_usec -= 1000000;
421 /* Write the values to the RRD-file */
422 srrd_update (queue_entry->filename, NULL,
423 values_num, (const char **)values);
424 DEBUG ("rrdtool plugin: queue thread: Wrote %i value%s to %s",
425 values_num, (values_num == 1) ? "" : "s",
426 queue_entry->filename);
428 for (i = 0; i < values_num; i++)
433 sfree (queue_entry->filename);
437 pthread_exit ((void *) 0);
439 } /* void *rrd_queue_thread */
441 static int rrd_queue_enqueue (const char *filename,
442 rrd_queue_t **head, rrd_queue_t **tail)
444 rrd_queue_t *queue_entry;
446 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
447 if (queue_entry == NULL)
450 queue_entry->filename = strdup (filename);
451 if (queue_entry->filename == NULL)
457 queue_entry->next = NULL;
459 pthread_mutex_lock (&queue_lock);
464 (*tail)->next = queue_entry;
467 pthread_cond_signal (&queue_cond);
468 pthread_mutex_unlock (&queue_lock);
471 } /* int rrd_queue_enqueue */
473 static int rrd_queue_dequeue (const char *filename,
474 rrd_queue_t **head, rrd_queue_t **tail)
479 pthread_mutex_lock (&queue_lock);
486 if (strcmp (this->filename, filename) == 0)
495 pthread_mutex_unlock (&queue_lock);
502 prev->next = this->next;
504 if (this->next == NULL)
507 pthread_mutex_unlock (&queue_lock);
509 sfree (this->filename);
513 } /* int rrd_queue_dequeue */
515 /* XXX: You must hold "cache_lock" when calling this function! */
516 static void rrd_cache_flush (cdtime_t timeout)
525 c_avl_iterator_t *iter;
528 DEBUG ("rrdtool plugin: Flushing cache, timeout = %.3f",
529 CDTIME_T_TO_DOUBLE (timeout));
532 timeout = TIME_T_TO_CDTIME_T (timeout);
534 /* Build a list of entries to be flushed */
535 iter = c_avl_get_iterator (cache);
536 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
538 if (rc->flags != FLAG_NONE)
540 /* timeout == 0 => flush everything */
541 else if ((timeout != 0)
542 && ((now - rc->first_value) < timeout))
544 else if (rc->values_num > 0)
548 status = rrd_queue_enqueue (key, &queue_head, &queue_tail);
550 rc->flags = FLAG_QUEUED;
552 else /* ancient and no values -> waste of memory */
554 char **tmp = (char **) realloc ((void *) keys,
555 (keys_num + 1) * sizeof (char *));
559 ERROR ("rrdtool plugin: "
560 "realloc failed: %s",
561 sstrerror (errno, errbuf,
563 c_avl_iterator_destroy (iter);
568 keys[keys_num] = key;
571 } /* while (c_avl_iterator_next) */
572 c_avl_iterator_destroy (iter);
574 for (i = 0; i < keys_num; i++)
576 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
578 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
582 assert (rc->values == NULL);
583 assert (rc->values_num == 0);
588 } /* for (i = 0..keys_num) */
592 cache_flush_last = now;
593 } /* void rrd_cache_flush */
595 static int rrd_cache_flush_identifier (cdtime_t timeout,
596 const char *identifier)
603 if (identifier == NULL)
605 rrd_cache_flush (timeout);
612 snprintf (key, sizeof (key), "%s.rrd",
615 snprintf (key, sizeof (key), "%s/%s.rrd",
616 datadir, identifier);
617 key[sizeof (key) - 1] = 0;
619 status = c_avl_get (cache, key, (void *) &rc);
622 INFO ("rrdtool plugin: rrd_cache_flush_identifier: "
623 "c_avl_get (%s) failed. Does that file really exist?",
628 if (rc->flags == FLAG_FLUSHQ)
632 else if (rc->flags == FLAG_QUEUED)
634 rrd_queue_dequeue (key, &queue_head, &queue_tail);
635 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
637 rc->flags = FLAG_FLUSHQ;
639 else if ((now - rc->first_value) < timeout)
643 else if (rc->values_num > 0)
645 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
647 rc->flags = FLAG_FLUSHQ;
651 } /* int rrd_cache_flush_identifier */
653 static int64_t rrd_get_random_variation (void)
656 cdtime_t ctm_timeout;
661 if (random_timeout <= 0)
664 /* Assure that "cache_timeout + random_variation" is never negative. */
665 if (random_timeout > cache_timeout)
667 INFO ("rrdtool plugin: Adjusting \"RandomTimeout\" to %.3f seconds.",
668 CDTIME_T_TO_DOUBLE (cache_timeout));
669 random_timeout = cache_timeout;
672 /* This seems a bit complicated, but "random_timeout" is likely larger than
673 * RAND_MAX, so we can't simply use modulo here. */
674 dbl_timeout = CDTIME_T_TO_DOUBLE (random_timeout);
675 rand_fact = ((double) random ())
676 / ((double) RAND_MAX);
677 negative = (_Bool) (random () % 2);
679 ctm_timeout = DOUBLE_TO_CDTIME_T (dbl_timeout * rand_fact);
681 ret = (int64_t) ctm_timeout;
686 } /* int64_t rrd_get_random_variation */
688 static int rrd_cache_insert (const char *filename,
689 const char *value, cdtime_t value_time)
691 rrd_cache_t *rc = NULL;
695 pthread_mutex_lock (&cache_lock);
697 /* This shouldn't happen, but it did happen at least once, so we'll be
701 pthread_mutex_unlock (&cache_lock);
702 WARNING ("rrdtool plugin: cache == NULL.");
706 c_avl_get (cache, filename, (void *) &rc);
710 rc = malloc (sizeof (*rc));
717 rc->random_variation = rrd_get_random_variation ();
718 rc->flags = FLAG_NONE;
722 if (rc->last_value >= value_time)
724 pthread_mutex_unlock (&cache_lock);
725 DEBUG ("rrdtool plugin: (rc->last_value = %"PRIu64") "
726 ">= (value_time = %"PRIu64")",
727 rc->last_value, value_time);
731 values_new = (char **) realloc ((void *) rc->values,
732 (rc->values_num + 1) * sizeof (char *));
733 if (values_new == NULL)
736 void *cache_key = NULL;
738 sstrerror (errno, errbuf, sizeof (errbuf));
740 c_avl_remove (cache, filename, &cache_key, NULL);
741 pthread_mutex_unlock (&cache_lock);
743 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
750 rc->values = values_new;
752 rc->values[rc->values_num] = strdup (value);
753 if (rc->values[rc->values_num] != NULL)
756 if (rc->values_num == 1)
757 rc->first_value = value_time;
758 rc->last_value = value_time;
760 /* Insert if this is the first value */
763 void *cache_key = strdup (filename);
765 if (cache_key == NULL)
768 sstrerror (errno, errbuf, sizeof (errbuf));
770 pthread_mutex_unlock (&cache_lock);
772 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
774 sfree (rc->values[0]);
780 c_avl_insert (cache, cache_key, rc);
783 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
784 "values_num = %i; age = %.3f;",
785 filename, rc->values_num,
786 CDTIME_T_TO_DOUBLE (rc->last_value - rc->first_value));
788 if ((rc->last_value - rc->first_value) >= (cache_timeout + rc->random_variation))
790 /* XXX: If you need to lock both, cache_lock and queue_lock, at
791 * the same time, ALWAYS lock `cache_lock' first! */
792 if (rc->flags == FLAG_NONE)
796 status = rrd_queue_enqueue (filename, &queue_head, &queue_tail);
798 rc->flags = FLAG_QUEUED;
800 rc->random_variation = rrd_get_random_variation ();
804 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
808 if ((cache_timeout > 0) &&
809 ((cdtime () - cache_flush_last) > cache_flush_timeout))
810 rrd_cache_flush (cache_flush_timeout);
812 pthread_mutex_unlock (&cache_lock);
815 } /* int rrd_cache_insert */
817 static int rrd_cache_destroy (void) /* {{{ */
824 pthread_mutex_lock (&cache_lock);
828 pthread_mutex_unlock (&cache_lock);
832 while (c_avl_pick (cache, &key, &value) == 0)
843 if (rc->values_num > 0)
846 for (i = 0; i < rc->values_num; i++)
847 sfree (rc->values[i]);
852 c_avl_destroy (cache);
857 INFO ("rrdtool plugin: %i cache %s had values when destroying the cache.",
858 non_empty, (non_empty == 1) ? "entry" : "entries");
862 DEBUG ("rrdtool plugin: No values have been lost "
863 "when destroying the cache.");
866 pthread_mutex_unlock (&cache_lock);
868 } /* }}} int rrd_cache_destroy */
870 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
872 int a = *((int *) a_ptr);
873 int b = *((int *) b_ptr);
881 } /* int rrd_compare_numeric */
883 static int rrd_write (const data_set_t *ds, const value_list_t *vl,
884 user_data_t __attribute__((unused)) *user_data)
894 if (0 != strcmp (ds->type, vl->type)) {
895 ERROR ("rrdtool plugin: DS type does not match value list type");
899 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
902 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
905 if (stat (filename, &statbuf) == -1)
909 status = cu_rrd_create_file (filename,
910 ds, vl, &rrdcreate_config);
917 ERROR ("stat(%s) failed: %s", filename,
918 sstrerror (errno, errbuf,
923 else if (!S_ISREG (statbuf.st_mode))
925 ERROR ("stat(%s): Not a regular file!",
930 status = rrd_cache_insert (filename, values, vl->time);
933 } /* int rrd_write */
935 static int rrd_flush (cdtime_t timeout, const char *identifier,
936 __attribute__((unused)) user_data_t *user_data)
938 pthread_mutex_lock (&cache_lock);
941 pthread_mutex_unlock (&cache_lock);
945 rrd_cache_flush_identifier (timeout, identifier);
947 pthread_mutex_unlock (&cache_lock);
949 } /* int rrd_flush */
951 static int rrd_config (const char *key, const char *value)
953 if (strcasecmp ("CacheTimeout", key) == 0)
955 double tmp = atof (value);
958 fprintf (stderr, "rrdtool: `CacheTimeout' must "
959 "be greater than 0.\n");
960 ERROR ("rrdtool: `CacheTimeout' must "
961 "be greater than 0.\n");
964 cache_timeout = DOUBLE_TO_CDTIME_T (tmp);
966 else if (strcasecmp ("CacheFlush", key) == 0)
968 int tmp = atoi (value);
971 fprintf (stderr, "rrdtool: `CacheFlush' must "
972 "be greater than 0.\n");
973 ERROR ("rrdtool: `CacheFlush' must "
974 "be greater than 0.\n");
977 cache_flush_timeout = tmp;
979 else if (strcasecmp ("DataDir", key) == 0)
983 datadir = strdup (value);
986 int len = strlen (datadir);
987 while ((len > 0) && (datadir[len - 1] == '/'))
999 else if (strcasecmp ("StepSize", key) == 0)
1001 unsigned long temp = strtoul (value, NULL, 0);
1003 rrdcreate_config.stepsize = temp;
1005 else if (strcasecmp ("HeartBeat", key) == 0)
1007 int temp = atoi (value);
1009 rrdcreate_config.heartbeat = temp;
1011 else if (strcasecmp ("RRARows", key) == 0)
1013 int tmp = atoi (value);
1016 fprintf (stderr, "rrdtool: `RRARows' must "
1017 "be greater than 0.\n");
1018 ERROR ("rrdtool: `RRARows' must "
1019 "be greater than 0.\n");
1022 rrdcreate_config.rrarows = tmp;
1024 else if (strcasecmp ("RRATimespan", key) == 0)
1026 char *saveptr = NULL;
1032 value_copy = strdup (value);
1033 if (value_copy == NULL)
1037 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1041 tmp_alloc = realloc (rrdcreate_config.timespans,
1042 sizeof (int) * (rrdcreate_config.timespans_num + 1));
1043 if (tmp_alloc == NULL)
1045 fprintf (stderr, "rrdtool: realloc failed.\n");
1046 ERROR ("rrdtool: realloc failed.\n");
1050 rrdcreate_config.timespans = tmp_alloc;
1051 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = atoi (ptr);
1052 if (rrdcreate_config.timespans[rrdcreate_config.timespans_num] != 0)
1053 rrdcreate_config.timespans_num++;
1054 } /* while (strtok_r) */
1056 qsort (/* base = */ rrdcreate_config.timespans,
1057 /* nmemb = */ rrdcreate_config.timespans_num,
1058 /* size = */ sizeof (rrdcreate_config.timespans[0]),
1059 /* compar = */ rrd_compare_numeric);
1063 else if (strcasecmp ("XFF", key) == 0)
1065 double tmp = atof (value);
1066 if ((tmp < 0.0) || (tmp >= 1.0))
1068 fprintf (stderr, "rrdtool: `XFF' must "
1069 "be in the range 0 to 1 (exclusive).");
1070 ERROR ("rrdtool: `XFF' must "
1071 "be in the range 0 to 1 (exclusive).");
1074 rrdcreate_config.xff = tmp;
1076 else if (strcasecmp ("WritesPerSecond", key) == 0)
1078 double wps = atof (value);
1082 fprintf (stderr, "rrdtool: `WritesPerSecond' must be "
1083 "greater than or equal to zero.");
1086 else if (wps == 0.0)
1092 write_rate = 1.0 / wps;
1095 else if (strcasecmp ("RandomTimeout", key) == 0)
1102 fprintf (stderr, "rrdtool: `RandomTimeout' must "
1103 "be greater than or equal to zero.\n");
1104 ERROR ("rrdtool: `RandomTimeout' must "
1105 "be greater then or equal to zero.");
1109 random_timeout = DOUBLE_TO_CDTIME_T (tmp);
1117 } /* int rrd_config */
1119 static int rrd_shutdown (void)
1121 pthread_mutex_lock (&cache_lock);
1122 rrd_cache_flush (0);
1123 pthread_mutex_unlock (&cache_lock);
1125 pthread_mutex_lock (&queue_lock);
1127 pthread_cond_signal (&queue_cond);
1128 pthread_mutex_unlock (&queue_lock);
1130 if ((queue_thread_running != 0)
1131 && ((queue_head != NULL) || (flushq_head != NULL)))
1133 INFO ("rrdtool plugin: Shutting down the queue thread. "
1134 "This may take a while.");
1136 else if (queue_thread_running != 0)
1138 INFO ("rrdtool plugin: Shutting down the queue thread.");
1141 /* Wait for all the values to be written to disk before returning. */
1142 if (queue_thread_running != 0)
1144 pthread_join (queue_thread, NULL);
1145 memset (&queue_thread, 0, sizeof (queue_thread));
1146 queue_thread_running = 0;
1147 DEBUG ("rrdtool plugin: queue_thread exited.");
1150 rrd_cache_destroy ();
1153 } /* int rrd_shutdown */
1155 static int rrd_init (void)
1157 static int init_once = 0;
1164 if (rrdcreate_config.heartbeat <= 0)
1165 rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize;
1167 /* Set the cache up */
1168 pthread_mutex_lock (&cache_lock);
1170 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1173 ERROR ("rrdtool plugin: c_avl_create failed.");
1177 cache_flush_last = cdtime ();
1178 if (cache_timeout == 0)
1180 cache_flush_timeout = 0;
1182 else if (cache_flush_timeout < cache_timeout)
1183 cache_flush_timeout = 10 * cache_timeout;
1185 pthread_mutex_unlock (&cache_lock);
1187 status = plugin_thread_create (&queue_thread, /* attr = */ NULL,
1188 rrd_queue_thread, /* args = */ NULL);
1191 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1194 queue_thread_running = 1;
1196 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %lu;"
1197 " heartbeat = %i; rrarows = %i; xff = %lf;",
1198 (datadir == NULL) ? "(null)" : datadir,
1199 rrdcreate_config.stepsize,
1200 rrdcreate_config.heartbeat,
1201 rrdcreate_config.rrarows,
1202 rrdcreate_config.xff);
1205 } /* int rrd_init */
1207 void module_register (void)
1209 plugin_register_config ("rrdtool", rrd_config,
1210 config_keys, config_keys_num);
1211 plugin_register_init ("rrdtool", rrd_init);
1212 plugin_register_write ("rrdtool", rrd_write, /* user_data = */ NULL);
1213 plugin_register_flush ("rrdtool", rrd_flush, /* user_data = */ NULL);
1214 plugin_register_shutdown ("rrdtool", rrd_shutdown);