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 (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 ":%lf", 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 */
247 static int value_list_to_filename (char *buffer, int buffer_len,
248 const data_set_t __attribute__((unused)) *ds, const value_list_t *vl)
255 status = ssnprintf (buffer + offset, buffer_len - offset,
257 if ((status < 1) || (status >= buffer_len - offset))
262 status = ssnprintf (buffer + offset, buffer_len - offset,
264 if ((status < 1) || (status >= buffer_len - offset))
268 if (strlen (vl->plugin_instance) > 0)
269 status = ssnprintf (buffer + offset, buffer_len - offset,
270 "%s-%s/", vl->plugin, vl->plugin_instance);
272 status = ssnprintf (buffer + offset, buffer_len - offset,
274 if ((status < 1) || (status >= buffer_len - offset))
278 if (strlen (vl->type_instance) > 0)
279 status = ssnprintf (buffer + offset, buffer_len - offset,
280 "%s-%s.rrd", vl->type, vl->type_instance);
282 status = ssnprintf (buffer + offset, buffer_len - offset,
284 if ((status < 1) || (status >= buffer_len - offset))
289 } /* int value_list_to_filename */
291 static void *rrd_queue_thread (void __attribute__((unused)) *data)
293 struct timeval tv_next_update;
294 struct timeval tv_now;
296 gettimeofday (&tv_next_update, /* timezone = */ NULL);
300 rrd_queue_t *queue_entry;
301 rrd_cache_t *cache_entry;
310 pthread_mutex_lock (&queue_lock);
311 /* Wait for values to arrive */
314 struct timespec ts_wait;
316 while ((flushq_head == NULL) && (queue_head == NULL)
317 && (do_shutdown == 0))
318 pthread_cond_wait (&queue_cond, &queue_lock);
320 if ((flushq_head == NULL) && (queue_head == NULL))
323 /* Don't delay if there's something to flush */
324 if (flushq_head != NULL)
327 /* Don't delay if we're shutting down */
328 if (do_shutdown != 0)
331 /* Don't delay if no delay was configured. */
332 if (write_rate <= 0.0)
335 gettimeofday (&tv_now, /* timezone = */ NULL);
336 status = timeval_cmp (tv_next_update, tv_now, NULL);
337 /* We're good to go */
341 /* We're supposed to wait a bit with this update, so we'll
342 * wait for the next addition to the queue or to the end of
343 * the wait period - whichever comes first. */
344 ts_wait.tv_sec = tv_next_update.tv_sec;
345 ts_wait.tv_nsec = 1000 * tv_next_update.tv_usec;
347 status = pthread_cond_timedwait (&queue_cond, &queue_lock,
349 if (status == ETIMEDOUT)
353 /* XXX: If you need to lock both, cache_lock and queue_lock, at
354 * the same time, ALWAYS lock `cache_lock' first! */
356 /* We're in the shutdown phase */
357 if ((flushq_head == NULL) && (queue_head == NULL))
359 pthread_mutex_unlock (&queue_lock);
363 if (flushq_head != NULL)
365 /* Dequeue the first flush entry */
366 queue_entry = flushq_head;
367 if (flushq_head == flushq_tail)
368 flushq_head = flushq_tail = NULL;
370 flushq_head = flushq_head->next;
372 else /* if (queue_head != NULL) */
374 /* Dequeue the first regular entry */
375 queue_entry = queue_head;
376 if (queue_head == queue_tail)
377 queue_head = queue_tail = NULL;
379 queue_head = queue_head->next;
382 /* Unlock the queue again */
383 pthread_mutex_unlock (&queue_lock);
385 /* We now need the cache lock so the entry isn't updated while
386 * we make a copy of it's values */
387 pthread_mutex_lock (&cache_lock);
389 status = c_avl_get (cache, queue_entry->filename,
390 (void *) &cache_entry);
394 values = cache_entry->values;
395 values_num = cache_entry->values_num;
397 cache_entry->values = NULL;
398 cache_entry->values_num = 0;
399 cache_entry->flags = FLAG_NONE;
402 pthread_mutex_unlock (&cache_lock);
406 sfree (queue_entry->filename);
411 /* Update `tv_next_update' */
412 if (write_rate > 0.0)
414 gettimeofday (&tv_now, /* timezone = */ NULL);
415 tv_next_update.tv_sec = tv_now.tv_sec;
416 tv_next_update.tv_usec = tv_now.tv_usec
417 + ((suseconds_t) (1000000 * write_rate));
418 while (tv_next_update.tv_usec > 1000000)
420 tv_next_update.tv_sec++;
421 tv_next_update.tv_usec -= 1000000;
425 /* Write the values to the RRD-file */
426 srrd_update (queue_entry->filename, NULL,
427 values_num, (const char **)values);
428 DEBUG ("rrdtool plugin: queue thread: Wrote %i value%s to %s",
429 values_num, (values_num == 1) ? "" : "s",
430 queue_entry->filename);
432 for (i = 0; i < values_num; i++)
437 sfree (queue_entry->filename);
441 pthread_exit ((void *) 0);
443 } /* void *rrd_queue_thread */
445 static int rrd_queue_enqueue (const char *filename,
446 rrd_queue_t **head, rrd_queue_t **tail)
448 rrd_queue_t *queue_entry;
450 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
451 if (queue_entry == NULL)
454 queue_entry->filename = strdup (filename);
455 if (queue_entry->filename == NULL)
461 queue_entry->next = NULL;
463 pthread_mutex_lock (&queue_lock);
468 (*tail)->next = queue_entry;
471 pthread_cond_signal (&queue_cond);
472 pthread_mutex_unlock (&queue_lock);
475 } /* int rrd_queue_enqueue */
477 static int rrd_queue_dequeue (const char *filename,
478 rrd_queue_t **head, rrd_queue_t **tail)
483 pthread_mutex_lock (&queue_lock);
490 if (strcmp (this->filename, filename) == 0)
499 pthread_mutex_unlock (&queue_lock);
506 prev->next = this->next;
508 if (this->next == NULL)
511 pthread_mutex_unlock (&queue_lock);
513 sfree (this->filename);
517 } /* int rrd_queue_dequeue */
519 /* XXX: You must hold "cache_lock" when calling this function! */
520 static void rrd_cache_flush (cdtime_t timeout)
529 c_avl_iterator_t *iter;
532 DEBUG ("rrdtool plugin: Flushing cache, timeout = %.3f",
533 CDTIME_T_TO_DOUBLE (timeout));
536 timeout = TIME_T_TO_CDTIME_T (timeout);
538 /* Build a list of entries to be flushed */
539 iter = c_avl_get_iterator (cache);
540 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
542 if (rc->flags != FLAG_NONE)
544 /* timeout == 0 => flush everything */
545 else if ((timeout != 0)
546 && ((now - rc->first_value) < timeout))
548 else if (rc->values_num > 0)
552 status = rrd_queue_enqueue (key, &queue_head, &queue_tail);
554 rc->flags = FLAG_QUEUED;
556 else /* ancient and no values -> waste of memory */
558 char **tmp = (char **) realloc ((void *) keys,
559 (keys_num + 1) * sizeof (char *));
563 ERROR ("rrdtool plugin: "
564 "realloc failed: %s",
565 sstrerror (errno, errbuf,
567 c_avl_iterator_destroy (iter);
572 keys[keys_num] = key;
575 } /* while (c_avl_iterator_next) */
576 c_avl_iterator_destroy (iter);
578 for (i = 0; i < keys_num; i++)
580 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
582 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
586 assert (rc->values == NULL);
587 assert (rc->values_num == 0);
592 } /* for (i = 0..keys_num) */
596 cache_flush_last = now;
597 } /* void rrd_cache_flush */
599 static int rrd_cache_flush_identifier (cdtime_t timeout,
600 const char *identifier)
607 if (identifier == NULL)
609 rrd_cache_flush (timeout);
616 snprintf (key, sizeof (key), "%s.rrd",
619 snprintf (key, sizeof (key), "%s/%s.rrd",
620 datadir, identifier);
621 key[sizeof (key) - 1] = 0;
623 status = c_avl_get (cache, key, (void *) &rc);
626 INFO ("rrdtool plugin: rrd_cache_flush_identifier: "
627 "c_avl_get (%s) failed. Does that file really exist?",
632 if (rc->flags == FLAG_FLUSHQ)
636 else if (rc->flags == FLAG_QUEUED)
638 rrd_queue_dequeue (key, &queue_head, &queue_tail);
639 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
641 rc->flags = FLAG_FLUSHQ;
643 else if ((now - rc->first_value) < timeout)
647 else if (rc->values_num > 0)
649 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
651 rc->flags = FLAG_FLUSHQ;
655 } /* int rrd_cache_flush_identifier */
657 static int64_t rrd_get_random_variation (void)
662 if (random_timeout <= 0)
665 /* Assure that "cache_timeout + random_variation" is never negative. */
666 if (random_timeout > cache_timeout)
668 INFO ("rrdtool plugin: Adjusting \"RandomTimeout\" to %.3f seconds.",
669 CDTIME_T_TO_DOUBLE (cache_timeout));
670 random_timeout = cache_timeout;
673 max = (long) (random_timeout / 2);
674 min = max - ((long) random_timeout);
676 return ((int64_t) cdrand_range (min, max));
677 } /* int64_t rrd_get_random_variation */
679 static int rrd_cache_insert (const char *filename,
680 const char *value, cdtime_t value_time)
682 rrd_cache_t *rc = NULL;
686 pthread_mutex_lock (&cache_lock);
688 /* This shouldn't happen, but it did happen at least once, so we'll be
692 pthread_mutex_unlock (&cache_lock);
693 WARNING ("rrdtool plugin: cache == NULL.");
697 c_avl_get (cache, filename, (void *) &rc);
701 rc = malloc (sizeof (*rc));
708 rc->random_variation = rrd_get_random_variation ();
709 rc->flags = FLAG_NONE;
713 if (rc->last_value >= value_time)
715 pthread_mutex_unlock (&cache_lock);
716 DEBUG ("rrdtool plugin: (rc->last_value = %"PRIu64") "
717 ">= (value_time = %"PRIu64")",
718 rc->last_value, value_time);
722 values_new = (char **) realloc ((void *) rc->values,
723 (rc->values_num + 1) * sizeof (char *));
724 if (values_new == NULL)
727 void *cache_key = NULL;
729 sstrerror (errno, errbuf, sizeof (errbuf));
731 c_avl_remove (cache, filename, &cache_key, NULL);
732 pthread_mutex_unlock (&cache_lock);
734 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
741 rc->values = values_new;
743 rc->values[rc->values_num] = strdup (value);
744 if (rc->values[rc->values_num] != NULL)
747 if (rc->values_num == 1)
748 rc->first_value = value_time;
749 rc->last_value = value_time;
751 /* Insert if this is the first value */
754 void *cache_key = strdup (filename);
756 if (cache_key == NULL)
759 sstrerror (errno, errbuf, sizeof (errbuf));
761 pthread_mutex_unlock (&cache_lock);
763 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
765 sfree (rc->values[0]);
771 c_avl_insert (cache, cache_key, rc);
774 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
775 "values_num = %i; age = %.3f;",
776 filename, rc->values_num,
777 CDTIME_T_TO_DOUBLE (rc->last_value - rc->first_value));
779 if ((rc->last_value - rc->first_value) >= (cache_timeout + rc->random_variation))
781 /* XXX: If you need to lock both, cache_lock and queue_lock, at
782 * the same time, ALWAYS lock `cache_lock' first! */
783 if (rc->flags == FLAG_NONE)
787 status = rrd_queue_enqueue (filename, &queue_head, &queue_tail);
789 rc->flags = FLAG_QUEUED;
791 rc->random_variation = rrd_get_random_variation ();
795 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
799 if ((cache_timeout > 0) &&
800 ((cdtime () - cache_flush_last) > cache_flush_timeout))
801 rrd_cache_flush (cache_flush_timeout);
803 pthread_mutex_unlock (&cache_lock);
806 } /* int rrd_cache_insert */
808 static int rrd_cache_destroy (void) /* {{{ */
815 pthread_mutex_lock (&cache_lock);
819 pthread_mutex_unlock (&cache_lock);
823 while (c_avl_pick (cache, &key, &value) == 0)
834 if (rc->values_num > 0)
837 for (i = 0; i < rc->values_num; i++)
838 sfree (rc->values[i]);
843 c_avl_destroy (cache);
848 INFO ("rrdtool plugin: %i cache %s had values when destroying the cache.",
849 non_empty, (non_empty == 1) ? "entry" : "entries");
853 DEBUG ("rrdtool plugin: No values have been lost "
854 "when destroying the cache.");
857 pthread_mutex_unlock (&cache_lock);
859 } /* }}} int rrd_cache_destroy */
861 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
863 int a = *((int *) a_ptr);
864 int b = *((int *) b_ptr);
872 } /* int rrd_compare_numeric */
874 static int rrd_write (const data_set_t *ds, const value_list_t *vl,
875 user_data_t __attribute__((unused)) *user_data)
885 if (0 != strcmp (ds->type, vl->type)) {
886 ERROR ("rrdtool plugin: DS type does not match value list type");
890 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
893 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
896 if (stat (filename, &statbuf) == -1)
900 status = cu_rrd_create_file (filename,
901 ds, vl, &rrdcreate_config);
904 else if (rrdcreate_config.async)
910 ERROR ("stat(%s) failed: %s", filename,
911 sstrerror (errno, errbuf,
916 else if (!S_ISREG (statbuf.st_mode))
918 ERROR ("stat(%s): Not a regular file!",
923 status = rrd_cache_insert (filename, values, vl->time);
926 } /* int rrd_write */
928 static int rrd_flush (cdtime_t timeout, const char *identifier,
929 __attribute__((unused)) user_data_t *user_data)
931 pthread_mutex_lock (&cache_lock);
934 pthread_mutex_unlock (&cache_lock);
938 rrd_cache_flush_identifier (timeout, identifier);
940 pthread_mutex_unlock (&cache_lock);
942 } /* int rrd_flush */
944 static int rrd_config (const char *key, const char *value)
946 if (strcasecmp ("CacheTimeout", key) == 0)
948 double tmp = atof (value);
951 fprintf (stderr, "rrdtool: `CacheTimeout' must "
952 "be greater than 0.\n");
953 ERROR ("rrdtool: `CacheTimeout' must "
954 "be greater than 0.\n");
957 cache_timeout = DOUBLE_TO_CDTIME_T (tmp);
959 else if (strcasecmp ("CacheFlush", key) == 0)
961 int tmp = atoi (value);
964 fprintf (stderr, "rrdtool: `CacheFlush' must "
965 "be greater than 0.\n");
966 ERROR ("rrdtool: `CacheFlush' must "
967 "be greater than 0.\n");
970 cache_flush_timeout = tmp;
972 else if (strcasecmp ("DataDir", key) == 0)
976 datadir = strdup (value);
979 int len = strlen (datadir);
980 while ((len > 0) && (datadir[len - 1] == '/'))
992 else if (strcasecmp ("StepSize", key) == 0)
994 unsigned long temp = strtoul (value, NULL, 0);
996 rrdcreate_config.stepsize = temp;
998 else if (strcasecmp ("HeartBeat", key) == 0)
1000 int temp = atoi (value);
1002 rrdcreate_config.heartbeat = temp;
1004 else if (strcasecmp ("CreateFilesAsync", key) == 0)
1006 if (IS_TRUE (value))
1007 rrdcreate_config.async = 1;
1009 rrdcreate_config.async = 0;
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);