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, 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, int argc, char **argv)
357 optind = 0; /* bug in librrd? */
360 status = rrd_update_r (filename, template, argc, argv);
364 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
365 filename, rrd_get_error ());
369 } /* int srrd_update */
370 /* #endif HAVE_THREADSAFE_LIBRRD */
372 #else /* !HAVE_THREADSAFE_LIBRRD */
373 static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
374 int argc, char **argv)
381 char pdp_step_str[16];
382 char last_up_str[16];
385 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
386 if (new_argv == NULL)
388 ERROR ("rrdtool plugin: malloc failed.");
393 last_up = time (NULL) - 10;
395 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
396 ssnprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);
398 new_argv[0] = "create";
399 new_argv[1] = filename;
401 new_argv[3] = pdp_step_str;
403 new_argv[5] = last_up_str;
405 memcpy (new_argv + 6, argv, argc * sizeof (char *));
406 new_argv[new_argc] = NULL;
408 pthread_mutex_lock (&librrd_lock);
409 optind = 0; /* bug in librrd? */
412 status = rrd_create (new_argc, new_argv);
413 pthread_mutex_unlock (&librrd_lock);
417 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
418 filename, rrd_get_error ());
424 } /* int srrd_create */
426 static int srrd_update (char *filename, char *template, int argc, char **argv)
433 assert (template == NULL);
436 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
437 if (new_argv == NULL)
439 ERROR ("rrdtool plugin: malloc failed.");
443 new_argv[0] = "update";
444 new_argv[1] = filename;
446 memcpy (new_argv + 2, argv, argc * sizeof (char *));
447 new_argv[new_argc] = NULL;
449 pthread_mutex_lock (&librrd_lock);
450 optind = 0; /* bug in librrd? */
453 status = rrd_update (new_argc, new_argv);
454 pthread_mutex_unlock (&librrd_lock);
458 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
459 argv[1], rrd_get_error ());
465 } /* int srrd_update */
466 #endif /* !HAVE_THREADSAFE_LIBRRD */
468 static int rrd_create_file (char *filename, const data_set_t *ds, const value_list_t *vl)
478 if (check_create_dir (filename))
481 if ((rra_num = rra_get (&rra_def, vl)) < 1)
483 ERROR ("rrd_create_file failed: Could not calculate RRAs");
487 if ((ds_num = ds_get (&ds_def, ds, vl)) < 1)
489 ERROR ("rrd_create_file failed: Could not calculate DSes");
493 argc = ds_num + rra_num;
495 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
498 ERROR ("rrd_create failed: %s",
499 sstrerror (errno, errbuf, sizeof (errbuf)));
503 memcpy (argv, ds_def, ds_num * sizeof (char *));
504 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
505 argv[ds_num + rra_num] = NULL;
507 assert (vl->time > 10);
508 status = srrd_create (filename,
509 (stepsize > 0) ? stepsize : vl->interval,
514 ds_free (ds_num, ds_def);
515 rra_free (rra_num, rra_def);
520 static int value_list_to_string (char *buffer, int buffer_len,
521 const data_set_t *ds, const value_list_t *vl)
527 memset (buffer, '\0', buffer_len);
529 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
530 if ((status < 1) || (status >= buffer_len))
534 for (i = 0; i < ds->ds_num; i++)
536 if ((ds->ds[i].type != DS_TYPE_COUNTER)
537 && (ds->ds[i].type != DS_TYPE_GAUGE))
540 if (ds->ds[i].type == DS_TYPE_COUNTER)
541 status = ssnprintf (buffer + offset, buffer_len - offset,
542 ":%llu", vl->values[i].counter);
544 status = ssnprintf (buffer + offset, buffer_len - offset,
545 ":%lf", vl->values[i].gauge);
547 if ((status < 1) || (status >= (buffer_len - offset)))
551 } /* for ds->ds_num */
554 } /* int value_list_to_string */
556 static int value_list_to_filename (char *buffer, int buffer_len,
557 const data_set_t *ds, const value_list_t *vl)
564 status = ssnprintf (buffer + offset, buffer_len - offset,
566 if ((status < 1) || (status >= buffer_len - offset))
571 status = ssnprintf (buffer + offset, buffer_len - offset,
573 if ((status < 1) || (status >= buffer_len - offset))
577 if (strlen (vl->plugin_instance) > 0)
578 status = ssnprintf (buffer + offset, buffer_len - offset,
579 "%s-%s/", vl->plugin, vl->plugin_instance);
581 status = ssnprintf (buffer + offset, buffer_len - offset,
583 if ((status < 1) || (status >= buffer_len - offset))
587 if (strlen (vl->type_instance) > 0)
588 status = ssnprintf (buffer + offset, buffer_len - offset,
589 "%s-%s.rrd", vl->type, vl->type_instance);
591 status = ssnprintf (buffer + offset, buffer_len - offset,
593 if ((status < 1) || (status >= buffer_len - offset))
598 } /* int value_list_to_filename */
600 static void *rrd_queue_thread (void *data)
604 rrd_queue_t *queue_entry;
605 rrd_cache_t *cache_entry;
610 /* XXX: If you need to lock both, cache_lock and queue_lock, at
611 * the same time, ALWAYS lock `cache_lock' first! */
613 /* wait until an entry is available */
614 pthread_mutex_lock (&queue_lock);
615 while ((queue_head == NULL) && (do_shutdown == 0))
616 pthread_cond_wait (&queue_cond, &queue_lock);
618 /* We're in the shutdown phase */
619 if (queue_head == NULL)
621 pthread_mutex_unlock (&queue_lock);
625 /* Dequeue the first entry */
626 queue_entry = queue_head;
627 if (queue_head == queue_tail)
628 queue_head = queue_tail = NULL;
630 queue_head = queue_head->next;
632 /* Unlock the queue again */
633 pthread_mutex_unlock (&queue_lock);
635 /* We now need the cache lock so the entry isn't updated while
636 * we make a copy of it's values */
637 pthread_mutex_lock (&cache_lock);
639 c_avl_get (cache, queue_entry->filename, (void *) &cache_entry);
641 values = cache_entry->values;
642 values_num = cache_entry->values_num;
644 cache_entry->values = NULL;
645 cache_entry->values_num = 0;
646 cache_entry->flags = FLAG_NONE;
648 pthread_mutex_unlock (&cache_lock);
650 /* Write the values to the RRD-file */
651 srrd_update (queue_entry->filename, NULL, values_num, values);
652 DEBUG ("rrdtool plugin: queue thread: Wrote %i values to %s",
653 values_num, queue_entry->filename);
655 for (i = 0; i < values_num; i++)
660 sfree (queue_entry->filename);
664 pthread_mutex_lock (&cache_lock);
665 c_avl_destroy (cache);
667 pthread_mutex_unlock (&cache_lock);
669 pthread_exit ((void *) 0);
671 } /* void *rrd_queue_thread */
673 static int rrd_queue_cache_entry (const char *filename, rrd_queue_dir_t dir)
675 rrd_queue_t *queue_entry;
677 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
678 if (queue_entry == NULL)
681 queue_entry->filename = strdup (filename);
682 if (queue_entry->filename == NULL)
688 queue_entry->next = NULL;
690 pthread_mutex_lock (&queue_lock);
691 if (dir == QUEUE_INSERT_FRONT)
693 queue_entry->next = queue_head;
694 queue_head = queue_entry;
695 if (queue_tail == NULL)
696 queue_tail = queue_head;
698 else /* (dir == QUEUE_INSERT_BACK) */
700 if (queue_tail == NULL)
701 queue_head = queue_entry;
703 queue_tail->next = queue_entry;
704 queue_tail = queue_entry;
706 pthread_cond_signal (&queue_cond);
707 pthread_mutex_unlock (&queue_lock);
709 DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
712 } /* int rrd_queue_cache_entry */
714 static int rrd_queue_move_to_front (const char *filename)
721 pthread_mutex_lock (&queue_lock);
722 for (this = queue_head; this != NULL; this = this->next)
724 if (strcmp (this->filename, filename) == 0)
729 /* Check if we found the entry and if it is NOT the first entry. */
730 if ((this != NULL) && (prev != NULL))
732 prev->next = this->next;
733 this->next = queue_head;
736 pthread_mutex_unlock (&queue_lock);
739 } /* int rrd_queue_move_to_front */
741 static void rrd_cache_flush (int timeout)
750 c_avl_iterator_t *iter;
753 DEBUG ("rrdtool plugin: Flushing cache, timeout = %i", timeout);
757 /* Build a list of entries to be flushed */
758 iter = c_avl_get_iterator (cache);
759 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
761 if (rc->flags == FLAG_QUEUED)
763 else if ((now - rc->first_value) < timeout)
765 else if (rc->values_num > 0)
767 if (rrd_queue_cache_entry (key, QUEUE_INSERT_BACK) == 0)
768 rc->flags = FLAG_QUEUED;
770 else /* ancient and no values -> waste of memory */
772 char **tmp = (char **) realloc ((void *) keys,
773 (keys_num + 1) * sizeof (char *));
777 ERROR ("rrdtool plugin: "
778 "realloc failed: %s",
779 sstrerror (errno, errbuf,
781 c_avl_iterator_destroy (iter);
786 keys[keys_num] = key;
789 } /* while (c_avl_iterator_next) */
790 c_avl_iterator_destroy (iter);
792 for (i = 0; i < keys_num; i++)
794 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
796 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
800 assert (rc->values == NULL);
801 assert (rc->values_num == 0);
806 } /* for (i = 0..keys_num) */
810 cache_flush_last = now;
811 } /* void rrd_cache_flush */
813 static int rrd_cache_flush_identifier (int timeout, const char *identifier)
820 if (identifier == NULL)
822 rrd_cache_flush (timeout);
829 snprintf (key, sizeof (key), "%s.rrd",
832 snprintf (key, sizeof (key), "%s/%s.rrd",
833 datadir, identifier);
834 key[sizeof (key) - 1] = 0;
836 status = c_avl_get (cache, key, (void *) &rc);
839 WARNING ("rrdtool plugin: rrd_cache_flush_identifier: "
840 "c_avl_get (%s) failed. Does that file really exist?",
845 if (rc->flags == FLAG_QUEUED)
846 status = rrd_queue_move_to_front (key);
847 else if ((now - rc->first_value) < timeout)
849 else if (rc->values_num > 0)
851 status = rrd_queue_cache_entry (key, QUEUE_INSERT_FRONT);
853 rc->flags = FLAG_QUEUED;
857 } /* int rrd_cache_flush_identifier */
859 static int rrd_cache_insert (const char *filename,
860 const char *value, time_t value_time)
862 rrd_cache_t *rc = NULL;
866 pthread_mutex_lock (&cache_lock);
868 c_avl_get (cache, filename, (void *) &rc);
872 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
879 rc->flags = FLAG_NONE;
883 if (rc->last_value >= value_time)
885 pthread_mutex_unlock (&cache_lock);
886 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
887 (unsigned int) rc->last_value,
888 (unsigned int) value_time);
892 values_new = (char **) realloc ((void *) rc->values,
893 (rc->values_num + 1) * sizeof (char *));
894 if (values_new == NULL)
897 void *cache_key = NULL;
899 sstrerror (errno, errbuf, sizeof (errbuf));
901 c_avl_remove (cache, filename, &cache_key, NULL);
902 pthread_mutex_unlock (&cache_lock);
904 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
911 rc->values = values_new;
913 rc->values[rc->values_num] = strdup (value);
914 if (rc->values[rc->values_num] != NULL)
917 if (rc->values_num == 1)
918 rc->first_value = value_time;
919 rc->last_value = value_time;
921 /* Insert if this is the first value */
924 void *cache_key = strdup (filename);
926 if (cache_key == NULL)
929 sstrerror (errno, errbuf, sizeof (errbuf));
931 pthread_mutex_unlock (&cache_lock);
933 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
935 sfree (rc->values[0]);
941 c_avl_insert (cache, cache_key, rc);
944 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
945 "values_num = %i; age = %u;",
946 filename, rc->values_num,
947 rc->last_value - rc->first_value);
949 if ((rc->last_value - rc->first_value) >= cache_timeout)
951 /* XXX: If you need to lock both, cache_lock and queue_lock, at
952 * the same time, ALWAYS lock `cache_lock' first! */
953 if (rc->flags != FLAG_QUEUED)
955 if (rrd_queue_cache_entry (filename, QUEUE_INSERT_BACK) == 0)
956 rc->flags = FLAG_QUEUED;
960 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
964 if ((cache_timeout > 0) &&
965 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
966 rrd_cache_flush (cache_flush_timeout);
969 pthread_mutex_unlock (&cache_lock);
972 } /* int rrd_cache_insert */
974 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
976 int a = *((int *) a_ptr);
977 int b = *((int *) b_ptr);
985 } /* int rrd_compare_numeric */
987 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
994 if (0 != strcmp (ds->type, vl->type)) {
995 ERROR ("rrdtool plugin: DS type does not match value list type");
999 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
1002 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
1005 if (stat (filename, &statbuf) == -1)
1007 if (errno == ENOENT)
1009 if (rrd_create_file (filename, ds, vl))
1015 ERROR ("stat(%s) failed: %s", filename,
1016 sstrerror (errno, errbuf,
1021 else if (!S_ISREG (statbuf.st_mode))
1023 ERROR ("stat(%s): Not a regular file!",
1028 status = rrd_cache_insert (filename, values, vl->time);
1031 } /* int rrd_write */
1033 static int rrd_flush (int timeout, const char *identifier)
1035 pthread_mutex_lock (&cache_lock);
1037 if (cache == NULL) {
1038 pthread_mutex_unlock (&cache_lock);
1042 rrd_cache_flush_identifier (timeout, identifier);
1044 pthread_mutex_unlock (&cache_lock);
1046 } /* int rrd_flush */
1048 static int rrd_config (const char *key, const char *value)
1050 if (strcasecmp ("CacheTimeout", key) == 0)
1052 int tmp = atoi (value);
1055 fprintf (stderr, "rrdtool: `CacheTimeout' must "
1056 "be greater than 0.\n");
1059 cache_timeout = tmp;
1061 else if (strcasecmp ("CacheFlush", key) == 0)
1063 int tmp = atoi (value);
1066 fprintf (stderr, "rrdtool: `CacheFlush' must "
1067 "be greater than 0.\n");
1070 cache_flush_timeout = tmp;
1072 else if (strcasecmp ("DataDir", key) == 0)
1074 if (datadir != NULL)
1076 datadir = strdup (value);
1077 if (datadir != NULL)
1079 int len = strlen (datadir);
1080 while ((len > 0) && (datadir[len - 1] == '/'))
1083 datadir[len] = '\0';
1092 else if (strcasecmp ("StepSize", key) == 0)
1094 stepsize = atoi (value);
1098 else if (strcasecmp ("HeartBeat", key) == 0)
1100 heartbeat = atoi (value);
1104 else if (strcasecmp ("RRARows", key) == 0)
1106 int tmp = atoi (value);
1109 fprintf (stderr, "rrdtool: `RRARows' must "
1110 "be greater than 0.\n");
1115 else if (strcasecmp ("RRATimespan", key) == 0)
1117 char *saveptr = NULL;
1123 value_copy = strdup (value);
1124 if (value_copy == NULL)
1128 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1132 tmp_alloc = realloc (rra_timespans_custom,
1133 sizeof (int) * (rra_timespans_custom_num + 1));
1134 if (tmp_alloc == NULL)
1136 fprintf (stderr, "rrdtool: realloc failed.\n");
1140 rra_timespans_custom = tmp_alloc;
1141 rra_timespans_custom[rra_timespans_custom_num] = atoi (ptr);
1142 if (rra_timespans_custom[rra_timespans_custom_num] != 0)
1143 rra_timespans_custom_num++;
1144 } /* while (strtok_r) */
1146 qsort (/* base = */ rra_timespans_custom,
1147 /* nmemb = */ rra_timespans_custom_num,
1148 /* size = */ sizeof (rra_timespans_custom[0]),
1149 /* compar = */ rrd_compare_numeric);
1153 else if (strcasecmp ("XFF", key) == 0)
1155 double tmp = atof (value);
1156 if ((tmp < 0.0) || (tmp >= 1.0))
1158 fprintf (stderr, "rrdtool: `XFF' must "
1159 "be in the range 0 to 1 (exclusive).");
1169 } /* int rrd_config */
1171 static int rrd_shutdown (void)
1173 pthread_mutex_lock (&cache_lock);
1174 rrd_cache_flush (-1);
1175 pthread_mutex_unlock (&cache_lock);
1177 pthread_mutex_lock (&queue_lock);
1179 pthread_cond_signal (&queue_cond);
1180 pthread_mutex_unlock (&queue_lock);
1182 /* Wait for all the values to be written to disk before returning. */
1183 if (queue_thread != 0)
1185 pthread_join (queue_thread, NULL);
1187 DEBUG ("rrdtool plugin: queue_thread exited.");
1191 } /* int rrd_shutdown */
1193 static int rrd_init (void)
1200 heartbeat = 2 * stepsize;
1202 if ((heartbeat > 0) && (heartbeat < interval_g))
1203 WARNING ("rrdtool plugin: Your `heartbeat' is "
1204 "smaller than your `interval'. This will "
1205 "likely cause problems.");
1206 else if ((stepsize > 0) && (stepsize < interval_g))
1207 WARNING ("rrdtool plugin: Your `stepsize' is "
1208 "smaller than your `interval'. This will "
1209 "create needlessly big RRD-files.");
1211 /* Set the cache up */
1212 pthread_mutex_lock (&cache_lock);
1214 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1217 ERROR ("rrdtool plugin: c_avl_create failed.");
1221 cache_flush_last = time (NULL);
1222 if (cache_timeout < 2)
1225 cache_flush_timeout = 0;
1227 else if (cache_flush_timeout < cache_timeout)
1228 cache_flush_timeout = 10 * cache_timeout;
1230 pthread_mutex_unlock (&cache_lock);
1232 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
1235 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1239 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1240 " heartbeat = %i; rrarows = %i; xff = %lf;",
1241 (datadir == NULL) ? "(null)" : datadir,
1242 stepsize, heartbeat, rrarows, xff);
1245 } /* int rrd_init */
1247 void module_register (void)
1249 plugin_register_config ("rrdtool", rrd_config,
1250 config_keys, config_keys_num);
1251 plugin_register_init ("rrdtool", rrd_init);
1252 plugin_register_write ("rrdtool", rrd_write);
1253 plugin_register_flush ("rrdtool", rrd_flush);
1254 plugin_register_shutdown ("rrdtool", rrd_shutdown);