2 * collectd - src/utils_rrdcreate.c
3 * Copyright (C) 2006-2013 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 collectd.org>
24 #include "utils_rrdcreate.h"
29 struct srrd_create_args_s
32 unsigned long pdp_step;
37 typedef struct srrd_create_args_s srrd_create_args_t;
39 struct async_create_file_s;
40 typedef struct async_create_file_s async_create_file_t;
41 struct async_create_file_s
44 async_create_file_t *next;
50 static int rra_timespans[] =
58 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
60 static char *rra_types[] =
66 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
68 #if !defined(HAVE_THREADSAFE_LIBRRD) || !HAVE_THREADSAFE_LIBRRD
69 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
72 static async_create_file_t *async_creation_list = NULL;
73 static pthread_mutex_t async_creation_lock = PTHREAD_MUTEX_INITIALIZER;
78 static void rra_free (int rra_num, char **rra_def) /* {{{ */
82 for (i = 0; i < rra_num; i++)
87 } /* }}} void rra_free */
89 static void srrd_create_args_destroy (srrd_create_args_t *args)
94 sfree (args->filename);
95 if (args->argv != NULL)
98 for (i = 0; i < args->argc; i++)
99 sfree (args->argv[i]);
103 } /* void srrd_create_args_destroy */
105 static srrd_create_args_t *srrd_create_args_create (const char *filename,
106 unsigned long pdp_step, time_t last_up,
107 int argc, const char **argv)
109 srrd_create_args_t *args;
111 args = malloc (sizeof (*args));
114 ERROR ("srrd_create_args_create: malloc failed.");
117 memset (args, 0, sizeof (*args));
118 args->filename = NULL;
119 args->pdp_step = pdp_step;
120 args->last_up = last_up;
123 args->filename = strdup (filename);
124 if (args->filename == NULL)
126 ERROR ("srrd_create_args_create: strdup failed.");
127 srrd_create_args_destroy (args);
131 args->argv = calloc ((size_t) (argc + 1), sizeof (*args->argv));
132 if (args->argv == NULL)
134 ERROR ("srrd_create_args_create: calloc failed.");
135 srrd_create_args_destroy (args);
139 for (args->argc = 0; args->argc < argc; args->argc++)
141 args->argv[args->argc] = strdup (argv[args->argc]);
142 if (args->argv[args->argc] == NULL)
144 ERROR ("srrd_create_args_create: strdup failed.");
145 srrd_create_args_destroy (args);
149 assert (args->argc == argc);
150 args->argv[args->argc] = NULL;
153 } /* srrd_create_args_t *srrd_create_args_create */
158 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
159 const rrdcreate_config_t *cfg)
177 /* The stepsize we use here: If it is user-set, use it. If not, use the
178 * interval of the value-list. */
181 if (cfg->rrarows <= 0)
187 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
193 if (cfg->stepsize > 0)
196 ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
203 /* Use the configured timespans or fall back to the built-in defaults */
204 if (cfg->timespans_num != 0)
206 rts = cfg->timespans;
207 rts_num = cfg->timespans_num;
212 rts_num = rra_timespans_num;
215 rra_max = rts_num * rra_types_num;
217 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
219 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
223 for (i = 0; i < rts_num; i++)
227 if ((span / ss) < cfg->rrarows)
228 span = ss * cfg->rrarows;
233 cdp_len = (int) floor (((double) span)
234 / ((double) (cfg->rrarows * ss)));
236 cdp_num = (int) ceil (((double) span)
237 / ((double) (cdp_len * ss)));
239 for (j = 0; j < rra_types_num; j++)
243 if (rra_num >= rra_max)
246 status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
247 rra_types[j], cfg->xff, cdp_len, cdp_num);
249 if ((status < 0) || ((size_t) status >= sizeof (buffer)))
251 ERROR ("rra_get: Buffer would have been truncated.");
255 rra_def[rra_num++] = sstrdup (buffer);
261 } /* }}} int rra_get */
263 static void ds_free (int ds_num, char **ds_def) /* {{{ */
267 for (i = 0; i < ds_num; i++)
268 if (ds_def[i] != NULL)
271 } /* }}} void ds_free */
273 static int ds_get (char ***ret, /* {{{ */
274 const data_set_t *ds, const value_list_t *vl,
275 const rrdcreate_config_t *cfg)
284 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
288 ERROR ("rrdtool plugin: malloc failed: %s",
289 sstrerror (errno, errbuf, sizeof (errbuf)));
292 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
294 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
296 data_source_t *d = ds->ds + ds_num;
300 ds_def[ds_num] = NULL;
302 if (d->type == DS_TYPE_COUNTER)
304 else if (d->type == DS_TYPE_GAUGE)
306 else if (d->type == DS_TYPE_DERIVE)
308 else if (d->type == DS_TYPE_ABSOLUTE)
312 ERROR ("rrdtool plugin: Unknown DS type: %i",
319 sstrncpy (min, "U", sizeof (min));
322 ssnprintf (min, sizeof (min), "%f", d->min);
326 sstrncpy (max, "U", sizeof (max));
329 ssnprintf (max, sizeof (max), "%f", d->max);
331 status = ssnprintf (buffer, sizeof (buffer),
336 : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
338 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
341 ds_def[ds_num] = sstrdup (buffer);
342 } /* for ds_num = 0 .. ds->ds_num */
344 if (ds_num != ds->ds_num)
346 ds_free (ds_num, ds_def);
352 } /* }}} int ds_get */
354 #if HAVE_THREADSAFE_LIBRRD
355 static int srrd_create (const char *filename, /* {{{ */
356 unsigned long pdp_step, time_t last_up,
357 int argc, const char **argv)
362 if ((filename == NULL) || (argv == NULL))
365 /* Some versions of librrd don't have the `const' qualifier for the first
366 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
367 * but what else can we do? :( -octo */
368 filename_copy = strdup (filename);
369 if (filename_copy == NULL)
371 ERROR ("srrd_create: strdup failed.");
375 optind = 0; /* bug in librrd? */
378 status = rrd_create_r (filename_copy, pdp_step, last_up,
379 argc, (void *) argv);
383 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
384 filename, rrd_get_error ());
387 sfree (filename_copy);
390 } /* }}} int srrd_create */
391 /* #endif HAVE_THREADSAFE_LIBRRD */
393 #else /* !HAVE_THREADSAFE_LIBRRD */
394 static int srrd_create (const char *filename, /* {{{ */
395 unsigned long pdp_step, time_t last_up,
396 int argc, const char **argv)
403 char pdp_step_str[16];
404 char last_up_str[16];
407 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
408 if (new_argv == NULL)
410 ERROR ("rrdtool plugin: malloc failed.");
415 last_up = time (NULL) - 10;
417 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
418 ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
420 new_argv[0] = "create";
421 new_argv[1] = (void *) filename;
423 new_argv[3] = pdp_step_str;
425 new_argv[5] = last_up_str;
427 memcpy (new_argv + 6, argv, argc * sizeof (char *));
428 new_argv[new_argc] = NULL;
430 pthread_mutex_lock (&librrd_lock);
431 optind = 0; /* bug in librrd? */
434 status = rrd_create (new_argc, new_argv);
435 pthread_mutex_unlock (&librrd_lock);
439 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
440 filename, rrd_get_error ());
446 } /* }}} int srrd_create */
447 #endif /* !HAVE_THREADSAFE_LIBRRD */
449 static int lock_file (char const *filename) /* {{{ */
451 async_create_file_t *ptr;
455 pthread_mutex_lock (&async_creation_lock);
457 for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
458 if (strcmp (filename, ptr->filename) == 0)
463 pthread_mutex_unlock (&async_creation_lock);
467 status = stat (filename, &sb);
468 if ((status == 0) || (errno != ENOENT))
470 pthread_mutex_unlock (&async_creation_lock);
474 ptr = malloc (sizeof (*ptr));
477 pthread_mutex_unlock (&async_creation_lock);
481 ptr->filename = strdup (filename);
482 if (ptr->filename == NULL)
484 pthread_mutex_unlock (&async_creation_lock);
489 ptr->next = async_creation_list;
490 async_creation_list = ptr;
492 pthread_mutex_unlock (&async_creation_lock);
495 } /* }}} int lock_file */
497 static int unlock_file (char const *filename) /* {{{ */
499 async_create_file_t *this;
500 async_create_file_t *prev;
503 pthread_mutex_lock (&async_creation_lock);
506 for (this = async_creation_list; this != NULL; this = this->next)
508 if (strcmp (filename, this->filename) == 0)
515 pthread_mutex_unlock (&async_creation_lock);
521 assert (this == async_creation_list);
522 async_creation_list = this->next;
526 assert (this == prev->next);
527 prev->next = this->next;
531 pthread_mutex_unlock (&async_creation_lock);
533 sfree (this->filename);
537 } /* }}} int unlock_file */
539 static void *srrd_create_thread (void *targs) /* {{{ */
541 srrd_create_args_t *args = targs;
542 char tmpfile[PATH_MAX];
545 status = lock_file (args->filename);
548 if (status == EEXIST)
549 NOTICE ("srrd_create_thread: File \"%s\" is already being created.",
552 ERROR ("srrd_create_thread: Unable to lock file \"%s\".",
554 srrd_create_args_destroy (args);
558 ssnprintf (tmpfile, sizeof (tmpfile), "%s.async", args->filename);
560 status = srrd_create (tmpfile, args->pdp_step, args->last_up,
561 args->argc, (void *) args->argv);
564 WARNING ("srrd_create_thread: srrd_create (%s) returned status %i.",
565 args->filename, status);
567 unlock_file (args->filename);
568 srrd_create_args_destroy (args);
572 status = rename (tmpfile, args->filename);
576 ERROR ("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s",
577 tmpfile, args->filename,
578 sstrerror (errno, errbuf, sizeof (errbuf)));
580 unlock_file (args->filename);
581 srrd_create_args_destroy (args);
585 DEBUG ("srrd_create_thread: Successfully created RRD file \"%s\".",
588 unlock_file (args->filename);
589 srrd_create_args_destroy (args);
592 } /* }}} void *srrd_create_thread */
594 static int srrd_create_async (const char *filename, /* {{{ */
595 unsigned long pdp_step, time_t last_up,
596 int argc, const char **argv)
598 srrd_create_args_t *args;
603 DEBUG ("srrd_create_async: Creating \"%s\" in the background.", filename);
605 args = srrd_create_args_create (filename, pdp_step, last_up, argc, argv);
609 status = pthread_attr_init (&attr);
612 srrd_create_args_destroy (args);
616 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
619 pthread_attr_destroy (&attr);
620 srrd_create_args_destroy (args);
624 status = pthread_create (&thread, &attr, srrd_create_thread, args);
628 ERROR ("srrd_create_async: pthread_create failed: %s",
629 sstrerror (status, errbuf, sizeof (errbuf)));
630 pthread_attr_destroy (&attr);
631 srrd_create_args_destroy (args);
635 pthread_attr_destroy (&attr);
636 /* args is freed in srrd_create_thread(). */
638 } /* }}} int srrd_create_async */
643 int cu_rrd_create_file (const char *filename, /* {{{ */
644 const data_set_t *ds, const value_list_t *vl,
645 const rrdcreate_config_t *cfg)
655 unsigned long stepsize;
657 if (check_create_dir (filename))
660 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
662 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
666 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
668 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
672 argc = ds_num + rra_num;
674 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
677 ERROR ("cu_rrd_create_file failed: %s",
678 sstrerror (errno, errbuf, sizeof (errbuf)));
682 memcpy (argv, ds_def, ds_num * sizeof (char *));
683 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
684 argv[ds_num + rra_num] = NULL;
686 last_up = CDTIME_T_TO_TIME_T (vl->time);
688 last_up = time (NULL);
691 if (cfg->stepsize > 0)
692 stepsize = cfg->stepsize;
694 stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
698 status = srrd_create_async (filename, stepsize, last_up,
699 argc, (const char **) argv);
701 WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
702 "returned status %i.",
705 else /* synchronous */
707 status = srrd_create (filename, stepsize, last_up,
708 argc, (const char **) argv);
712 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
717 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
723 ds_free (ds_num, ds_def);
724 rra_free (rra_num, rra_def);
727 } /* }}} int cu_rrd_create_file */
729 /* vim: set sw=2 sts=2 et fdm=marker : */