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_llist.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[] =
81 static int config_keys_num = 1;
83 static int cache_timeout = 0;
84 static time_t cache_flush;
85 static llist_t *cache = NULL;
90 static int rra_get (char ***ret)
92 static char **rra_def = NULL;
93 static int rra_num = 0;
95 int rra_max = rra_timespans_num * rra_types_num;
107 if ((rra_num != 0) && (rra_def != NULL))
113 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
115 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
117 step = atoi (COLLECTD_STEP);
118 rows = atoi (COLLECTD_ROWS);
120 if ((step <= 0) || (rows <= 0))
127 for (i = 0; i < rra_timespans_num; i++)
129 span = rra_timespans[i];
131 if ((span / step) < rows)
137 cdp_len = (int) floor (((double) span) / ((double) (rows * step)));
139 cdp_num = (int) ceil (((double) span) / ((double) (cdp_len * step)));
141 for (j = 0; j < rra_types_num; j++)
143 if (rra_num >= rra_max)
146 if (snprintf (buffer, sizeof(buffer), "RRA:%s:%3.1f:%u:%u",
147 rra_types[j], COLLECTD_XFF,
148 cdp_len, cdp_num) >= sizeof (buffer))
150 syslog (LOG_ERR, "rra_get: Buffer would have been truncated.");
154 rra_def[rra_num++] = sstrdup (buffer);
159 DBG ("rra_num = %i", rra_num);
160 for (i = 0; i < rra_num; i++)
161 DBG (" %s", rra_def[i]);
168 static void ds_free (int ds_num, char **ds_def)
172 for (i = 0; i < ds_num; i++)
173 if (ds_def[i] != NULL)
178 static int ds_get (char ***ret, const data_set_t *ds)
187 DBG ("ds->ds_num = %i", ds->ds_num);
189 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
192 syslog (LOG_ERR, "rrdtool plugin: malloc failed: %s",
196 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
198 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
200 data_source_t *d = ds->ds + ds_num;
204 ds_def[ds_num] = NULL;
206 if (d->type == DS_TYPE_COUNTER)
208 else if (d->type == DS_TYPE_GAUGE)
212 syslog (LOG_ERR, "rrdtool plugin: Unknown DS type: %i",
223 snprintf (min, sizeof (min), "%lf", d->min);
224 min[sizeof (min) - 1] = '\0';
233 snprintf (max, sizeof (max), "%lf", d->max);
234 max[sizeof (max) - 1] = '\0';
237 status = snprintf (buffer, sizeof (buffer),
239 d->name, type, COLLECTD_HEARTBEAT,
241 if ((status < 1) || (status >= sizeof (buffer)))
244 ds_def[ds_num] = sstrdup (buffer);
245 } /* for ds_num = 0 .. ds->ds_num */
250 DBG ("ds_num = %i", ds_num);
251 for (i = 0; i < ds_num; i++)
252 DBG (" %s", ds_def[i]);
256 if (ds_num != ds->ds_num)
258 ds_free (ds_num, ds_def);
266 static int rrd_create_file (char *filename, const data_set_t *ds)
277 if (check_create_dir (filename))
280 if ((rra_num = rra_get (&rra_def)) < 1)
282 syslog (LOG_ERR, "rrd_create_file failed: Could not calculate RRAs");
286 if ((ds_num = ds_get (&ds_def, ds)) < 1)
288 syslog (LOG_ERR, "rrd_create_file failed: Could not calculate DSes");
292 argc = ds_num + rra_num + 4;
294 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
296 syslog (LOG_ERR, "rrd_create failed: %s", strerror (errno));
303 argv[3] = COLLECTD_STEP;
306 for (i = 0; i < ds_num; i++)
307 argv[j++] = ds_def[i];
308 for (i = 0; i < rra_num; i++)
309 argv[j++] = rra_def[i];
312 optind = 0; /* bug in librrd? */
314 if (rrd_create (argc, argv) == -1)
316 syslog (LOG_ERR, "rrd_create failed: %s: %s", filename, rrd_get_error ());
321 ds_free (ds_num, ds_def);
326 static int value_list_to_string (char *buffer, int buffer_len,
327 const data_set_t *ds, const value_list_t *vl)
333 memset (buffer, '\0', sizeof (buffer_len));
335 status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
336 if ((status < 1) || (status >= buffer_len))
340 for (i = 0; i < ds->ds_num; i++)
342 if ((ds->ds[i].type != DS_TYPE_COUNTER)
343 && (ds->ds[i].type != DS_TYPE_GAUGE))
346 if (ds->ds[i].type == DS_TYPE_COUNTER)
347 status = snprintf (buffer + offset, buffer_len - offset,
348 ":%llu", vl->values[i].counter);
350 status = snprintf (buffer + offset, buffer_len - offset,
351 ":%lf", vl->values[i].gauge);
353 if ((status < 1) || (status >= (buffer_len - offset)))
357 } /* for ds->ds_num */
360 } /* int value_list_to_string */
362 static int value_list_to_filename (char *buffer, int buffer_len,
363 const data_set_t *ds, const value_list_t *vl)
368 status = snprintf (buffer + offset, buffer_len - offset,
370 if ((status < 1) || (status >= buffer_len - offset))
374 if (strlen (vl->plugin_instance) > 0)
375 status = snprintf (buffer + offset, buffer_len - offset,
376 "%s-%s/", vl->plugin, vl->plugin_instance);
378 status = snprintf (buffer + offset, buffer_len - offset,
380 if ((status < 1) || (status >= buffer_len - offset))
384 if (strlen (vl->type_instance) > 0)
385 status = snprintf (buffer + offset, buffer_len - offset,
386 "%s-%s.rrd", ds->type, vl->type_instance);
388 status = snprintf (buffer + offset, buffer_len - offset,
390 if ((status < 1) || (status >= buffer_len - offset))
395 } /* int value_list_to_filename */
397 static rrd_cache_t *rrd_cache_insert (const char *filename,
400 rrd_cache_t *rc = NULL;
401 llentry_t *le = NULL;
405 le = llist_search (cache, filename);
407 rc = (rrd_cache_t *) le->value;
412 rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
420 rc->values = (char **) realloc ((void *) rc->values,
421 (rc->values_num + 1) * sizeof (char *));
422 if (rc->values == NULL)
424 syslog (LOG_ERR, "rrdtool plugin: realloc failed: %s",
429 llist_remove (cache, le);
430 llentry_destroy (le);
435 rc->values[rc->values_num] = strdup (value);
436 if (rc->values[rc->values_num] != NULL)
439 if (rc->values_num == 1)
440 rc->first_value = time (NULL);
442 if ((cache != NULL) && (le == NULL))
444 le = llentry_create (filename, (void *) rc);
446 llist_prepend (cache, le);
449 DBG ("rrd_cache_insert (%s, %s) = %p", filename, value, (void *) rc);
452 } /* rrd_cache_t *rrd_cache_insert */
454 static int rrd_write_cache_entry (const char *filename, rrd_cache_t *rc)
462 argc = rc->values_num + 2;
463 argv = (char **) malloc ((argc + 1) * sizeof (char *));
467 fn = strdup (filename);
476 memcpy (argv + 2, rc->values, rc->values_num * sizeof (char *));
479 DBG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
481 optind = 0; /* bug in librrd? */
483 status = rrd_update (argc, argv);
494 syslog (LOG_WARNING, "rrd_update failed: %s: %s",
495 filename, rrd_get_error ());
500 } /* int rrd_update_file */
502 static void rrd_cache_flush (int timeout)
511 DBG ("Flushing cache, timeout = %i", timeout);
515 /* Remove empty entries */
516 le = llist_head (cache);
519 llentry_t *next = le->next;
520 rc = (rrd_cache_t *) le->value;
521 if (rc->values_num == 0)
523 DBG ("Removing cache entry for `%s'", le->key);
526 llist_remove (cache, le);
531 /* Write timed out entries */
532 le = llist_head (cache);
535 rc = (rrd_cache_t *) le->value;
536 if ((now - rc->first_value) >= timeout)
537 rrd_write_cache_entry (le->key, rc);
543 } /* void rrd_cache_flush */
545 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
553 if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
556 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
559 if (stat (filename, &statbuf) == -1)
563 if (rrd_create_file (filename, ds))
568 syslog (LOG_ERR, "stat(%s) failed: %s",
569 filename, strerror (errno));
573 else if (!S_ISREG (statbuf.st_mode))
575 syslog (LOG_ERR, "stat(%s): Not a regular file!",
580 rc = rrd_cache_insert (filename, values);
586 rrd_write_cache_entry (filename, rc);
594 DBG ("age (%s) = %i", filename, now - rc->first_value);
596 if ((now - rc->first_value) >= cache_timeout)
597 rrd_write_cache_entry (filename, rc);
599 if ((time (NULL) - cache_flush) >= cache_timeout)
601 rrd_cache_flush (cache_timeout);
605 } /* int rrd_dispatch */
607 static int rrd_config (const char *key, const char *val)
609 if (strcasecmp ("CacheTimeout", key) == 0)
611 int tmp = atoi (val);
614 fprintf (stderr, "rrdtool: `CacheTimeout' must "
615 "be greater than 0.\n");
625 } /* int rrd_config */
627 static int rrd_shutdown (void)
629 rrd_cache_flush (-1);
632 } /* int rrd_shutdown */
634 static int rrd_init (void)
636 if (cache_timeout < 2)
642 cache = llist_create ();
643 cache_flush = time (NULL);
644 plugin_register_shutdown ("rrdtool", rrd_shutdown);
649 void module_register (void)
651 plugin_register_config ("rrdtool", rrd_config,
652 config_keys, config_keys_num);
653 plugin_register_init ("rrdtool", rrd_init);
654 plugin_register_write ("rrdtool", rrd_write);