1 /**
2 * collectd - src/rrdtool.c
3 * Copyright (C) 2006-2008 Florian octo Forster
4 * Copyright (C) 2008-2008 Sebastian Harl
5 * Copyright (C) 2009 Mariusz Gronczewski
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <octo at verplant.org>
22 * Sebastian Harl <sh at tokkee.org>
23 * Mariusz Gronczewski <xani666 at gmail.com>
24 **/
26 #include "collectd.h"
27 #include "plugin.h"
28 #include "common.h"
29 #include "utils_avltree.h"
30 #include "utils_rrdcreate.h"
32 #include <rrd.h>
34 #if HAVE_PTHREAD_H
35 # include <pthread.h>
36 #endif
38 /*
39 * Private types
40 */
41 struct rrd_cache_s
42 {
43 int values_num;
44 char **values;
45 cdtime_t first_value;
46 cdtime_t last_value;
47 int64_t random_variation;
48 enum
49 {
50 FLAG_NONE = 0x00,
51 FLAG_QUEUED = 0x01,
52 FLAG_FLUSHQ = 0x02
53 } flags;
54 };
55 typedef struct rrd_cache_s rrd_cache_t;
57 enum rrd_queue_dir_e
58 {
59 QUEUE_INSERT_FRONT,
60 QUEUE_INSERT_BACK
61 };
62 typedef enum rrd_queue_dir_e rrd_queue_dir_t;
64 struct rrd_queue_s
65 {
66 char *filename;
67 struct rrd_queue_s *next;
68 };
69 typedef struct rrd_queue_s rrd_queue_t;
71 /*
72 * Private variables
73 */
74 static const char *config_keys[] =
75 {
76 "CacheTimeout",
77 "CacheFlush",
78 "DataDir",
79 "StepSize",
80 "HeartBeat",
81 "RRARows",
82 "RRATimespan",
83 "XFF",
84 "WritesPerSecond",
85 "RandomTimeout"
86 };
87 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
89 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
90 * is zero a default, depending on the `interval' member of the value list is
91 * being used. */
92 static char *datadir = NULL;
93 static double write_rate = 0.0;
94 static rrdcreate_config_t rrdcreate_config =
95 {
96 /* stepsize = */ 0,
97 /* heartbeat = */ 0,
98 /* rrarows = */ 1200,
99 /* xff = */ 0.1,
101 /* timespans = */ NULL,
102 /* timespans_num = */ 0,
104 /* consolidation_functions = */ NULL,
105 /* consolidation_functions_num = */ 0
106 };
108 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
109 * ALWAYS lock `cache_lock' first! */
110 static cdtime_t cache_timeout = 0;
111 static cdtime_t cache_flush_timeout = 0;
112 static cdtime_t random_timeout = TIME_T_TO_CDTIME_T (1);
113 static cdtime_t cache_flush_last;
114 static c_avl_tree_t *cache = NULL;
115 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
117 static rrd_queue_t *queue_head = NULL;
118 static rrd_queue_t *queue_tail = NULL;
119 static rrd_queue_t *flushq_head = NULL;
120 static rrd_queue_t *flushq_tail = NULL;
121 static pthread_t queue_thread;
122 static int queue_thread_running = 1;
123 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
124 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
126 #if !HAVE_THREADSAFE_LIBRRD
127 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
128 #endif
130 static int do_shutdown = 0;
132 #if HAVE_THREADSAFE_LIBRRD
133 static int srrd_update (char *filename, char *template,
134 int argc, const char **argv)
135 {
136 int status;
138 optind = 0; /* bug in librrd? */
139 rrd_clear_error ();
141 status = rrd_update_r (filename, template, argc, (void *) argv);
143 if (status != 0)
144 {
145 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
146 filename, rrd_get_error ());
147 }
149 return (status);
150 } /* int srrd_update */
151 /* #endif HAVE_THREADSAFE_LIBRRD */
153 #else /* !HAVE_THREADSAFE_LIBRRD */
154 static int srrd_update (char *filename, char *template,
155 int argc, const char **argv)
156 {
157 int status;
159 int new_argc;
160 char **new_argv;
162 assert (template == NULL);
164 new_argc = 2 + argc;
165 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
166 if (new_argv == NULL)
167 {
168 ERROR ("rrdtool plugin: malloc failed.");
169 return (-1);
170 }
172 new_argv[0] = "update";
173 new_argv[1] = filename;
175 memcpy (new_argv + 2, argv, argc * sizeof (char *));
176 new_argv[new_argc] = NULL;
178 pthread_mutex_lock (&librrd_lock);
179 optind = 0; /* bug in librrd? */
180 rrd_clear_error ();
182 status = rrd_update (new_argc, new_argv);
183 pthread_mutex_unlock (&librrd_lock);
185 if (status != 0)
186 {
187 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
188 filename, rrd_get_error ());
189 }
191 sfree (new_argv);
193 return (status);
194 } /* int srrd_update */
195 #endif /* !HAVE_THREADSAFE_LIBRRD */
197 static int value_list_to_string (char *buffer, int buffer_len,
198 const data_set_t *ds, const value_list_t *vl)
199 {
200 int offset;
201 int status;
202 time_t tt;
203 int i;
205 memset (buffer, '\0', buffer_len);
207 tt = CDTIME_T_TO_TIME_T (vl->time);
208 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) tt);
209 if ((status < 1) || (status >= buffer_len))
210 return (-1);
211 offset = status;
213 for (i = 0; i < ds->ds_num; i++)
214 {
215 if ((ds->ds[i].type != DS_TYPE_COUNTER)
216 && (ds->ds[i].type != DS_TYPE_GAUGE)
217 && (ds->ds[i].type != DS_TYPE_DERIVE)
218 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
219 return (-1);
221 if (ds->ds[i].type == DS_TYPE_COUNTER)
222 status = ssnprintf (buffer + offset, buffer_len - offset,
223 ":%llu", vl->values[i].counter);
224 else if (ds->ds[i].type == DS_TYPE_GAUGE)
225 status = ssnprintf (buffer + offset, buffer_len - offset,
226 ":%lf", vl->values[i].gauge);
227 else if (ds->ds[i].type == DS_TYPE_DERIVE)
228 status = ssnprintf (buffer + offset, buffer_len - offset,
229 ":%"PRIi64, vl->values[i].derive);
230 else /*if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */
231 status = ssnprintf (buffer + offset, buffer_len - offset,
232 ":%"PRIu64, vl->values[i].absolute);
234 if ((status < 1) || (status >= (buffer_len - offset)))
235 return (-1);
237 offset += status;
238 } /* for ds->ds_num */
240 return (0);
241 } /* int value_list_to_string */
243 static int value_list_to_filename (char *buffer, size_t buffer_size,
244 value_list_t const *vl)
245 {
246 char const suffix[] = ".rrd";
247 int status;
248 size_t len;
250 status = FORMAT_VL (buffer, buffer_size, vl);
251 if (status != 0)
252 return (status);
254 len = strlen (buffer);
255 assert (len < buffer_size);
256 buffer += len;
257 buffer_size -= len;
259 if (buffer_size <= sizeof (suffix))
260 return (ENOMEM);
262 memcpy (buffer, suffix, sizeof (suffix));
263 return (0);
264 } /* int value_list_to_filename */
266 static void *rrd_queue_thread (void __attribute__((unused)) *data)
267 {
268 struct timeval tv_next_update;
269 struct timeval tv_now;
271 gettimeofday (&tv_next_update, /* timezone = */ NULL);
273 while (42)
274 {
275 rrd_queue_t *queue_entry;
276 rrd_cache_t *cache_entry;
277 char **values;
278 int values_num;
279 int status;
280 int i;
282 values = NULL;
283 values_num = 0;
285 pthread_mutex_lock (&queue_lock);
286 /* Wait for values to arrive */
287 while (42)
288 {
289 struct timespec ts_wait;
291 while ((flushq_head == NULL) && (queue_head == NULL)
292 && (do_shutdown == 0))
293 pthread_cond_wait (&queue_cond, &queue_lock);
295 if ((flushq_head == NULL) && (queue_head == NULL))
296 break;
298 /* Don't delay if there's something to flush */
299 if (flushq_head != NULL)
300 break;
302 /* Don't delay if we're shutting down */
303 if (do_shutdown != 0)
304 break;
306 /* Don't delay if no delay was configured. */
307 if (write_rate <= 0.0)
308 break;
310 gettimeofday (&tv_now, /* timezone = */ NULL);
311 status = timeval_cmp (tv_next_update, tv_now, NULL);
312 /* We're good to go */
313 if (status <= 0)
314 break;
316 /* We're supposed to wait a bit with this update, so we'll
317 * wait for the next addition to the queue or to the end of
318 * the wait period - whichever comes first. */
319 ts_wait.tv_sec = tv_next_update.tv_sec;
320 ts_wait.tv_nsec = 1000 * tv_next_update.tv_usec;
322 status = pthread_cond_timedwait (&queue_cond, &queue_lock,
323 &ts_wait);
324 if (status == ETIMEDOUT)
325 break;
326 } /* while (42) */
328 /* XXX: If you need to lock both, cache_lock and queue_lock, at
329 * the same time, ALWAYS lock `cache_lock' first! */
331 /* We're in the shutdown phase */
332 if ((flushq_head == NULL) && (queue_head == NULL))
333 {
334 pthread_mutex_unlock (&queue_lock);
335 break;
336 }
338 if (flushq_head != NULL)
339 {
340 /* Dequeue the first flush entry */
341 queue_entry = flushq_head;
342 if (flushq_head == flushq_tail)
343 flushq_head = flushq_tail = NULL;
344 else
345 flushq_head = flushq_head->next;
346 }
347 else /* if (queue_head != NULL) */
348 {
349 /* Dequeue the first regular entry */
350 queue_entry = queue_head;
351 if (queue_head == queue_tail)
352 queue_head = queue_tail = NULL;
353 else
354 queue_head = queue_head->next;
355 }
357 /* Unlock the queue again */
358 pthread_mutex_unlock (&queue_lock);
360 /* We now need the cache lock so the entry isn't updated while
361 * we make a copy of it's values */
362 pthread_mutex_lock (&cache_lock);
364 status = c_avl_get (cache, queue_entry->filename,
365 (void *) &cache_entry);
367 if (status == 0)
368 {
369 values = cache_entry->values;
370 values_num = cache_entry->values_num;
372 cache_entry->values = NULL;
373 cache_entry->values_num = 0;
374 cache_entry->flags = FLAG_NONE;
375 }
377 pthread_mutex_unlock (&cache_lock);
379 if (status != 0)
380 {
381 sfree (queue_entry->filename);
382 sfree (queue_entry);
383 continue;
384 }
386 /* Update `tv_next_update' */
387 if (write_rate > 0.0)
388 {
389 gettimeofday (&tv_now, /* timezone = */ NULL);
390 tv_next_update.tv_sec = tv_now.tv_sec;
391 tv_next_update.tv_usec = tv_now.tv_usec
392 + ((suseconds_t) (1000000 * write_rate));
393 while (tv_next_update.tv_usec > 1000000)
394 {
395 tv_next_update.tv_sec++;
396 tv_next_update.tv_usec -= 1000000;
397 }
398 }
400 /* Write the values to the RRD-file */
401 srrd_update (queue_entry->filename, NULL,
402 values_num, (const char **)values);
403 DEBUG ("rrdtool plugin: queue thread: Wrote %i value%s to %s",
404 values_num, (values_num == 1) ? "" : "s",
405 queue_entry->filename);
407 for (i = 0; i < values_num; i++)
408 {
409 sfree (values[i]);
410 }
411 sfree (values);
412 sfree (queue_entry->filename);
413 sfree (queue_entry);
414 } /* while (42) */
416 pthread_exit ((void *) 0);
417 return ((void *) 0);
418 } /* void *rrd_queue_thread */
420 static int rrd_queue_enqueue (const char *filename,
421 rrd_queue_t **head, rrd_queue_t **tail)
422 {
423 rrd_queue_t *queue_entry;
425 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
426 if (queue_entry == NULL)
427 return (-1);
429 queue_entry->filename = strdup (filename);
430 if (queue_entry->filename == NULL)
431 {
432 free (queue_entry);
433 return (-1);
434 }
436 queue_entry->next = NULL;
438 pthread_mutex_lock (&queue_lock);
440 if (*tail == NULL)
441 *head = queue_entry;
442 else
443 (*tail)->next = queue_entry;
444 *tail = queue_entry;
446 pthread_cond_signal (&queue_cond);
447 pthread_mutex_unlock (&queue_lock);
449 return (0);
450 } /* int rrd_queue_enqueue */
452 static int rrd_queue_dequeue (const char *filename,
453 rrd_queue_t **head, rrd_queue_t **tail)
454 {
455 rrd_queue_t *this;
456 rrd_queue_t *prev;
458 pthread_mutex_lock (&queue_lock);
460 prev = NULL;
461 this = *head;
463 while (this != NULL)
464 {
465 if (strcmp (this->filename, filename) == 0)
466 break;
468 prev = this;
469 this = this->next;
470 }
472 if (this == NULL)
473 {
474 pthread_mutex_unlock (&queue_lock);
475 return (-1);
476 }
478 if (prev == NULL)
479 *head = this->next;
480 else
481 prev->next = this->next;
483 if (this->next == NULL)
484 *tail = prev;
486 pthread_mutex_unlock (&queue_lock);
488 sfree (this->filename);
489 sfree (this);
491 return (0);
492 } /* int rrd_queue_dequeue */
494 /* XXX: You must hold "cache_lock" when calling this function! */
495 static void rrd_cache_flush (cdtime_t timeout)
496 {
497 rrd_cache_t *rc;
498 cdtime_t now;
500 char **keys = NULL;
501 int keys_num = 0;
503 char *key;
504 c_avl_iterator_t *iter;
505 int i;
507 DEBUG ("rrdtool plugin: Flushing cache, timeout = %.3f",
508 CDTIME_T_TO_DOUBLE (timeout));
510 now = cdtime ();
511 timeout = TIME_T_TO_CDTIME_T (timeout);
513 /* Build a list of entries to be flushed */
514 iter = c_avl_get_iterator (cache);
515 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
516 {
517 if (rc->flags != FLAG_NONE)
518 continue;
519 /* timeout == 0 => flush everything */
520 else if ((timeout != 0)
521 && ((now - rc->first_value) < timeout))
522 continue;
523 else if (rc->values_num > 0)
524 {
525 int status;
527 status = rrd_queue_enqueue (key, &queue_head, &queue_tail);
528 if (status == 0)
529 rc->flags = FLAG_QUEUED;
530 }
531 else /* ancient and no values -> waste of memory */
532 {
533 char **tmp = (char **) realloc ((void *) keys,
534 (keys_num + 1) * sizeof (char *));
535 if (tmp == NULL)
536 {
537 char errbuf[1024];
538 ERROR ("rrdtool plugin: "
539 "realloc failed: %s",
540 sstrerror (errno, errbuf,
541 sizeof (errbuf)));
542 c_avl_iterator_destroy (iter);
543 sfree (keys);
544 return;
545 }
546 keys = tmp;
547 keys[keys_num] = key;
548 keys_num++;
549 }
550 } /* while (c_avl_iterator_next) */
551 c_avl_iterator_destroy (iter);
553 for (i = 0; i < keys_num; i++)
554 {
555 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
556 {
557 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
558 continue;
559 }
561 assert (rc->values == NULL);
562 assert (rc->values_num == 0);
564 sfree (rc);
565 sfree (key);
566 keys[i] = NULL;
567 } /* for (i = 0..keys_num) */
569 sfree (keys);
571 cache_flush_last = now;
572 } /* void rrd_cache_flush */
574 static int rrd_cache_flush_identifier (cdtime_t timeout,
575 const char *identifier)
576 {
577 rrd_cache_t *rc;
578 cdtime_t now;
579 int status;
580 char key[2048];
582 if (identifier == NULL)
583 {
584 rrd_cache_flush (timeout);
585 return (0);
586 }
588 now = cdtime ();
590 if (datadir == NULL)
591 snprintf (key, sizeof (key), "%s.rrd",
592 identifier);
593 else
594 snprintf (key, sizeof (key), "%s/%s.rrd",
595 datadir, identifier);
596 key[sizeof (key) - 1] = 0;
598 status = c_avl_get (cache, key, (void *) &rc);
599 if (status != 0)
600 {
601 INFO ("rrdtool plugin: rrd_cache_flush_identifier: "
602 "c_avl_get (%s) failed. Does that file really exist?",
603 key);
604 return (status);
605 }
607 if (rc->flags == FLAG_FLUSHQ)
608 {
609 status = 0;
610 }
611 else if (rc->flags == FLAG_QUEUED)
612 {
613 rrd_queue_dequeue (key, &queue_head, &queue_tail);
614 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
615 if (status == 0)
616 rc->flags = FLAG_FLUSHQ;
617 }
618 else if ((now - rc->first_value) < timeout)
619 {
620 status = 0;
621 }
622 else if (rc->values_num > 0)
623 {
624 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
625 if (status == 0)
626 rc->flags = FLAG_FLUSHQ;
627 }
629 return (status);
630 } /* int rrd_cache_flush_identifier */
632 static int64_t rrd_get_random_variation (void)
633 {
634 double dbl_timeout;
635 cdtime_t ctm_timeout;
636 double rand_fact;
637 _Bool negative;
638 int64_t ret;
640 if (random_timeout <= 0)
641 return (0);
643 /* Assure that "cache_timeout + random_variation" is never negative. */
644 if (random_timeout > cache_timeout)
645 {
646 INFO ("rrdtool plugin: Adjusting \"RandomTimeout\" to %.3f seconds.",
647 CDTIME_T_TO_DOUBLE (cache_timeout));
648 random_timeout = cache_timeout;
649 }
651 /* This seems a bit complicated, but "random_timeout" is likely larger than
652 * RAND_MAX, so we can't simply use modulo here. */
653 dbl_timeout = CDTIME_T_TO_DOUBLE (random_timeout);
654 rand_fact = ((double) random ())
655 / ((double) RAND_MAX);
656 negative = (_Bool) (random () % 2);
658 ctm_timeout = DOUBLE_TO_CDTIME_T (dbl_timeout * rand_fact);
660 ret = (int64_t) ctm_timeout;
661 if (negative)
662 ret *= -1;
664 return (ret);
665 } /* int64_t rrd_get_random_variation */
667 static int rrd_cache_insert (const char *filename,
668 const char *value, cdtime_t value_time)
669 {
670 rrd_cache_t *rc = NULL;
671 int new_rc = 0;
672 char **values_new;
674 pthread_mutex_lock (&cache_lock);
676 /* This shouldn't happen, but it did happen at least once, so we'll be
677 * careful. */
678 if (cache == NULL)
679 {
680 pthread_mutex_unlock (&cache_lock);
681 WARNING ("rrdtool plugin: cache == NULL.");
682 return (-1);
683 }
685 c_avl_get (cache, filename, (void *) &rc);
687 if (rc == NULL)
688 {
689 rc = malloc (sizeof (*rc));
690 if (rc == NULL)
691 return (-1);
692 rc->values_num = 0;
693 rc->values = NULL;
694 rc->first_value = 0;
695 rc->last_value = 0;
696 rc->random_variation = rrd_get_random_variation ();
697 rc->flags = FLAG_NONE;
698 new_rc = 1;
699 }
701 if (rc->last_value >= value_time)
702 {
703 pthread_mutex_unlock (&cache_lock);
704 DEBUG ("rrdtool plugin: (rc->last_value = %"PRIu64") "
705 ">= (value_time = %"PRIu64")",
706 rc->last_value, value_time);
707 return (-1);
708 }
710 values_new = (char **) realloc ((void *) rc->values,
711 (rc->values_num + 1) * sizeof (char *));
712 if (values_new == NULL)
713 {
714 char errbuf[1024];
715 void *cache_key = NULL;
717 sstrerror (errno, errbuf, sizeof (errbuf));
719 c_avl_remove (cache, filename, &cache_key, NULL);
720 pthread_mutex_unlock (&cache_lock);
722 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
724 sfree (cache_key);
725 sfree (rc->values);
726 sfree (rc);
727 return (-1);
728 }
729 rc->values = values_new;
731 rc->values[rc->values_num] = strdup (value);
732 if (rc->values[rc->values_num] != NULL)
733 rc->values_num++;
735 if (rc->values_num == 1)
736 rc->first_value = value_time;
737 rc->last_value = value_time;
739 /* Insert if this is the first value */
740 if (new_rc == 1)
741 {
742 void *cache_key = strdup (filename);
744 if (cache_key == NULL)
745 {
746 char errbuf[1024];
747 sstrerror (errno, errbuf, sizeof (errbuf));
749 pthread_mutex_unlock (&cache_lock);
751 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
753 sfree (rc->values[0]);
754 sfree (rc->values);
755 sfree (rc);
756 return (-1);
757 }
759 c_avl_insert (cache, cache_key, rc);
760 }
762 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
763 "values_num = %i; age = %.3f;",
764 filename, rc->values_num,
765 CDTIME_T_TO_DOUBLE (rc->last_value - rc->first_value));
767 if ((rc->last_value - rc->first_value) >= (cache_timeout + rc->random_variation))
768 {
769 /* XXX: If you need to lock both, cache_lock and queue_lock, at
770 * the same time, ALWAYS lock `cache_lock' first! */
771 if (rc->flags == FLAG_NONE)
772 {
773 int status;
775 status = rrd_queue_enqueue (filename, &queue_head, &queue_tail);
776 if (status == 0)
777 rc->flags = FLAG_QUEUED;
779 rc->random_variation = rrd_get_random_variation ();
780 }
781 else
782 {
783 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
784 }
785 }
787 if ((cache_timeout > 0) &&
788 ((cdtime () - cache_flush_last) > cache_flush_timeout))
789 rrd_cache_flush (cache_flush_timeout);
791 pthread_mutex_unlock (&cache_lock);
793 return (0);
794 } /* int rrd_cache_insert */
796 static int rrd_cache_destroy (void) /* {{{ */
797 {
798 void *key = NULL;
799 void *value = NULL;
801 int non_empty = 0;
803 pthread_mutex_lock (&cache_lock);
805 if (cache == NULL)
806 {
807 pthread_mutex_unlock (&cache_lock);
808 return (0);
809 }
811 while (c_avl_pick (cache, &key, &value) == 0)
812 {
813 rrd_cache_t *rc;
814 int i;
816 sfree (key);
817 key = NULL;
819 rc = value;
820 value = NULL;
822 if (rc->values_num > 0)
823 non_empty++;
825 for (i = 0; i < rc->values_num; i++)
826 sfree (rc->values[i]);
827 sfree (rc->values);
828 sfree (rc);
829 }
831 c_avl_destroy (cache);
832 cache = NULL;
834 if (non_empty > 0)
835 {
836 INFO ("rrdtool plugin: %i cache %s had values when destroying the cache.",
837 non_empty, (non_empty == 1) ? "entry" : "entries");
838 }
839 else
840 {
841 DEBUG ("rrdtool plugin: No values have been lost "
842 "when destroying the cache.");
843 }
845 pthread_mutex_unlock (&cache_lock);
846 return (0);
847 } /* }}} int rrd_cache_destroy */
849 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
850 {
851 int a = *((int *) a_ptr);
852 int b = *((int *) b_ptr);
854 if (a < b)
855 return (-1);
856 else if (a > b)
857 return (1);
858 else
859 return (0);
860 } /* int rrd_compare_numeric */
862 static int rrd_write (const data_set_t *ds, const value_list_t *vl,
863 user_data_t __attribute__((unused)) *user_data)
864 {
865 struct stat statbuf;
866 char filename[512];
867 char values[512];
868 int status;
870 if (do_shutdown)
871 return (0);
873 if (0 != strcmp (ds->type, vl->type)) {
874 ERROR ("rrdtool plugin: DS type does not match value list type");
875 return -1;
876 }
878 if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
879 return (-1);
881 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
882 return (-1);
884 if (stat (filename, &statbuf) == -1)
885 {
886 if (errno == ENOENT)
887 {
888 status = cu_rrd_create_file (filename,
889 ds, vl, &rrdcreate_config);
890 if (status != 0)
891 return (-1);
892 }
893 else
894 {
895 char errbuf[1024];
896 ERROR ("stat(%s) failed: %s", filename,
897 sstrerror (errno, errbuf,
898 sizeof (errbuf)));
899 return (-1);
900 }
901 }
902 else if (!S_ISREG (statbuf.st_mode))
903 {
904 ERROR ("stat(%s): Not a regular file!",
905 filename);
906 return (-1);
907 }
909 status = rrd_cache_insert (filename, values, vl->time);
911 return (status);
912 } /* int rrd_write */
914 static int rrd_flush (cdtime_t timeout, const char *identifier,
915 __attribute__((unused)) user_data_t *user_data)
916 {
917 pthread_mutex_lock (&cache_lock);
919 if (cache == NULL) {
920 pthread_mutex_unlock (&cache_lock);
921 return (0);
922 }
924 rrd_cache_flush_identifier (timeout, identifier);
926 pthread_mutex_unlock (&cache_lock);
927 return (0);
928 } /* int rrd_flush */
930 static int rrd_config (const char *key, const char *value)
931 {
932 if (strcasecmp ("CacheTimeout", key) == 0)
933 {
934 double tmp = atof (value);
935 if (tmp < 0)
936 {
937 fprintf (stderr, "rrdtool: `CacheTimeout' must "
938 "be greater than 0.\n");
939 ERROR ("rrdtool: `CacheTimeout' must "
940 "be greater than 0.\n");
941 return (1);
942 }
943 cache_timeout = DOUBLE_TO_CDTIME_T (tmp);
944 }
945 else if (strcasecmp ("CacheFlush", key) == 0)
946 {
947 int tmp = atoi (value);
948 if (tmp < 0)
949 {
950 fprintf (stderr, "rrdtool: `CacheFlush' must "
951 "be greater than 0.\n");
952 ERROR ("rrdtool: `CacheFlush' must "
953 "be greater than 0.\n");
954 return (1);
955 }
956 cache_flush_timeout = tmp;
957 }
958 else if (strcasecmp ("DataDir", key) == 0)
959 {
960 if (datadir != NULL)
961 free (datadir);
962 datadir = strdup (value);
963 if (datadir != NULL)
964 {
965 int len = strlen (datadir);
966 while ((len > 0) && (datadir[len - 1] == '/'))
967 {
968 len--;
969 datadir[len] = '\0';
970 }
971 if (len <= 0)
972 {
973 free (datadir);
974 datadir = NULL;
975 }
976 }
977 }
978 else if (strcasecmp ("StepSize", key) == 0)
979 {
980 unsigned long temp = strtoul (value, NULL, 0);
981 if (temp > 0)
982 rrdcreate_config.stepsize = temp;
983 }
984 else if (strcasecmp ("HeartBeat", key) == 0)
985 {
986 int temp = atoi (value);
987 if (temp > 0)
988 rrdcreate_config.heartbeat = temp;
989 }
990 else if (strcasecmp ("RRARows", key) == 0)
991 {
992 int tmp = atoi (value);
993 if (tmp <= 0)
994 {
995 fprintf (stderr, "rrdtool: `RRARows' must "
996 "be greater than 0.\n");
997 ERROR ("rrdtool: `RRARows' must "
998 "be greater than 0.\n");
999 return (1);
1000 }
1001 rrdcreate_config.rrarows = tmp;
1002 }
1003 else if (strcasecmp ("RRATimespan", key) == 0)
1004 {
1005 char *saveptr = NULL;
1006 char *dummy;
1007 char *ptr;
1008 char *value_copy;
1009 int *tmp_alloc;
1011 value_copy = strdup (value);
1012 if (value_copy == NULL)
1013 return (1);
1015 dummy = value_copy;
1016 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1017 {
1018 dummy = NULL;
1020 tmp_alloc = realloc (rrdcreate_config.timespans,
1021 sizeof (int) * (rrdcreate_config.timespans_num + 1));
1022 if (tmp_alloc == NULL)
1023 {
1024 fprintf (stderr, "rrdtool: realloc failed.\n");
1025 ERROR ("rrdtool: realloc failed.\n");
1026 free (value_copy);
1027 return (1);
1028 }
1029 rrdcreate_config.timespans = tmp_alloc;
1030 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = atoi (ptr);
1031 if (rrdcreate_config.timespans[rrdcreate_config.timespans_num] != 0)
1032 rrdcreate_config.timespans_num++;
1033 } /* while (strtok_r) */
1035 qsort (/* base = */ rrdcreate_config.timespans,
1036 /* nmemb = */ rrdcreate_config.timespans_num,
1037 /* size = */ sizeof (rrdcreate_config.timespans[0]),
1038 /* compar = */ rrd_compare_numeric);
1040 free (value_copy);
1041 }
1042 else if (strcasecmp ("XFF", key) == 0)
1043 {
1044 double tmp = atof (value);
1045 if ((tmp < 0.0) || (tmp >= 1.0))
1046 {
1047 fprintf (stderr, "rrdtool: `XFF' must "
1048 "be in the range 0 to 1 (exclusive).");
1049 ERROR ("rrdtool: `XFF' must "
1050 "be in the range 0 to 1 (exclusive).");
1051 return (1);
1052 }
1053 rrdcreate_config.xff = tmp;
1054 }
1055 else if (strcasecmp ("WritesPerSecond", key) == 0)
1056 {
1057 double wps = atof (value);
1059 if (wps < 0.0)
1060 {
1061 fprintf (stderr, "rrdtool: `WritesPerSecond' must be "
1062 "greater than or equal to zero.");
1063 return (1);
1064 }
1065 else if (wps == 0.0)
1066 {
1067 write_rate = 0.0;
1068 }
1069 else
1070 {
1071 write_rate = 1.0 / wps;
1072 }
1073 }
1074 else if (strcasecmp ("RandomTimeout", key) == 0)
1075 {
1076 double tmp;
1078 tmp = atof (value);
1079 if (tmp < 0.0)
1080 {
1081 fprintf (stderr, "rrdtool: `RandomTimeout' must "
1082 "be greater than or equal to zero.\n");
1083 ERROR ("rrdtool: `RandomTimeout' must "
1084 "be greater then or equal to zero.");
1085 }
1086 else
1087 {
1088 random_timeout = DOUBLE_TO_CDTIME_T (tmp);
1089 }
1090 }
1091 else
1092 {
1093 return (-1);
1094 }
1095 return (0);
1096 } /* int rrd_config */
1098 static int rrd_shutdown (void)
1099 {
1100 pthread_mutex_lock (&cache_lock);
1101 rrd_cache_flush (0);
1102 pthread_mutex_unlock (&cache_lock);
1104 pthread_mutex_lock (&queue_lock);
1105 do_shutdown = 1;
1106 pthread_cond_signal (&queue_cond);
1107 pthread_mutex_unlock (&queue_lock);
1109 if ((queue_thread_running != 0)
1110 && ((queue_head != NULL) || (flushq_head != NULL)))
1111 {
1112 INFO ("rrdtool plugin: Shutting down the queue thread. "
1113 "This may take a while.");
1114 }
1115 else if (queue_thread_running != 0)
1116 {
1117 INFO ("rrdtool plugin: Shutting down the queue thread.");
1118 }
1120 /* Wait for all the values to be written to disk before returning. */
1121 if (queue_thread_running != 0)
1122 {
1123 pthread_join (queue_thread, NULL);
1124 memset (&queue_thread, 0, sizeof (queue_thread));
1125 queue_thread_running = 0;
1126 DEBUG ("rrdtool plugin: queue_thread exited.");
1127 }
1129 rrd_cache_destroy ();
1131 return (0);
1132 } /* int rrd_shutdown */
1134 static int rrd_init (void)
1135 {
1136 static int init_once = 0;
1137 int status;
1139 if (init_once != 0)
1140 return (0);
1141 init_once = 1;
1143 if (rrdcreate_config.heartbeat <= 0)
1144 rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize;
1146 /* Set the cache up */
1147 pthread_mutex_lock (&cache_lock);
1149 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1150 if (cache == NULL)
1151 {
1152 ERROR ("rrdtool plugin: c_avl_create failed.");
1153 return (-1);
1154 }
1156 cache_flush_last = cdtime ();
1157 if (cache_timeout == 0)
1158 {
1159 cache_flush_timeout = 0;
1160 }
1161 else if (cache_flush_timeout < cache_timeout)
1162 cache_flush_timeout = 10 * cache_timeout;
1164 pthread_mutex_unlock (&cache_lock);
1166 status = plugin_thread_create (&queue_thread, /* attr = */ NULL,
1167 rrd_queue_thread, /* args = */ NULL);
1168 if (status != 0)
1169 {
1170 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1171 return (-1);
1172 }
1173 queue_thread_running = 1;
1175 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %lu;"
1176 " heartbeat = %i; rrarows = %i; xff = %lf;",
1177 (datadir == NULL) ? "(null)" : datadir,
1178 rrdcreate_config.stepsize,
1179 rrdcreate_config.heartbeat,
1180 rrdcreate_config.rrarows,
1181 rrdcreate_config.xff);
1183 return (0);
1184 } /* int rrd_init */
1186 void module_register (void)
1187 {
1188 plugin_register_config ("rrdtool", rrd_config,
1189 config_keys, config_keys_num);
1190 plugin_register_init ("rrdtool", rrd_init);
1191 plugin_register_write ("rrdtool", rrd_write, /* user_data = */ NULL);
1192 plugin_register_flush ("rrdtool", rrd_flush, /* user_data = */ NULL);
1193 plugin_register_shutdown ("rrdtool", rrd_shutdown);
1194 }