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[] =
88 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
90 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
91 * is zero a default, depending on the `interval' member of the value list is
93 static char *datadir = NULL;
94 static double write_rate = 0.0;
95 static rrdcreate_config_t rrdcreate_config =
102 /* timespans = */ NULL,
103 /* timespans_num = */ 0,
105 /* consolidation_functions = */ NULL,
106 /* consolidation_functions_num = */ 0,
111 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
112 * ALWAYS lock `cache_lock' first! */
113 static cdtime_t cache_timeout = 0;
114 static cdtime_t cache_flush_timeout = 0;
115 static cdtime_t random_timeout = TIME_T_TO_CDTIME_T (1);
116 static cdtime_t cache_flush_last;
117 static c_avl_tree_t *cache = NULL;
118 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
120 static rrd_queue_t *queue_head = NULL;
121 static rrd_queue_t *queue_tail = NULL;
122 static rrd_queue_t *flushq_head = NULL;
123 static rrd_queue_t *flushq_tail = NULL;
124 static pthread_t queue_thread;
125 static int queue_thread_running = 1;
126 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
127 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
129 #if !HAVE_THREADSAFE_LIBRRD
130 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
133 static int do_shutdown = 0;
135 #if HAVE_THREADSAFE_LIBRRD
136 static int srrd_update (char *filename, char *template,
137 int argc, const char **argv)
141 optind = 0; /* bug in librrd? */
144 status = rrd_update_r (filename, template, argc, (void *) argv);
148 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
149 filename, rrd_get_error ());
153 } /* int srrd_update */
154 /* #endif HAVE_THREADSAFE_LIBRRD */
156 #else /* !HAVE_THREADSAFE_LIBRRD */
157 static int srrd_update (char *filename, char *template,
158 int argc, const char **argv)
165 assert (template == NULL);
168 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
169 if (new_argv == NULL)
171 ERROR ("rrdtool plugin: malloc failed.");
175 new_argv[0] = "update";
176 new_argv[1] = filename;
178 memcpy (new_argv + 2, argv, argc * sizeof (char *));
179 new_argv[new_argc] = NULL;
181 pthread_mutex_lock (&librrd_lock);
182 optind = 0; /* bug in librrd? */
185 status = rrd_update (new_argc, new_argv);
186 pthread_mutex_unlock (&librrd_lock);
190 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
191 filename, rrd_get_error ());
197 } /* int srrd_update */
198 #endif /* !HAVE_THREADSAFE_LIBRRD */
200 static int value_list_to_string (char *buffer, int buffer_len,
201 const data_set_t *ds, const value_list_t *vl)
208 memset (buffer, '\0', buffer_len);
210 tt = CDTIME_T_TO_TIME_T (vl->time);
211 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) tt);
212 if ((status < 1) || (status >= buffer_len))
216 for (i = 0; i < ds->ds_num; i++)
218 if ((ds->ds[i].type != DS_TYPE_COUNTER)
219 && (ds->ds[i].type != DS_TYPE_GAUGE)
220 && (ds->ds[i].type != DS_TYPE_DERIVE)
221 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
224 if (ds->ds[i].type == DS_TYPE_COUNTER)
225 status = ssnprintf (buffer + offset, buffer_len - offset,
226 ":%llu", vl->values[i].counter);
227 else if (ds->ds[i].type == DS_TYPE_GAUGE)
228 status = ssnprintf (buffer + offset, buffer_len - offset,
229 ":%lf", vl->values[i].gauge);
230 else if (ds->ds[i].type == DS_TYPE_DERIVE)
231 status = ssnprintf (buffer + offset, buffer_len - offset,
232 ":%"PRIi64, vl->values[i].derive);
233 else /*if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */
234 status = ssnprintf (buffer + offset, buffer_len - offset,
235 ":%"PRIu64, vl->values[i].absolute);
237 if ((status < 1) || (status >= (buffer_len - offset)))
241 } /* for ds->ds_num */
244 } /* int value_list_to_string */
246 static int value_list_to_filename (char *buffer, int buffer_len,
247 const data_set_t __attribute__((unused)) *ds, const value_list_t *vl)
254 status = ssnprintf (buffer + offset, buffer_len - offset,
256 if ((status < 1) || (status >= buffer_len - offset))
261 status = ssnprintf (buffer + offset, buffer_len - offset,
263 if ((status < 1) || (status >= buffer_len - offset))
267 if (strlen (vl->plugin_instance) > 0)
268 status = ssnprintf (buffer + offset, buffer_len - offset,
269 "%s-%s/", vl->plugin, vl->plugin_instance);
271 status = ssnprintf (buffer + offset, buffer_len - offset,
273 if ((status < 1) || (status >= buffer_len - offset))
277 if (strlen (vl->type_instance) > 0)
278 status = ssnprintf (buffer + offset, buffer_len - offset,
279 "%s-%s.rrd", vl->type, vl->type_instance);
281 status = ssnprintf (buffer + offset, buffer_len - offset,
283 if ((status < 1) || (status >= buffer_len - offset))
288 } /* int value_list_to_filename */
290 static void *rrd_queue_thread (void __attribute__((unused)) *data)
292 struct timeval tv_next_update;
293 struct timeval tv_now;
295 gettimeofday (&tv_next_update, /* timezone = */ NULL);
299 rrd_queue_t *queue_entry;
300 rrd_cache_t *cache_entry;
309 pthread_mutex_lock (&queue_lock);
310 /* Wait for values to arrive */
313 struct timespec ts_wait;
315 while ((flushq_head == NULL) && (queue_head == NULL)
316 && (do_shutdown == 0))
317 pthread_cond_wait (&queue_cond, &queue_lock);
319 if ((flushq_head == NULL) && (queue_head == NULL))
322 /* Don't delay if there's something to flush */
323 if (flushq_head != NULL)
326 /* Don't delay if we're shutting down */
327 if (do_shutdown != 0)
330 /* Don't delay if no delay was configured. */
331 if (write_rate <= 0.0)
334 gettimeofday (&tv_now, /* timezone = */ NULL);
335 status = timeval_cmp (tv_next_update, tv_now, NULL);
336 /* We're good to go */
340 /* We're supposed to wait a bit with this update, so we'll
341 * wait for the next addition to the queue or to the end of
342 * the wait period - whichever comes first. */
343 ts_wait.tv_sec = tv_next_update.tv_sec;
344 ts_wait.tv_nsec = 1000 * tv_next_update.tv_usec;
346 status = pthread_cond_timedwait (&queue_cond, &queue_lock,
348 if (status == ETIMEDOUT)
352 /* XXX: If you need to lock both, cache_lock and queue_lock, at
353 * the same time, ALWAYS lock `cache_lock' first! */
355 /* We're in the shutdown phase */
356 if ((flushq_head == NULL) && (queue_head == NULL))
358 pthread_mutex_unlock (&queue_lock);
362 if (flushq_head != NULL)
364 /* Dequeue the first flush entry */
365 queue_entry = flushq_head;
366 if (flushq_head == flushq_tail)
367 flushq_head = flushq_tail = NULL;
369 flushq_head = flushq_head->next;
371 else /* if (queue_head != NULL) */
373 /* Dequeue the first regular entry */
374 queue_entry = queue_head;
375 if (queue_head == queue_tail)
376 queue_head = queue_tail = NULL;
378 queue_head = queue_head->next;
381 /* Unlock the queue again */
382 pthread_mutex_unlock (&queue_lock);
384 /* We now need the cache lock so the entry isn't updated while
385 * we make a copy of it's values */
386 pthread_mutex_lock (&cache_lock);
388 status = c_avl_get (cache, queue_entry->filename,
389 (void *) &cache_entry);
393 values = cache_entry->values;
394 values_num = cache_entry->values_num;
396 cache_entry->values = NULL;
397 cache_entry->values_num = 0;
398 cache_entry->flags = FLAG_NONE;
401 pthread_mutex_unlock (&cache_lock);
405 sfree (queue_entry->filename);
410 /* Update `tv_next_update' */
411 if (write_rate > 0.0)
413 gettimeofday (&tv_now, /* timezone = */ NULL);
414 tv_next_update.tv_sec = tv_now.tv_sec;
415 tv_next_update.tv_usec = tv_now.tv_usec
416 + ((suseconds_t) (1000000 * write_rate));
417 while (tv_next_update.tv_usec > 1000000)
419 tv_next_update.tv_sec++;
420 tv_next_update.tv_usec -= 1000000;
424 /* Write the values to the RRD-file */
425 srrd_update (queue_entry->filename, NULL,
426 values_num, (const char **)values);
427 DEBUG ("rrdtool plugin: queue thread: Wrote %i value%s to %s",
428 values_num, (values_num == 1) ? "" : "s",
429 queue_entry->filename);
431 for (i = 0; i < values_num; i++)
436 sfree (queue_entry->filename);
440 pthread_exit ((void *) 0);
442 } /* void *rrd_queue_thread */
444 static int rrd_queue_enqueue (const char *filename,
445 rrd_queue_t **head, rrd_queue_t **tail)
447 rrd_queue_t *queue_entry;
449 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
450 if (queue_entry == NULL)
453 queue_entry->filename = strdup (filename);
454 if (queue_entry->filename == NULL)
460 queue_entry->next = NULL;
462 pthread_mutex_lock (&queue_lock);
467 (*tail)->next = queue_entry;
470 pthread_cond_signal (&queue_cond);
471 pthread_mutex_unlock (&queue_lock);
474 } /* int rrd_queue_enqueue */
476 static int rrd_queue_dequeue (const char *filename,
477 rrd_queue_t **head, rrd_queue_t **tail)
482 pthread_mutex_lock (&queue_lock);
489 if (strcmp (this->filename, filename) == 0)
498 pthread_mutex_unlock (&queue_lock);
505 prev->next = this->next;
507 if (this->next == NULL)
510 pthread_mutex_unlock (&queue_lock);
512 sfree (this->filename);
516 } /* int rrd_queue_dequeue */
518 /* XXX: You must hold "cache_lock" when calling this function! */
519 static void rrd_cache_flush (cdtime_t timeout)
528 c_avl_iterator_t *iter;
531 DEBUG ("rrdtool plugin: Flushing cache, timeout = %.3f",
532 CDTIME_T_TO_DOUBLE (timeout));
535 timeout = TIME_T_TO_CDTIME_T (timeout);
537 /* Build a list of entries to be flushed */
538 iter = c_avl_get_iterator (cache);
539 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
541 if (rc->flags != FLAG_NONE)
543 /* timeout == 0 => flush everything */
544 else if ((timeout != 0)
545 && ((now - rc->first_value) < timeout))
547 else if (rc->values_num > 0)
551 status = rrd_queue_enqueue (key, &queue_head, &queue_tail);
553 rc->flags = FLAG_QUEUED;
555 else /* ancient and no values -> waste of memory */
557 char **tmp = (char **) realloc ((void *) keys,
558 (keys_num + 1) * sizeof (char *));
562 ERROR ("rrdtool plugin: "
563 "realloc failed: %s",
564 sstrerror (errno, errbuf,
566 c_avl_iterator_destroy (iter);
571 keys[keys_num] = key;
574 } /* while (c_avl_iterator_next) */
575 c_avl_iterator_destroy (iter);
577 for (i = 0; i < keys_num; i++)
579 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
581 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
585 assert (rc->values == NULL);
586 assert (rc->values_num == 0);
591 } /* for (i = 0..keys_num) */
595 cache_flush_last = now;
596 } /* void rrd_cache_flush */
598 static int rrd_cache_flush_identifier (cdtime_t timeout,
599 const char *identifier)
606 if (identifier == NULL)
608 rrd_cache_flush (timeout);
615 snprintf (key, sizeof (key), "%s.rrd",
618 snprintf (key, sizeof (key), "%s/%s.rrd",
619 datadir, identifier);
620 key[sizeof (key) - 1] = 0;
622 status = c_avl_get (cache, key, (void *) &rc);
625 INFO ("rrdtool plugin: rrd_cache_flush_identifier: "
626 "c_avl_get (%s) failed. Does that file really exist?",
631 if (rc->flags == FLAG_FLUSHQ)
635 else if (rc->flags == FLAG_QUEUED)
637 rrd_queue_dequeue (key, &queue_head, &queue_tail);
638 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
640 rc->flags = FLAG_FLUSHQ;
642 else if ((now - rc->first_value) < timeout)
646 else if (rc->values_num > 0)
648 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
650 rc->flags = FLAG_FLUSHQ;
654 } /* int rrd_cache_flush_identifier */
656 static int64_t rrd_get_random_variation (void)
659 cdtime_t ctm_timeout;
664 if (random_timeout <= 0)
667 /* Assure that "cache_timeout + random_variation" is never negative. */
668 if (random_timeout > cache_timeout)
670 INFO ("rrdtool plugin: Adjusting \"RandomTimeout\" to %.3f seconds.",
671 CDTIME_T_TO_DOUBLE (cache_timeout));
672 random_timeout = cache_timeout;
675 /* This seems a bit complicated, but "random_timeout" is likely larger than
676 * RAND_MAX, so we can't simply use modulo here. */
677 dbl_timeout = CDTIME_T_TO_DOUBLE (random_timeout);
678 rand_fact = ((double) random ())
679 / ((double) RAND_MAX);
680 negative = (_Bool) (random () % 2);
682 ctm_timeout = DOUBLE_TO_CDTIME_T (dbl_timeout * rand_fact);
684 ret = (int64_t) ctm_timeout;
689 } /* int64_t rrd_get_random_variation */
691 static int rrd_cache_insert (const char *filename,
692 const char *value, cdtime_t value_time)
694 rrd_cache_t *rc = NULL;
698 pthread_mutex_lock (&cache_lock);
700 /* This shouldn't happen, but it did happen at least once, so we'll be
704 pthread_mutex_unlock (&cache_lock);
705 WARNING ("rrdtool plugin: cache == NULL.");
709 c_avl_get (cache, filename, (void *) &rc);
713 rc = malloc (sizeof (*rc));
720 rc->random_variation = rrd_get_random_variation ();
721 rc->flags = FLAG_NONE;
725 if (rc->last_value >= value_time)
727 pthread_mutex_unlock (&cache_lock);
728 DEBUG ("rrdtool plugin: (rc->last_value = %"PRIu64") "
729 ">= (value_time = %"PRIu64")",
730 rc->last_value, value_time);
734 values_new = (char **) realloc ((void *) rc->values,
735 (rc->values_num + 1) * sizeof (char *));
736 if (values_new == NULL)
739 void *cache_key = NULL;
741 sstrerror (errno, errbuf, sizeof (errbuf));
743 c_avl_remove (cache, filename, &cache_key, NULL);
744 pthread_mutex_unlock (&cache_lock);
746 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
753 rc->values = values_new;
755 rc->values[rc->values_num] = strdup (value);
756 if (rc->values[rc->values_num] != NULL)
759 if (rc->values_num == 1)
760 rc->first_value = value_time;
761 rc->last_value = value_time;
763 /* Insert if this is the first value */
766 void *cache_key = strdup (filename);
768 if (cache_key == NULL)
771 sstrerror (errno, errbuf, sizeof (errbuf));
773 pthread_mutex_unlock (&cache_lock);
775 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
777 sfree (rc->values[0]);
783 c_avl_insert (cache, cache_key, rc);
786 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
787 "values_num = %i; age = %.3f;",
788 filename, rc->values_num,
789 CDTIME_T_TO_DOUBLE (rc->last_value - rc->first_value));
791 if ((rc->last_value - rc->first_value) >= (cache_timeout + rc->random_variation))
793 /* XXX: If you need to lock both, cache_lock and queue_lock, at
794 * the same time, ALWAYS lock `cache_lock' first! */
795 if (rc->flags == FLAG_NONE)
799 status = rrd_queue_enqueue (filename, &queue_head, &queue_tail);
801 rc->flags = FLAG_QUEUED;
803 rc->random_variation = rrd_get_random_variation ();
807 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
811 if ((cache_timeout > 0) &&
812 ((cdtime () - cache_flush_last) > cache_flush_timeout))
813 rrd_cache_flush (cache_flush_timeout);
815 pthread_mutex_unlock (&cache_lock);
818 } /* int rrd_cache_insert */
820 static int rrd_cache_destroy (void) /* {{{ */
827 pthread_mutex_lock (&cache_lock);
831 pthread_mutex_unlock (&cache_lock);
835 while (c_avl_pick (cache, &key, &value) == 0)
846 if (rc->values_num > 0)
849 for (i = 0; i < rc->values_num; i++)
850 sfree (rc->values[i]);
855 c_avl_destroy (cache);
860 INFO ("rrdtool plugin: %i cache %s had values when destroying the cache.",
861 non_empty, (non_empty == 1) ? "entry" : "entries");
865 DEBUG ("rrdtool plugin: No values have been lost "
866 "when destroying the cache.");
869 pthread_mutex_unlock (&cache_lock);
871 } /* }}} int rrd_cache_destroy */
873 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
875 int a = *((int *) a_ptr);
876 int b = *((int *) b_ptr);
884 } /* int rrd_compare_numeric */
886 static int rrd_write (const data_set_t *ds, const value_list_t *vl,
887 user_data_t __attribute__((unused)) *user_data)
897 if (0 != strcmp (ds->type, vl->type)) {
898 ERROR ("rrdtool plugin: DS type does not match value list type");
902 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
905 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
908 if (stat (filename, &statbuf) == -1)
912 status = cu_rrd_create_file (filename,
913 ds, vl, &rrdcreate_config);
920 ERROR ("stat(%s) failed: %s", filename,
921 sstrerror (errno, errbuf,
926 else if (!S_ISREG (statbuf.st_mode))
928 ERROR ("stat(%s): Not a regular file!",
933 status = rrd_cache_insert (filename, values, vl->time);
936 } /* int rrd_write */
938 static int rrd_flush (cdtime_t timeout, const char *identifier,
939 __attribute__((unused)) user_data_t *user_data)
941 pthread_mutex_lock (&cache_lock);
944 pthread_mutex_unlock (&cache_lock);
948 rrd_cache_flush_identifier (timeout, identifier);
950 pthread_mutex_unlock (&cache_lock);
952 } /* int rrd_flush */
954 static int rrd_config (const char *key, const char *value)
956 if (strcasecmp ("CacheTimeout", key) == 0)
958 double tmp = atof (value);
961 fprintf (stderr, "rrdtool: `CacheTimeout' must "
962 "be greater than 0.\n");
963 ERROR ("rrdtool: `CacheTimeout' must "
964 "be greater than 0.\n");
967 cache_timeout = DOUBLE_TO_CDTIME_T (tmp);
969 else if (strcasecmp ("CacheFlush", key) == 0)
971 int tmp = atoi (value);
974 fprintf (stderr, "rrdtool: `CacheFlush' must "
975 "be greater than 0.\n");
976 ERROR ("rrdtool: `CacheFlush' must "
977 "be greater than 0.\n");
980 cache_flush_timeout = tmp;
982 else if (strcasecmp ("DataDir", key) == 0)
986 datadir = strdup (value);
989 int len = strlen (datadir);
990 while ((len > 0) && (datadir[len - 1] == '/'))
1002 else if (strcasecmp ("StepSize", key) == 0)
1004 unsigned long temp = strtoul (value, NULL, 0);
1006 rrdcreate_config.stepsize = temp;
1008 else if (strcasecmp ("HeartBeat", key) == 0)
1010 int temp = atoi (value);
1012 rrdcreate_config.heartbeat = temp;
1014 else if (strcasecmp ("CreateFilesAsync", key) == 0)
1016 if (IS_TRUE (value))
1017 rrdcreate_config.async = 1;
1019 rrdcreate_config.async = 0;
1021 else if (strcasecmp ("RRARows", key) == 0)
1023 int tmp = atoi (value);
1026 fprintf (stderr, "rrdtool: `RRARows' must "
1027 "be greater than 0.\n");
1028 ERROR ("rrdtool: `RRARows' must "
1029 "be greater than 0.\n");
1032 rrdcreate_config.rrarows = tmp;
1034 else if (strcasecmp ("RRATimespan", key) == 0)
1036 char *saveptr = NULL;
1042 value_copy = strdup (value);
1043 if (value_copy == NULL)
1047 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1051 tmp_alloc = realloc (rrdcreate_config.timespans,
1052 sizeof (int) * (rrdcreate_config.timespans_num + 1));
1053 if (tmp_alloc == NULL)
1055 fprintf (stderr, "rrdtool: realloc failed.\n");
1056 ERROR ("rrdtool: realloc failed.\n");
1060 rrdcreate_config.timespans = tmp_alloc;
1061 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = atoi (ptr);
1062 if (rrdcreate_config.timespans[rrdcreate_config.timespans_num] != 0)
1063 rrdcreate_config.timespans_num++;
1064 } /* while (strtok_r) */
1066 qsort (/* base = */ rrdcreate_config.timespans,
1067 /* nmemb = */ rrdcreate_config.timespans_num,
1068 /* size = */ sizeof (rrdcreate_config.timespans[0]),
1069 /* compar = */ rrd_compare_numeric);
1073 else if (strcasecmp ("XFF", key) == 0)
1075 double tmp = atof (value);
1076 if ((tmp < 0.0) || (tmp >= 1.0))
1078 fprintf (stderr, "rrdtool: `XFF' must "
1079 "be in the range 0 to 1 (exclusive).");
1080 ERROR ("rrdtool: `XFF' must "
1081 "be in the range 0 to 1 (exclusive).");
1084 rrdcreate_config.xff = tmp;
1086 else if (strcasecmp ("WritesPerSecond", key) == 0)
1088 double wps = atof (value);
1092 fprintf (stderr, "rrdtool: `WritesPerSecond' must be "
1093 "greater than or equal to zero.");
1096 else if (wps == 0.0)
1102 write_rate = 1.0 / wps;
1105 else if (strcasecmp ("RandomTimeout", key) == 0)
1112 fprintf (stderr, "rrdtool: `RandomTimeout' must "
1113 "be greater than or equal to zero.\n");
1114 ERROR ("rrdtool: `RandomTimeout' must "
1115 "be greater then or equal to zero.");
1119 random_timeout = DOUBLE_TO_CDTIME_T (tmp);
1127 } /* int rrd_config */
1129 static int rrd_shutdown (void)
1131 pthread_mutex_lock (&cache_lock);
1132 rrd_cache_flush (0);
1133 pthread_mutex_unlock (&cache_lock);
1135 pthread_mutex_lock (&queue_lock);
1137 pthread_cond_signal (&queue_cond);
1138 pthread_mutex_unlock (&queue_lock);
1140 if ((queue_thread_running != 0)
1141 && ((queue_head != NULL) || (flushq_head != NULL)))
1143 INFO ("rrdtool plugin: Shutting down the queue thread. "
1144 "This may take a while.");
1146 else if (queue_thread_running != 0)
1148 INFO ("rrdtool plugin: Shutting down the queue thread.");
1151 /* Wait for all the values to be written to disk before returning. */
1152 if (queue_thread_running != 0)
1154 pthread_join (queue_thread, NULL);
1155 memset (&queue_thread, 0, sizeof (queue_thread));
1156 queue_thread_running = 0;
1157 DEBUG ("rrdtool plugin: queue_thread exited.");
1160 rrd_cache_destroy ();
1163 } /* int rrd_shutdown */
1165 static int rrd_init (void)
1167 static int init_once = 0;
1174 if (rrdcreate_config.heartbeat <= 0)
1175 rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize;
1177 /* Set the cache up */
1178 pthread_mutex_lock (&cache_lock);
1180 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1183 ERROR ("rrdtool plugin: c_avl_create failed.");
1187 cache_flush_last = cdtime ();
1188 if (cache_timeout == 0)
1190 cache_flush_timeout = 0;
1192 else if (cache_flush_timeout < cache_timeout)
1193 cache_flush_timeout = 10 * cache_timeout;
1195 pthread_mutex_unlock (&cache_lock);
1197 status = plugin_thread_create (&queue_thread, /* attr = */ NULL,
1198 rrd_queue_thread, /* args = */ NULL);
1201 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1204 queue_thread_running = 1;
1206 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %lu;"
1207 " heartbeat = %i; rrarows = %i; xff = %lf;",
1208 (datadir == NULL) ? "(null)" : datadir,
1209 rrdcreate_config.stepsize,
1210 rrdcreate_config.heartbeat,
1211 rrdcreate_config.rrarows,
1212 rrdcreate_config.xff);
1215 } /* int rrd_init */
1217 void module_register (void)
1219 plugin_register_config ("rrdtool", rrd_config,
1220 config_keys, config_keys_num);
1221 plugin_register_init ("rrdtool", rrd_init);
1222 plugin_register_write ("rrdtool", rrd_write, /* user_data = */ NULL);
1223 plugin_register_flush ("rrdtool", rrd_flush, /* user_data = */ NULL);
1224 plugin_register_shutdown ("rrdtool", rrd_shutdown);