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"
26 #include "utils_rrdcreate.h"
49 typedef struct rrd_cache_s rrd_cache_t;
56 typedef enum rrd_queue_dir_e rrd_queue_dir_t;
61 struct rrd_queue_s *next;
63 typedef struct rrd_queue_s rrd_queue_t;
68 static const char *config_keys[] =
79 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
81 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
82 * is zero a default, depending on the `interval' member of the value list is
84 static char *datadir = NULL;
85 static rrdcreate_config_t rrdcreate_config =
92 /* timespans = */ NULL,
93 /* timespans_num = */ 0,
95 /* consolidation_functions = */ NULL,
96 /* consolidation_functions_num = */ 0
99 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
100 * ALWAYS lock `cache_lock' first! */
101 static int cache_timeout = 0;
102 static int cache_flush_timeout = 0;
103 static time_t cache_flush_last;
104 static c_avl_tree_t *cache = NULL;
105 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
107 static rrd_queue_t *queue_head = NULL;
108 static rrd_queue_t *queue_tail = NULL;
109 static pthread_t queue_thread = 0;
110 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
111 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
113 #if !HAVE_THREADSAFE_LIBRRD
114 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
117 static int do_shutdown = 0;
119 #if HAVE_THREADSAFE_LIBRRD
120 static int srrd_update (char *filename, char *template,
121 int argc, const char **argv)
125 optind = 0; /* bug in librrd? */
128 status = rrd_update_r (filename, template, argc, (void *) argv);
132 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
133 filename, rrd_get_error ());
137 } /* int srrd_update */
138 /* #endif HAVE_THREADSAFE_LIBRRD */
140 #else /* !HAVE_THREADSAFE_LIBRRD */
141 static int srrd_update (char *filename, char *template,
142 int argc, const char **argv)
149 assert (template == NULL);
152 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
153 if (new_argv == NULL)
155 ERROR ("rrdtool plugin: malloc failed.");
159 new_argv[0] = "update";
160 new_argv[1] = filename;
162 memcpy (new_argv + 2, argv, argc * sizeof (char *));
163 new_argv[new_argc] = NULL;
165 pthread_mutex_lock (&librrd_lock);
166 optind = 0; /* bug in librrd? */
169 status = rrd_update (new_argc, new_argv);
170 pthread_mutex_unlock (&librrd_lock);
174 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
175 argv[1], rrd_get_error ());
181 } /* int srrd_update */
182 #endif /* !HAVE_THREADSAFE_LIBRRD */
184 static int value_list_to_string (char *buffer, int buffer_len,
185 const data_set_t *ds, const value_list_t *vl)
191 memset (buffer, '\0', buffer_len);
193 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
194 if ((status < 1) || (status >= buffer_len))
198 for (i = 0; i < ds->ds_num; i++)
200 if ((ds->ds[i].type != DS_TYPE_COUNTER)
201 && (ds->ds[i].type != DS_TYPE_GAUGE))
204 if (ds->ds[i].type == DS_TYPE_COUNTER)
205 status = ssnprintf (buffer + offset, buffer_len - offset,
206 ":%llu", vl->values[i].counter);
208 status = ssnprintf (buffer + offset, buffer_len - offset,
209 ":%lf", vl->values[i].gauge);
211 if ((status < 1) || (status >= (buffer_len - offset)))
215 } /* for ds->ds_num */
218 } /* int value_list_to_string */
220 static int value_list_to_filename (char *buffer, int buffer_len,
221 const data_set_t *ds, const value_list_t *vl)
228 status = ssnprintf (buffer + offset, buffer_len - offset,
230 if ((status < 1) || (status >= buffer_len - offset))
235 status = ssnprintf (buffer + offset, buffer_len - offset,
237 if ((status < 1) || (status >= buffer_len - offset))
241 if (strlen (vl->plugin_instance) > 0)
242 status = ssnprintf (buffer + offset, buffer_len - offset,
243 "%s-%s/", vl->plugin, vl->plugin_instance);
245 status = ssnprintf (buffer + offset, buffer_len - offset,
247 if ((status < 1) || (status >= buffer_len - offset))
251 if (strlen (vl->type_instance) > 0)
252 status = ssnprintf (buffer + offset, buffer_len - offset,
253 "%s-%s.rrd", vl->type, vl->type_instance);
255 status = ssnprintf (buffer + offset, buffer_len - offset,
257 if ((status < 1) || (status >= buffer_len - offset))
262 } /* int value_list_to_filename */
264 static void *rrd_queue_thread (void *data)
268 rrd_queue_t *queue_entry;
269 rrd_cache_t *cache_entry;
274 /* XXX: If you need to lock both, cache_lock and queue_lock, at
275 * the same time, ALWAYS lock `cache_lock' first! */
277 /* wait until an entry is available */
278 pthread_mutex_lock (&queue_lock);
279 while ((queue_head == NULL) && (do_shutdown == 0))
280 pthread_cond_wait (&queue_cond, &queue_lock);
282 /* We're in the shutdown phase */
283 if (queue_head == NULL)
285 pthread_mutex_unlock (&queue_lock);
289 /* Dequeue the first entry */
290 queue_entry = queue_head;
291 if (queue_head == queue_tail)
292 queue_head = queue_tail = NULL;
294 queue_head = queue_head->next;
296 /* Unlock the queue again */
297 pthread_mutex_unlock (&queue_lock);
299 /* We now need the cache lock so the entry isn't updated while
300 * we make a copy of it's values */
301 pthread_mutex_lock (&cache_lock);
303 c_avl_get (cache, queue_entry->filename, (void *) &cache_entry);
305 values = cache_entry->values;
306 values_num = cache_entry->values_num;
308 cache_entry->values = NULL;
309 cache_entry->values_num = 0;
310 cache_entry->flags = FLAG_NONE;
312 pthread_mutex_unlock (&cache_lock);
314 /* Write the values to the RRD-file */
315 srrd_update (queue_entry->filename, NULL,
316 values_num, (const char **)values);
317 DEBUG ("rrdtool plugin: queue thread: Wrote %i values to %s",
318 values_num, queue_entry->filename);
320 for (i = 0; i < values_num; i++)
325 sfree (queue_entry->filename);
329 pthread_mutex_lock (&cache_lock);
330 c_avl_destroy (cache);
332 pthread_mutex_unlock (&cache_lock);
334 pthread_exit ((void *) 0);
336 } /* void *rrd_queue_thread */
338 static int rrd_queue_cache_entry (const char *filename, rrd_queue_dir_t dir)
340 rrd_queue_t *queue_entry;
342 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
343 if (queue_entry == NULL)
346 queue_entry->filename = strdup (filename);
347 if (queue_entry->filename == NULL)
353 queue_entry->next = NULL;
355 pthread_mutex_lock (&queue_lock);
356 if (dir == QUEUE_INSERT_FRONT)
358 queue_entry->next = queue_head;
359 queue_head = queue_entry;
360 if (queue_tail == NULL)
361 queue_tail = queue_head;
363 else /* (dir == QUEUE_INSERT_BACK) */
365 if (queue_tail == NULL)
366 queue_head = queue_entry;
368 queue_tail->next = queue_entry;
369 queue_tail = queue_entry;
371 pthread_cond_signal (&queue_cond);
372 pthread_mutex_unlock (&queue_lock);
374 DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
377 } /* int rrd_queue_cache_entry */
379 static int rrd_queue_move_to_front (const char *filename)
386 pthread_mutex_lock (&queue_lock);
387 for (this = queue_head; this != NULL; this = this->next)
389 if (strcmp (this->filename, filename) == 0)
394 /* Check if we found the entry and if it is NOT the first entry. */
395 if ((this != NULL) && (prev != NULL))
397 prev->next = this->next;
398 this->next = queue_head;
401 pthread_mutex_unlock (&queue_lock);
404 } /* int rrd_queue_move_to_front */
406 static void rrd_cache_flush (int timeout)
415 c_avl_iterator_t *iter;
418 DEBUG ("rrdtool plugin: Flushing cache, timeout = %i", timeout);
422 /* Build a list of entries to be flushed */
423 iter = c_avl_get_iterator (cache);
424 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
426 if (rc->flags == FLAG_QUEUED)
428 else if ((now - rc->first_value) < timeout)
430 else if (rc->values_num > 0)
432 if (rrd_queue_cache_entry (key, QUEUE_INSERT_BACK) == 0)
433 rc->flags = FLAG_QUEUED;
435 else /* ancient and no values -> waste of memory */
437 char **tmp = (char **) realloc ((void *) keys,
438 (keys_num + 1) * sizeof (char *));
442 ERROR ("rrdtool plugin: "
443 "realloc failed: %s",
444 sstrerror (errno, errbuf,
446 c_avl_iterator_destroy (iter);
451 keys[keys_num] = key;
454 } /* while (c_avl_iterator_next) */
455 c_avl_iterator_destroy (iter);
457 for (i = 0; i < keys_num; i++)
459 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
461 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
465 assert (rc->values == NULL);
466 assert (rc->values_num == 0);
471 } /* for (i = 0..keys_num) */
475 cache_flush_last = now;
476 } /* void rrd_cache_flush */
478 static int rrd_cache_flush_identifier (int timeout, const char *identifier)
485 if (identifier == NULL)
487 rrd_cache_flush (timeout);
494 snprintf (key, sizeof (key), "%s.rrd",
497 snprintf (key, sizeof (key), "%s/%s.rrd",
498 datadir, identifier);
499 key[sizeof (key) - 1] = 0;
501 status = c_avl_get (cache, key, (void *) &rc);
504 WARNING ("rrdtool plugin: rrd_cache_flush_identifier: "
505 "c_avl_get (%s) failed. Does that file really exist?",
510 if (rc->flags == FLAG_QUEUED)
511 status = rrd_queue_move_to_front (key);
512 else if ((now - rc->first_value) < timeout)
514 else if (rc->values_num > 0)
516 status = rrd_queue_cache_entry (key, QUEUE_INSERT_FRONT);
518 rc->flags = FLAG_QUEUED;
522 } /* int rrd_cache_flush_identifier */
524 static int rrd_cache_insert (const char *filename,
525 const char *value, time_t value_time)
527 rrd_cache_t *rc = NULL;
531 pthread_mutex_lock (&cache_lock);
533 c_avl_get (cache, filename, (void *) &rc);
537 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
544 rc->flags = FLAG_NONE;
548 if (rc->last_value >= value_time)
550 pthread_mutex_unlock (&cache_lock);
551 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
552 (unsigned int) rc->last_value,
553 (unsigned int) value_time);
557 values_new = (char **) realloc ((void *) rc->values,
558 (rc->values_num + 1) * sizeof (char *));
559 if (values_new == NULL)
562 void *cache_key = NULL;
564 sstrerror (errno, errbuf, sizeof (errbuf));
566 c_avl_remove (cache, filename, &cache_key, NULL);
567 pthread_mutex_unlock (&cache_lock);
569 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
576 rc->values = values_new;
578 rc->values[rc->values_num] = strdup (value);
579 if (rc->values[rc->values_num] != NULL)
582 if (rc->values_num == 1)
583 rc->first_value = value_time;
584 rc->last_value = value_time;
586 /* Insert if this is the first value */
589 void *cache_key = strdup (filename);
591 if (cache_key == NULL)
594 sstrerror (errno, errbuf, sizeof (errbuf));
596 pthread_mutex_unlock (&cache_lock);
598 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
600 sfree (rc->values[0]);
606 c_avl_insert (cache, cache_key, rc);
609 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
610 "values_num = %i; age = %lu;",
611 filename, rc->values_num,
612 (unsigned long)(rc->last_value - rc->first_value));
614 if ((rc->last_value - rc->first_value) >= cache_timeout)
616 /* XXX: If you need to lock both, cache_lock and queue_lock, at
617 * the same time, ALWAYS lock `cache_lock' first! */
618 if (rc->flags != FLAG_QUEUED)
620 if (rrd_queue_cache_entry (filename, QUEUE_INSERT_BACK) == 0)
621 rc->flags = FLAG_QUEUED;
625 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
629 if ((cache_timeout > 0) &&
630 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
631 rrd_cache_flush (cache_flush_timeout);
634 pthread_mutex_unlock (&cache_lock);
637 } /* int rrd_cache_insert */
639 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
641 int a = *((int *) a_ptr);
642 int b = *((int *) b_ptr);
650 } /* int rrd_compare_numeric */
652 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
659 if (0 != strcmp (ds->type, vl->type)) {
660 ERROR ("rrdtool plugin: DS type does not match value list type");
664 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
667 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
670 if (stat (filename, &statbuf) == -1)
674 status = cu_rrd_create_file (filename,
675 ds, vl, &rrdcreate_config);
682 ERROR ("stat(%s) failed: %s", filename,
683 sstrerror (errno, errbuf,
688 else if (!S_ISREG (statbuf.st_mode))
690 ERROR ("stat(%s): Not a regular file!",
695 status = rrd_cache_insert (filename, values, vl->time);
698 } /* int rrd_write */
700 static int rrd_flush (int timeout, const char *identifier)
702 pthread_mutex_lock (&cache_lock);
705 pthread_mutex_unlock (&cache_lock);
709 rrd_cache_flush_identifier (timeout, identifier);
711 pthread_mutex_unlock (&cache_lock);
713 } /* int rrd_flush */
715 static int rrd_config (const char *key, const char *value)
717 if (strcasecmp ("CacheTimeout", key) == 0)
719 int tmp = atoi (value);
722 fprintf (stderr, "rrdtool: `CacheTimeout' must "
723 "be greater than 0.\n");
728 else if (strcasecmp ("CacheFlush", key) == 0)
730 int tmp = atoi (value);
733 fprintf (stderr, "rrdtool: `CacheFlush' must "
734 "be greater than 0.\n");
737 cache_flush_timeout = tmp;
739 else if (strcasecmp ("DataDir", key) == 0)
743 datadir = strdup (value);
746 int len = strlen (datadir);
747 while ((len > 0) && (datadir[len - 1] == '/'))
759 else if (strcasecmp ("StepSize", key) == 0)
761 int temp = atoi (value);
763 rrdcreate_config.stepsize = temp;
765 else if (strcasecmp ("HeartBeat", key) == 0)
767 int temp = atoi (value);
769 rrdcreate_config.heartbeat = temp;
771 else if (strcasecmp ("RRARows", key) == 0)
773 int tmp = atoi (value);
776 fprintf (stderr, "rrdtool: `RRARows' must "
777 "be greater than 0.\n");
780 rrdcreate_config.rrarows = tmp;
782 else if (strcasecmp ("RRATimespan", key) == 0)
784 char *saveptr = NULL;
790 value_copy = strdup (value);
791 if (value_copy == NULL)
795 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
799 tmp_alloc = realloc (rrdcreate_config.timespans,
800 sizeof (int) * (rrdcreate_config.timespans_num + 1));
801 if (tmp_alloc == NULL)
803 fprintf (stderr, "rrdtool: realloc failed.\n");
807 rrdcreate_config.timespans = tmp_alloc;
808 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = atoi (ptr);
809 if (rrdcreate_config.timespans[rrdcreate_config.timespans_num] != 0)
810 rrdcreate_config.timespans_num++;
811 } /* while (strtok_r) */
813 qsort (/* base = */ rrdcreate_config.timespans,
814 /* nmemb = */ rrdcreate_config.timespans_num,
815 /* size = */ sizeof (rrdcreate_config.timespans[0]),
816 /* compar = */ rrd_compare_numeric);
820 else if (strcasecmp ("XFF", key) == 0)
822 double tmp = atof (value);
823 if ((tmp < 0.0) || (tmp >= 1.0))
825 fprintf (stderr, "rrdtool: `XFF' must "
826 "be in the range 0 to 1 (exclusive).");
829 rrdcreate_config.xff = tmp;
836 } /* int rrd_config */
838 static int rrd_shutdown (void)
840 pthread_mutex_lock (&cache_lock);
841 rrd_cache_flush (-1);
842 pthread_mutex_unlock (&cache_lock);
844 pthread_mutex_lock (&queue_lock);
846 pthread_cond_signal (&queue_cond);
847 pthread_mutex_unlock (&queue_lock);
849 /* Wait for all the values to be written to disk before returning. */
850 if (queue_thread != 0)
852 pthread_join (queue_thread, NULL);
854 DEBUG ("rrdtool plugin: queue_thread exited.");
858 } /* int rrd_shutdown */
860 static int rrd_init (void)
864 if (rrdcreate_config.stepsize < 0)
865 rrdcreate_config.stepsize = 0;
866 if (rrdcreate_config.heartbeat <= 0)
867 rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize;
869 if ((rrdcreate_config.heartbeat > 0)
870 && (rrdcreate_config.heartbeat < interval_g))
871 WARNING ("rrdtool plugin: Your `heartbeat' is "
872 "smaller than your `interval'. This will "
873 "likely cause problems.");
874 else if ((rrdcreate_config.stepsize > 0)
875 && (rrdcreate_config.stepsize < interval_g))
876 WARNING ("rrdtool plugin: Your `stepsize' is "
877 "smaller than your `interval'. This will "
878 "create needlessly big RRD-files.");
880 /* Set the cache up */
881 pthread_mutex_lock (&cache_lock);
883 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
886 ERROR ("rrdtool plugin: c_avl_create failed.");
890 cache_flush_last = time (NULL);
891 if (cache_timeout < 2)
894 cache_flush_timeout = 0;
896 else if (cache_flush_timeout < cache_timeout)
897 cache_flush_timeout = 10 * cache_timeout;
899 pthread_mutex_unlock (&cache_lock);
901 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
904 ERROR ("rrdtool plugin: Cannot create queue-thread.");
908 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
909 " heartbeat = %i; rrarows = %i; xff = %lf;",
910 (datadir == NULL) ? "(null)" : datadir,
911 rrdcreate_config.stepsize,
912 rrdcreate_config.heartbeat,
913 rrdcreate_config.rrarows,
914 rrdcreate_config.xff);
919 void module_register (void)
921 plugin_register_config ("rrdtool", rrd_config,
922 config_keys, config_keys_num);
923 plugin_register_init ("rrdtool", rrd_init);
924 plugin_register_write ("rrdtool", rrd_write);
925 plugin_register_flush ("rrdtool", rrd_flush);
926 plugin_register_shutdown ("rrdtool", rrd_shutdown);