2 * collectd - src/rrdtool.c
3 * Copyright (C) 2006-2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_avltree.h"
48 typedef struct rrd_cache_s rrd_cache_t;
55 typedef enum rrd_queue_dir_e rrd_queue_dir_t;
60 struct rrd_queue_s *next;
62 typedef struct rrd_queue_s rrd_queue_t;
67 static int rra_timespans[] =
75 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
77 static int *rra_timespans_custom = NULL;
78 static int rra_timespans_custom_num = 0;
80 static char *rra_types[] =
86 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
88 static const char *config_keys[] =
99 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
101 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
102 * is zero a default, depending on the `interval' member of the value list is
104 static char *datadir = NULL;
105 static int stepsize = 0;
106 static int heartbeat = 0;
107 static int rrarows = 1200;
108 static double xff = 0.1;
110 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
111 * ALWAYS lock `cache_lock' first! */
112 static int cache_timeout = 0;
113 static int cache_flush_timeout = 0;
114 static time_t cache_flush_last;
115 static c_avl_tree_t *cache = NULL;
116 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
118 static rrd_queue_t *queue_head = NULL;
119 static rrd_queue_t *queue_tail = NULL;
120 static pthread_t queue_thread = 0;
121 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
122 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
124 #if !HAVE_THREADSAFE_LIBRRD
125 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
128 static int do_shutdown = 0;
134 static void rra_free (int rra_num, char **rra_def)
138 for (i = 0; i < rra_num; i++)
143 } /* void rra_free */
145 static int rra_get (char ***ret, const value_list_t *vl)
163 /* The stepsize we use here: If it is user-set, use it. If not, use the
164 * interval of the value-list. */
173 ss = (stepsize > 0) ? stepsize : vl->interval;
180 /* Use the configured timespans or fall back to the built-in defaults */
181 if (rra_timespans_custom_num != 0)
183 rts = rra_timespans_custom;
184 rts_num = rra_timespans_custom_num;
189 rts_num = rra_timespans_num;
192 rra_max = rts_num * rra_types_num;
194 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
196 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
200 for (i = 0; i < rts_num; i++)
204 if ((span / ss) < rrarows)
210 cdp_len = (int) floor (((double) span)
211 / ((double) (rrarows * ss)));
213 cdp_num = (int) ceil (((double) span)
214 / ((double) (cdp_len * ss)));
216 for (j = 0; j < rra_types_num; j++)
218 if (rra_num >= rra_max)
221 if (ssnprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
223 cdp_len, cdp_num) >= sizeof (buffer))
225 ERROR ("rra_get: Buffer would have been truncated.");
229 rra_def[rra_num++] = sstrdup (buffer);
234 DEBUG ("rra_num = %i", rra_num);
235 for (i = 0; i < rra_num; i++)
236 DEBUG (" %s", rra_def[i]);
243 static void ds_free (int ds_num, char **ds_def)
247 for (i = 0; i < ds_num; i++)
248 if (ds_def[i] != NULL)
253 static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
262 DEBUG ("ds->ds_num = %i", ds->ds_num);
264 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
268 ERROR ("rrdtool plugin: malloc failed: %s",
269 sstrerror (errno, errbuf, sizeof (errbuf)));
272 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
274 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
276 data_source_t *d = ds->ds + ds_num;
280 ds_def[ds_num] = NULL;
282 if (d->type == DS_TYPE_COUNTER)
284 else if (d->type == DS_TYPE_GAUGE)
288 ERROR ("rrdtool plugin: Unknown DS type: %i",
296 ssnprintf (min, sizeof (min), "%lf", d->min);
301 ssnprintf (max, sizeof (max), "%lf", d->max);
303 status = ssnprintf (buffer, sizeof (buffer),
306 (heartbeat > 0) ? heartbeat : (2 * vl->interval),
308 if ((status < 1) || (status >= sizeof (buffer)))
311 ds_def[ds_num] = sstrdup (buffer);
312 } /* for ds_num = 0 .. ds->ds_num */
317 DEBUG ("ds_num = %i", ds_num);
318 for (i = 0; i < ds_num; i++)
319 DEBUG (" %s", ds_def[i]);
323 if (ds_num != ds->ds_num)
325 ds_free (ds_num, ds_def);
333 #if HAVE_THREADSAFE_LIBRRD
334 static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
335 int argc, const char **argv)
339 optind = 0; /* bug in librrd? */
342 status = rrd_create_r (filename, pdp_step, last_up, argc, argv);
346 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
347 filename, rrd_get_error ());
351 } /* int srrd_create */
353 static int srrd_update (char *filename, char *template,
354 int argc, const char **argv)
358 optind = 0; /* bug in librrd? */
361 status = rrd_update_r (filename, template, argc, argv);
365 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
366 filename, rrd_get_error ());
370 } /* int srrd_update */
371 /* #endif HAVE_THREADSAFE_LIBRRD */
373 #else /* !HAVE_THREADSAFE_LIBRRD */
374 static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
375 int argc, const char **argv)
382 char pdp_step_str[16];
383 char last_up_str[16];
386 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
387 if (new_argv == NULL)
389 ERROR ("rrdtool plugin: malloc failed.");
394 last_up = time (NULL) - 10;
396 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
397 ssnprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);
399 new_argv[0] = "create";
400 new_argv[1] = filename;
402 new_argv[3] = pdp_step_str;
404 new_argv[5] = last_up_str;
406 memcpy (new_argv + 6, argv, argc * sizeof (char *));
407 new_argv[new_argc] = NULL;
409 pthread_mutex_lock (&librrd_lock);
410 optind = 0; /* bug in librrd? */
413 status = rrd_create (new_argc, new_argv);
414 pthread_mutex_unlock (&librrd_lock);
418 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
419 filename, rrd_get_error ());
425 } /* int srrd_create */
427 static int srrd_update (char *filename, char *template,
428 int argc, const char **argv)
435 assert (template == NULL);
438 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
439 if (new_argv == NULL)
441 ERROR ("rrdtool plugin: malloc failed.");
445 new_argv[0] = "update";
446 new_argv[1] = filename;
448 memcpy (new_argv + 2, argv, argc * sizeof (char *));
449 new_argv[new_argc] = NULL;
451 pthread_mutex_lock (&librrd_lock);
452 optind = 0; /* bug in librrd? */
455 status = rrd_update (new_argc, new_argv);
456 pthread_mutex_unlock (&librrd_lock);
460 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
461 argv[1], rrd_get_error ());
467 } /* int srrd_update */
468 #endif /* !HAVE_THREADSAFE_LIBRRD */
470 static int rrd_create_file (char *filename, const data_set_t *ds, const value_list_t *vl)
480 if (check_create_dir (filename))
483 if ((rra_num = rra_get (&rra_def, vl)) < 1)
485 ERROR ("rrd_create_file failed: Could not calculate RRAs");
489 if ((ds_num = ds_get (&ds_def, ds, vl)) < 1)
491 ERROR ("rrd_create_file failed: Could not calculate DSes");
495 argc = ds_num + rra_num;
497 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
500 ERROR ("rrd_create failed: %s",
501 sstrerror (errno, errbuf, sizeof (errbuf)));
505 memcpy (argv, ds_def, ds_num * sizeof (char *));
506 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
507 argv[ds_num + rra_num] = NULL;
509 assert (vl->time > 10);
510 status = srrd_create (filename,
511 (stepsize > 0) ? stepsize : vl->interval,
513 argc, (const char **)argv);
516 ds_free (ds_num, ds_def);
517 rra_free (rra_num, rra_def);
522 static int value_list_to_string (char *buffer, int buffer_len,
523 const data_set_t *ds, const value_list_t *vl)
529 memset (buffer, '\0', buffer_len);
531 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
532 if ((status < 1) || (status >= buffer_len))
536 for (i = 0; i < ds->ds_num; i++)
538 if ((ds->ds[i].type != DS_TYPE_COUNTER)
539 && (ds->ds[i].type != DS_TYPE_GAUGE))
542 if (ds->ds[i].type == DS_TYPE_COUNTER)
543 status = ssnprintf (buffer + offset, buffer_len - offset,
544 ":%llu", vl->values[i].counter);
546 status = ssnprintf (buffer + offset, buffer_len - offset,
547 ":%lf", vl->values[i].gauge);
549 if ((status < 1) || (status >= (buffer_len - offset)))
553 } /* for ds->ds_num */
556 } /* int value_list_to_string */
558 static int value_list_to_filename (char *buffer, int buffer_len,
559 const data_set_t *ds, const value_list_t *vl)
566 status = ssnprintf (buffer + offset, buffer_len - offset,
568 if ((status < 1) || (status >= buffer_len - offset))
573 status = ssnprintf (buffer + offset, buffer_len - offset,
575 if ((status < 1) || (status >= buffer_len - offset))
579 if (strlen (vl->plugin_instance) > 0)
580 status = ssnprintf (buffer + offset, buffer_len - offset,
581 "%s-%s/", vl->plugin, vl->plugin_instance);
583 status = ssnprintf (buffer + offset, buffer_len - offset,
585 if ((status < 1) || (status >= buffer_len - offset))
589 if (strlen (vl->type_instance) > 0)
590 status = ssnprintf (buffer + offset, buffer_len - offset,
591 "%s-%s.rrd", vl->type, vl->type_instance);
593 status = ssnprintf (buffer + offset, buffer_len - offset,
595 if ((status < 1) || (status >= buffer_len - offset))
600 } /* int value_list_to_filename */
602 static void *rrd_queue_thread (void *data)
606 rrd_queue_t *queue_entry;
607 rrd_cache_t *cache_entry;
612 /* XXX: If you need to lock both, cache_lock and queue_lock, at
613 * the same time, ALWAYS lock `cache_lock' first! */
615 /* wait until an entry is available */
616 pthread_mutex_lock (&queue_lock);
617 while ((queue_head == NULL) && (do_shutdown == 0))
618 pthread_cond_wait (&queue_cond, &queue_lock);
620 /* We're in the shutdown phase */
621 if (queue_head == NULL)
623 pthread_mutex_unlock (&queue_lock);
627 /* Dequeue the first entry */
628 queue_entry = queue_head;
629 if (queue_head == queue_tail)
630 queue_head = queue_tail = NULL;
632 queue_head = queue_head->next;
634 /* Unlock the queue again */
635 pthread_mutex_unlock (&queue_lock);
637 /* We now need the cache lock so the entry isn't updated while
638 * we make a copy of it's values */
639 pthread_mutex_lock (&cache_lock);
641 c_avl_get (cache, queue_entry->filename, (void *) &cache_entry);
643 values = cache_entry->values;
644 values_num = cache_entry->values_num;
646 cache_entry->values = NULL;
647 cache_entry->values_num = 0;
648 cache_entry->flags = FLAG_NONE;
650 pthread_mutex_unlock (&cache_lock);
652 /* Write the values to the RRD-file */
653 srrd_update (queue_entry->filename, NULL,
654 values_num, (const char **)values);
655 DEBUG ("rrdtool plugin: queue thread: Wrote %i values to %s",
656 values_num, queue_entry->filename);
658 for (i = 0; i < values_num; i++)
663 sfree (queue_entry->filename);
667 pthread_mutex_lock (&cache_lock);
668 c_avl_destroy (cache);
670 pthread_mutex_unlock (&cache_lock);
672 pthread_exit ((void *) 0);
674 } /* void *rrd_queue_thread */
676 static int rrd_queue_cache_entry (const char *filename, rrd_queue_dir_t dir)
678 rrd_queue_t *queue_entry;
680 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
681 if (queue_entry == NULL)
684 queue_entry->filename = strdup (filename);
685 if (queue_entry->filename == NULL)
691 queue_entry->next = NULL;
693 pthread_mutex_lock (&queue_lock);
694 if (dir == QUEUE_INSERT_FRONT)
696 queue_entry->next = queue_head;
697 queue_head = queue_entry;
698 if (queue_tail == NULL)
699 queue_tail = queue_head;
701 else /* (dir == QUEUE_INSERT_BACK) */
703 if (queue_tail == NULL)
704 queue_head = queue_entry;
706 queue_tail->next = queue_entry;
707 queue_tail = queue_entry;
709 pthread_cond_signal (&queue_cond);
710 pthread_mutex_unlock (&queue_lock);
712 DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
715 } /* int rrd_queue_cache_entry */
717 static int rrd_queue_move_to_front (const char *filename)
724 pthread_mutex_lock (&queue_lock);
725 for (this = queue_head; this != NULL; this = this->next)
727 if (strcmp (this->filename, filename) == 0)
732 /* Check if we found the entry and if it is NOT the first entry. */
733 if ((this != NULL) && (prev != NULL))
735 prev->next = this->next;
736 this->next = queue_head;
739 pthread_mutex_unlock (&queue_lock);
742 } /* int rrd_queue_move_to_front */
744 static void rrd_cache_flush (int timeout)
753 c_avl_iterator_t *iter;
756 DEBUG ("rrdtool plugin: Flushing cache, timeout = %i", timeout);
760 /* Build a list of entries to be flushed */
761 iter = c_avl_get_iterator (cache);
762 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
764 if (rc->flags == FLAG_QUEUED)
766 else if ((now - rc->first_value) < timeout)
768 else if (rc->values_num > 0)
770 if (rrd_queue_cache_entry (key, QUEUE_INSERT_BACK) == 0)
771 rc->flags = FLAG_QUEUED;
773 else /* ancient and no values -> waste of memory */
775 char **tmp = (char **) realloc ((void *) keys,
776 (keys_num + 1) * sizeof (char *));
780 ERROR ("rrdtool plugin: "
781 "realloc failed: %s",
782 sstrerror (errno, errbuf,
784 c_avl_iterator_destroy (iter);
789 keys[keys_num] = key;
792 } /* while (c_avl_iterator_next) */
793 c_avl_iterator_destroy (iter);
795 for (i = 0; i < keys_num; i++)
797 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
799 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
803 assert (rc->values == NULL);
804 assert (rc->values_num == 0);
809 } /* for (i = 0..keys_num) */
813 cache_flush_last = now;
814 } /* void rrd_cache_flush */
816 static int rrd_cache_flush_identifier (int timeout, const char *identifier)
823 if (identifier == NULL)
825 rrd_cache_flush (timeout);
832 snprintf (key, sizeof (key), "%s.rrd",
835 snprintf (key, sizeof (key), "%s/%s.rrd",
836 datadir, identifier);
837 key[sizeof (key) - 1] = 0;
839 status = c_avl_get (cache, key, (void *) &rc);
842 WARNING ("rrdtool plugin: rrd_cache_flush_identifier: "
843 "c_avl_get (%s) failed. Does that file really exist?",
848 if (rc->flags == FLAG_QUEUED)
849 status = rrd_queue_move_to_front (key);
850 else if ((now - rc->first_value) < timeout)
852 else if (rc->values_num > 0)
854 status = rrd_queue_cache_entry (key, QUEUE_INSERT_FRONT);
856 rc->flags = FLAG_QUEUED;
860 } /* int rrd_cache_flush_identifier */
862 static int rrd_cache_insert (const char *filename,
863 const char *value, time_t value_time)
865 rrd_cache_t *rc = NULL;
869 pthread_mutex_lock (&cache_lock);
871 c_avl_get (cache, filename, (void *) &rc);
875 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
882 rc->flags = FLAG_NONE;
886 if (rc->last_value >= value_time)
888 pthread_mutex_unlock (&cache_lock);
889 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
890 (unsigned int) rc->last_value,
891 (unsigned int) value_time);
895 values_new = (char **) realloc ((void *) rc->values,
896 (rc->values_num + 1) * sizeof (char *));
897 if (values_new == NULL)
900 void *cache_key = NULL;
902 sstrerror (errno, errbuf, sizeof (errbuf));
904 c_avl_remove (cache, filename, &cache_key, NULL);
905 pthread_mutex_unlock (&cache_lock);
907 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
914 rc->values = values_new;
916 rc->values[rc->values_num] = strdup (value);
917 if (rc->values[rc->values_num] != NULL)
920 if (rc->values_num == 1)
921 rc->first_value = value_time;
922 rc->last_value = value_time;
924 /* Insert if this is the first value */
927 void *cache_key = strdup (filename);
929 if (cache_key == NULL)
932 sstrerror (errno, errbuf, sizeof (errbuf));
934 pthread_mutex_unlock (&cache_lock);
936 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
938 sfree (rc->values[0]);
944 c_avl_insert (cache, cache_key, rc);
947 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
948 "values_num = %i; age = %lu;",
949 filename, rc->values_num,
950 (unsigned long)(rc->last_value - rc->first_value));
952 if ((rc->last_value - rc->first_value) >= cache_timeout)
954 /* XXX: If you need to lock both, cache_lock and queue_lock, at
955 * the same time, ALWAYS lock `cache_lock' first! */
956 if (rc->flags != FLAG_QUEUED)
958 if (rrd_queue_cache_entry (filename, QUEUE_INSERT_BACK) == 0)
959 rc->flags = FLAG_QUEUED;
963 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
967 if ((cache_timeout > 0) &&
968 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
969 rrd_cache_flush (cache_flush_timeout);
972 pthread_mutex_unlock (&cache_lock);
975 } /* int rrd_cache_insert */
977 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
979 int a = *((int *) a_ptr);
980 int b = *((int *) b_ptr);
988 } /* int rrd_compare_numeric */
990 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
997 if (0 != strcmp (ds->type, vl->type)) {
998 ERROR ("rrdtool plugin: DS type does not match value list type");
1002 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
1005 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
1008 if (stat (filename, &statbuf) == -1)
1010 if (errno == ENOENT)
1012 if (rrd_create_file (filename, ds, vl))
1018 ERROR ("stat(%s) failed: %s", filename,
1019 sstrerror (errno, errbuf,
1024 else if (!S_ISREG (statbuf.st_mode))
1026 ERROR ("stat(%s): Not a regular file!",
1031 status = rrd_cache_insert (filename, values, vl->time);
1034 } /* int rrd_write */
1036 static int rrd_flush (int timeout, const char *identifier)
1038 pthread_mutex_lock (&cache_lock);
1040 if (cache == NULL) {
1041 pthread_mutex_unlock (&cache_lock);
1045 rrd_cache_flush_identifier (timeout, identifier);
1047 pthread_mutex_unlock (&cache_lock);
1049 } /* int rrd_flush */
1051 static int rrd_config (const char *key, const char *value)
1053 if (strcasecmp ("CacheTimeout", key) == 0)
1055 int tmp = atoi (value);
1058 fprintf (stderr, "rrdtool: `CacheTimeout' must "
1059 "be greater than 0.\n");
1062 cache_timeout = tmp;
1064 else if (strcasecmp ("CacheFlush", key) == 0)
1066 int tmp = atoi (value);
1069 fprintf (stderr, "rrdtool: `CacheFlush' must "
1070 "be greater than 0.\n");
1073 cache_flush_timeout = tmp;
1075 else if (strcasecmp ("DataDir", key) == 0)
1077 if (datadir != NULL)
1079 datadir = strdup (value);
1080 if (datadir != NULL)
1082 int len = strlen (datadir);
1083 while ((len > 0) && (datadir[len - 1] == '/'))
1086 datadir[len] = '\0';
1095 else if (strcasecmp ("StepSize", key) == 0)
1097 stepsize = atoi (value);
1101 else if (strcasecmp ("HeartBeat", key) == 0)
1103 heartbeat = atoi (value);
1107 else if (strcasecmp ("RRARows", key) == 0)
1109 int tmp = atoi (value);
1112 fprintf (stderr, "rrdtool: `RRARows' must "
1113 "be greater than 0.\n");
1118 else if (strcasecmp ("RRATimespan", key) == 0)
1120 char *saveptr = NULL;
1126 value_copy = strdup (value);
1127 if (value_copy == NULL)
1131 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1135 tmp_alloc = realloc (rra_timespans_custom,
1136 sizeof (int) * (rra_timespans_custom_num + 1));
1137 if (tmp_alloc == NULL)
1139 fprintf (stderr, "rrdtool: realloc failed.\n");
1143 rra_timespans_custom = tmp_alloc;
1144 rra_timespans_custom[rra_timespans_custom_num] = atoi (ptr);
1145 if (rra_timespans_custom[rra_timespans_custom_num] != 0)
1146 rra_timespans_custom_num++;
1147 } /* while (strtok_r) */
1149 qsort (/* base = */ rra_timespans_custom,
1150 /* nmemb = */ rra_timespans_custom_num,
1151 /* size = */ sizeof (rra_timespans_custom[0]),
1152 /* compar = */ rrd_compare_numeric);
1156 else if (strcasecmp ("XFF", key) == 0)
1158 double tmp = atof (value);
1159 if ((tmp < 0.0) || (tmp >= 1.0))
1161 fprintf (stderr, "rrdtool: `XFF' must "
1162 "be in the range 0 to 1 (exclusive).");
1172 } /* int rrd_config */
1174 static int rrd_shutdown (void)
1176 pthread_mutex_lock (&cache_lock);
1177 rrd_cache_flush (-1);
1178 pthread_mutex_unlock (&cache_lock);
1180 pthread_mutex_lock (&queue_lock);
1182 pthread_cond_signal (&queue_cond);
1183 pthread_mutex_unlock (&queue_lock);
1185 /* Wait for all the values to be written to disk before returning. */
1186 if (queue_thread != 0)
1188 pthread_join (queue_thread, NULL);
1190 DEBUG ("rrdtool plugin: queue_thread exited.");
1194 } /* int rrd_shutdown */
1196 static int rrd_init (void)
1203 heartbeat = 2 * stepsize;
1205 if ((heartbeat > 0) && (heartbeat < interval_g))
1206 WARNING ("rrdtool plugin: Your `heartbeat' is "
1207 "smaller than your `interval'. This will "
1208 "likely cause problems.");
1209 else if ((stepsize > 0) && (stepsize < interval_g))
1210 WARNING ("rrdtool plugin: Your `stepsize' is "
1211 "smaller than your `interval'. This will "
1212 "create needlessly big RRD-files.");
1214 /* Set the cache up */
1215 pthread_mutex_lock (&cache_lock);
1217 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1220 ERROR ("rrdtool plugin: c_avl_create failed.");
1224 cache_flush_last = time (NULL);
1225 if (cache_timeout < 2)
1228 cache_flush_timeout = 0;
1230 else if (cache_flush_timeout < cache_timeout)
1231 cache_flush_timeout = 10 * cache_timeout;
1233 pthread_mutex_unlock (&cache_lock);
1235 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
1238 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1242 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1243 " heartbeat = %i; rrarows = %i; xff = %lf;",
1244 (datadir == NULL) ? "(null)" : datadir,
1245 stepsize, heartbeat, rrarows, xff);
1248 } /* int rrd_init */
1250 void module_register (void)
1252 plugin_register_config ("rrdtool", rrd_config,
1253 config_keys, config_keys_num);
1254 plugin_register_init ("rrdtool", rrd_init);
1255 plugin_register_write ("rrdtool", rrd_write);
1256 plugin_register_flush ("rrdtool", rrd_flush);
1257 plugin_register_shutdown ("rrdtool", rrd_shutdown);