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 (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 */
247 static int value_list_to_filename (char *buffer, int buffer_len,
248 const data_set_t __attribute__((unused)) *ds, const value_list_t *vl)
249 {
250 int offset = 0;
251 int status;
253 if (datadir != NULL)
254 {
255 status = ssnprintf (buffer + offset, buffer_len - offset,
256 "%s/", datadir);
257 if ((status < 1) || (status >= buffer_len - offset))
258 return (-1);
259 offset += status;
260 }
262 status = ssnprintf (buffer + offset, buffer_len - offset,
263 "%s/", vl->host);
264 if ((status < 1) || (status >= buffer_len - offset))
265 return (-1);
266 offset += status;
268 if (strlen (vl->plugin_instance) > 0)
269 status = ssnprintf (buffer + offset, buffer_len - offset,
270 "%s-%s/", vl->plugin, vl->plugin_instance);
271 else
272 status = ssnprintf (buffer + offset, buffer_len - offset,
273 "%s/", vl->plugin);
274 if ((status < 1) || (status >= buffer_len - offset))
275 return (-1);
276 offset += status;
278 if (strlen (vl->type_instance) > 0)
279 status = ssnprintf (buffer + offset, buffer_len - offset,
280 "%s-%s.rrd", vl->type, vl->type_instance);
281 else
282 status = ssnprintf (buffer + offset, buffer_len - offset,
283 "%s.rrd", vl->type);
284 if ((status < 1) || (status >= buffer_len - offset))
285 return (-1);
286 offset += status;
288 return (0);
289 } /* int value_list_to_filename */
291 static void *rrd_queue_thread (void __attribute__((unused)) *data)
292 {
293 struct timeval tv_next_update;
294 struct timeval tv_now;
296 gettimeofday (&tv_next_update, /* timezone = */ NULL);
298 while (42)
299 {
300 rrd_queue_t *queue_entry;
301 rrd_cache_t *cache_entry;
302 char **values;
303 int values_num;
304 int status;
305 int i;
307 values = NULL;
308 values_num = 0;
310 pthread_mutex_lock (&queue_lock);
311 /* Wait for values to arrive */
312 while (42)
313 {
314 struct timespec ts_wait;
316 while ((flushq_head == NULL) && (queue_head == NULL)
317 && (do_shutdown == 0))
318 pthread_cond_wait (&queue_cond, &queue_lock);
320 if ((flushq_head == NULL) && (queue_head == NULL))
321 break;
323 /* Don't delay if there's something to flush */
324 if (flushq_head != NULL)
325 break;
327 /* Don't delay if we're shutting down */
328 if (do_shutdown != 0)
329 break;
331 /* Don't delay if no delay was configured. */
332 if (write_rate <= 0.0)
333 break;
335 gettimeofday (&tv_now, /* timezone = */ NULL);
336 status = timeval_cmp (tv_next_update, tv_now, NULL);
337 /* We're good to go */
338 if (status <= 0)
339 break;
341 /* We're supposed to wait a bit with this update, so we'll
342 * wait for the next addition to the queue or to the end of
343 * the wait period - whichever comes first. */
344 ts_wait.tv_sec = tv_next_update.tv_sec;
345 ts_wait.tv_nsec = 1000 * tv_next_update.tv_usec;
347 status = pthread_cond_timedwait (&queue_cond, &queue_lock,
348 &ts_wait);
349 if (status == ETIMEDOUT)
350 break;
351 } /* while (42) */
353 /* XXX: If you need to lock both, cache_lock and queue_lock, at
354 * the same time, ALWAYS lock `cache_lock' first! */
356 /* We're in the shutdown phase */
357 if ((flushq_head == NULL) && (queue_head == NULL))
358 {
359 pthread_mutex_unlock (&queue_lock);
360 break;
361 }
363 if (flushq_head != NULL)
364 {
365 /* Dequeue the first flush entry */
366 queue_entry = flushq_head;
367 if (flushq_head == flushq_tail)
368 flushq_head = flushq_tail = NULL;
369 else
370 flushq_head = flushq_head->next;
371 }
372 else /* if (queue_head != NULL) */
373 {
374 /* Dequeue the first regular entry */
375 queue_entry = queue_head;
376 if (queue_head == queue_tail)
377 queue_head = queue_tail = NULL;
378 else
379 queue_head = queue_head->next;
380 }
382 /* Unlock the queue again */
383 pthread_mutex_unlock (&queue_lock);
385 /* We now need the cache lock so the entry isn't updated while
386 * we make a copy of it's values */
387 pthread_mutex_lock (&cache_lock);
389 status = c_avl_get (cache, queue_entry->filename,
390 (void *) &cache_entry);
392 if (status == 0)
393 {
394 values = cache_entry->values;
395 values_num = cache_entry->values_num;
397 cache_entry->values = NULL;
398 cache_entry->values_num = 0;
399 cache_entry->flags = FLAG_NONE;
400 }
402 pthread_mutex_unlock (&cache_lock);
404 if (status != 0)
405 {
406 sfree (queue_entry->filename);
407 sfree (queue_entry);
408 continue;
409 }
411 /* Update `tv_next_update' */
412 if (write_rate > 0.0)
413 {
414 gettimeofday (&tv_now, /* timezone = */ NULL);
415 tv_next_update.tv_sec = tv_now.tv_sec;
416 tv_next_update.tv_usec = tv_now.tv_usec
417 + ((suseconds_t) (1000000 * write_rate));
418 while (tv_next_update.tv_usec > 1000000)
419 {
420 tv_next_update.tv_sec++;
421 tv_next_update.tv_usec -= 1000000;
422 }
423 }
425 /* Write the values to the RRD-file */
426 srrd_update (queue_entry->filename, NULL,
427 values_num, (const char **)values);
428 DEBUG ("rrdtool plugin: queue thread: Wrote %i value%s to %s",
429 values_num, (values_num == 1) ? "" : "s",
430 queue_entry->filename);
432 for (i = 0; i < values_num; i++)
433 {
434 sfree (values[i]);
435 }
436 sfree (values);
437 sfree (queue_entry->filename);
438 sfree (queue_entry);
439 } /* while (42) */
441 pthread_exit ((void *) 0);
442 return ((void *) 0);
443 } /* void *rrd_queue_thread */
445 static int rrd_queue_enqueue (const char *filename,
446 rrd_queue_t **head, rrd_queue_t **tail)
447 {
448 rrd_queue_t *queue_entry;
450 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
451 if (queue_entry == NULL)
452 return (-1);
454 queue_entry->filename = strdup (filename);
455 if (queue_entry->filename == NULL)
456 {
457 free (queue_entry);
458 return (-1);
459 }
461 queue_entry->next = NULL;
463 pthread_mutex_lock (&queue_lock);
465 if (*tail == NULL)
466 *head = queue_entry;
467 else
468 (*tail)->next = queue_entry;
469 *tail = queue_entry;
471 pthread_cond_signal (&queue_cond);
472 pthread_mutex_unlock (&queue_lock);
474 return (0);
475 } /* int rrd_queue_enqueue */
477 static int rrd_queue_dequeue (const char *filename,
478 rrd_queue_t **head, rrd_queue_t **tail)
479 {
480 rrd_queue_t *this;
481 rrd_queue_t *prev;
483 pthread_mutex_lock (&queue_lock);
485 prev = NULL;
486 this = *head;
488 while (this != NULL)
489 {
490 if (strcmp (this->filename, filename) == 0)
491 break;
493 prev = this;
494 this = this->next;
495 }
497 if (this == NULL)
498 {
499 pthread_mutex_unlock (&queue_lock);
500 return (-1);
501 }
503 if (prev == NULL)
504 *head = this->next;
505 else
506 prev->next = this->next;
508 if (this->next == NULL)
509 *tail = prev;
511 pthread_mutex_unlock (&queue_lock);
513 sfree (this->filename);
514 sfree (this);
516 return (0);
517 } /* int rrd_queue_dequeue */
519 /* XXX: You must hold "cache_lock" when calling this function! */
520 static void rrd_cache_flush (cdtime_t timeout)
521 {
522 rrd_cache_t *rc;
523 cdtime_t now;
525 char **keys = NULL;
526 int keys_num = 0;
528 char *key;
529 c_avl_iterator_t *iter;
530 int i;
532 DEBUG ("rrdtool plugin: Flushing cache, timeout = %.3f",
533 CDTIME_T_TO_DOUBLE (timeout));
535 now = cdtime ();
536 timeout = TIME_T_TO_CDTIME_T (timeout);
538 /* Build a list of entries to be flushed */
539 iter = c_avl_get_iterator (cache);
540 while (c_avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
541 {
542 if (rc->flags != FLAG_NONE)
543 continue;
544 /* timeout == 0 => flush everything */
545 else if ((timeout != 0)
546 && ((now - rc->first_value) < timeout))
547 continue;
548 else if (rc->values_num > 0)
549 {
550 int status;
552 status = rrd_queue_enqueue (key, &queue_head, &queue_tail);
553 if (status == 0)
554 rc->flags = FLAG_QUEUED;
555 }
556 else /* ancient and no values -> waste of memory */
557 {
558 char **tmp = (char **) realloc ((void *) keys,
559 (keys_num + 1) * sizeof (char *));
560 if (tmp == NULL)
561 {
562 char errbuf[1024];
563 ERROR ("rrdtool plugin: "
564 "realloc failed: %s",
565 sstrerror (errno, errbuf,
566 sizeof (errbuf)));
567 c_avl_iterator_destroy (iter);
568 sfree (keys);
569 return;
570 }
571 keys = tmp;
572 keys[keys_num] = key;
573 keys_num++;
574 }
575 } /* while (c_avl_iterator_next) */
576 c_avl_iterator_destroy (iter);
578 for (i = 0; i < keys_num; i++)
579 {
580 if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
581 {
582 DEBUG ("rrdtool plugin: c_avl_remove (%s) failed.", keys[i]);
583 continue;
584 }
586 assert (rc->values == NULL);
587 assert (rc->values_num == 0);
589 sfree (rc);
590 sfree (key);
591 keys[i] = NULL;
592 } /* for (i = 0..keys_num) */
594 sfree (keys);
596 cache_flush_last = now;
597 } /* void rrd_cache_flush */
599 static int rrd_cache_flush_identifier (cdtime_t timeout,
600 const char *identifier)
601 {
602 rrd_cache_t *rc;
603 cdtime_t now;
604 int status;
605 char key[2048];
607 if (identifier == NULL)
608 {
609 rrd_cache_flush (timeout);
610 return (0);
611 }
613 now = cdtime ();
615 if (datadir == NULL)
616 snprintf (key, sizeof (key), "%s.rrd",
617 identifier);
618 else
619 snprintf (key, sizeof (key), "%s/%s.rrd",
620 datadir, identifier);
621 key[sizeof (key) - 1] = 0;
623 status = c_avl_get (cache, key, (void *) &rc);
624 if (status != 0)
625 {
626 INFO ("rrdtool plugin: rrd_cache_flush_identifier: "
627 "c_avl_get (%s) failed. Does that file really exist?",
628 key);
629 return (status);
630 }
632 if (rc->flags == FLAG_FLUSHQ)
633 {
634 status = 0;
635 }
636 else if (rc->flags == FLAG_QUEUED)
637 {
638 rrd_queue_dequeue (key, &queue_head, &queue_tail);
639 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
640 if (status == 0)
641 rc->flags = FLAG_FLUSHQ;
642 }
643 else if ((now - rc->first_value) < timeout)
644 {
645 status = 0;
646 }
647 else if (rc->values_num > 0)
648 {
649 status = rrd_queue_enqueue (key, &flushq_head, &flushq_tail);
650 if (status == 0)
651 rc->flags = FLAG_FLUSHQ;
652 }
654 return (status);
655 } /* int rrd_cache_flush_identifier */
657 static int64_t rrd_get_random_variation (void)
658 {
659 long min;
660 long max;
662 if (random_timeout <= 0)
663 return (0);
665 /* Assure that "cache_timeout + random_variation" is never negative. */
666 if (random_timeout > cache_timeout)
667 {
668 INFO ("rrdtool plugin: Adjusting \"RandomTimeout\" to %.3f seconds.",
669 CDTIME_T_TO_DOUBLE (cache_timeout));
670 random_timeout = cache_timeout;
671 }
673 max = (long) (random_timeout / 2);
674 min = max - ((long) random_timeout);
676 return ((int64_t) cdrand_range (min, max));
677 } /* int64_t rrd_get_random_variation */
679 static int rrd_cache_insert (const char *filename,
680 const char *value, cdtime_t value_time)
681 {
682 rrd_cache_t *rc = NULL;
683 int new_rc = 0;
684 char **values_new;
686 pthread_mutex_lock (&cache_lock);
688 /* This shouldn't happen, but it did happen at least once, so we'll be
689 * careful. */
690 if (cache == NULL)
691 {
692 pthread_mutex_unlock (&cache_lock);
693 WARNING ("rrdtool plugin: cache == NULL.");
694 return (-1);
695 }
697 c_avl_get (cache, filename, (void *) &rc);
699 if (rc == NULL)
700 {
701 rc = malloc (sizeof (*rc));
702 if (rc == NULL)
703 return (-1);
704 rc->values_num = 0;
705 rc->values = NULL;
706 rc->first_value = 0;
707 rc->last_value = 0;
708 rc->random_variation = rrd_get_random_variation ();
709 rc->flags = FLAG_NONE;
710 new_rc = 1;
711 }
713 if (rc->last_value >= value_time)
714 {
715 pthread_mutex_unlock (&cache_lock);
716 DEBUG ("rrdtool plugin: (rc->last_value = %"PRIu64") "
717 ">= (value_time = %"PRIu64")",
718 rc->last_value, value_time);
719 return (-1);
720 }
722 values_new = (char **) realloc ((void *) rc->values,
723 (rc->values_num + 1) * sizeof (char *));
724 if (values_new == NULL)
725 {
726 char errbuf[1024];
727 void *cache_key = NULL;
729 sstrerror (errno, errbuf, sizeof (errbuf));
731 c_avl_remove (cache, filename, &cache_key, NULL);
732 pthread_mutex_unlock (&cache_lock);
734 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
736 sfree (cache_key);
737 sfree (rc->values);
738 sfree (rc);
739 return (-1);
740 }
741 rc->values = values_new;
743 rc->values[rc->values_num] = strdup (value);
744 if (rc->values[rc->values_num] != NULL)
745 rc->values_num++;
747 if (rc->values_num == 1)
748 rc->first_value = value_time;
749 rc->last_value = value_time;
751 /* Insert if this is the first value */
752 if (new_rc == 1)
753 {
754 void *cache_key = strdup (filename);
756 if (cache_key == NULL)
757 {
758 char errbuf[1024];
759 sstrerror (errno, errbuf, sizeof (errbuf));
761 pthread_mutex_unlock (&cache_lock);
763 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
765 sfree (rc->values[0]);
766 sfree (rc->values);
767 sfree (rc);
768 return (-1);
769 }
771 c_avl_insert (cache, cache_key, rc);
772 }
774 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
775 "values_num = %i; age = %.3f;",
776 filename, rc->values_num,
777 CDTIME_T_TO_DOUBLE (rc->last_value - rc->first_value));
779 if ((rc->last_value - rc->first_value) >= (cache_timeout + rc->random_variation))
780 {
781 /* XXX: If you need to lock both, cache_lock and queue_lock, at
782 * the same time, ALWAYS lock `cache_lock' first! */
783 if (rc->flags == FLAG_NONE)
784 {
785 int status;
787 status = rrd_queue_enqueue (filename, &queue_head, &queue_tail);
788 if (status == 0)
789 rc->flags = FLAG_QUEUED;
791 rc->random_variation = rrd_get_random_variation ();
792 }
793 else
794 {
795 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
796 }
797 }
799 if ((cache_timeout > 0) &&
800 ((cdtime () - cache_flush_last) > cache_flush_timeout))
801 rrd_cache_flush (cache_flush_timeout);
803 pthread_mutex_unlock (&cache_lock);
805 return (0);
806 } /* int rrd_cache_insert */
808 static int rrd_cache_destroy (void) /* {{{ */
809 {
810 void *key = NULL;
811 void *value = NULL;
813 int non_empty = 0;
815 pthread_mutex_lock (&cache_lock);
817 if (cache == NULL)
818 {
819 pthread_mutex_unlock (&cache_lock);
820 return (0);
821 }
823 while (c_avl_pick (cache, &key, &value) == 0)
824 {
825 rrd_cache_t *rc;
826 int i;
828 sfree (key);
829 key = NULL;
831 rc = value;
832 value = NULL;
834 if (rc->values_num > 0)
835 non_empty++;
837 for (i = 0; i < rc->values_num; i++)
838 sfree (rc->values[i]);
839 sfree (rc->values);
840 sfree (rc);
841 }
843 c_avl_destroy (cache);
844 cache = NULL;
846 if (non_empty > 0)
847 {
848 INFO ("rrdtool plugin: %i cache %s had values when destroying the cache.",
849 non_empty, (non_empty == 1) ? "entry" : "entries");
850 }
851 else
852 {
853 DEBUG ("rrdtool plugin: No values have been lost "
854 "when destroying the cache.");
855 }
857 pthread_mutex_unlock (&cache_lock);
858 return (0);
859 } /* }}} int rrd_cache_destroy */
861 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
862 {
863 int a = *((int *) a_ptr);
864 int b = *((int *) b_ptr);
866 if (a < b)
867 return (-1);
868 else if (a > b)
869 return (1);
870 else
871 return (0);
872 } /* int rrd_compare_numeric */
874 static int rrd_write (const data_set_t *ds, const value_list_t *vl,
875 user_data_t __attribute__((unused)) *user_data)
876 {
877 struct stat statbuf;
878 char filename[512];
879 char values[512];
880 int status;
882 if (do_shutdown)
883 return (0);
885 if (0 != strcmp (ds->type, vl->type)) {
886 ERROR ("rrdtool plugin: DS type does not match value list type");
887 return -1;
888 }
890 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
891 return (-1);
893 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
894 return (-1);
896 if (stat (filename, &statbuf) == -1)
897 {
898 if (errno == ENOENT)
899 {
900 status = cu_rrd_create_file (filename,
901 ds, vl, &rrdcreate_config);
902 if (status != 0)
903 return (-1);
904 else if (rrdcreate_config.async)
905 return (0);
906 }
907 else
908 {
909 char errbuf[1024];
910 ERROR ("stat(%s) failed: %s", filename,
911 sstrerror (errno, errbuf,
912 sizeof (errbuf)));
913 return (-1);
914 }
915 }
916 else if (!S_ISREG (statbuf.st_mode))
917 {
918 ERROR ("stat(%s): Not a regular file!",
919 filename);
920 return (-1);
921 }
923 status = rrd_cache_insert (filename, values, vl->time);
925 return (status);
926 } /* int rrd_write */
928 static int rrd_flush (cdtime_t timeout, const char *identifier,
929 __attribute__((unused)) user_data_t *user_data)
930 {
931 pthread_mutex_lock (&cache_lock);
933 if (cache == NULL) {
934 pthread_mutex_unlock (&cache_lock);
935 return (0);
936 }
938 rrd_cache_flush_identifier (timeout, identifier);
940 pthread_mutex_unlock (&cache_lock);
941 return (0);
942 } /* int rrd_flush */
944 static int rrd_config (const char *key, const char *value)
945 {
946 if (strcasecmp ("CacheTimeout", key) == 0)
947 {
948 double tmp = atof (value);
949 if (tmp < 0)
950 {
951 fprintf (stderr, "rrdtool: `CacheTimeout' must "
952 "be greater than 0.\n");
953 ERROR ("rrdtool: `CacheTimeout' must "
954 "be greater than 0.\n");
955 return (1);
956 }
957 cache_timeout = DOUBLE_TO_CDTIME_T (tmp);
958 }
959 else if (strcasecmp ("CacheFlush", key) == 0)
960 {
961 int tmp = atoi (value);
962 if (tmp < 0)
963 {
964 fprintf (stderr, "rrdtool: `CacheFlush' must "
965 "be greater than 0.\n");
966 ERROR ("rrdtool: `CacheFlush' must "
967 "be greater than 0.\n");
968 return (1);
969 }
970 cache_flush_timeout = tmp;
971 }
972 else if (strcasecmp ("DataDir", key) == 0)
973 {
974 if (datadir != NULL)
975 free (datadir);
976 datadir = strdup (value);
977 if (datadir != NULL)
978 {
979 int len = strlen (datadir);
980 while ((len > 0) && (datadir[len - 1] == '/'))
981 {
982 len--;
983 datadir[len] = '\0';
984 }
985 if (len <= 0)
986 {
987 free (datadir);
988 datadir = NULL;
989 }
990 }
991 }
992 else if (strcasecmp ("StepSize", key) == 0)
993 {
994 unsigned long temp = strtoul (value, NULL, 0);
995 if (temp > 0)
996 rrdcreate_config.stepsize = temp;
997 }
998 else if (strcasecmp ("HeartBeat", key) == 0)
999 {
1000 int temp = atoi (value);
1001 if (temp > 0)
1002 rrdcreate_config.heartbeat = temp;
1003 }
1004 else if (strcasecmp ("CreateFilesAsync", key) == 0)
1005 {
1006 if (IS_TRUE (value))
1007 rrdcreate_config.async = 1;
1008 else
1009 rrdcreate_config.async = 0;
1010 }
1011 else if (strcasecmp ("RRARows", key) == 0)
1012 {
1013 int tmp = atoi (value);
1014 if (tmp <= 0)
1015 {
1016 fprintf (stderr, "rrdtool: `RRARows' must "
1017 "be greater than 0.\n");
1018 ERROR ("rrdtool: `RRARows' must "
1019 "be greater than 0.\n");
1020 return (1);
1021 }
1022 rrdcreate_config.rrarows = tmp;
1023 }
1024 else if (strcasecmp ("RRATimespan", key) == 0)
1025 {
1026 char *saveptr = NULL;
1027 char *dummy;
1028 char *ptr;
1029 char *value_copy;
1030 int *tmp_alloc;
1032 value_copy = strdup (value);
1033 if (value_copy == NULL)
1034 return (1);
1036 dummy = value_copy;
1037 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1038 {
1039 dummy = NULL;
1041 tmp_alloc = realloc (rrdcreate_config.timespans,
1042 sizeof (int) * (rrdcreate_config.timespans_num + 1));
1043 if (tmp_alloc == NULL)
1044 {
1045 fprintf (stderr, "rrdtool: realloc failed.\n");
1046 ERROR ("rrdtool: realloc failed.\n");
1047 free (value_copy);
1048 return (1);
1049 }
1050 rrdcreate_config.timespans = tmp_alloc;
1051 rrdcreate_config.timespans[rrdcreate_config.timespans_num] = atoi (ptr);
1052 if (rrdcreate_config.timespans[rrdcreate_config.timespans_num] != 0)
1053 rrdcreate_config.timespans_num++;
1054 } /* while (strtok_r) */
1056 qsort (/* base = */ rrdcreate_config.timespans,
1057 /* nmemb = */ rrdcreate_config.timespans_num,
1058 /* size = */ sizeof (rrdcreate_config.timespans[0]),
1059 /* compar = */ rrd_compare_numeric);
1061 free (value_copy);
1062 }
1063 else if (strcasecmp ("XFF", key) == 0)
1064 {
1065 double tmp = atof (value);
1066 if ((tmp < 0.0) || (tmp >= 1.0))
1067 {
1068 fprintf (stderr, "rrdtool: `XFF' must "
1069 "be in the range 0 to 1 (exclusive).");
1070 ERROR ("rrdtool: `XFF' must "
1071 "be in the range 0 to 1 (exclusive).");
1072 return (1);
1073 }
1074 rrdcreate_config.xff = tmp;
1075 }
1076 else if (strcasecmp ("WritesPerSecond", key) == 0)
1077 {
1078 double wps = atof (value);
1080 if (wps < 0.0)
1081 {
1082 fprintf (stderr, "rrdtool: `WritesPerSecond' must be "
1083 "greater than or equal to zero.");
1084 return (1);
1085 }
1086 else if (wps == 0.0)
1087 {
1088 write_rate = 0.0;
1089 }
1090 else
1091 {
1092 write_rate = 1.0 / wps;
1093 }
1094 }
1095 else if (strcasecmp ("RandomTimeout", key) == 0)
1096 {
1097 double tmp;
1099 tmp = atof (value);
1100 if (tmp < 0.0)
1101 {
1102 fprintf (stderr, "rrdtool: `RandomTimeout' must "
1103 "be greater than or equal to zero.\n");
1104 ERROR ("rrdtool: `RandomTimeout' must "
1105 "be greater then or equal to zero.");
1106 }
1107 else
1108 {
1109 random_timeout = DOUBLE_TO_CDTIME_T (tmp);
1110 }
1111 }
1112 else
1113 {
1114 return (-1);
1115 }
1116 return (0);
1117 } /* int rrd_config */
1119 static int rrd_shutdown (void)
1120 {
1121 pthread_mutex_lock (&cache_lock);
1122 rrd_cache_flush (0);
1123 pthread_mutex_unlock (&cache_lock);
1125 pthread_mutex_lock (&queue_lock);
1126 do_shutdown = 1;
1127 pthread_cond_signal (&queue_cond);
1128 pthread_mutex_unlock (&queue_lock);
1130 if ((queue_thread_running != 0)
1131 && ((queue_head != NULL) || (flushq_head != NULL)))
1132 {
1133 INFO ("rrdtool plugin: Shutting down the queue thread. "
1134 "This may take a while.");
1135 }
1136 else if (queue_thread_running != 0)
1137 {
1138 INFO ("rrdtool plugin: Shutting down the queue thread.");
1139 }
1141 /* Wait for all the values to be written to disk before returning. */
1142 if (queue_thread_running != 0)
1143 {
1144 pthread_join (queue_thread, NULL);
1145 memset (&queue_thread, 0, sizeof (queue_thread));
1146 queue_thread_running = 0;
1147 DEBUG ("rrdtool plugin: queue_thread exited.");
1148 }
1150 rrd_cache_destroy ();
1152 return (0);
1153 } /* int rrd_shutdown */
1155 static int rrd_init (void)
1156 {
1157 static int init_once = 0;
1158 int status;
1160 if (init_once != 0)
1161 return (0);
1162 init_once = 1;
1164 if (rrdcreate_config.heartbeat <= 0)
1165 rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize;
1167 /* Set the cache up */
1168 pthread_mutex_lock (&cache_lock);
1170 cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1171 if (cache == NULL)
1172 {
1173 ERROR ("rrdtool plugin: c_avl_create failed.");
1174 return (-1);
1175 }
1177 cache_flush_last = cdtime ();
1178 if (cache_timeout == 0)
1179 {
1180 cache_flush_timeout = 0;
1181 }
1182 else if (cache_flush_timeout < cache_timeout)
1183 cache_flush_timeout = 10 * cache_timeout;
1185 pthread_mutex_unlock (&cache_lock);
1187 status = plugin_thread_create (&queue_thread, /* attr = */ NULL,
1188 rrd_queue_thread, /* args = */ NULL);
1189 if (status != 0)
1190 {
1191 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1192 return (-1);
1193 }
1194 queue_thread_running = 1;
1196 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %lu;"
1197 " heartbeat = %i; rrarows = %i; xff = %lf;",
1198 (datadir == NULL) ? "(null)" : datadir,
1199 rrdcreate_config.stepsize,
1200 rrdcreate_config.heartbeat,
1201 rrdcreate_config.rrarows,
1202 rrdcreate_config.xff);
1204 return (0);
1205 } /* int rrd_init */
1207 void module_register (void)
1208 {
1209 plugin_register_config ("rrdtool", rrd_config,
1210 config_keys, config_keys_num);
1211 plugin_register_init ("rrdtool", rrd_init);
1212 plugin_register_write ("rrdtool", rrd_write, /* user_data = */ NULL);
1213 plugin_register_flush ("rrdtool", rrd_flush, /* user_data = */ NULL);
1214 plugin_register_shutdown ("rrdtool", rrd_shutdown);
1215 }