2 * collectd - src/rrdtool.c
3 * Copyright (C) 2006,2007 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"
46 typedef struct rrd_cache_s rrd_cache_t;
51 struct rrd_queue_s *next;
53 typedef struct rrd_queue_s rrd_queue_t;
58 static int rra_timespans[] =
66 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
68 static int *rra_timespans_custom = NULL;
69 static int rra_timespans_custom_num = 0;
71 static char *rra_types[] =
77 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
79 static const char *config_keys[] =
90 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
92 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
93 * is zero a default, depending on the `interval' member of the value list is
95 static char *datadir = NULL;
96 static int stepsize = 0;
97 static int heartbeat = 0;
98 static int rrarows = 1200;
99 static double xff = 0.1;
101 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
102 * ALWAYS lock `cache_lock' first! */
103 static int cache_timeout = 0;
104 static int cache_flush_timeout = 0;
105 static time_t cache_flush_last;
106 static avl_tree_t *cache = NULL;
107 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
109 static rrd_queue_t *queue_head = NULL;
110 static rrd_queue_t *queue_tail = NULL;
111 static pthread_t queue_thread = 0;
112 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
113 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
115 static int do_shutdown = 0;
121 static void rra_free (int rra_num, char **rra_def)
125 for (i = 0; i < rra_num; i++)
130 } /* void rra_free */
132 static int rra_get (char ***ret, const value_list_t *vl)
150 /* The stepsize we use here: If it is user-set, use it. If not, use the
151 * interval of the value-list. */
160 ss = (stepsize > 0) ? stepsize : vl->interval;
167 /* Use the configured timespans or fall back to the built-in defaults */
168 if (rra_timespans_custom_num != 0)
170 rts = rra_timespans_custom;
171 rts_num = rra_timespans_custom_num;
176 rts_num = rra_timespans_num;
179 rra_max = rts_num * rra_types_num;
181 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
183 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
187 for (i = 0; i < rts_num; i++)
191 if ((span / ss) < rrarows)
197 cdp_len = (int) floor (((double) span)
198 / ((double) (rrarows * ss)));
200 cdp_num = (int) ceil (((double) span)
201 / ((double) (cdp_len * ss)));
203 for (j = 0; j < rra_types_num; j++)
205 if (rra_num >= rra_max)
208 if (snprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
210 cdp_len, cdp_num) >= sizeof (buffer))
212 ERROR ("rra_get: Buffer would have been truncated.");
216 rra_def[rra_num++] = sstrdup (buffer);
221 DEBUG ("rra_num = %i", rra_num);
222 for (i = 0; i < rra_num; i++)
223 DEBUG (" %s", rra_def[i]);
230 static void ds_free (int ds_num, char **ds_def)
234 for (i = 0; i < ds_num; i++)
235 if (ds_def[i] != NULL)
240 static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
249 DEBUG ("ds->ds_num = %i", ds->ds_num);
251 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
255 ERROR ("rrdtool plugin: malloc failed: %s",
256 sstrerror (errno, errbuf, sizeof (errbuf)));
259 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
261 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
263 data_source_t *d = ds->ds + ds_num;
267 ds_def[ds_num] = NULL;
269 if (d->type == DS_TYPE_COUNTER)
271 else if (d->type == DS_TYPE_GAUGE)
275 ERROR ("rrdtool plugin: Unknown DS type: %i",
286 snprintf (min, sizeof (min), "%lf", d->min);
287 min[sizeof (min) - 1] = '\0';
296 snprintf (max, sizeof (max), "%lf", d->max);
297 max[sizeof (max) - 1] = '\0';
300 status = snprintf (buffer, sizeof (buffer),
303 (heartbeat > 0) ? heartbeat : (2 * vl->interval),
305 if ((status < 1) || (status >= sizeof (buffer)))
308 ds_def[ds_num] = sstrdup (buffer);
309 } /* for ds_num = 0 .. ds->ds_num */
314 DEBUG ("ds_num = %i", ds_num);
315 for (i = 0; i < ds_num; i++)
316 DEBUG (" %s", ds_def[i]);
320 if (ds_num != ds->ds_num)
322 ds_free (ds_num, ds_def);
330 static int rrd_create_file (char *filename, const data_set_t *ds, const value_list_t *vl)
339 char stepsize_str[16];
342 if (check_create_dir (filename))
345 if ((rra_num = rra_get (&rra_def, vl)) < 1)
347 ERROR ("rrd_create_file failed: Could not calculate RRAs");
351 if ((ds_num = ds_get (&ds_def, ds, vl)) < 1)
353 ERROR ("rrd_create_file failed: Could not calculate DSes");
357 argc = ds_num + rra_num + 4;
359 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
362 ERROR ("rrd_create failed: %s",
363 sstrerror (errno, errbuf, sizeof (errbuf)));
367 status = snprintf (stepsize_str, sizeof (stepsize_str),
368 "%i", (stepsize > 0) ? stepsize : vl->interval);
369 if ((status < 1) || (status >= sizeof (stepsize_str)))
371 ERROR ("rrdtool plugin: snprintf failed.");
373 ds_free (ds_num, ds_def);
374 rra_free (rra_num, rra_def);
381 argv[3] = stepsize_str;
384 for (i = 0; i < ds_num; i++)
385 argv[j++] = ds_def[i];
386 for (i = 0; i < rra_num; i++)
387 argv[j++] = rra_def[i];
390 optind = 0; /* bug in librrd? */
392 if (rrd_create (argc, argv) == -1)
394 ERROR ("rrd_create failed: %s: %s", filename, rrd_get_error ());
399 ds_free (ds_num, ds_def);
400 rra_free (rra_num, rra_def);
405 static int value_list_to_string (char *buffer, int buffer_len,
406 const data_set_t *ds, const value_list_t *vl)
412 memset (buffer, '\0', buffer_len);
414 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
415 if ((status < 1) || (status >= buffer_len))
419 for (i = 0; i < ds->ds_num; i++)
421 if ((ds->ds[i].type != DS_TYPE_COUNTER)
422 && (ds->ds[i].type != DS_TYPE_GAUGE))
425 if (ds->ds[i].type == DS_TYPE_COUNTER)
426 status = snprintf (buffer + offset, buffer_len - offset,
427 ":%llu", vl->values[i].counter);
429 status = snprintf (buffer + offset, buffer_len - offset,
430 ":%lf", vl->values[i].gauge);
432 if ((status < 1) || (status >= (buffer_len - offset)))
436 } /* for ds->ds_num */
439 } /* int value_list_to_string */
441 static int value_list_to_filename (char *buffer, int buffer_len,
442 const data_set_t *ds, const value_list_t *vl)
449 status = snprintf (buffer + offset, buffer_len - offset,
451 if ((status < 1) || (status >= buffer_len - offset))
456 status = snprintf (buffer + offset, buffer_len - offset,
458 if ((status < 1) || (status >= buffer_len - offset))
462 if (strlen (vl->plugin_instance) > 0)
463 status = snprintf (buffer + offset, buffer_len - offset,
464 "%s-%s/", vl->plugin, vl->plugin_instance);
466 status = snprintf (buffer + offset, buffer_len - offset,
468 if ((status < 1) || (status >= buffer_len - offset))
472 if (strlen (vl->type_instance) > 0)
473 status = snprintf (buffer + offset, buffer_len - offset,
474 "%s-%s.rrd", ds->type, vl->type_instance);
476 status = snprintf (buffer + offset, buffer_len - offset,
478 if ((status < 1) || (status >= buffer_len - offset))
483 } /* int value_list_to_filename */
485 static int rrd_write_to_file (char *filename, char **values, int values_num)
494 argc = values_num + 2;
495 argv = (char **) malloc ((argc + 1) * sizeof (char *));
501 memcpy (argv + 2, values, values_num * sizeof (char *));
504 DEBUG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
506 optind = 0; /* bug in librrd? */
508 status = rrd_update (argc, argv);
511 WARNING ("rrd_update failed: %s: %s",
512 filename, rrd_get_error ());
519 } /* int rrd_write_cache_entry */
521 static void *rrd_queue_thread (void *data)
525 rrd_queue_t *queue_entry;
526 rrd_cache_t *cache_entry;
531 /* XXX: If you need to lock both, cache_lock and queue_lock, at
532 * the same time, ALWAYS lock `cache_lock' first! */
534 /* wait until an entry is available */
535 pthread_mutex_lock (&queue_lock);
536 while ((queue_head == NULL) && (do_shutdown == 0))
537 pthread_cond_wait (&queue_cond, &queue_lock);
539 /* We're in the shutdown phase */
540 if (queue_head == NULL)
542 pthread_mutex_unlock (&queue_lock);
546 /* Dequeue the first entry */
547 queue_entry = queue_head;
548 if (queue_head == queue_tail)
549 queue_head = queue_tail = NULL;
551 queue_head = queue_head->next;
553 /* Unlock the queue again */
554 pthread_mutex_unlock (&queue_lock);
556 /* We now need the cache lock so the entry isn't updated while
557 * we make a copy of it's values */
558 pthread_mutex_lock (&cache_lock);
560 avl_get (cache, queue_entry->filename, (void *) &cache_entry);
562 values = cache_entry->values;
563 values_num = cache_entry->values_num;
565 cache_entry->values = NULL;
566 cache_entry->values_num = 0;
567 cache_entry->flags = FLAG_NONE;
569 pthread_mutex_unlock (&cache_lock);
571 /* Write the values to the RRD-file */
572 rrd_write_to_file (queue_entry->filename, values, values_num);
574 for (i = 0; i < values_num; i++)
579 sfree (queue_entry->filename);
583 pthread_mutex_lock (&cache_lock);
586 pthread_mutex_unlock (&cache_lock);
588 pthread_exit ((void *) 0);
590 } /* void *rrd_queue_thread */
592 static int rrd_queue_cache_entry (const char *filename)
594 rrd_queue_t *queue_entry;
596 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
597 if (queue_entry == NULL)
600 queue_entry->filename = strdup (filename);
601 if (queue_entry->filename == NULL)
607 queue_entry->next = NULL;
609 pthread_mutex_lock (&queue_lock);
610 if (queue_tail == NULL)
611 queue_head = queue_entry;
613 queue_tail->next = queue_entry;
614 queue_tail = queue_entry;
615 pthread_cond_signal (&queue_cond);
616 pthread_mutex_unlock (&queue_lock);
618 DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
621 } /* int rrd_queue_cache_entry */
623 static void rrd_cache_flush (int timeout)
632 avl_iterator_t *iter;
635 DEBUG ("Flushing cache, timeout = %i", timeout);
639 /* Build a list of entries to be flushed */
640 iter = avl_get_iterator (cache);
641 while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
643 DEBUG ("key = %s; age = %i;", key, now - rc->first_value);
645 if (rc->flags == FLAG_QUEUED)
647 else if ((now - rc->first_value) < timeout)
649 else if (rc->values_num > 0)
651 if (rrd_queue_cache_entry (key) == 0)
652 rc->flags = FLAG_QUEUED;
654 else /* ancient and no values -> waste of memory */
656 keys = (char **) realloc ((void *) keys,
657 (keys_num + 1) * sizeof (char *));
661 ERROR ("rrdtool plugin: "
662 "realloc failed: %s",
663 sstrerror (errno, errbuf,
665 avl_iterator_destroy (iter);
668 keys[keys_num] = key;
671 } /* while (avl_iterator_next) */
672 avl_iterator_destroy (iter);
674 for (i = 0; i < keys_num; i++)
676 if (avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
678 DEBUG ("avl_remove (%s) failed.", keys[i]);
682 assert (rc->values == NULL);
683 assert (rc->values_num == 0);
688 } /* for (i = 0..keys_num) */
691 DEBUG ("Flushed %i value(s)", keys_num);
693 cache_flush_last = now;
694 } /* void rrd_cache_flush */
696 static int rrd_cache_insert (const char *filename,
697 const char *value, time_t value_time)
699 rrd_cache_t *rc = NULL;
703 pthread_mutex_lock (&cache_lock);
705 avl_get (cache, filename, (void *) &rc);
709 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
716 rc->flags = FLAG_NONE;
720 if (rc->last_value >= value_time)
722 pthread_mutex_unlock (&cache_lock);
723 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
724 (unsigned int) rc->last_value,
725 (unsigned int) value_time);
729 values_new = (char **) realloc ((void *) rc->values,
730 (rc->values_num + 1) * sizeof (char *));
731 if (values_new == NULL)
734 void *cache_key = NULL;
736 sstrerror (errno, errbuf, sizeof (errbuf));
738 avl_remove (cache, filename, &cache_key, NULL);
739 pthread_mutex_unlock (&cache_lock);
741 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
748 rc->values = values_new;
750 rc->values[rc->values_num] = strdup (value);
751 if (rc->values[rc->values_num] != NULL)
754 if (rc->values_num == 1)
755 rc->first_value = value_time;
756 rc->last_value = value_time;
758 /* Insert if this is the first value */
761 void *cache_key = strdup (filename);
763 if (cache_key == NULL)
766 sstrerror (errno, errbuf, sizeof (errbuf));
768 pthread_mutex_unlock (&cache_lock);
770 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
772 sfree (rc->values[0]);
778 avl_insert (cache, cache_key, rc);
781 DEBUG ("rrd_cache_insert (%s, %s, %u) = %p", filename, value,
782 (unsigned int) value_time, (void *) rc);
784 if ((rc->last_value - rc->first_value) >= cache_timeout)
786 /* XXX: If you need to lock both, cache_lock and queue_lock, at
787 * the same time, ALWAYS lock `cache_lock' first! */
788 if (rc->flags != FLAG_QUEUED)
790 if (rrd_queue_cache_entry (filename) == 0)
791 rc->flags = FLAG_QUEUED;
795 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
799 if ((cache_timeout > 0) &&
800 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
801 rrd_cache_flush (cache_flush_timeout);
804 pthread_mutex_unlock (&cache_lock);
807 } /* int rrd_cache_insert */
809 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
816 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
819 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
822 if (stat (filename, &statbuf) == -1)
826 if (rrd_create_file (filename, ds, vl))
832 ERROR ("stat(%s) failed: %s", filename,
833 sstrerror (errno, errbuf,
838 else if (!S_ISREG (statbuf.st_mode))
840 ERROR ("stat(%s): Not a regular file!",
845 status = rrd_cache_insert (filename, values, vl->time);
848 } /* int rrd_write */
850 static int rrd_config (const char *key, const char *value)
852 if (strcasecmp ("CacheTimeout", key) == 0)
854 int tmp = atoi (value);
857 fprintf (stderr, "rrdtool: `CacheTimeout' must "
858 "be greater than 0.\n");
863 else if (strcasecmp ("CacheFlush", key) == 0)
865 int tmp = atoi (value);
868 fprintf (stderr, "rrdtool: `CacheFlush' must "
869 "be greater than 0.\n");
872 cache_flush_timeout = tmp;
874 else if (strcasecmp ("DataDir", key) == 0)
878 datadir = strdup (value);
881 int len = strlen (datadir);
882 while ((len > 0) && (datadir[len - 1] == '/'))
894 else if (strcasecmp ("StepSize", key) == 0)
896 stepsize = atoi (value);
900 else if (strcasecmp ("HeartBeat", key) == 0)
902 heartbeat = atoi (value);
906 else if (strcasecmp ("RRARows", key) == 0)
908 int tmp = atoi (value);
911 fprintf (stderr, "rrdtool: `RRARows' must "
912 "be greater than 0.\n");
917 else if (strcasecmp ("RRATimespan", key) == 0)
919 char *saveptr = NULL;
925 value_copy = strdup (value);
926 if (value_copy == NULL)
930 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
934 tmp_alloc = realloc (rra_timespans_custom,
935 sizeof (int) * (rra_timespans_custom_num + 1));
936 if (tmp_alloc == NULL)
938 fprintf (stderr, "rrdtool: realloc failed.\n");
942 rra_timespans_custom = tmp_alloc;
943 rra_timespans_custom[rra_timespans_custom_num] = atoi (ptr);
944 if (rra_timespans_custom[rra_timespans_custom_num] != 0)
945 rra_timespans_custom_num++;
946 } /* while (strtok_r) */
949 else if (strcasecmp ("XFF", key) == 0)
951 double tmp = atof (value);
952 if ((tmp < 0.0) || (tmp >= 1.0))
954 fprintf (stderr, "rrdtool: `XFF' must "
955 "be in the range 0 to 1 (exclusive).");
965 } /* int rrd_config */
967 static int rrd_shutdown (void)
969 pthread_mutex_lock (&cache_lock);
970 rrd_cache_flush (-1);
971 pthread_mutex_unlock (&cache_lock);
973 pthread_mutex_lock (&queue_lock);
975 pthread_cond_signal (&queue_cond);
976 pthread_mutex_unlock (&queue_lock);
979 } /* int rrd_shutdown */
981 static int rrd_init (void)
990 heartbeat = 2 * stepsize;
995 if ((heartbeat > 0) && (heartbeat < interval_g))
996 WARNING ("rrdtool plugin: Your `heartbeat' is "
997 "smaller than your `interval'. This will "
998 "likely cause problems.");
999 else if ((stepsize > 0) && (stepsize < interval_g))
1000 WARNING ("rrdtool plugin: Your `stepsize' is "
1001 "smaller than your `interval'. This will "
1002 "create needlessly big RRD-files.");
1004 /* Set the cache up */
1005 pthread_mutex_lock (&cache_lock);
1007 cache = avl_create ((int (*) (const void *, const void *)) strcmp);
1010 ERROR ("rrdtool plugin: avl_create failed.");
1014 cache_flush_last = time (NULL);
1015 if (cache_timeout < 2)
1018 cache_flush_timeout = 0;
1020 else if (cache_flush_timeout < cache_timeout)
1021 cache_flush_timeout = 10 * cache_timeout;
1023 pthread_mutex_unlock (&cache_lock);
1025 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
1028 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1032 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1033 " heartbeat = %i; rrarows = %i; xff = %lf;",
1034 (datadir == NULL) ? "(null)" : datadir,
1035 stepsize, heartbeat, rrarows, xff);
1038 } /* int rrd_init */
1040 void module_register (void)
1042 plugin_register_config ("rrdtool", rrd_config,
1043 config_keys, config_keys_num);
1044 plugin_register_init ("rrdtool", rrd_init);
1045 plugin_register_write ("rrdtool", rrd_write);
1046 plugin_register_shutdown ("rrdtool", rrd_shutdown);