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"
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 int cache_timeout = 0;
111 static int cache_flush_timeout = 0;
112 static int random_timeout = 1;
113 static time_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 argv[1], 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)
204 memset (buffer, '\0', buffer_len);
206 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
207 if ((status < 1) || (status >= buffer_len))
211 for (i = 0; i < ds->ds_num; i++)
213 if ((ds->ds[i].type != DS_TYPE_COUNTER)
214 && (ds->ds[i].type != DS_TYPE_GAUGE)
215 && (ds->ds[i].type != DS_TYPE_DERIVE)
216 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
219 if (ds->ds[i].type == DS_TYPE_COUNTER)
220 status = ssnprintf (buffer + offset, buffer_len - offset,
221 ":%llu", vl->values[i].counter);
222 else if (ds->ds[i].type == DS_TYPE_GAUGE)
223 status = ssnprintf (buffer + offset, buffer_len - offset,
224 ":%lf", vl->values[i].gauge);
225 else if (ds->ds[i].type == DS_TYPE_DERIVE)
226 status = ssnprintf (buffer + offset, buffer_len - offset,
227 ":%"PRIi64, vl->values[i].derive);
228 else /*if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */
229 status = ssnprintf (buffer + offset, buffer_len - offset,
230 ":%"PRIu64, vl->values[i].absolute);
232 if ((status < 1) || (status >= (buffer_len - offset)))
236 } /* for ds->ds_num */
239 } /* int value_list_to_string */
241 static int value_list_to_filename (char *buffer, int buffer_len,
242 const data_set_t __attribute__((unused)) *ds, const value_list_t *vl)
249 status = ssnprintf (buffer + offset, buffer_len - offset,
251 if ((status < 1) || (status >= buffer_len - offset))
256 status = ssnprintf (buffer + offset, buffer_len - offset,
258 if ((status < 1) || (status >= buffer_len - offset))
262 if (strlen (vl->plugin_instance) > 0)
263 status = ssnprintf (buffer + offset, buffer_len - offset,
264 "%s-%s/", vl->plugin, vl->plugin_instance);
266 status = ssnprintf (buffer + offset, buffer_len - offset,
268 if ((status < 1) || (status >= buffer_len - offset))
272 if (strlen (vl->type_instance) > 0)
273 status = ssnprintf (buffer + offset, buffer_len - offset,
274 "%s-%s.rrd", vl->type, vl->type_instance);
276 status = ssnprintf (buffer + offset, buffer_len - offset,
278 if ((status < 1) || (status >= buffer_len - offset))
283 } /* int value_list_to_filename */
285 static void *rrd_queue_thread (void __attribute__((unused)) *data)
287 struct timeval tv_next_update;
288 struct timeval tv_now;
290 gettimeofday (&tv_next_update, /* timezone = */ NULL);
294 rrd_queue_t *queue_entry;
295 rrd_cache_t *cache_entry;
304 pthread_mutex_lock (&queue_lock);
305 /* Wait for values to arrive */
308 struct timespec ts_wait;
310 while ((flushq_head == NULL) && (queue_head == NULL)
311 && (do_shutdown == 0))
312 pthread_cond_wait (&queue_cond, &queue_lock);
314 if ((flushq_head == NULL) && (queue_head == NULL))
317 /* Don't delay if there's something to flush */
318 if (flushq_head != NULL)
321 /* Don't delay if we're shutting down */
322 if (do_shutdown != 0)
325 /* Don't delay if no delay was configured. */
326 if (write_rate <= 0.0)
329 gettimeofday (&tv_now, /* timezone = */ NULL);
330 status = timeval_cmp (tv_next_update, tv_now, NULL);
331 /* We're good to go */
335 /* We're supposed to wait a bit with this update, so we'll
336 * wait for the next addition to the queue or to the end of
337 * the wait period - whichever comes first. */
338 ts_wait.tv_sec = tv_next_update.tv_sec;
339 ts_wait.tv_nsec = 1000 * tv_next_update.tv_usec;
341 status = pthread_cond_timedwait (&queue_cond, &queue_lock,
343 if (status == ETIMEDOUT)
347 /* XXX: If you need to lock both, cache_lock and queue_lock, at
348 * the same time, ALWAYS lock `cache_lock' first! */
350 /* We're in the shutdown phase */
351 if ((flushq_head == NULL) && (queue_head == NULL))
353 pthread_mutex_unlock (&queue_lock);
357 if (flushq_head != NULL)
359 /* Dequeue the first flush entry */
360 queue_entry = flushq_head;
361 if (flushq_head == flushq_tail)
362 flushq_head = flushq_tail = NULL;
364 flushq_head = flushq_head->next;
366 else /* if (queue_head != NULL) */
368 /* Dequeue the first regular entry */
369 queue_entry = queue_head;
370 if (queue_head == queue_tail)
371 queue_head = queue_tail = NULL;
373 queue_head = queue_head->next;
376 /* Unlock the queue again */
377 pthread_mutex_unlock (&queue_lock);
379 /* We now need the cache lock so the entry isn't updated while
380 * we make a copy of it's values */
381 pthread_mutex_lock (&cache_lock);
383 status = c_avl_get (cache, queue_entry->filename,
384 (void *) &cache_entry);
388 values = cache_entry->values;
389 values_num = cache_entry->values_num;
391 cache_entry->values = NULL;
392 cache_entry->values_num = 0;
393 cache_entry->flags = FLAG_NONE;
396 pthread_mutex_unlock (&cache_lock);
400 sfree (queue_entry->filename);
405 /* Update `tv_next_update' */
406 if (write_rate > 0.0)
408 gettimeofday (&tv_now, /* timezone = */ NULL);
409 tv_next_update.tv_sec = tv_now.tv_sec;
410 tv_next_update.tv_usec = tv_now.tv_usec
411 + ((suseconds_t) (1000000 * write_rate));
412 while (tv_next_update.tv_usec > 1000000)
414 tv_next_update.tv_sec++;
415 tv_next_update.tv_usec -= 1000000;
419 /* Write the values to the RRD-file */
420 srrd_update (queue_entry->filename, NULL,
421 values_num, (const char **)values);
422 DEBUG ("rrdtool plugin: queue thread: Wrote %i value%s to %s",
423 values_num, (values_num == 1) ? "" : "s",
424 queue_entry->filename);
426 for (i = 0; i < values_num; i++)
431 sfree (queue_entry->filename);
435 pthread_exit ((void *) 0);
437 } /* void *rrd_queue_thread */
439 static int rrd_queue_enqueue (const char *filename,
440 rrd_queue_t **head, rrd_queue_t **tail)
442 rrd_queue_t *queue_entry;
444 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
445 if (queue_entry == NULL)
448 queue_entry->filename = strdup (filename);
449 if (queue_entry->filename == NULL)
455 queue_entry->next = NULL;
457 pthread_mutex_lock (&queue_lock);
462 (*tail)->next = queue_entry;
465 pthread_cond_signal (&queue_cond);
466 pthread_mutex_unlock (&queue_lock);
469 } /* int rrd_queue_enqueue */
471 static int rrd_queue_dequeue (const char *filename,
472 rrd_queue_t **head, rrd_queue_t **tail)
477 pthread_mutex_lock (&queue_lock);
484 if (strcmp (this->filename, filename) == 0)
493 pthread_mutex_unlock (&queue_lock);
500 prev->next = this->next;
502 if (this->next == NULL)
505 pthread_mutex_unlock (&queue_lock);
507 sfree (this->filename);
511 } /* int rrd_queue_dequeue */
513 static void rrd_cache_flush (int timeout)
522 c_avl_iterator_t *iter;
525 DEBUG ("rrdtool plugin: Flushing cache, timeout = %i", timeout);
529 /* Build a list of entries to be flushed */
530 iter = c_avl_get_iterator (cache);
531 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
533 if (rc->flags != FLAG_NONE)
535 else if ((now - rc->first_value) < timeout)
537 else if (rc->values_num > 0)
541 status = rrd_queue_enqueue (key, &queue_head, &queue_tail);
543 rc->flags = FLAG_QUEUED;
545 else /* ancient and no values -> waste of memory */
547 char **tmp = (char **) realloc ((void *) keys,
548 (keys_num + 1) * sizeof (char *));
552 ERROR ("rrdtool plugin: "
553 "realloc failed: %s",
554 sstrerror (errno, errbuf,
556 c_avl_iterator_destroy (iter);
561 keys[keys_num] = key;
564 } /* while (c_avl_iterator_next) */
565 c_avl_iterator_destroy (iter);
567 for (i = 0; i < keys_num; i++)
569 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
571 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
575 assert (rc->values == NULL);
576 assert (rc->values_num == 0);
581 } /* for (i = 0..keys_num) */
585 cache_flush_last = now;
586 } /* void rrd_cache_flush */
588 static int rrd_cache_flush_identifier (int timeout, const char *identifier)
595 if (identifier == NULL)
597 rrd_cache_flush (timeout);
604 snprintf (key, sizeof (key), "%s.rrd",
607 snprintf (key, sizeof (key), "%s/%s.rrd",
608 datadir, identifier);
609 key[sizeof (key) - 1] = 0;
611 status = c_avl_get (cache, key, (void *) &rc);
614 INFO ("rrdtool plugin: rrd_cache_flush_identifier: "
615 "c_avl_get (%s) failed. Does that file really exist?",
620 if (rc->flags == FLAG_FLUSHQ)
624 else if (rc->flags == FLAG_QUEUED)
626 rrd_queue_dequeue (key, &queue_head, &queue_tail);
627 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
629 rc->flags = FLAG_FLUSHQ;
631 else if ((now - rc->first_value) < timeout)
635 else if (rc->values_num > 0)
637 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
639 rc->flags = FLAG_FLUSHQ;
643 } /* int rrd_cache_flush_identifier */
645 static int rrd_cache_insert (const char *filename,
646 const char *value, time_t value_time)
648 rrd_cache_t *rc = NULL;
652 pthread_mutex_lock (&cache_lock);
654 /* This shouldn't happen, but it did happen at least once, so we'll be
658 pthread_mutex_unlock (&cache_lock);
659 WARNING ("rrdtool plugin: cache == NULL.");
663 c_avl_get (cache, filename, (void *) &rc);
667 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
674 rc->random_variation = 0;
675 rc->flags = FLAG_NONE;
679 if (rc->last_value >= value_time)
681 pthread_mutex_unlock (&cache_lock);
682 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
683 (unsigned int) rc->last_value,
684 (unsigned int) value_time);
688 values_new = (char **) realloc ((void *) rc->values,
689 (rc->values_num + 1) * sizeof (char *));
690 if (values_new == NULL)
693 void *cache_key = NULL;
695 sstrerror (errno, errbuf, sizeof (errbuf));
697 c_avl_remove (cache, filename, &cache_key, NULL);
698 pthread_mutex_unlock (&cache_lock);
700 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
707 rc->values = values_new;
709 rc->values[rc->values_num] = strdup (value);
710 if (rc->values[rc->values_num] != NULL)
713 if (rc->values_num == 1)
714 rc->first_value = value_time;
715 rc->last_value = value_time;
717 /* Insert if this is the first value */
720 void *cache_key = strdup (filename);
722 if (cache_key == NULL)
725 sstrerror (errno, errbuf, sizeof (errbuf));
727 pthread_mutex_unlock (&cache_lock);
729 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
731 sfree (rc->values[0]);
737 c_avl_insert (cache, cache_key, rc);
740 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
741 "values_num = %i; age = %lu;",
742 filename, rc->values_num,
743 (unsigned long)(rc->last_value - rc->first_value));
745 if ((rc->last_value + rc->random_variation - rc->first_value) >= cache_timeout)
747 /* XXX: If you need to lock both, cache_lock and queue_lock, at
748 * the same time, ALWAYS lock `cache_lock' first! */
749 if (rc->flags == FLAG_NONE)
753 status = rrd_queue_enqueue (filename, &queue_head, &queue_tail);
755 rc->flags = FLAG_QUEUED;
757 /* Update the jitter value. Negative values are
758 * slightly preferred. */
759 if (random_timeout > 0)
761 rc->random_variation = (rand () % (2 * random_timeout))
766 rc->random_variation = 0;
771 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
775 if ((cache_timeout > 0) &&
776 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
777 rrd_cache_flush (cache_flush_timeout);
779 pthread_mutex_unlock (&cache_lock);
782 } /* int rrd_cache_insert */
784 static int rrd_cache_destroy (void) /* {{{ */
791 pthread_mutex_lock (&cache_lock);
795 pthread_mutex_unlock (&cache_lock);
799 while (c_avl_pick (cache, &key, &value) == 0)
810 if (rc->values_num > 0)
813 for (i = 0; i < rc->values_num; i++)
814 sfree (rc->values[i]);
819 c_avl_destroy (cache);
824 INFO ("rrdtool plugin: %i cache %s had values when destroying the cache.",
825 non_empty, (non_empty == 1) ? "entry" : "entries");
829 DEBUG ("rrdtool plugin: No values have been lost "
830 "when destroying the cache.");
833 pthread_mutex_unlock (&cache_lock);
835 } /* }}} int rrd_cache_destroy */
837 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
839 int a = *((int *) a_ptr);
840 int b = *((int *) b_ptr);
848 } /* int rrd_compare_numeric */
850 static int rrd_write (const data_set_t *ds, const value_list_t *vl,
851 user_data_t __attribute__((unused)) *user_data)
861 if (0 != strcmp (ds->type, vl->type)) {
862 ERROR ("rrdtool plugin: DS type does not match value list type");
866 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
869 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
872 if (stat (filename, &statbuf) == -1)
876 status = cu_rrd_create_file (filename,
877 ds, vl, &rrdcreate_config);
884 ERROR ("stat(%s) failed: %s", filename,
885 sstrerror (errno, errbuf,
890 else if (!S_ISREG (statbuf.st_mode))
892 ERROR ("stat(%s): Not a regular file!",
897 status = rrd_cache_insert (filename, values, vl->time);
900 } /* int rrd_write */
902 static int rrd_flush (int timeout, const char *identifier,
903 user_data_t __attribute__((unused)) *user_data)
905 pthread_mutex_lock (&cache_lock);
908 pthread_mutex_unlock (&cache_lock);
912 rrd_cache_flush_identifier (timeout, identifier);
914 pthread_mutex_unlock (&cache_lock);
916 } /* int rrd_flush */
918 static int rrd_config (const char *key, const char *value)
920 if (strcasecmp ("CacheTimeout", key) == 0)
922 int tmp = atoi (value);
925 fprintf (stderr, "rrdtool: `CacheTimeout' must "
926 "be greater than 0.\n");
927 ERROR ("rrdtool: `CacheTimeout' must "
928 "be greater than 0.\n");
933 else if (strcasecmp ("CacheFlush", key) == 0)
935 int tmp = atoi (value);
938 fprintf (stderr, "rrdtool: `CacheFlush' must "
939 "be greater than 0.\n");
940 ERROR ("rrdtool: `CacheFlush' must "
941 "be greater than 0.\n");
944 cache_flush_timeout = tmp;
946 else if (strcasecmp ("DataDir", key) == 0)
950 datadir = strdup (value);
953 int len = strlen (datadir);
954 while ((len > 0) && (datadir[len - 1] == '/'))
966 else if (strcasecmp ("StepSize", key) == 0)
968 int temp = atoi (value);
970 rrdcreate_config.stepsize = temp;
972 else if (strcasecmp ("HeartBeat", key) == 0)
974 int temp = atoi (value);
976 rrdcreate_config.heartbeat = temp;
978 else if (strcasecmp ("RRARows", key) == 0)
980 int tmp = atoi (value);
983 fprintf (stderr, "rrdtool: `RRARows' must "
984 "be greater than 0.\n");
985 ERROR ("rrdtool: `RRARows' must "
986 "be greater than 0.\n");
989 rrdcreate_config.rrarows = tmp;
991 else if (strcasecmp ("RRATimespan", key) == 0)
993 char *saveptr = NULL;
999 value_copy = strdup (value);
1000 if (value_copy == NULL)
1004 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1008 tmp_alloc = realloc (rrdcreate_config.timespans,
1009 sizeof (int) * (rrdcreate_config.timespans_num + 1));
1010 if (tmp_alloc == NULL)
1012 fprintf (stderr, "rrdtool: realloc failed.\n");
1013 ERROR ("rrdtool: realloc failed.\n");
1017 rrdcreate_config.timespans = tmp_alloc;
1018 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = atoi (ptr);
1019 if (rrdcreate_config.timespans[rrdcreate_config.timespans_num] != 0)
1020 rrdcreate_config.timespans_num++;
1021 } /* while (strtok_r) */
1023 qsort (/* base = */ rrdcreate_config.timespans,
1024 /* nmemb = */ rrdcreate_config.timespans_num,
1025 /* size = */ sizeof (rrdcreate_config.timespans[0]),
1026 /* compar = */ rrd_compare_numeric);
1030 else if (strcasecmp ("XFF", key) == 0)
1032 double tmp = atof (value);
1033 if ((tmp < 0.0) || (tmp >= 1.0))
1035 fprintf (stderr, "rrdtool: `XFF' must "
1036 "be in the range 0 to 1 (exclusive).");
1037 ERROR ("rrdtool: `XFF' must "
1038 "be in the range 0 to 1 (exclusive).");
1041 rrdcreate_config.xff = tmp;
1043 else if (strcasecmp ("WritesPerSecond", key) == 0)
1045 double wps = atof (value);
1049 fprintf (stderr, "rrdtool: `WritesPerSecond' must be "
1050 "greater than or equal to zero.");
1053 else if (wps == 0.0)
1059 write_rate = 1.0 / wps;
1062 else if (strcasecmp ("RandomTimeout", key) == 0)
1069 fprintf (stderr, "rrdtool: `RandomTimeout' must "
1070 "be greater than or equal to zero.\n");
1071 ERROR ("rrdtool: `RandomTimeout' must "
1072 "be greater then or equal to zero.");
1076 random_timeout = tmp;
1084 } /* int rrd_config */
1086 static int rrd_shutdown (void)
1088 pthread_mutex_lock (&cache_lock);
1089 rrd_cache_flush (-1);
1090 pthread_mutex_unlock (&cache_lock);
1092 pthread_mutex_lock (&queue_lock);
1094 pthread_cond_signal (&queue_cond);
1095 pthread_mutex_unlock (&queue_lock);
1097 if ((queue_thread_running != 0)
1098 && ((queue_head != NULL) || (flushq_head != NULL)))
1100 INFO ("rrdtool plugin: Shutting down the queue thread. "
1101 "This may take a while.");
1103 else if (queue_thread_running != 0)
1105 INFO ("rrdtool plugin: Shutting down the queue thread.");
1108 /* Wait for all the values to be written to disk before returning. */
1109 if (queue_thread_running != 0)
1111 pthread_join (queue_thread, NULL);
1112 memset (&queue_thread, 0, sizeof (queue_thread));
1113 queue_thread_running = 0;
1114 DEBUG ("rrdtool plugin: queue_thread exited.");
1117 rrd_cache_destroy ();
1120 } /* int rrd_shutdown */
1122 static int rrd_init (void)
1124 static int init_once = 0;
1131 if (rrdcreate_config.stepsize < 0)
1132 rrdcreate_config.stepsize = 0;
1133 if (rrdcreate_config.heartbeat <= 0)
1134 rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize;
1136 if ((rrdcreate_config.heartbeat > 0)
1137 && (rrdcreate_config.heartbeat < interval_g))
1138 WARNING ("rrdtool plugin: Your `heartbeat' is "
1139 "smaller than your `interval'. This will "
1140 "likely cause problems.");
1141 else if ((rrdcreate_config.stepsize > 0)
1142 && (rrdcreate_config.stepsize < interval_g))
1143 WARNING ("rrdtool plugin: Your `stepsize' is "
1144 "smaller than your `interval'. This will "
1145 "create needlessly big RRD-files.");
1147 /* Set the cache up */
1148 pthread_mutex_lock (&cache_lock);
1150 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1153 ERROR ("rrdtool plugin: c_avl_create failed.");
1157 cache_flush_last = time (NULL);
1158 if (cache_timeout < 2)
1161 cache_flush_timeout = 0;
1163 else if (cache_flush_timeout < cache_timeout)
1164 cache_flush_timeout = 10 * cache_timeout;
1166 pthread_mutex_unlock (&cache_lock);
1168 status = pthread_create (&queue_thread, /* attr = */ NULL,
1169 rrd_queue_thread, /* args = */ NULL);
1172 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1175 queue_thread_running = 1;
1177 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1178 " heartbeat = %i; rrarows = %i; xff = %lf;",
1179 (datadir == NULL) ? "(null)" : datadir,
1180 rrdcreate_config.stepsize,
1181 rrdcreate_config.heartbeat,
1182 rrdcreate_config.rrarows,
1183 rrdcreate_config.xff);
1186 } /* int rrd_init */
1188 void module_register (void)
1190 plugin_register_config ("rrdtool", rrd_config,
1191 config_keys, config_keys_num);
1192 plugin_register_init ("rrdtool", rrd_init);
1193 plugin_register_write ("rrdtool", rrd_write, /* user_data = */ NULL);
1194 plugin_register_flush ("rrdtool", rrd_flush, /* user_data = */ NULL);
1195 plugin_register_shutdown ("rrdtool", rrd_shutdown);