1 /**
2 * collectd - src/rrdtool.c
3 * Copyright (C) 2006,2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "utils_avltree.h"
27 #if HAVE_PTHREAD_H
28 # include <pthread.h>
29 #endif
31 /*
32 * Private types
33 */
34 struct rrd_cache_s
35 {
36 int values_num;
37 char **values;
38 time_t first_value;
39 time_t last_value;
40 enum
41 {
42 FLAG_NONE = 0x00,
43 FLAG_QUEUED = 0x01
44 } flags;
45 };
46 typedef struct rrd_cache_s rrd_cache_t;
48 struct rrd_queue_s
49 {
50 char *filename;
51 struct rrd_queue_s *next;
52 };
53 typedef struct rrd_queue_s rrd_queue_t;
55 /*
56 * Private variables
57 */
58 static int rra_timespans[] =
59 {
60 3600,
61 86400,
62 604800,
63 2678400,
64 31622400
65 };
66 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
68 static int *rra_timespans_custom = NULL;
69 static int rra_timespans_custom_num = 0;
71 static char *rra_types[] =
72 {
73 "AVERAGE",
74 "MIN",
75 "MAX"
76 };
77 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
79 static const char *config_keys[] =
80 {
81 "CacheTimeout",
82 "CacheFlush",
83 "DataDir",
84 "StepSize",
85 "HeartBeat",
86 "RRARows",
87 "RRATimespan",
88 "XFF"
89 };
90 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
92 /* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
93 * is zero a default, depending on the `interval' member of the value list is
94 * being used. */
95 static char *datadir = NULL;
96 static int stepsize = 0;
97 static int heartbeat = 0;
98 static int rrarows = 1200;
99 static double xff = 0.1;
101 /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
102 * ALWAYS lock `cache_lock' first! */
103 static int cache_timeout = 0;
104 static int cache_flush_timeout = 0;
105 static time_t cache_flush_last;
106 static avl_tree_t *cache = NULL;
107 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
109 static rrd_queue_t *queue_head = NULL;
110 static rrd_queue_t *queue_tail = NULL;
111 static pthread_t queue_thread = 0;
112 static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
113 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
115 #if !HAVE_THREADSAFE_LIBRRD
116 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
117 #endif
119 static int do_shutdown = 0;
121 /* * * * * * * * * *
122 * WARNING: Magic *
123 * * * * * * * * * */
125 static void rra_free (int rra_num, char **rra_def)
126 {
127 int i;
129 for (i = 0; i < rra_num; i++)
130 {
131 sfree (rra_def[i]);
132 }
133 sfree (rra_def);
134 } /* void rra_free */
136 static int rra_get (char ***ret, const value_list_t *vl)
137 {
138 char **rra_def;
139 int rra_num;
141 int *rts;
142 int rts_num;
144 int rra_max;
146 int span;
148 int cdp_num;
149 int cdp_len;
150 int i, j;
152 char buffer[64];
154 /* The stepsize we use here: If it is user-set, use it. If not, use the
155 * interval of the value-list. */
156 int ss;
158 if (rrarows <= 0)
159 {
160 *ret = NULL;
161 return (-1);
162 }
164 ss = (stepsize > 0) ? stepsize : vl->interval;
165 if (ss <= 0)
166 {
167 *ret = NULL;
168 return (-1);
169 }
171 /* Use the configured timespans or fall back to the built-in defaults */
172 if (rra_timespans_custom_num != 0)
173 {
174 rts = rra_timespans_custom;
175 rts_num = rra_timespans_custom_num;
176 }
177 else
178 {
179 rts = rra_timespans;
180 rts_num = rra_timespans_num;
181 }
183 rra_max = rts_num * rra_types_num;
185 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
186 return (-1);
187 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
188 rra_num = 0;
190 cdp_len = 0;
191 for (i = 0; i < rts_num; i++)
192 {
193 span = rts[i];
195 if ((span / ss) < rrarows)
196 continue;
198 if (cdp_len == 0)
199 cdp_len = 1;
200 else
201 cdp_len = (int) floor (((double) span)
202 / ((double) (rrarows * ss)));
204 cdp_num = (int) ceil (((double) span)
205 / ((double) (cdp_len * ss)));
207 for (j = 0; j < rra_types_num; j++)
208 {
209 if (rra_num >= rra_max)
210 break;
212 if (snprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
213 rra_types[j], xff,
214 cdp_len, cdp_num) >= sizeof (buffer))
215 {
216 ERROR ("rra_get: Buffer would have been truncated.");
217 continue;
218 }
220 rra_def[rra_num++] = sstrdup (buffer);
221 }
222 }
224 #if COLLECT_DEBUG
225 DEBUG ("rra_num = %i", rra_num);
226 for (i = 0; i < rra_num; i++)
227 DEBUG (" %s", rra_def[i]);
228 #endif
230 *ret = rra_def;
231 return (rra_num);
232 } /* int rra_get */
234 static void ds_free (int ds_num, char **ds_def)
235 {
236 int i;
238 for (i = 0; i < ds_num; i++)
239 if (ds_def[i] != NULL)
240 free (ds_def[i]);
241 free (ds_def);
242 }
244 static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
245 {
246 char **ds_def;
247 int ds_num;
249 char min[32];
250 char max[32];
251 char buffer[128];
253 DEBUG ("ds->ds_num = %i", ds->ds_num);
255 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
256 if (ds_def == NULL)
257 {
258 char errbuf[1024];
259 ERROR ("rrdtool plugin: malloc failed: %s",
260 sstrerror (errno, errbuf, sizeof (errbuf)));
261 return (-1);
262 }
263 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
265 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
266 {
267 data_source_t *d = ds->ds + ds_num;
268 char *type;
269 int status;
271 ds_def[ds_num] = NULL;
273 if (d->type == DS_TYPE_COUNTER)
274 type = "COUNTER";
275 else if (d->type == DS_TYPE_GAUGE)
276 type = "GAUGE";
277 else
278 {
279 ERROR ("rrdtool plugin: Unknown DS type: %i",
280 d->type);
281 break;
282 }
284 if (isnan (d->min))
285 {
286 strcpy (min, "U");
287 }
288 else
289 {
290 snprintf (min, sizeof (min), "%lf", d->min);
291 min[sizeof (min) - 1] = '\0';
292 }
294 if (isnan (d->max))
295 {
296 strcpy (max, "U");
297 }
298 else
299 {
300 snprintf (max, sizeof (max), "%lf", d->max);
301 max[sizeof (max) - 1] = '\0';
302 }
304 status = snprintf (buffer, sizeof (buffer),
305 "DS:%s:%s:%i:%s:%s",
306 d->name, type,
307 (heartbeat > 0) ? heartbeat : (2 * vl->interval),
308 min, max);
309 if ((status < 1) || (status >= sizeof (buffer)))
310 break;
312 ds_def[ds_num] = sstrdup (buffer);
313 } /* for ds_num = 0 .. ds->ds_num */
315 #if COLLECT_DEBUG
316 {
317 int i;
318 DEBUG ("ds_num = %i", ds_num);
319 for (i = 0; i < ds_num; i++)
320 DEBUG (" %s", ds_def[i]);
321 }
322 #endif
324 if (ds_num != ds->ds_num)
325 {
326 ds_free (ds_num, ds_def);
327 return (-1);
328 }
330 *ret = ds_def;
331 return (ds_num);
332 }
334 #if HAVE_THREADSAFE_LIBRRD
335 static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
336 int argc, char **argv)
337 {
338 int status;
340 optind = 0; /* bug in librrd? */
341 rrd_clear_error ();
343 status = rrd_create_r (filename, pdp_step, last_up, argc, argv);
345 if (status != 0)
346 {
347 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
348 filename, rrd_get_error ());
349 }
351 return (status);
352 } /* int srrd_create */
354 static int srrd_update (char *filename, char *template, int argc, char **argv)
355 {
356 int status;
358 optind = 0; /* bug in librrd? */
359 rrd_clear_error ();
361 status = rrd_update_r (filename, template, argc, argv);
363 if (status != 0)
364 {
365 WARNING ("rrdtool plugin: rrd_update_r (%s) failed: %s",
366 filename, rrd_get_error ());
367 }
369 return (status);
370 } /* int srrd_update */
371 /* #endif HAVE_THREADSAFE_LIBRRD */
373 #else /* !HAVE_THREADSAFE_LIBRRD */
374 static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
375 int argc, char **argv)
376 {
377 int status;
379 int new_argc;
380 char **new_argv;
382 char pdp_step_str[16];
383 char last_up_str[16];
385 new_argc = 6 + argc;
386 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
387 if (new_argv == NULL)
388 {
389 ERROR ("rrdtool plugin: malloc failed.");
390 return (-1);
391 }
393 if (last_up == 0)
394 last_up = time (NULL) - 10;
396 snprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
397 pdp_step_str[sizeof (pdp_step_str) - 1] = '\0';
398 snprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);
399 last_up_str[sizeof (last_up_str) - 1] = '\0';
401 new_argv[0] = "create";
402 new_argv[1] = filename;
403 new_argv[2] = "-s";
404 new_argv[3] = pdp_step_str;
405 new_argv[4] = "-b";
406 new_argv[5] = last_up_str;
408 memcpy (new_argv + 6, argv, argc * sizeof (char *));
409 new_argv[new_argc] = NULL;
411 pthread_mutex_lock (&librrd_lock);
412 optind = 0; /* bug in librrd? */
413 rrd_clear_error ();
415 status = rrd_create (new_argc, new_argv);
416 pthread_mutex_unlock (&librrd_lock);
418 if (status != 0)
419 {
420 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
421 filename, rrd_get_error ());
422 }
424 sfree (new_argv);
426 return (status);
427 } /* int srrd_create */
429 static int srrd_update (char *filename, char *template, int argc, char **argv)
430 {
431 int status;
433 int new_argc;
434 char **new_argv;
436 assert (template == NULL);
438 new_argc = 2 + argc;
439 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
440 if (new_argv == NULL)
441 {
442 ERROR ("rrdtool plugin: malloc failed.");
443 return (-1);
444 }
446 new_argv[0] = "update";
447 new_argv[1] = filename;
449 memcpy (new_argv + 2, argv, argc * sizeof (char *));
450 new_argv[new_argc] = NULL;
452 pthread_mutex_lock (&librrd_lock);
453 optind = 0; /* bug in librrd? */
454 rrd_clear_error ();
456 status = rrd_update (new_argc, new_argv);
457 pthread_mutex_unlock (&librrd_lock);
459 if (status != 0)
460 {
461 WARNING ("rrdtool plugin: rrd_update_r failed: %s: %s",
462 argv[1], rrd_get_error ());
463 }
465 sfree (new_argv);
467 return (status);
468 } /* int srrd_update */
469 #endif /* !HAVE_THREADSAFE_LIBRRD */
471 static int rrd_create_file (char *filename, const data_set_t *ds, const value_list_t *vl)
472 {
473 char **argv;
474 int argc;
475 char **rra_def;
476 int rra_num;
477 char **ds_def;
478 int ds_num;
479 int status = 0;
481 if (check_create_dir (filename))
482 return (-1);
484 if ((rra_num = rra_get (&rra_def, vl)) < 1)
485 {
486 ERROR ("rrd_create_file failed: Could not calculate RRAs");
487 return (-1);
488 }
490 if ((ds_num = ds_get (&ds_def, ds, vl)) < 1)
491 {
492 ERROR ("rrd_create_file failed: Could not calculate DSes");
493 return (-1);
494 }
496 argc = ds_num + rra_num;
498 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
499 {
500 char errbuf[1024];
501 ERROR ("rrd_create failed: %s",
502 sstrerror (errno, errbuf, sizeof (errbuf)));
503 return (-1);
504 }
506 memcpy (argv, ds_def, ds_num * sizeof (char *));
507 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
508 argv[ds_num + rra_num] = NULL;
510 assert (vl->time > 10);
511 status = srrd_create (filename,
512 (stepsize > 0) ? stepsize : vl->interval,
513 vl->time - 10,
514 argc, argv);
516 free (argv);
517 ds_free (ds_num, ds_def);
518 rra_free (rra_num, rra_def);
520 return (status);
521 }
523 static int value_list_to_string (char *buffer, int buffer_len,
524 const data_set_t *ds, const value_list_t *vl)
525 {
526 int offset;
527 int status;
528 int i;
530 memset (buffer, '\0', buffer_len);
532 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
533 if ((status < 1) || (status >= buffer_len))
534 return (-1);
535 offset = status;
537 for (i = 0; i < ds->ds_num; i++)
538 {
539 if ((ds->ds[i].type != DS_TYPE_COUNTER)
540 && (ds->ds[i].type != DS_TYPE_GAUGE))
541 return (-1);
543 if (ds->ds[i].type == DS_TYPE_COUNTER)
544 status = snprintf (buffer + offset, buffer_len - offset,
545 ":%llu", vl->values[i].counter);
546 else
547 status = snprintf (buffer + offset, buffer_len - offset,
548 ":%lf", vl->values[i].gauge);
550 if ((status < 1) || (status >= (buffer_len - offset)))
551 return (-1);
553 offset += status;
554 } /* for ds->ds_num */
556 return (0);
557 } /* int value_list_to_string */
559 static int value_list_to_filename (char *buffer, int buffer_len,
560 const data_set_t *ds, const value_list_t *vl)
561 {
562 int offset = 0;
563 int status;
565 if (datadir != NULL)
566 {
567 status = snprintf (buffer + offset, buffer_len - offset,
568 "%s/", datadir);
569 if ((status < 1) || (status >= buffer_len - offset))
570 return (-1);
571 offset += status;
572 }
574 status = snprintf (buffer + offset, buffer_len - offset,
575 "%s/", vl->host);
576 if ((status < 1) || (status >= buffer_len - offset))
577 return (-1);
578 offset += status;
580 if (strlen (vl->plugin_instance) > 0)
581 status = snprintf (buffer + offset, buffer_len - offset,
582 "%s-%s/", vl->plugin, vl->plugin_instance);
583 else
584 status = snprintf (buffer + offset, buffer_len - offset,
585 "%s/", vl->plugin);
586 if ((status < 1) || (status >= buffer_len - offset))
587 return (-1);
588 offset += status;
590 if (strlen (vl->type_instance) > 0)
591 status = snprintf (buffer + offset, buffer_len - offset,
592 "%s-%s.rrd", ds->type, vl->type_instance);
593 else
594 status = snprintf (buffer + offset, buffer_len - offset,
595 "%s.rrd", ds->type);
596 if ((status < 1) || (status >= buffer_len - offset))
597 return (-1);
598 offset += status;
600 return (0);
601 } /* int value_list_to_filename */
603 static void *rrd_queue_thread (void *data)
604 {
605 while (42)
606 {
607 rrd_queue_t *queue_entry;
608 rrd_cache_t *cache_entry;
609 char **values;
610 int values_num;
611 int i;
613 /* XXX: If you need to lock both, cache_lock and queue_lock, at
614 * the same time, ALWAYS lock `cache_lock' first! */
616 /* wait until an entry is available */
617 pthread_mutex_lock (&queue_lock);
618 while ((queue_head == NULL) && (do_shutdown == 0))
619 pthread_cond_wait (&queue_cond, &queue_lock);
621 /* We're in the shutdown phase */
622 if (queue_head == NULL)
623 {
624 pthread_mutex_unlock (&queue_lock);
625 break;
626 }
628 /* Dequeue the first entry */
629 queue_entry = queue_head;
630 if (queue_head == queue_tail)
631 queue_head = queue_tail = NULL;
632 else
633 queue_head = queue_head->next;
635 /* Unlock the queue again */
636 pthread_mutex_unlock (&queue_lock);
638 /* We now need the cache lock so the entry isn't updated while
639 * we make a copy of it's values */
640 pthread_mutex_lock (&cache_lock);
642 avl_get (cache, queue_entry->filename, (void *) &cache_entry);
644 values = cache_entry->values;
645 values_num = cache_entry->values_num;
647 cache_entry->values = NULL;
648 cache_entry->values_num = 0;
649 cache_entry->flags = FLAG_NONE;
651 pthread_mutex_unlock (&cache_lock);
653 /* Write the values to the RRD-file */
654 srrd_update (queue_entry->filename, NULL, values_num, values);
655 DEBUG ("rrdtool plugin: queue thread: Wrote %i values to %s",
656 values_num, queue_entry->filename);
658 for (i = 0; i < values_num; i++)
659 {
660 sfree (values[i]);
661 }
662 sfree (values);
663 sfree (queue_entry->filename);
664 sfree (queue_entry);
665 } /* while (42) */
667 pthread_mutex_lock (&cache_lock);
668 avl_destroy (cache);
669 cache = NULL;
670 pthread_mutex_unlock (&cache_lock);
672 pthread_exit ((void *) 0);
673 return ((void *) 0);
674 } /* void *rrd_queue_thread */
676 static int rrd_queue_cache_entry (const char *filename)
677 {
678 rrd_queue_t *queue_entry;
680 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
681 if (queue_entry == NULL)
682 return (-1);
684 queue_entry->filename = strdup (filename);
685 if (queue_entry->filename == NULL)
686 {
687 free (queue_entry);
688 return (-1);
689 }
691 queue_entry->next = NULL;
693 pthread_mutex_lock (&queue_lock);
694 if (queue_tail == NULL)
695 queue_head = queue_entry;
696 else
697 queue_tail->next = queue_entry;
698 queue_tail = queue_entry;
699 pthread_cond_signal (&queue_cond);
700 pthread_mutex_unlock (&queue_lock);
702 DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
704 return (0);
705 } /* int rrd_queue_cache_entry */
707 static void rrd_cache_flush (int timeout)
708 {
709 rrd_cache_t *rc;
710 time_t now;
712 char **keys = NULL;
713 int keys_num = 0;
715 char *key;
716 avl_iterator_t *iter;
717 int i;
719 DEBUG ("rrdtool plugin: Flushing cache, timeout = %i", timeout);
721 now = time (NULL);
723 /* Build a list of entries to be flushed */
724 iter = avl_get_iterator (cache);
725 while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
726 {
727 if (rc->flags == FLAG_QUEUED)
728 continue;
729 else if ((now - rc->first_value) < timeout)
730 continue;
731 else if (rc->values_num > 0)
732 {
733 if (rrd_queue_cache_entry (key) == 0)
734 rc->flags = FLAG_QUEUED;
735 }
736 else /* ancient and no values -> waste of memory */
737 {
738 keys = (char **) realloc ((void *) keys,
739 (keys_num + 1) * sizeof (char *));
740 if (keys == NULL)
741 {
742 char errbuf[1024];
743 ERROR ("rrdtool plugin: "
744 "realloc failed: %s",
745 sstrerror (errno, errbuf,
746 sizeof (errbuf)));
747 avl_iterator_destroy (iter);
748 return;
749 }
750 keys[keys_num] = key;
751 keys_num++;
752 }
753 } /* while (avl_iterator_next) */
754 avl_iterator_destroy (iter);
756 for (i = 0; i < keys_num; i++)
757 {
758 if (avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
759 {
760 DEBUG ("rrdtool plugin: avl_remove (%s) failed.", keys[i]);
761 continue;
762 }
764 assert (rc->values == NULL);
765 assert (rc->values_num == 0);
767 sfree (rc);
768 sfree (key);
769 keys[i] = NULL;
770 } /* for (i = 0..keys_num) */
772 free (keys);
774 cache_flush_last = now;
775 } /* void rrd_cache_flush */
777 static int rrd_cache_insert (const char *filename,
778 const char *value, time_t value_time)
779 {
780 rrd_cache_t *rc = NULL;
781 int new_rc = 0;
782 char **values_new;
784 pthread_mutex_lock (&cache_lock);
786 avl_get (cache, filename, (void *) &rc);
788 if (rc == NULL)
789 {
790 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
791 if (rc == NULL)
792 return (-1);
793 rc->values_num = 0;
794 rc->values = NULL;
795 rc->first_value = 0;
796 rc->last_value = 0;
797 rc->flags = FLAG_NONE;
798 new_rc = 1;
799 }
801 if (rc->last_value >= value_time)
802 {
803 pthread_mutex_unlock (&cache_lock);
804 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
805 (unsigned int) rc->last_value,
806 (unsigned int) value_time);
807 return (-1);
808 }
810 values_new = (char **) realloc ((void *) rc->values,
811 (rc->values_num + 1) * sizeof (char *));
812 if (values_new == NULL)
813 {
814 char errbuf[1024];
815 void *cache_key = NULL;
817 sstrerror (errno, errbuf, sizeof (errbuf));
819 avl_remove (cache, filename, &cache_key, NULL);
820 pthread_mutex_unlock (&cache_lock);
822 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
824 sfree (cache_key);
825 sfree (rc->values);
826 sfree (rc);
827 return (-1);
828 }
829 rc->values = values_new;
831 rc->values[rc->values_num] = strdup (value);
832 if (rc->values[rc->values_num] != NULL)
833 rc->values_num++;
835 if (rc->values_num == 1)
836 rc->first_value = value_time;
837 rc->last_value = value_time;
839 /* Insert if this is the first value */
840 if (new_rc == 1)
841 {
842 void *cache_key = strdup (filename);
844 if (cache_key == NULL)
845 {
846 char errbuf[1024];
847 sstrerror (errno, errbuf, sizeof (errbuf));
849 pthread_mutex_unlock (&cache_lock);
851 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
853 sfree (rc->values[0]);
854 sfree (rc->values);
855 sfree (rc);
856 return (-1);
857 }
859 avl_insert (cache, cache_key, rc);
860 }
862 DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
863 "values_num = %i; age = %u;",
864 filename, rc->values_num,
865 rc->last_value - rc->first_value);
867 if ((rc->last_value - rc->first_value) >= cache_timeout)
868 {
869 /* XXX: If you need to lock both, cache_lock and queue_lock, at
870 * the same time, ALWAYS lock `cache_lock' first! */
871 if (rc->flags != FLAG_QUEUED)
872 {
873 if (rrd_queue_cache_entry (filename) == 0)
874 rc->flags = FLAG_QUEUED;
875 }
876 else
877 {
878 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
879 }
880 }
882 if ((cache_timeout > 0) &&
883 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
884 rrd_cache_flush (cache_flush_timeout);
887 pthread_mutex_unlock (&cache_lock);
889 return (0);
890 } /* int rrd_cache_insert */
892 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
893 {
894 struct stat statbuf;
895 char filename[512];
896 char values[512];
897 int status;
899 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
900 return (-1);
902 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
903 return (-1);
905 if (stat (filename, &statbuf) == -1)
906 {
907 if (errno == ENOENT)
908 {
909 if (rrd_create_file (filename, ds, vl))
910 return (-1);
911 }
912 else
913 {
914 char errbuf[1024];
915 ERROR ("stat(%s) failed: %s", filename,
916 sstrerror (errno, errbuf,
917 sizeof (errbuf)));
918 return (-1);
919 }
920 }
921 else if (!S_ISREG (statbuf.st_mode))
922 {
923 ERROR ("stat(%s): Not a regular file!",
924 filename);
925 return (-1);
926 }
928 status = rrd_cache_insert (filename, values, vl->time);
930 return (status);
931 } /* int rrd_write */
933 static int rrd_config (const char *key, const char *value)
934 {
935 if (strcasecmp ("CacheTimeout", key) == 0)
936 {
937 int tmp = atoi (value);
938 if (tmp < 0)
939 {
940 fprintf (stderr, "rrdtool: `CacheTimeout' must "
941 "be greater than 0.\n");
942 return (1);
943 }
944 cache_timeout = tmp;
945 }
946 else if (strcasecmp ("CacheFlush", key) == 0)
947 {
948 int tmp = atoi (value);
949 if (tmp < 0)
950 {
951 fprintf (stderr, "rrdtool: `CacheFlush' must "
952 "be greater than 0.\n");
953 return (1);
954 }
955 cache_flush_timeout = tmp;
956 }
957 else if (strcasecmp ("DataDir", key) == 0)
958 {
959 if (datadir != NULL)
960 free (datadir);
961 datadir = strdup (value);
962 if (datadir != NULL)
963 {
964 int len = strlen (datadir);
965 while ((len > 0) && (datadir[len - 1] == '/'))
966 {
967 len--;
968 datadir[len] = '\0';
969 }
970 if (len <= 0)
971 {
972 free (datadir);
973 datadir = NULL;
974 }
975 }
976 }
977 else if (strcasecmp ("StepSize", key) == 0)
978 {
979 stepsize = atoi (value);
980 if (stepsize < 0)
981 stepsize = 0;
982 }
983 else if (strcasecmp ("HeartBeat", key) == 0)
984 {
985 heartbeat = atoi (value);
986 if (heartbeat < 0)
987 heartbeat = 0;
988 }
989 else if (strcasecmp ("RRARows", key) == 0)
990 {
991 int tmp = atoi (value);
992 if (tmp <= 0)
993 {
994 fprintf (stderr, "rrdtool: `RRARows' must "
995 "be greater than 0.\n");
996 return (1);
997 }
998 rrarows = tmp;
999 }
1000 else if (strcasecmp ("RRATimespan", key) == 0)
1001 {
1002 char *saveptr = NULL;
1003 char *dummy;
1004 char *ptr;
1005 char *value_copy;
1006 int *tmp_alloc;
1008 value_copy = strdup (value);
1009 if (value_copy == NULL)
1010 return (1);
1012 dummy = value_copy;
1013 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1014 {
1015 dummy = NULL;
1017 tmp_alloc = realloc (rra_timespans_custom,
1018 sizeof (int) * (rra_timespans_custom_num + 1));
1019 if (tmp_alloc == NULL)
1020 {
1021 fprintf (stderr, "rrdtool: realloc failed.\n");
1022 free (value_copy);
1023 return (1);
1024 }
1025 rra_timespans_custom = tmp_alloc;
1026 rra_timespans_custom[rra_timespans_custom_num] = atoi (ptr);
1027 if (rra_timespans_custom[rra_timespans_custom_num] != 0)
1028 rra_timespans_custom_num++;
1029 } /* while (strtok_r) */
1030 free (value_copy);
1031 }
1032 else if (strcasecmp ("XFF", key) == 0)
1033 {
1034 double tmp = atof (value);
1035 if ((tmp < 0.0) || (tmp >= 1.0))
1036 {
1037 fprintf (stderr, "rrdtool: `XFF' must "
1038 "be in the range 0 to 1 (exclusive).");
1039 return (1);
1040 }
1041 xff = tmp;
1042 }
1043 else
1044 {
1045 return (-1);
1046 }
1047 return (0);
1048 } /* int rrd_config */
1050 static int rrd_shutdown (void)
1051 {
1052 pthread_mutex_lock (&cache_lock);
1053 rrd_cache_flush (-1);
1054 pthread_mutex_unlock (&cache_lock);
1056 pthread_mutex_lock (&queue_lock);
1057 do_shutdown = 1;
1058 pthread_cond_signal (&queue_cond);
1059 pthread_mutex_unlock (&queue_lock);
1061 /* Wait for all the values to be written to disk before returning. */
1062 if (queue_thread != 0)
1063 {
1064 pthread_join (queue_thread, NULL);
1065 queue_thread = 0;
1066 DEBUG ("rrdtool plugin: queue_thread exited.");
1067 }
1069 return (0);
1070 } /* int rrd_shutdown */
1072 static int rrd_init (void)
1073 {
1074 int status;
1076 if (stepsize < 0)
1077 stepsize = 0;
1078 if (heartbeat <= 0)
1079 {
1080 if (stepsize > 0)
1081 heartbeat = 2 * stepsize;
1082 else
1083 heartbeat = 0;
1084 }
1086 if ((heartbeat > 0) && (heartbeat < interval_g))
1087 WARNING ("rrdtool plugin: Your `heartbeat' is "
1088 "smaller than your `interval'. This will "
1089 "likely cause problems.");
1090 else if ((stepsize > 0) && (stepsize < interval_g))
1091 WARNING ("rrdtool plugin: Your `stepsize' is "
1092 "smaller than your `interval'. This will "
1093 "create needlessly big RRD-files.");
1095 /* Set the cache up */
1096 pthread_mutex_lock (&cache_lock);
1098 cache = avl_create ((int (*) (const void *, const void *)) strcmp);
1099 if (cache == NULL)
1100 {
1101 ERROR ("rrdtool plugin: avl_create failed.");
1102 return (-1);
1103 }
1105 cache_flush_last = time (NULL);
1106 if (cache_timeout < 2)
1107 {
1108 cache_timeout = 0;
1109 cache_flush_timeout = 0;
1110 }
1111 else if (cache_flush_timeout < cache_timeout)
1112 cache_flush_timeout = 10 * cache_timeout;
1114 pthread_mutex_unlock (&cache_lock);
1116 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
1117 if (status != 0)
1118 {
1119 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1120 return (-1);
1121 }
1123 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1124 " heartbeat = %i; rrarows = %i; xff = %lf;",
1125 (datadir == NULL) ? "(null)" : datadir,
1126 stepsize, heartbeat, rrarows, xff);
1128 return (0);
1129 } /* int rrd_init */
1131 void module_register (void)
1132 {
1133 plugin_register_config ("rrdtool", rrd_config,
1134 config_keys, config_keys_num);
1135 plugin_register_init ("rrdtool", rrd_init);
1136 plugin_register_write ("rrdtool", rrd_write);
1137 plugin_register_shutdown ("rrdtool", rrd_shutdown);
1138 }