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);
656 for (i = 0; i < values_num; i++)
657 {
658 sfree (values[i]);
659 }
660 sfree (values);
661 sfree (queue_entry->filename);
662 sfree (queue_entry);
663 } /* while (42) */
665 pthread_mutex_lock (&cache_lock);
666 avl_destroy (cache);
667 cache = NULL;
668 pthread_mutex_unlock (&cache_lock);
670 pthread_exit ((void *) 0);
671 return ((void *) 0);
672 } /* void *rrd_queue_thread */
674 static int rrd_queue_cache_entry (const char *filename)
675 {
676 rrd_queue_t *queue_entry;
678 queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
679 if (queue_entry == NULL)
680 return (-1);
682 queue_entry->filename = strdup (filename);
683 if (queue_entry->filename == NULL)
684 {
685 free (queue_entry);
686 return (-1);
687 }
689 queue_entry->next = NULL;
691 pthread_mutex_lock (&queue_lock);
692 if (queue_tail == NULL)
693 queue_head = queue_entry;
694 else
695 queue_tail->next = queue_entry;
696 queue_tail = queue_entry;
697 pthread_cond_signal (&queue_cond);
698 pthread_mutex_unlock (&queue_lock);
700 DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
702 return (0);
703 } /* int rrd_queue_cache_entry */
705 static void rrd_cache_flush (int timeout)
706 {
707 rrd_cache_t *rc;
708 time_t now;
710 char **keys = NULL;
711 int keys_num = 0;
713 char *key;
714 avl_iterator_t *iter;
715 int i;
717 DEBUG ("Flushing cache, timeout = %i", timeout);
719 now = time (NULL);
721 /* Build a list of entries to be flushed */
722 iter = avl_get_iterator (cache);
723 while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
724 {
725 DEBUG ("key = %s; age = %i;", key, now - rc->first_value);
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 ("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);
773 DEBUG ("Flushed %i value(s)", keys_num);
775 cache_flush_last = now;
776 } /* void rrd_cache_flush */
778 static int rrd_cache_insert (const char *filename,
779 const char *value, time_t value_time)
780 {
781 rrd_cache_t *rc = NULL;
782 int new_rc = 0;
783 char **values_new;
785 pthread_mutex_lock (&cache_lock);
787 avl_get (cache, filename, (void *) &rc);
789 if (rc == NULL)
790 {
791 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
792 if (rc == NULL)
793 return (-1);
794 rc->values_num = 0;
795 rc->values = NULL;
796 rc->first_value = 0;
797 rc->last_value = 0;
798 rc->flags = FLAG_NONE;
799 new_rc = 1;
800 }
802 if (rc->last_value >= value_time)
803 {
804 pthread_mutex_unlock (&cache_lock);
805 WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
806 (unsigned int) rc->last_value,
807 (unsigned int) value_time);
808 return (-1);
809 }
811 values_new = (char **) realloc ((void *) rc->values,
812 (rc->values_num + 1) * sizeof (char *));
813 if (values_new == NULL)
814 {
815 char errbuf[1024];
816 void *cache_key = NULL;
818 sstrerror (errno, errbuf, sizeof (errbuf));
820 avl_remove (cache, filename, &cache_key, NULL);
821 pthread_mutex_unlock (&cache_lock);
823 ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
825 sfree (cache_key);
826 sfree (rc->values);
827 sfree (rc);
828 return (-1);
829 }
830 rc->values = values_new;
832 rc->values[rc->values_num] = strdup (value);
833 if (rc->values[rc->values_num] != NULL)
834 rc->values_num++;
836 if (rc->values_num == 1)
837 rc->first_value = value_time;
838 rc->last_value = value_time;
840 /* Insert if this is the first value */
841 if (new_rc == 1)
842 {
843 void *cache_key = strdup (filename);
845 if (cache_key == NULL)
846 {
847 char errbuf[1024];
848 sstrerror (errno, errbuf, sizeof (errbuf));
850 pthread_mutex_unlock (&cache_lock);
852 ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
854 sfree (rc->values[0]);
855 sfree (rc->values);
856 sfree (rc);
857 return (-1);
858 }
860 avl_insert (cache, cache_key, rc);
861 }
863 DEBUG ("rrd_cache_insert (%s, %s, %u) = %p", filename, value,
864 (unsigned int) value_time, (void *) rc);
866 if ((rc->last_value - rc->first_value) >= cache_timeout)
867 {
868 /* XXX: If you need to lock both, cache_lock and queue_lock, at
869 * the same time, ALWAYS lock `cache_lock' first! */
870 if (rc->flags != FLAG_QUEUED)
871 {
872 if (rrd_queue_cache_entry (filename) == 0)
873 rc->flags = FLAG_QUEUED;
874 }
875 else
876 {
877 DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
878 }
879 }
881 if ((cache_timeout > 0) &&
882 ((time (NULL) - cache_flush_last) > cache_flush_timeout))
883 rrd_cache_flush (cache_flush_timeout);
886 pthread_mutex_unlock (&cache_lock);
888 return (0);
889 } /* int rrd_cache_insert */
891 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
892 {
893 struct stat statbuf;
894 char filename[512];
895 char values[512];
896 int status;
898 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
899 return (-1);
901 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
902 return (-1);
904 if (stat (filename, &statbuf) == -1)
905 {
906 if (errno == ENOENT)
907 {
908 if (rrd_create_file (filename, ds, vl))
909 return (-1);
910 }
911 else
912 {
913 char errbuf[1024];
914 ERROR ("stat(%s) failed: %s", filename,
915 sstrerror (errno, errbuf,
916 sizeof (errbuf)));
917 return (-1);
918 }
919 }
920 else if (!S_ISREG (statbuf.st_mode))
921 {
922 ERROR ("stat(%s): Not a regular file!",
923 filename);
924 return (-1);
925 }
927 status = rrd_cache_insert (filename, values, vl->time);
929 return (status);
930 } /* int rrd_write */
932 static int rrd_config (const char *key, const char *value)
933 {
934 if (strcasecmp ("CacheTimeout", key) == 0)
935 {
936 int tmp = atoi (value);
937 if (tmp < 0)
938 {
939 fprintf (stderr, "rrdtool: `CacheTimeout' must "
940 "be greater than 0.\n");
941 return (1);
942 }
943 cache_timeout = 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 return (1);
953 }
954 cache_flush_timeout = tmp;
955 }
956 else if (strcasecmp ("DataDir", key) == 0)
957 {
958 if (datadir != NULL)
959 free (datadir);
960 datadir = strdup (value);
961 if (datadir != NULL)
962 {
963 int len = strlen (datadir);
964 while ((len > 0) && (datadir[len - 1] == '/'))
965 {
966 len--;
967 datadir[len] = '\0';
968 }
969 if (len <= 0)
970 {
971 free (datadir);
972 datadir = NULL;
973 }
974 }
975 }
976 else if (strcasecmp ("StepSize", key) == 0)
977 {
978 stepsize = atoi (value);
979 if (stepsize < 0)
980 stepsize = 0;
981 }
982 else if (strcasecmp ("HeartBeat", key) == 0)
983 {
984 heartbeat = atoi (value);
985 if (heartbeat < 0)
986 heartbeat = 0;
987 }
988 else if (strcasecmp ("RRARows", key) == 0)
989 {
990 int tmp = atoi (value);
991 if (tmp <= 0)
992 {
993 fprintf (stderr, "rrdtool: `RRARows' must "
994 "be greater than 0.\n");
995 return (1);
996 }
997 rrarows = tmp;
998 }
999 else if (strcasecmp ("RRATimespan", key) == 0)
1000 {
1001 char *saveptr = NULL;
1002 char *dummy;
1003 char *ptr;
1004 char *value_copy;
1005 int *tmp_alloc;
1007 value_copy = strdup (value);
1008 if (value_copy == NULL)
1009 return (1);
1011 dummy = value_copy;
1012 while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL)
1013 {
1014 dummy = NULL;
1016 tmp_alloc = realloc (rra_timespans_custom,
1017 sizeof (int) * (rra_timespans_custom_num + 1));
1018 if (tmp_alloc == NULL)
1019 {
1020 fprintf (stderr, "rrdtool: realloc failed.\n");
1021 free (value_copy);
1022 return (1);
1023 }
1024 rra_timespans_custom = tmp_alloc;
1025 rra_timespans_custom[rra_timespans_custom_num] = atoi (ptr);
1026 if (rra_timespans_custom[rra_timespans_custom_num] != 0)
1027 rra_timespans_custom_num++;
1028 } /* while (strtok_r) */
1029 free (value_copy);
1030 }
1031 else if (strcasecmp ("XFF", key) == 0)
1032 {
1033 double tmp = atof (value);
1034 if ((tmp < 0.0) || (tmp >= 1.0))
1035 {
1036 fprintf (stderr, "rrdtool: `XFF' must "
1037 "be in the range 0 to 1 (exclusive).");
1038 return (1);
1039 }
1040 xff = tmp;
1041 }
1042 else
1043 {
1044 return (-1);
1045 }
1046 return (0);
1047 } /* int rrd_config */
1049 static int rrd_shutdown (void)
1050 {
1051 pthread_mutex_lock (&cache_lock);
1052 rrd_cache_flush (-1);
1053 pthread_mutex_unlock (&cache_lock);
1055 /* Wait for all the values to be written to disk before returning. */
1056 if (queue_thread != 0)
1057 {
1058 pthread_join (queue_thread, NULL);
1059 queue_thread = 0;
1060 DEBUG ("rrdtool plugin: queue_thread exited.");
1061 }
1063 pthread_mutex_lock (&queue_lock);
1064 do_shutdown = 1;
1065 pthread_cond_signal (&queue_cond);
1066 pthread_mutex_unlock (&queue_lock);
1068 return (0);
1069 } /* int rrd_shutdown */
1071 static int rrd_init (void)
1072 {
1073 int status;
1075 if (stepsize < 0)
1076 stepsize = 0;
1077 if (heartbeat <= 0)
1078 {
1079 if (stepsize > 0)
1080 heartbeat = 2 * stepsize;
1081 else
1082 heartbeat = 0;
1083 }
1085 if ((heartbeat > 0) && (heartbeat < interval_g))
1086 WARNING ("rrdtool plugin: Your `heartbeat' is "
1087 "smaller than your `interval'. This will "
1088 "likely cause problems.");
1089 else if ((stepsize > 0) && (stepsize < interval_g))
1090 WARNING ("rrdtool plugin: Your `stepsize' is "
1091 "smaller than your `interval'. This will "
1092 "create needlessly big RRD-files.");
1094 /* Set the cache up */
1095 pthread_mutex_lock (&cache_lock);
1097 cache = avl_create ((int (*) (const void *, const void *)) strcmp);
1098 if (cache == NULL)
1099 {
1100 ERROR ("rrdtool plugin: avl_create failed.");
1101 return (-1);
1102 }
1104 cache_flush_last = time (NULL);
1105 if (cache_timeout < 2)
1106 {
1107 cache_timeout = 0;
1108 cache_flush_timeout = 0;
1109 }
1110 else if (cache_flush_timeout < cache_timeout)
1111 cache_flush_timeout = 10 * cache_timeout;
1113 pthread_mutex_unlock (&cache_lock);
1115 status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
1116 if (status != 0)
1117 {
1118 ERROR ("rrdtool plugin: Cannot create queue-thread.");
1119 return (-1);
1120 }
1122 DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
1123 " heartbeat = %i; rrarows = %i; xff = %lf;",
1124 (datadir == NULL) ? "(null)" : datadir,
1125 stepsize, heartbeat, rrarows, xff);
1127 return (0);
1128 } /* int rrd_init */
1130 void module_register (void)
1131 {
1132 plugin_register_config ("rrdtool", rrd_config,
1133 config_keys, config_keys_num);
1134 plugin_register_init ("rrdtool", rrd_init);
1135 plugin_register_write ("rrdtool", rrd_write);
1136 plugin_register_shutdown ("rrdtool", rrd_shutdown);
1137 }