1 /**
2 * collectd - src/rrdtool.c
3 * Copyright (C) 2006-2013 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 collectd.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_random.h"
31 #include "utils_rrdcreate.h"
33 #include <rrd.h>
35 #if HAVE_PTHREAD_H
36 # include <pthread.h>
37 #endif
39 /*
40 * Private types
41 */
42 struct rrd_cache_s
43 {
44 int values_num;
45 char **values;
46 cdtime_t first_value;
47 cdtime_t last_value;
48 int64_t random_variation;
49 enum
50 {
51 FLAG_NONE = 0x00,
52 FLAG_QUEUED = 0x01,
53 FLAG_FLUSHQ = 0x02
54 } flags;
55 };
56 typedef struct rrd_cache_s rrd_cache_t;
58 enum rrd_queue_dir_e
59 {
60 QUEUE_INSERT_FRONT,
61 QUEUE_INSERT_BACK
62 };
63 typedef enum rrd_queue_dir_e rrd_queue_dir_t;
65 struct rrd_queue_s
66 {
67 char *filename;
68 struct rrd_queue_s *next;
69 };
70 typedef struct rrd_queue_s rrd_queue_t;
72 /*
73 * Private variables
74 */
75 static const char *config_keys[] =
76 {
77 "CacheTimeout",
78 "CacheFlush",
79 "CreateFilesAsync",
80 "DataDir",
81 "StepSize",
82 "HeartBeat",
83 "RRARows",
84 "RRATimespan",
85 "XFF",
86 "WritesPerSecond",
87 "RandomTimeout"
88 };
89 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
91 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
92 * is zero a default, depending on the `interval' member of the value list is
93 * being used. */
94 static char *datadir = NULL;
95 static double write_rate = 0.0;
96 static rrdcreate_config_t rrdcreate_config =
97 {
98 /* stepsize = */ 0,
99 /* heartbeat = */ 0,
100 /* rrarows = */ 1200,
101 /* xff = */ 0.1,
103 /* timespans = */ NULL,
104 /* timespans_num = */ 0,
106 /* consolidation_functions = */ NULL,
107 /* consolidation_functions_num = */ 0,
109 /* async = */ 0
110 };
112 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
113 * ALWAYS lock `cache_lock' first! */
114 static cdtime_t cache_timeout = 0;
115 static cdtime_t cache_flush_timeout = 0;
116 static cdtime_t random_timeout = TIME_T_TO_CDTIME_T (1);
117 static cdtime_t cache_flush_last;
118 static c_avl_tree_t *cache = NULL;
119 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
121 static rrd_queue_t *queue_head = NULL;
122 static rrd_queue_t *queue_tail = NULL;
123 static rrd_queue_t *flushq_head = NULL;
124 static rrd_queue_t *flushq_tail = NULL;
125 static pthread_t queue_thread;
126 static int queue_thread_running = 1;
127 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
128 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
130 #if !HAVE_THREADSAFE_LIBRRD
131 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
132 #endif
134 static int do_shutdown = 0;
136 #if HAVE_THREADSAFE_LIBRRD
137 static int srrd_update (char *filename, char *template,
138 int argc, const char **argv)
139 {
140 int status;
142 optind = 0; /* bug in librrd? */
143 rrd_clear_error ();
145 status = rrd_update_r (filename, template, argc, (void *) argv);
147 if (status != 0)
148 {
149 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
150 filename, rrd_get_error ());
151 }
153 return (status);
154 } /* int srrd_update */
155 /* #endif HAVE_THREADSAFE_LIBRRD */
157 #else /* !HAVE_THREADSAFE_LIBRRD */
158 static int srrd_update (char *filename, char *template,
159 int argc, const char **argv)
160 {
161 int status;
163 int new_argc;
164 char **new_argv;
166 assert (template == NULL);
168 new_argc = 2 + argc;
169 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
170 if (new_argv == NULL)
171 {
172 ERROR ("rrdtool plugin: malloc failed.");
173 return (-1);
174 }
176 new_argv[0] = "update";
177 new_argv[1] = filename;
179 memcpy (new_argv + 2, argv, argc * sizeof (char *));
180 new_argv[new_argc] = NULL;
182 pthread_mutex_lock (&librrd_lock);
183 optind = 0; /* bug in librrd? */
184 rrd_clear_error ();
186 status = rrd_update (new_argc, new_argv);
187 pthread_mutex_unlock (&librrd_lock);
189 if (status != 0)
190 {
191 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
192 filename, rrd_get_error ());
193 }
195 sfree (new_argv);
197 return (status);
198 } /* int srrd_update */
199 #endif /* !HAVE_THREADSAFE_LIBRRD */
201 static int value_list_to_string_multiple (char *buffer, int buffer_len,
202 const data_set_t *ds, const value_list_t *vl)
203 {
204 int offset;
205 int status;
206 time_t tt;
207 int i;
209 memset (buffer, '\0', buffer_len);
211 tt = CDTIME_T_TO_TIME_T (vl->time);
212 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) tt);
213 if ((status < 1) || (status >= buffer_len))
214 return (-1);
215 offset = status;
217 for (i = 0; i < ds->ds_num; i++)
218 {
219 if ((ds->ds[i].type != DS_TYPE_COUNTER)
220 && (ds->ds[i].type != DS_TYPE_GAUGE)
221 && (ds->ds[i].type != DS_TYPE_DERIVE)
222 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
223 return (-1);
225 if (ds->ds[i].type == DS_TYPE_COUNTER)
226 status = ssnprintf (buffer + offset, buffer_len - offset,
227 ":%llu", vl->values[i].counter);
228 else if (ds->ds[i].type == DS_TYPE_GAUGE)
229 status = ssnprintf (buffer + offset, buffer_len - offset,
230 ":%lf", vl->values[i].gauge);
231 else if (ds->ds[i].type == DS_TYPE_DERIVE)
232 status = ssnprintf (buffer + offset, buffer_len - offset,
233 ":%"PRIi64, vl->values[i].derive);
234 else /*if (ds->ds[i].type == DS_TYPE_ABSOLUTE) */
235 status = ssnprintf (buffer + offset, buffer_len - offset,
236 ":%"PRIu64, vl->values[i].absolute);
238 if ((status < 1) || (status >= (buffer_len - offset)))
239 return (-1);
241 offset += status;
242 } /* for ds->ds_num */
244 return (0);
245 } /* int value_list_to_string_multiple */
247 static int value_list_to_string (char *buffer, int buffer_len,
248 const data_set_t *ds, const value_list_t *vl)
249 {
250 int status;
251 time_t tt;
253 if (ds->ds_num != 1)
254 return (value_list_to_string_multiple (buffer, buffer_len,
255 ds, vl));
257 tt = CDTIME_T_TO_TIME_T (vl->time);
258 switch (ds->ds[0].type)
259 {
260 case DS_TYPE_DERIVE:
261 status = ssnprintf (buffer, buffer_len, "%u:%"PRIi64,
262 (unsigned) tt, vl->values[0].derive);
263 break;
264 case DS_TYPE_GAUGE:
265 status = ssnprintf (buffer, buffer_len, "%u:%lf",
266 (unsigned) tt, vl->values[0].gauge);
267 break;
268 case DS_TYPE_COUNTER:
269 status = ssnprintf (buffer, buffer_len, "%u:%llu",
270 (unsigned) tt, vl->values[0].counter);
271 break;
272 case DS_TYPE_ABSOLUTE:
273 status = ssnprintf (buffer, buffer_len, "%u:%"PRIu64,
274 (unsigned) tt, vl->values[0].absolute);
275 break;
276 default:
277 return (EINVAL);
278 }
280 if ((status < 1) || (status >= buffer_len))
281 return (ENOMEM);
283 return (0);
284 } /* int value_list_to_string */
286 static int value_list_to_filename (char *buffer, size_t buffer_size,
287 value_list_t const *vl)
288 {
289 char const suffix[] = ".rrd";
290 int status;
291 size_t len;
293 status = FORMAT_VL (buffer, buffer_size, vl);
294 if (status != 0)
295 return (status);
297 len = strlen (buffer);
298 assert (len < buffer_size);
299 buffer += len;
300 buffer_size -= len;
302 if (buffer_size <= sizeof (suffix))
303 return (ENOMEM);
305 memcpy (buffer, suffix, sizeof (suffix));
306 return (0);
307 } /* int value_list_to_filename */
309 static void *rrd_queue_thread (void __attribute__((unused)) *data)
310 {
311 struct timeval tv_next_update;
312 struct timeval tv_now;
314 gettimeofday (&tv_next_update, /* timezone = */ NULL);
316 while (42)
317 {
318 rrd_queue_t *queue_entry;
319 rrd_cache_t *cache_entry;
320 char **values;
321 int values_num;
322 int status;
323 int i;
325 values = NULL;
326 values_num = 0;
328 pthread_mutex_lock (&queue_lock);
329 /* Wait for values to arrive */
330 while (42)
331 {
332 struct timespec ts_wait;
334 while ((flushq_head == NULL) && (queue_head == NULL)
335 && (do_shutdown == 0))
336 pthread_cond_wait (&queue_cond, &queue_lock);
338 if ((flushq_head == NULL) && (queue_head == NULL))
339 break;
341 /* Don't delay if there's something to flush */
342 if (flushq_head != NULL)
343 break;
345 /* Don't delay if we're shutting down */
346 if (do_shutdown != 0)
347 break;
349 /* Don't delay if no delay was configured. */
350 if (write_rate <= 0.0)
351 break;
353 gettimeofday (&tv_now, /* timezone = */ NULL);
354 status = timeval_cmp (tv_next_update, tv_now, NULL);
355 /* We're good to go */
356 if (status <= 0)
357 break;
359 /* We're supposed to wait a bit with this update, so we'll
360 * wait for the next addition to the queue or to the end of
361 * the wait period - whichever comes first. */
362 ts_wait.tv_sec = tv_next_update.tv_sec;
363 ts_wait.tv_nsec = 1000 * tv_next_update.tv_usec;
365 status = pthread_cond_timedwait (&queue_cond, &queue_lock,
366 &ts_wait);
367 if (status == ETIMEDOUT)
368 break;
369 } /* while (42) */
371 /* XXX: If you need to lock both, cache_lock and queue_lock, at
372 * the same time, ALWAYS lock `cache_lock' first! */
374 /* We're in the shutdown phase */
375 if ((flushq_head == NULL) && (queue_head == NULL))
376 {
377 pthread_mutex_unlock (&queue_lock);
378 break;
379 }
381 if (flushq_head != NULL)
382 {
383 /* Dequeue the first flush entry */
384 queue_entry = flushq_head;
385 if (flushq_head == flushq_tail)
386 flushq_head = flushq_tail = NULL;
387 else
388 flushq_head = flushq_head->next;
389 }
390 else /* if (queue_head != NULL) */
391 {
392 /* Dequeue the first regular entry */
393 queue_entry = queue_head;
394 if (queue_head == queue_tail)
395 queue_head = queue_tail = NULL;
396 else
397 queue_head = queue_head->next;
398 }
400 /* Unlock the queue again */
401 pthread_mutex_unlock (&queue_lock);
403 /* We now need the cache lock so the entry isn't updated while
404 * we make a copy of it's values */
405 pthread_mutex_lock (&cache_lock);
407 status = c_avl_get (cache, queue_entry->filename,
408 (void *) &cache_entry);
410 if (status == 0)
411 {
412 values = cache_entry->values;
413 values_num = cache_entry->values_num;
415 cache_entry->values = NULL;
416 cache_entry->values_num = 0;
417 cache_entry->flags = FLAG_NONE;
418 }
420 pthread_mutex_unlock (&cache_lock);
422 if (status != 0)
423 {
424 sfree (queue_entry->filename);
425 sfree (queue_entry);
426 continue;
427 }
429 /* Update `tv_next_update' */
430 if (write_rate > 0.0)
431 {
432 gettimeofday (&tv_now, /* timezone = */ NULL);
433 tv_next_update.tv_sec = tv_now.tv_sec;
434 tv_next_update.tv_usec = tv_now.tv_usec
435 + ((suseconds_t) (1000000 * write_rate));
436 while (tv_next_update.tv_usec > 1000000)
437 {
438 tv_next_update.tv_sec++;
439 tv_next_update.tv_usec -= 1000000;
440 }
441 }
443 /* Write the values to the RRD-file */
444 srrd_update (queue_entry->filename, NULL,
445 values_num, (const char **)values);
446 DEBUG ("rrdtool plugin: queue thread: Wrote %i value%s to %s",
447 values_num, (values_num == 1) ? "" : "s",
448 queue_entry->filename);
450 for (i = 0; i < values_num; i++)
451 {
452 sfree (values[i]);
453 }
454 sfree (values);
455 sfree (queue_entry->filename);
456 sfree (queue_entry);
457 } /* while (42) */
459 pthread_exit ((void *) 0);
460 return ((void *) 0);
461 } /* void *rrd_queue_thread */
463 static int rrd_queue_enqueue (const char *filename,
464 rrd_queue_t **head, rrd_queue_t **tail)
465 {
466 rrd_queue_t *queue_entry;
468 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
469 if (queue_entry == NULL)
470 return (-1);
472 queue_entry->filename = strdup (filename);
473 if (queue_entry->filename == NULL)
474 {
475 free (queue_entry);
476 return (-1);
477 }
479 queue_entry->next = NULL;
481 pthread_mutex_lock (&queue_lock);
483 if (*tail == NULL)
484 *head = queue_entry;
485 else
486 (*tail)->next = queue_entry;
487 *tail = queue_entry;
489 pthread_cond_signal (&queue_cond);
490 pthread_mutex_unlock (&queue_lock);
492 return (0);
493 } /* int rrd_queue_enqueue */
495 static int rrd_queue_dequeue (const char *filename,
496 rrd_queue_t **head, rrd_queue_t **tail)
497 {
498 rrd_queue_t *this;
499 rrd_queue_t *prev;
501 pthread_mutex_lock (&queue_lock);
503 prev = NULL;
504 this = *head;
506 while (this != NULL)
507 {
508 if (strcmp (this->filename, filename) == 0)
509 break;
511 prev = this;
512 this = this->next;
513 }
515 if (this == NULL)
516 {
517 pthread_mutex_unlock (&queue_lock);
518 return (-1);
519 }
521 if (prev == NULL)
522 *head = this->next;
523 else
524 prev->next = this->next;
526 if (this->next == NULL)
527 *tail = prev;
529 pthread_mutex_unlock (&queue_lock);
531 sfree (this->filename);
532 sfree (this);
534 return (0);
535 } /* int rrd_queue_dequeue */
537 /* XXX: You must hold "cache_lock" when calling this function! */
538 static void rrd_cache_flush (cdtime_t timeout)
539 {
540 rrd_cache_t *rc;
541 cdtime_t now;
543 char **keys = NULL;
544 int keys_num = 0;
546 char *key;
547 c_avl_iterator_t *iter;
548 int i;
550 DEBUG ("rrdtool plugin: Flushing cache, timeout = %.3f",
551 CDTIME_T_TO_DOUBLE (timeout));
553 now = cdtime ();
554 timeout = TIME_T_TO_CDTIME_T (timeout);
556 /* Build a list of entries to be flushed */
557 iter = c_avl_get_iterator (cache);
558 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
559 {
560 if (rc->flags != FLAG_NONE)
561 continue;
562 /* timeout == 0 => flush everything */
563 else if ((timeout != 0)
564 && ((now - rc->first_value) < timeout))
565 continue;
566 else if (rc->values_num > 0)
567 {
568 int status;
570 status = rrd_queue_enqueue (key, &queue_head, &queue_tail);
571 if (status == 0)
572 rc->flags = FLAG_QUEUED;
573 }
574 else /* ancient and no values -> waste of memory */
575 {
576 char **tmp = (char **) realloc ((void *) keys,
577 (keys_num + 1) * sizeof (char *));
578 if (tmp == NULL)
579 {
580 char errbuf[1024];
581 ERROR ("rrdtool plugin: "
582 "realloc failed: %s",
583 sstrerror (errno, errbuf,
584 sizeof (errbuf)));
585 c_avl_iterator_destroy (iter);
586 sfree (keys);
587 return;
588 }
589 keys = tmp;
590 keys[keys_num] = key;
591 keys_num++;
592 }
593 } /* while (c_avl_iterator_next) */
594 c_avl_iterator_destroy (iter);
596 for (i = 0; i < keys_num; i++)
597 {
598 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
599 {
600 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
601 continue;
602 }
604 assert (rc->values == NULL);
605 assert (rc->values_num == 0);
607 sfree (rc);
608 sfree (key);
609 keys[i] = NULL;
610 } /* for (i = 0..keys_num) */
612 sfree (keys);
614 cache_flush_last = now;
615 } /* void rrd_cache_flush */
617 static int rrd_cache_flush_identifier (cdtime_t timeout,
618 const char *identifier)
619 {
620 rrd_cache_t *rc;
621 cdtime_t now;
622 int status;
623 char key[2048];
625 if (identifier == NULL)
626 {
627 rrd_cache_flush (timeout);
628 return (0);
629 }
631 now = cdtime ();
633 if (datadir == NULL)
634 snprintf (key, sizeof (key), "%s.rrd",
635 identifier);
636 else
637 snprintf (key, sizeof (key), "%s/%s.rrd",
638 datadir, identifier);
639 key[sizeof (key) - 1] = 0;
641 status = c_avl_get (cache, key, (void *) &rc);
642 if (status != 0)
643 {
644 INFO ("rrdtool plugin: rrd_cache_flush_identifier: "
645 "c_avl_get (%s) failed. Does that file really exist?",
646 key);
647 return (status);
648 }
650 if (rc->flags == FLAG_FLUSHQ)
651 {
652 status = 0;
653 }
654 else if (rc->flags == FLAG_QUEUED)
655 {
656 rrd_queue_dequeue (key, &queue_head, &queue_tail);
657 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
658 if (status == 0)
659 rc->flags = FLAG_FLUSHQ;
660 }
661 else if ((now - rc->first_value) < timeout)
662 {
663 status = 0;
664 }
665 else if (rc->values_num > 0)
666 {
667 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
668 if (status == 0)
669 rc->flags = FLAG_FLUSHQ;
670 }
672 return (status);
673 } /* int rrd_cache_flush_identifier */
675 static int64_t rrd_get_random_variation (void)
676 {
677 long min;
678 long max;
680 if (random_timeout <= 0)
681 return (0);
683 /* Assure that "cache_timeout + random_variation" is never negative. */
684 if (random_timeout > cache_timeout)
685 {
686 INFO ("rrdtool plugin: Adjusting \"RandomTimeout\" to %.3f seconds.",
687 CDTIME_T_TO_DOUBLE (cache_timeout));
688 random_timeout = cache_timeout;
689 }
691 max = (long) (random_timeout / 2);
692 min = max - ((long) random_timeout);
694 return ((int64_t) cdrand_range (min, max));
695 } /* int64_t rrd_get_random_variation */
697 static int rrd_cache_insert (const char *filename,
698 const char *value, cdtime_t value_time)
699 {
700 rrd_cache_t *rc = NULL;
701 int new_rc = 0;
702 char **values_new;
704 pthread_mutex_lock (&cache_lock);
706 /* This shouldn't happen, but it did happen at least once, so we'll be
707 * careful. */
708 if (cache == NULL)
709 {
710 pthread_mutex_unlock (&cache_lock);
711 WARNING ("rrdtool plugin: cache == NULL.");
712 return (-1);
713 }
715 c_avl_get (cache, filename, (void *) &rc);
717 if (rc == NULL)
718 {
719 rc = malloc (sizeof (*rc));
720 if (rc == NULL)
721 return (-1);
722 rc->values_num = 0;
723 rc->values = NULL;
724 rc->first_value = 0;
725 rc->last_value = 0;
726 rc->random_variation = rrd_get_random_variation ();
727 rc->flags = FLAG_NONE;
728 new_rc = 1;
729 }
731 if (rc->last_value >= value_time)
732 {
733 pthread_mutex_unlock (&cache_lock);
734 DEBUG ("rrdtool plugin: (rc->last_value = %"PRIu64") "
735 ">= (value_time = %"PRIu64")",
736 rc->last_value, value_time);
737 return (-1);
738 }
740 values_new = (char **) realloc ((void *) rc->values,
741 (rc->values_num + 1) * sizeof (char *));
742 if (values_new == NULL)
743 {
744 char errbuf[1024];
745 void *cache_key = NULL;
747 sstrerror (errno, errbuf, sizeof (errbuf));
749 c_avl_remove (cache, filename, &cache_key, NULL);
750 pthread_mutex_unlock (&cache_lock);
752 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
754 sfree (cache_key);
755 sfree (rc->values);
756 sfree (rc);
757 return (-1);
758 }
759 rc->values = values_new;
761 rc->values[rc->values_num] = strdup (value);
762 if (rc->values[rc->values_num] != NULL)
763 rc->values_num++;
765 if (rc->values_num == 1)
766 rc->first_value = value_time;
767 rc->last_value = value_time;
769 /* Insert if this is the first value */
770 if (new_rc == 1)
771 {
772 void *cache_key = strdup (filename);
774 if (cache_key == NULL)
775 {
776 char errbuf[1024];
777 sstrerror (errno, errbuf, sizeof (errbuf));
779 pthread_mutex_unlock (&cache_lock);
781 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
783 sfree (rc->values[0]);
784 sfree (rc->values);
785 sfree (rc);
786 return (-1);
787 }
789 c_avl_insert (cache, cache_key, rc);
790 }
792 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
793 "values_num = %i; age = %.3f;",
794 filename, rc->values_num,
795 CDTIME_T_TO_DOUBLE (rc->last_value - rc->first_value));
797 if ((rc->last_value - rc->first_value) >= (cache_timeout + rc->random_variation))
798 {
799 /* XXX: If you need to lock both, cache_lock and queue_lock, at
800 * the same time, ALWAYS lock `cache_lock' first! */
801 if (rc->flags == FLAG_NONE)
802 {
803 int status;
805 status = rrd_queue_enqueue (filename, &queue_head, &queue_tail);
806 if (status == 0)
807 rc->flags = FLAG_QUEUED;
809 rc->random_variation = rrd_get_random_variation ();
810 }
811 else
812 {
813 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
814 }
815 }
817 if ((cache_timeout > 0) &&
818 ((cdtime () - cache_flush_last) > cache_flush_timeout))
819 rrd_cache_flush (cache_flush_timeout);
821 pthread_mutex_unlock (&cache_lock);
823 return (0);
824 } /* int rrd_cache_insert */
826 static int rrd_cache_destroy (void) /* {{{ */
827 {
828 void *key = NULL;
829 void *value = NULL;
831 int non_empty = 0;
833 pthread_mutex_lock (&cache_lock);
835 if (cache == NULL)
836 {
837 pthread_mutex_unlock (&cache_lock);
838 return (0);
839 }
841 while (c_avl_pick (cache, &key, &value) == 0)
842 {
843 rrd_cache_t *rc;
844 int i;
846 sfree (key);
847 key = NULL;
849 rc = value;
850 value = NULL;
852 if (rc->values_num > 0)
853 non_empty++;
855 for (i = 0; i < rc->values_num; i++)
856 sfree (rc->values[i]);
857 sfree (rc->values);
858 sfree (rc);
859 }
861 c_avl_destroy (cache);
862 cache = NULL;
864 if (non_empty > 0)
865 {
866 INFO ("rrdtool plugin: %i cache %s had values when destroying the cache.",
867 non_empty, (non_empty == 1) ? "entry" : "entries");
868 }
869 else
870 {
871 DEBUG ("rrdtool plugin: No values have been lost "
872 "when destroying the cache.");
873 }
875 pthread_mutex_unlock (&cache_lock);
876 return (0);
877 } /* }}} int rrd_cache_destroy */
879 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
880 {
881 int a = *((int *) a_ptr);
882 int b = *((int *) b_ptr);
884 if (a < b)
885 return (-1);
886 else if (a > b)
887 return (1);
888 else
889 return (0);
890 } /* int rrd_compare_numeric */
892 static int rrd_write (const data_set_t *ds, const value_list_t *vl,
893 user_data_t __attribute__((unused)) *user_data)
894 {
895 struct stat statbuf;
896 char filename[512];
897 char values[512];
898 int status;
900 if (do_shutdown)
901 return (0);
903 if (0 != strcmp (ds->type, vl->type)) {
904 ERROR ("rrdtool plugin: DS type does not match value list type");
905 return -1;
906 }
908 if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
909 return (-1);
911 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
912 return (-1);
914 if (stat (filename, &statbuf) == -1)
915 {
916 if (errno == ENOENT)
917 {
918 status = cu_rrd_create_file (filename,
919 ds, vl, &rrdcreate_config);
920 if (status != 0)
921 return (-1);
922 else if (rrdcreate_config.async)
923 return (0);
924 }
925 else
926 {
927 char errbuf[1024];
928 ERROR ("stat(%s) failed: %s", filename,
929 sstrerror (errno, errbuf,
930 sizeof (errbuf)));
931 return (-1);
932 }
933 }
934 else if (!S_ISREG (statbuf.st_mode))
935 {
936 ERROR ("stat(%s): Not a regular file!",
937 filename);
938 return (-1);
939 }
941 status = rrd_cache_insert (filename, values, vl->time);
943 return (status);
944 } /* int rrd_write */
946 static int rrd_flush (cdtime_t timeout, const char *identifier,
947 __attribute__((unused)) user_data_t *user_data)
948 {
949 pthread_mutex_lock (&cache_lock);
951 if (cache == NULL) {
952 pthread_mutex_unlock (&cache_lock);
953 return (0);
954 }
956 rrd_cache_flush_identifier (timeout, identifier);
958 pthread_mutex_unlock (&cache_lock);
959 return (0);
960 } /* int rrd_flush */
962 static int rrd_config (const char *key, const char *value)
963 {
964 if (strcasecmp ("CacheTimeout", key) == 0)
965 {
966 double tmp = atof (value);
967 if (tmp < 0)
968 {
969 fprintf (stderr, "rrdtool: `CacheTimeout' must "
970 "be greater than 0.\n");
971 ERROR ("rrdtool: `CacheTimeout' must "
972 "be greater than 0.\n");
973 return (1);
974 }
975 cache_timeout = DOUBLE_TO_CDTIME_T (tmp);
976 }
977 else if (strcasecmp ("CacheFlush", key) == 0)
978 {
979 int tmp = atoi (value);
980 if (tmp < 0)
981 {
982 fprintf (stderr, "rrdtool: `CacheFlush' must "
983 "be greater than 0.\n");
984 ERROR ("rrdtool: `CacheFlush' must "
985 "be greater than 0.\n");
986 return (1);
987 }
988 cache_flush_timeout = tmp;
989 }
990 else if (strcasecmp ("DataDir", key) == 0)
991 {
992 if (datadir != NULL)
993 free (datadir);
994 datadir = strdup (value);
995 if (datadir != NULL)
996 {
997 int len = strlen (datadir);
998 while ((len > 0) && (datadir[len - 1] == '/'))
999 {
1000 len--;
1001 datadir[len] = '\0';
1002 }
1003 if (len <= 0)
1004 {
1005 free (datadir);
1006 datadir = NULL;
1007 }
1008 }
1009 }
1010 else if (strcasecmp ("StepSize", key) == 0)
1011 {
1012 unsigned long temp = strtoul (value, NULL, 0);
1013 if (temp > 0)
1014 rrdcreate_config.stepsize = temp;
1015 }
1016 else if (strcasecmp ("HeartBeat", key) == 0)
1017 {
1018 int temp = atoi (value);
1019 if (temp > 0)
1020 rrdcreate_config.heartbeat = temp;
1021 }
1022 else if (strcasecmp ("CreateFilesAsync", key) == 0)
1023 {
1024 if (IS_TRUE (value))
1025 rrdcreate_config.async = 1;
1026 else
1027 rrdcreate_config.async = 0;
1028 }
1029 else if (strcasecmp ("RRARows", key) == 0)
1030 {
1031 int tmp = atoi (value);
1032 if (tmp <= 0)
1033 {
1034 fprintf (stderr, "rrdtool: `RRARows' must "
1035 "be greater than 0.\n");
1036 ERROR ("rrdtool: `RRARows' must "
1037 "be greater than 0.\n");
1038 return (1);
1039 }
1040 rrdcreate_config.rrarows = tmp;
1041 }
1042 else if (strcasecmp ("RRATimespan", key) == 0)
1043 {
1044 char *saveptr = NULL;
1045 char *dummy;
1046 char *ptr;
1047 char *value_copy;
1048 int *tmp_alloc;
1050 value_copy = strdup (value);
1051 if (value_copy == NULL)
1052 return (1);
1054 dummy = value_copy;
1055 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1056 {
1057 dummy = NULL;
1059 tmp_alloc = realloc (rrdcreate_config.timespans,
1060 sizeof (int) * (rrdcreate_config.timespans_num + 1));
1061 if (tmp_alloc == NULL)
1062 {
1063 fprintf (stderr, "rrdtool: realloc failed.\n");
1064 ERROR ("rrdtool: realloc failed.\n");
1065 free (value_copy);
1066 return (1);
1067 }
1068 rrdcreate_config.timespans = tmp_alloc;
1069 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = atoi (ptr);
1070 if (rrdcreate_config.timespans[rrdcreate_config.timespans_num] != 0)
1071 rrdcreate_config.timespans_num++;
1072 } /* while (strtok_r) */
1074 qsort (/* base = */ rrdcreate_config.timespans,
1075 /* nmemb = */ rrdcreate_config.timespans_num,
1076 /* size = */ sizeof (rrdcreate_config.timespans[0]),
1077 /* compar = */ rrd_compare_numeric);
1079 free (value_copy);
1080 }
1081 else if (strcasecmp ("XFF", key) == 0)
1082 {
1083 double tmp = atof (value);
1084 if ((tmp < 0.0) || (tmp >= 1.0))
1085 {
1086 fprintf (stderr, "rrdtool: `XFF' must "
1087 "be in the range 0 to 1 (exclusive).");
1088 ERROR ("rrdtool: `XFF' must "
1089 "be in the range 0 to 1 (exclusive).");
1090 return (1);
1091 }
1092 rrdcreate_config.xff = tmp;
1093 }
1094 else if (strcasecmp ("WritesPerSecond", key) == 0)
1095 {
1096 double wps = atof (value);
1098 if (wps < 0.0)
1099 {
1100 fprintf (stderr, "rrdtool: `WritesPerSecond' must be "
1101 "greater than or equal to zero.");
1102 return (1);
1103 }
1104 else if (wps == 0.0)
1105 {
1106 write_rate = 0.0;
1107 }
1108 else
1109 {
1110 write_rate = 1.0 / wps;
1111 }
1112 }
1113 else if (strcasecmp ("RandomTimeout", key) == 0)
1114 {
1115 double tmp;
1117 tmp = atof (value);
1118 if (tmp < 0.0)
1119 {
1120 fprintf (stderr, "rrdtool: `RandomTimeout' must "
1121 "be greater than or equal to zero.\n");
1122 ERROR ("rrdtool: `RandomTimeout' must "
1123 "be greater then or equal to zero.");
1124 }
1125 else
1126 {
1127 random_timeout = DOUBLE_TO_CDTIME_T (tmp);
1128 }
1129 }
1130 else
1131 {
1132 return (-1);
1133 }
1134 return (0);
1135 } /* int rrd_config */
1137 static int rrd_shutdown (void)
1138 {
1139 pthread_mutex_lock (&cache_lock);
1140 rrd_cache_flush (0);
1141 pthread_mutex_unlock (&cache_lock);
1143 pthread_mutex_lock (&queue_lock);
1144 do_shutdown = 1;
1145 pthread_cond_signal (&queue_cond);
1146 pthread_mutex_unlock (&queue_lock);
1148 if ((queue_thread_running != 0)
1149 && ((queue_head != NULL) || (flushq_head != NULL)))
1150 {
1151 INFO ("rrdtool plugin: Shutting down the queue thread. "
1152 "This may take a while.");
1153 }
1154 else if (queue_thread_running != 0)
1155 {
1156 INFO ("rrdtool plugin: Shutting down the queue thread.");
1157 }
1159 /* Wait for all the values to be written to disk before returning. */
1160 if (queue_thread_running != 0)
1161 {
1162 pthread_join (queue_thread, NULL);
1163 memset (&queue_thread, 0, sizeof (queue_thread));
1164 queue_thread_running = 0;
1165 DEBUG ("rrdtool plugin: queue_thread exited.");
1166 }
1168 rrd_cache_destroy ();
1170 return (0);
1171 } /* int rrd_shutdown */
1173 static int rrd_init (void)
1174 {
1175 static int init_once = 0;
1176 int status;
1178 if (init_once != 0)
1179 return (0);
1180 init_once = 1;
1182 if (rrdcreate_config.heartbeat <= 0)
1183 rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize;
1185 /* Set the cache up */
1186 pthread_mutex_lock (&cache_lock);
1188 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1189 if (cache == NULL)
1190 {
1191 ERROR ("rrdtool plugin: c_avl_create failed.");
1192 return (-1);
1193 }
1195 cache_flush_last = cdtime ();
1196 if (cache_timeout == 0)
1197 {
1198 cache_flush_timeout = 0;
1199 }
1200 else if (cache_flush_timeout < cache_timeout)
1201 cache_flush_timeout = 10 * cache_timeout;
1203 pthread_mutex_unlock (&cache_lock);
1205 status = plugin_thread_create (&queue_thread, /* attr = */ NULL,
1206 rrd_queue_thread, /* args = */ NULL);
1207 if (status != 0)
1208 {
1209 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1210 return (-1);
1211 }
1212 queue_thread_running = 1;
1214 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %lu;"
1215 " heartbeat = %i; rrarows = %i; xff = %lf;",
1216 (datadir == NULL) ? "(null)" : datadir,
1217 rrdcreate_config.stepsize,
1218 rrdcreate_config.heartbeat,
1219 rrdcreate_config.rrarows,
1220 rrdcreate_config.xff);
1222 return (0);
1223 } /* int rrd_init */
1225 void module_register (void)
1226 {
1227 plugin_register_config ("rrdtool", rrd_config,
1228 config_keys, config_keys_num);
1229 plugin_register_init ("rrdtool", rrd_init);
1230 plugin_register_write ("rrdtool", rrd_write, /* user_data = */ NULL);
1231 plugin_register_flush ("rrdtool", rrd_flush, /* user_data = */ NULL);
1232 plugin_register_shutdown ("rrdtool", rrd_shutdown);
1233 }