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 static char *datadir = NULL;
93 static int stepsize = 0;
94 static int heartbeat = 0;
95 static int rrarows = 1200;
96 static double xff = 0.1;
98 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
99 * ALWAYS lock `cache_lock' first! */
100 static int cache_timeout = 0;
101 static int cache_flush_timeout = 0;
102 static time_t cache_flush_last;
103 static avl_tree_t *cache = NULL;
104 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
106 static rrd_queue_t *queue_head = NULL;
107 static rrd_queue_t *queue_tail = NULL;
108 static pthread_t queue_thread = 0;
109 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
110 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
112 static int do_shutdown = 0;
117 static int rra_get (char ***ret)
119 static char **rra_def = NULL;
120 static int rra_num = 0;
135 if ((rra_num != 0) && (rra_def != NULL))
141 /* Use the configured timespans or fall back to the built-in defaults */
142 if (rra_timespans_custom_num != 0)
144 rts = rra_timespans_custom;
145 rts_num = rra_timespans_custom_num;
150 rts_num = rra_timespans_num;
153 rra_max = rts_num * rra_types_num;
155 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
157 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
159 if ((stepsize <= 0) || (rrarows <= 0))
166 for (i = 0; i < rts_num; i++)
170 if ((span / stepsize) < rrarows)
176 cdp_len = (int) floor (((double) span)
177 / ((double) (rrarows * stepsize)));
179 cdp_num = (int) ceil (((double) span)
180 / ((double) (cdp_len * stepsize)));
182 for (j = 0; j < rra_types_num; j++)
184 if (rra_num >= rra_max)
187 if (snprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
189 cdp_len, cdp_num) >= sizeof (buffer))
191 ERROR ("rra_get: Buffer would have been truncated.");
195 rra_def[rra_num++] = sstrdup (buffer);
200 DEBUG ("rra_num = %i", rra_num);
201 for (i = 0; i < rra_num; i++)
202 DEBUG (" %s", rra_def[i]);
209 static void ds_free (int ds_num, char **ds_def)
213 for (i = 0; i < ds_num; i++)
214 if (ds_def[i] != NULL)
219 static int ds_get (char ***ret, const data_set_t *ds)
228 DEBUG ("ds->ds_num = %i", ds->ds_num);
230 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
234 ERROR ("rrdtool plugin: malloc failed: %s",
235 sstrerror (errno, errbuf, sizeof (errbuf)));
238 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
240 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
242 data_source_t *d = ds->ds + ds_num;
246 ds_def[ds_num] = NULL;
248 if (d->type == DS_TYPE_COUNTER)
250 else if (d->type == DS_TYPE_GAUGE)
254 ERROR ("rrdtool plugin: Unknown DS type: %i",
265 snprintf (min, sizeof (min), "%lf", d->min);
266 min[sizeof (min) - 1] = '\0';
275 snprintf (max, sizeof (max), "%lf", d->max);
276 max[sizeof (max) - 1] = '\0';
279 status = snprintf (buffer, sizeof (buffer),
281 d->name, type, heartbeat,
283 if ((status < 1) || (status >= sizeof (buffer)))
286 ds_def[ds_num] = sstrdup (buffer);
287 } /* for ds_num = 0 .. ds->ds_num */
292 DEBUG ("ds_num = %i", ds_num);
293 for (i = 0; i < ds_num; i++)
294 DEBUG (" %s", ds_def[i]);
298 if (ds_num != ds->ds_num)
300 ds_free (ds_num, ds_def);
308 static int rrd_create_file (char *filename, const data_set_t *ds)
317 char stepsize_str[16];
320 if (check_create_dir (filename))
323 if ((rra_num = rra_get (&rra_def)) < 1)
325 ERROR ("rrd_create_file failed: Could not calculate RRAs");
329 if ((ds_num = ds_get (&ds_def, ds)) < 1)
331 ERROR ("rrd_create_file failed: Could not calculate DSes");
335 argc = ds_num + rra_num + 4;
337 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
340 ERROR ("rrd_create failed: %s",
341 sstrerror (errno, errbuf, sizeof (errbuf)));
345 status = snprintf (stepsize_str, sizeof (stepsize_str),
347 if ((status < 1) || (status >= sizeof (stepsize_str)))
349 ERROR ("rrdtool plugin: snprintf failed.");
356 argv[3] = stepsize_str;
359 for (i = 0; i < ds_num; i++)
360 argv[j++] = ds_def[i];
361 for (i = 0; i < rra_num; i++)
362 argv[j++] = rra_def[i];
365 optind = 0; /* bug in librrd? */
367 if (rrd_create (argc, argv) == -1)
369 ERROR ("rrd_create failed: %s: %s", filename, rrd_get_error ());
374 ds_free (ds_num, ds_def);
379 static int value_list_to_string (char *buffer, int buffer_len,
380 const data_set_t *ds, const value_list_t *vl)
386 memset (buffer, '\0', sizeof (buffer_len));
388 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
389 if ((status < 1) || (status >= buffer_len))
393 for (i = 0; i < ds->ds_num; i++)
395 if ((ds->ds[i].type != DS_TYPE_COUNTER)
396 && (ds->ds[i].type != DS_TYPE_GAUGE))
399 if (ds->ds[i].type == DS_TYPE_COUNTER)
400 status = snprintf (buffer + offset, buffer_len - offset,
401 ":%llu", vl->values[i].counter);
403 status = snprintf (buffer + offset, buffer_len - offset,
404 ":%lf", vl->values[i].gauge);
406 if ((status < 1) || (status >= (buffer_len - offset)))
410 } /* for ds->ds_num */
413 } /* int value_list_to_string */
415 static int value_list_to_filename (char *buffer, int buffer_len,
416 const data_set_t *ds, const value_list_t *vl)
423 status = snprintf (buffer + offset, buffer_len - offset,
425 if ((status < 1) || (status >= buffer_len - offset))
430 status = snprintf (buffer + offset, buffer_len - offset,
432 if ((status < 1) || (status >= buffer_len - offset))
436 if (strlen (vl->plugin_instance) > 0)
437 status = snprintf (buffer + offset, buffer_len - offset,
438 "%s-%s/", vl->plugin, vl->plugin_instance);
440 status = snprintf (buffer + offset, buffer_len - offset,
442 if ((status < 1) || (status >= buffer_len - offset))
446 if (strlen (vl->type_instance) > 0)
447 status = snprintf (buffer + offset, buffer_len - offset,
448 "%s-%s.rrd", ds->type, vl->type_instance);
450 status = snprintf (buffer + offset, buffer_len - offset,
452 if ((status < 1) || (status >= buffer_len - offset))
457 } /* int value_list_to_filename */
459 static int rrd_write_to_file (char *filename, char **values, int values_num)
470 argc = values_num + 2;
471 argv = (char **) malloc ((argc + 1) * sizeof (char *));
477 memcpy (argv + 2, values, values_num * sizeof (char *));
480 DEBUG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
482 optind = 0; /* bug in librrd? */
484 status = rrd_update (argc, argv);
487 WARNING ("rrd_update failed: %s: %s",
488 filename, rrd_get_error ());
496 } /* int rrd_write_cache_entry */
498 static void *rrd_queue_thread (void *data)
502 rrd_queue_t *queue_entry;
503 rrd_cache_t *cache_entry;
508 /* XXX: If you need to lock both, cache_lock and queue_lock, at
509 * the same time, ALWAYS lock `cache_lock' first! */
511 /* wait until an entry is available */
512 pthread_mutex_lock (&queue_lock);
513 while ((queue_head == NULL) && (do_shutdown == 0))
514 pthread_cond_wait (&queue_cond, &queue_lock);
516 /* We're in the shutdown phase */
517 if (queue_head == NULL)
519 pthread_mutex_unlock (&queue_lock);
523 /* Dequeue the first entry */
524 queue_entry = queue_head;
525 if (queue_head == queue_tail)
526 queue_head = queue_tail = NULL;
528 queue_head = queue_head->next;
530 /* Unlock the queue again */
531 pthread_mutex_unlock (&queue_lock);
533 /* We now need the cache lock so the entry isn't updated while
534 * we make a copy of it's values */
535 pthread_mutex_lock (&cache_lock);
537 avl_get (cache, queue_entry->filename, (void *) &cache_entry);
539 values = cache_entry->values;
540 values_num = cache_entry->values_num;
542 cache_entry->values = NULL;
543 cache_entry->values_num = 0;
544 cache_entry->flags = FLAG_NONE;
546 pthread_mutex_unlock (&cache_lock);
548 /* Write the values to the RRD-file */
549 rrd_write_to_file (queue_entry->filename, values, values_num);
551 for (i = 0; i < values_num; i++)
556 sfree (queue_entry->filename);
560 pthread_mutex_lock (&cache_lock);
563 pthread_mutex_unlock (&cache_lock);
565 pthread_exit ((void *) 0);
567 } /* void *rrd_queue_thread */
569 static int rrd_queue_cache_entry (const char *filename)
571 rrd_queue_t *queue_entry;
573 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
574 if (queue_entry == NULL)
577 queue_entry->filename = strdup (filename);
578 if (queue_entry->filename == NULL)
584 queue_entry->next = NULL;
586 pthread_mutex_lock (&queue_lock);
587 if (queue_tail == NULL)
588 queue_head = queue_entry;
590 queue_tail->next = queue_entry;
591 queue_tail = queue_entry;
592 pthread_cond_signal (&queue_cond);
593 pthread_mutex_unlock (&queue_lock);
596 } /* int rrd_queue_cache_entry */
598 static void rrd_cache_flush (int timeout)
607 avl_iterator_t *iter;
610 DEBUG ("Flushing cache, timeout = %i", timeout);
614 /* Build a list of entries to be flushed */
615 iter = avl_get_iterator (cache);
616 while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
618 DEBUG ("key = %s; age = %i;", key, now - rc->first_value);
620 if (rc->flags == FLAG_QUEUED)
622 else if ((now - rc->first_value) < timeout)
624 else if (rc->values_num > 0)
626 if (rrd_queue_cache_entry (key) == 0)
627 rc->flags = FLAG_QUEUED;
629 else /* ancient and no values -> waste of memory */
631 keys = (char **) realloc ((void *) keys,
632 (keys_num + 1) * sizeof (char *));
636 ERROR ("rrdtool plugin: "
637 "realloc failed: %s",
638 sstrerror (errno, errbuf,
640 avl_iterator_destroy (iter);
643 keys[keys_num] = key;
646 } /* while (avl_iterator_next) */
647 avl_iterator_destroy (iter);
649 for (i = 0; i < keys_num; i++)
651 if (avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
653 DEBUG ("avl_remove (%s) failed.", keys[i]);
657 assert (rc->values == NULL);
658 assert (rc->values_num == 0);
663 } /* for (i = 0..keys_num) */
666 DEBUG ("Flushed %i value(s)", keys_num);
668 cache_flush_last = now;
669 } /* void rrd_cache_flush */
671 static int rrd_cache_insert (const char *filename,
672 const char *value, time_t value_time)
674 rrd_cache_t *rc = NULL;
678 pthread_mutex_lock (&cache_lock);
680 avl_get (cache, filename, (void *) &rc);
684 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
691 rc->flags = FLAG_NONE;
695 if (rc->last_value >= value_time)
697 pthread_mutex_unlock (&cache_lock);
698 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
699 (unsigned int) rc->last_value,
700 (unsigned int) value_time);
704 values_new = (char **) realloc ((void *) rc->values,
705 (rc->values_num + 1) * sizeof (char *));
706 if (values_new == NULL)
709 void *cache_key = NULL;
711 sstrerror (errno, errbuf, sizeof (errbuf));
713 avl_remove (cache, filename, &cache_key, NULL);
714 pthread_mutex_unlock (&cache_lock);
716 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
724 rc->values[rc->values_num] = strdup (value);
725 if (rc->values[rc->values_num] != NULL)
728 if (rc->values_num == 1)
729 rc->first_value = value_time;
730 rc->last_value = value_time;
732 /* Insert if this is the first value */
735 void *cache_key = strdup (filename);
737 if (cache_key == NULL)
740 sstrerror (errno, errbuf, sizeof (errbuf));
742 pthread_mutex_unlock (&cache_lock);
744 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
746 sfree (rc->values[0]);
752 avl_insert (cache, cache_key, rc);
755 DEBUG ("rrd_cache_insert (%s, %s, %u) = %p", filename, value,
756 (unsigned int) value_time, (void *) rc);
758 if (((rc->last_value - rc->first_value) >= cache_timeout)
759 && (rc->flags != FLAG_QUEUED))
761 /* XXX: If you need to lock both, cache_lock and queue_lock, at
762 * the same time, ALWAYS lock `cache_lock' first! */
763 if (rrd_queue_cache_entry (filename) == 0)
764 rc->flags = FLAG_QUEUED;
767 if ((cache_timeout > 0) &&
768 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
769 rrd_cache_flush (cache_flush_timeout);
772 pthread_mutex_unlock (&cache_lock);
775 } /* int rrd_cache_insert */
777 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
784 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
787 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
790 if (stat (filename, &statbuf) == -1)
794 if (rrd_create_file (filename, ds))
800 ERROR ("stat(%s) failed: %s", filename,
801 sstrerror (errno, errbuf,
806 else if (!S_ISREG (statbuf.st_mode))
808 ERROR ("stat(%s): Not a regular file!",
813 status = rrd_cache_insert (filename, values, vl->time);
816 } /* int rrd_write */
818 static int rrd_config (const char *key, const char *value)
820 if (strcasecmp ("CacheTimeout", key) == 0)
822 int tmp = atoi (value);
825 fprintf (stderr, "rrdtool: `CacheTimeout' must "
826 "be greater than 0.\n");
831 else if (strcasecmp ("CacheFlush", key) == 0)
833 int tmp = atoi (value);
836 fprintf (stderr, "rrdtool: `CacheFlush' must "
837 "be greater than 0.\n");
840 cache_flush_timeout = tmp;
842 else if (strcasecmp ("DataDir", key) == 0)
846 datadir = strdup (value);
849 int len = strlen (datadir);
850 while ((len > 0) && (datadir[len - 1] == '/'))
862 else if (strcasecmp ("StepSize", key) == 0)
864 int tmp = atoi (value);
867 fprintf (stderr, "rrdtool: `StepSize' must "
868 "be greater than 0.\n");
873 else if (strcasecmp ("HeartBeat", key) == 0)
875 int tmp = atoi (value);
878 fprintf (stderr, "rrdtool: `HeartBeat' must "
879 "be greater than 0.\n");
884 else if (strcasecmp ("RRARows", key) == 0)
886 int tmp = atoi (value);
889 fprintf (stderr, "rrdtool: `RRARows' must "
890 "be greater than 0.\n");
895 else if (strcasecmp ("RRATimespan", key) == 0)
897 char *saveptr = NULL;
903 value_copy = strdup (value);
904 if (value_copy == NULL)
908 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
912 tmp_alloc = realloc (rra_timespans_custom,
913 sizeof (int) * (rra_timespans_custom_num + 1));
914 if (tmp_alloc == NULL)
916 fprintf (stderr, "rrdtool: realloc failed.\n");
920 rra_timespans_custom = tmp_alloc;
921 rra_timespans_custom[rra_timespans_custom_num] = atoi (ptr);
922 if (rra_timespans_custom[rra_timespans_custom_num] != 0)
923 rra_timespans_custom_num++;
924 } /* while (strtok_r) */
927 else if (strcasecmp ("XFF", key) == 0)
929 double tmp = atof (value);
930 if ((tmp < 0.0) || (tmp >= 1.0))
932 fprintf (stderr, "rrdtool: `XFF' must "
933 "be in the range 0 to 1 (exclusive).");
943 } /* int rrd_config */
945 static int rrd_shutdown (void)
947 pthread_mutex_lock (&cache_lock);
948 rrd_cache_flush (-1);
949 pthread_mutex_unlock (&cache_lock);
951 pthread_mutex_lock (&queue_lock);
953 pthread_cond_signal (&queue_cond);
954 pthread_mutex_unlock (&queue_lock);
957 } /* int rrd_shutdown */
959 static int rrd_init (void)
964 stepsize = interval_g;
966 heartbeat = 2 * interval_g;
968 if (heartbeat < interval_g)
969 WARNING ("rrdtool plugin: Your `heartbeat' is "
970 "smaller than your `interval'. This will "
971 "likely cause problems.");
972 else if (stepsize < interval_g)
973 WARNING ("rrdtool plugin: Your `stepsize' is "
974 "smaller than your `interval'. This will "
975 "create needlessly big RRD-files.");
977 /* Set the cache up */
978 pthread_mutex_lock (&cache_lock);
980 cache = avl_create ((int (*) (const void *, const void *)) strcmp);
983 ERROR ("rrdtool plugin: avl_create failed.");
987 cache_flush_last = time (NULL);
988 if (cache_timeout < 2)
991 cache_flush_timeout = 0;
993 else if (cache_flush_timeout < cache_timeout)
994 cache_flush_timeout = 10 * cache_timeout;
996 pthread_mutex_unlock (&cache_lock);
998 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
1001 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1005 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1006 " heartbeat = %i; rrarows = %i; xff = %lf;",
1007 (datadir == NULL) ? "(null)" : datadir,
1008 stepsize, heartbeat, rrarows, xff);
1011 } /* int rrd_init */
1013 void module_register (void)
1015 plugin_register_config ("rrdtool", rrd_config,
1016 config_keys, config_keys_num);
1017 plugin_register_init ("rrdtool", rrd_init);
1018 plugin_register_write ("rrdtool", rrd_write);
1019 plugin_register_shutdown ("rrdtool", rrd_shutdown);