2 * collectd - src/rrdtool.c
3 * Copyright (C) 2006 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_avltree.h"
26 #include "utils_debug.h"
29 * This weird macro cascade forces the glibc to define `NAN'. I don't know
30 * another way to solve this, so more intelligent solutions are welcome. -octo
33 # define DISABLE__USE_ISOC99 1
34 # define __USE_ISOC99 1
37 #ifdef DISABLE__USE_ISOC99
38 # undef DISABLE__USE_ISOC99
51 typedef struct rrd_cache_s rrd_cache_t;
56 static int rra_timespans[] =
65 static int rra_timespans_num = 5;
67 static char *rra_types[] =
74 static int rra_types_num = 3;
76 static const char *config_keys[] =
83 static int config_keys_num = 3;
85 static char *datadir = NULL;
87 static int cache_timeout = 0;
88 static int cache_flush_timeout = 0;
89 static time_t cache_flush_last;
90 static avl_tree_t *cache = NULL;
95 static int rra_get (char ***ret)
97 static char **rra_def = NULL;
98 static int rra_num = 0;
100 int rra_max = rra_timespans_num * rra_types_num;
112 if ((rra_num != 0) && (rra_def != NULL))
118 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
120 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
122 step = atoi (COLLECTD_STEP);
123 rows = atoi (COLLECTD_ROWS);
125 if ((step <= 0) || (rows <= 0))
132 for (i = 0; i < rra_timespans_num; i++)
134 span = rra_timespans[i];
136 if ((span / step) < rows)
142 cdp_len = (int) floor (((double) span) / ((double) (rows * step)));
144 cdp_num = (int) ceil (((double) span) / ((double) (cdp_len * step)));
146 for (j = 0; j < rra_types_num; j++)
148 if (rra_num >= rra_max)
151 if (snprintf (buffer, sizeof(buffer), "RRA:%s:%3.1f:%u:%u",
152 rra_types[j], COLLECTD_XFF,
153 cdp_len, cdp_num) >= sizeof (buffer))
155 syslog (LOG_ERR, "rra_get: Buffer would have been truncated.");
159 rra_def[rra_num++] = sstrdup (buffer);
164 DBG ("rra_num = %i", rra_num);
165 for (i = 0; i < rra_num; i++)
166 DBG (" %s", rra_def[i]);
173 static void ds_free (int ds_num, char **ds_def)
177 for (i = 0; i < ds_num; i++)
178 if (ds_def[i] != NULL)
183 static int ds_get (char ***ret, const data_set_t *ds)
192 DBG ("ds->ds_num = %i", ds->ds_num);
194 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
197 syslog (LOG_ERR, "rrdtool plugin: malloc failed: %s",
201 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
203 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
205 data_source_t *d = ds->ds + ds_num;
209 ds_def[ds_num] = NULL;
211 if (d->type == DS_TYPE_COUNTER)
213 else if (d->type == DS_TYPE_GAUGE)
217 syslog (LOG_ERR, "rrdtool plugin: Unknown DS type: %i",
228 snprintf (min, sizeof (min), "%lf", d->min);
229 min[sizeof (min) - 1] = '\0';
238 snprintf (max, sizeof (max), "%lf", d->max);
239 max[sizeof (max) - 1] = '\0';
242 status = snprintf (buffer, sizeof (buffer),
244 d->name, type, COLLECTD_HEARTBEAT,
246 if ((status < 1) || (status >= sizeof (buffer)))
249 ds_def[ds_num] = sstrdup (buffer);
250 } /* for ds_num = 0 .. ds->ds_num */
255 DBG ("ds_num = %i", ds_num);
256 for (i = 0; i < ds_num; i++)
257 DBG (" %s", ds_def[i]);
261 if (ds_num != ds->ds_num)
263 ds_free (ds_num, ds_def);
271 static int rrd_create_file (char *filename, const data_set_t *ds)
282 if (check_create_dir (filename))
285 if ((rra_num = rra_get (&rra_def)) < 1)
287 syslog (LOG_ERR, "rrd_create_file failed: Could not calculate RRAs");
291 if ((ds_num = ds_get (&ds_def, ds)) < 1)
293 syslog (LOG_ERR, "rrd_create_file failed: Could not calculate DSes");
297 argc = ds_num + rra_num + 4;
299 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
301 syslog (LOG_ERR, "rrd_create failed: %s", strerror (errno));
308 argv[3] = COLLECTD_STEP;
311 for (i = 0; i < ds_num; i++)
312 argv[j++] = ds_def[i];
313 for (i = 0; i < rra_num; i++)
314 argv[j++] = rra_def[i];
317 optind = 0; /* bug in librrd? */
319 if (rrd_create (argc, argv) == -1)
321 syslog (LOG_ERR, "rrd_create failed: %s: %s", filename, rrd_get_error ());
326 ds_free (ds_num, ds_def);
331 static int value_list_to_string (char *buffer, int buffer_len,
332 const data_set_t *ds, const value_list_t *vl)
338 memset (buffer, '\0', sizeof (buffer_len));
340 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
341 if ((status < 1) || (status >= buffer_len))
345 for (i = 0; i < ds->ds_num; i++)
347 if ((ds->ds[i].type != DS_TYPE_COUNTER)
348 && (ds->ds[i].type != DS_TYPE_GAUGE))
351 if (ds->ds[i].type == DS_TYPE_COUNTER)
352 status = snprintf (buffer + offset, buffer_len - offset,
353 ":%llu", vl->values[i].counter);
355 status = snprintf (buffer + offset, buffer_len - offset,
356 ":%lf", vl->values[i].gauge);
358 if ((status < 1) || (status >= (buffer_len - offset)))
362 } /* for ds->ds_num */
365 } /* int value_list_to_string */
367 static int value_list_to_filename (char *buffer, int buffer_len,
368 const data_set_t *ds, const value_list_t *vl)
375 status = snprintf (buffer + offset, buffer_len - offset,
377 if ((status < 1) || (status >= buffer_len - offset))
382 status = snprintf (buffer + offset, buffer_len - offset,
384 if ((status < 1) || (status >= buffer_len - offset))
388 if (strlen (vl->plugin_instance) > 0)
389 status = snprintf (buffer + offset, buffer_len - offset,
390 "%s-%s/", vl->plugin, vl->plugin_instance);
392 status = snprintf (buffer + offset, buffer_len - offset,
394 if ((status < 1) || (status >= buffer_len - offset))
398 if (strlen (vl->type_instance) > 0)
399 status = snprintf (buffer + offset, buffer_len - offset,
400 "%s-%s.rrd", ds->type, vl->type_instance);
402 status = snprintf (buffer + offset, buffer_len - offset,
404 if ((status < 1) || (status >= buffer_len - offset))
409 } /* int value_list_to_filename */
411 static rrd_cache_t *rrd_cache_insert (const char *filename,
414 rrd_cache_t *rc = NULL;
418 avl_get (cache, filename, (void *) &rc);
422 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
431 rc->values = (char **) realloc ((void *) rc->values,
432 (rc->values_num + 1) * sizeof (char *));
433 if (rc->values == NULL)
435 syslog (LOG_ERR, "rrdtool plugin: realloc failed: %s",
439 void *cache_key = NULL;
440 avl_remove (cache, filename, &cache_key, NULL);
447 rc->values[rc->values_num] = strdup (value);
448 if (rc->values[rc->values_num] != NULL)
451 if (rc->values_num == 1)
452 rc->first_value = time (NULL);
454 /* Insert if this is the first value */
455 if ((cache != NULL) && (new_rc == 1))
457 void *cache_key = strdup (filename);
459 if (cache_key == NULL)
461 syslog (LOG_ERR, "rrdtool plugin: strdup failed: %s",
463 sfree (rc->values[0]);
469 avl_insert (cache, cache_key, rc);
472 DBG ("rrd_cache_insert (%s, %s) = %p", filename, value, (void *) rc);
475 } /* rrd_cache_t *rrd_cache_insert */
477 static int rrd_write_cache_entry (const char *filename, rrd_cache_t *rc)
487 argc = rc->values_num + 2;
488 argv = (char **) malloc ((argc + 1) * sizeof (char *));
492 fn = strdup (filename);
501 memcpy (argv + 2, rc->values, rc->values_num * sizeof (char *));
504 DBG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
506 optind = 0; /* bug in librrd? */
508 status = rrd_update (argc, argv);
513 /* Free the value list of `rc' */
514 for (i = 0; i < rc->values_num; i++)
515 free (rc->values[i]);
522 syslog (LOG_WARNING, "rrd_update failed: %s: %s",
523 filename, rrd_get_error ());
528 } /* int rrd_write_cache_entry */
530 static void rrd_cache_flush (int timeout)
539 avl_iterator_t *iter;
545 DBG ("Flushing cache, timeout = %i", timeout);
549 /* Build a list of entries to be flushed */
550 iter = avl_get_iterator (cache);
551 while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
553 DBG ("key = %s; age = %i;", key, now - rc->first_value);
554 if ((now - rc->first_value) >= timeout)
556 keys = (char **) realloc ((void *) keys,
557 (keys_num + 1) * sizeof (char *));
560 DBG ("realloc failed: %s", strerror (errno));
561 syslog (LOG_ERR, "rrdtool plugin: "
562 "realloc failed: %s",
564 avl_iterator_destroy (iter);
567 keys[keys_num] = key;
570 } /* while (avl_iterator_next) */
571 avl_iterator_destroy (iter);
573 for (i = 0; i < keys_num; i++)
575 if (avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0)
577 DBG ("avl_remove (%s) failed.", keys[i]);
581 rrd_write_cache_entry (keys[i], rc);
582 /* rc's value-list is free's by `rrd_write_cache_entry' */
586 } /* for (i = 0..keys_num) */
589 DBG ("Flushed %i value(s)", keys_num);
591 cache_flush_last = now;
592 } /* void rrd_cache_flush */
594 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
602 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
605 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
608 if (stat (filename, &statbuf) == -1)
612 if (rrd_create_file (filename, ds))
617 syslog (LOG_ERR, "stat(%s) failed: %s",
618 filename, strerror (errno));
622 else if (!S_ISREG (statbuf.st_mode))
624 syslog (LOG_ERR, "stat(%s): Not a regular file!",
629 rc = rrd_cache_insert (filename, values);
635 rrd_write_cache_entry (filename, rc);
636 /* rc's value-list is free's by `rrd_write_cache_entry' */
643 DBG ("age (%s) = %i", filename, now - rc->first_value);
645 /* `rc' is not free'd here, because we'll likely reuse it. If not, then
646 * the next flush will remove this entry. */
647 if ((now - rc->first_value) >= cache_timeout)
648 rrd_write_cache_entry (filename, rc);
650 if ((now - cache_flush_last) >= cache_flush_timeout)
651 rrd_cache_flush (cache_flush_timeout);
654 } /* int rrd_write */
656 static int rrd_config (const char *key, const char *val)
658 if (strcasecmp ("CacheTimeout", key) == 0)
660 int tmp = atoi (val);
663 fprintf (stderr, "rrdtool: `CacheTimeout' must "
664 "be greater than 0.\n");
669 else if (strcasecmp ("CacheFlush", key) == 0)
671 int tmp = atoi (val);
674 fprintf (stderr, "rrdtool: `CacheFlush' must "
675 "be greater than 0.\n");
678 cache_flush_timeout = tmp;
680 else if (strcasecmp ("DataDir", key) == 0)
684 datadir = strdup (val);
687 int len = strlen (datadir);
688 while ((len > 0) && (datadir[len - 1] == '/'))
705 } /* int rrd_config */
707 static int rrd_shutdown (void)
709 rrd_cache_flush (-1);
715 } /* int rrd_shutdown */
717 static int rrd_init (void)
719 if (cache_timeout < 2)
722 cache_flush_timeout = 0;
726 if (cache_flush_timeout < cache_timeout)
727 cache_flush_timeout = 10 * cache_timeout;
729 cache = avl_create ((int (*) (const void *, const void *)) strcmp);
730 cache_flush_last = time (NULL);
731 plugin_register_shutdown ("rrdtool", rrd_shutdown);
736 void module_register (void)
738 plugin_register_config ("rrdtool", rrd_config,
739 config_keys, config_keys_num);
740 plugin_register_init ("rrdtool", rrd_init);
741 plugin_register_write ("rrdtool", rrd_write);