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]);
102 } /* void srrd_create_args_destroy */
104 static srrd_create_args_t *srrd_create_args_create (const char *filename,
105 unsigned long pdp_step, time_t last_up,
106 int argc, const char **argv)
108 srrd_create_args_t *args;
110 args = malloc (sizeof (*args));
113 ERROR ("srrd_create_args_create: malloc failed.");
116 memset (args, 0, sizeof (*args));
117 args->filename = NULL;
118 args->pdp_step = pdp_step;
119 args->last_up = last_up;
122 args->filename = strdup (filename);
123 if (args->filename == NULL)
125 ERROR ("srrd_create_args_create: strdup failed.");
126 srrd_create_args_destroy (args);
130 args->argv = calloc ((size_t) (argc + 1), sizeof (*args->argv));
131 if (args->argv == NULL)
133 ERROR ("srrd_create_args_create: calloc failed.");
134 srrd_create_args_destroy (args);
138 for (args->argc = 0; args->argc < argc; args->argc++)
140 args->argv[args->argc] = strdup (argv[args->argc]);
141 if (args->argv[args->argc] == NULL)
143 ERROR ("srrd_create_args_create: strdup failed.");
144 srrd_create_args_destroy (args);
148 assert (args->argc == argc);
149 args->argv[args->argc] = NULL;
152 } /* srrd_create_args_t *srrd_create_args_create */
157 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
158 const rrdcreate_config_t *cfg)
176 /* The stepsize we use here: If it is user-set, use it. If not, use the
177 * interval of the value-list. */
180 if (cfg->rrarows <= 0)
186 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
192 if (cfg->stepsize > 0)
195 ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
202 /* Use the configured timespans or fall back to the built-in defaults */
203 if (cfg->timespans_num != 0)
205 rts = cfg->timespans;
206 rts_num = cfg->timespans_num;
211 rts_num = rra_timespans_num;
214 rra_max = rts_num * rra_types_num;
216 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
218 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
222 for (i = 0; i < rts_num; i++)
226 if ((span / ss) < cfg->rrarows)
227 span = ss * cfg->rrarows;
232 cdp_len = (int) floor (((double) span)
233 / ((double) (cfg->rrarows * ss)));
235 cdp_num = (int) ceil (((double) span)
236 / ((double) (cdp_len * ss)));
238 for (j = 0; j < rra_types_num; j++)
242 if (rra_num >= rra_max)
245 status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
246 rra_types[j], cfg->xff, cdp_len, cdp_num);
248 if ((status < 0) || ((size_t) status >= sizeof (buffer)))
250 ERROR ("rra_get: Buffer would have been truncated.");
254 rra_def[rra_num++] = sstrdup (buffer);
260 } /* }}} int rra_get */
262 static void ds_free (int ds_num, char **ds_def) /* {{{ */
266 for (i = 0; i < ds_num; i++)
267 if (ds_def[i] != NULL)
270 } /* }}} void ds_free */
272 static int ds_get (char ***ret, /* {{{ */
273 const data_set_t *ds, const value_list_t *vl,
274 const rrdcreate_config_t *cfg)
283 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
287 ERROR ("rrdtool plugin: malloc failed: %s",
288 sstrerror (errno, errbuf, sizeof (errbuf)));
291 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
293 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
295 data_source_t *d = ds->ds + ds_num;
299 ds_def[ds_num] = NULL;
301 if (d->type == DS_TYPE_COUNTER)
303 else if (d->type == DS_TYPE_GAUGE)
305 else if (d->type == DS_TYPE_DERIVE)
307 else if (d->type == DS_TYPE_ABSOLUTE)
311 ERROR ("rrdtool plugin: Unknown DS type: %i",
318 sstrncpy (min, "U", sizeof (min));
321 ssnprintf (min, sizeof (min), "%f", d->min);
325 sstrncpy (max, "U", sizeof (max));
328 ssnprintf (max, sizeof (max), "%f", d->max);
330 status = ssnprintf (buffer, sizeof (buffer),
335 : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
337 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
340 ds_def[ds_num] = sstrdup (buffer);
341 } /* for ds_num = 0 .. ds->ds_num */
343 if (ds_num != ds->ds_num)
345 ds_free (ds_num, ds_def);
351 } /* }}} int ds_get */
353 #if HAVE_THREADSAFE_LIBRRD
354 static int srrd_create (const char *filename, /* {{{ */
355 unsigned long pdp_step, time_t last_up,
356 int argc, const char **argv)
361 if ((filename == NULL) || (argv == NULL))
364 /* Some versions of librrd don't have the `const' qualifier for the first
365 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
366 * but what else can we do? :( -octo */
367 filename_copy = strdup (filename);
368 if (filename_copy == NULL)
370 ERROR ("srrd_create: strdup failed.");
374 optind = 0; /* bug in librrd? */
377 status = rrd_create_r (filename_copy, pdp_step, last_up,
378 argc, (void *) argv);
382 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
383 filename, rrd_get_error ());
386 sfree (filename_copy);
389 } /* }}} int srrd_create */
390 /* #endif HAVE_THREADSAFE_LIBRRD */
392 #else /* !HAVE_THREADSAFE_LIBRRD */
393 static int srrd_create (const char *filename, /* {{{ */
394 unsigned long pdp_step, time_t last_up,
395 int argc, const char **argv)
402 char pdp_step_str[16];
403 char last_up_str[16];
406 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
407 if (new_argv == NULL)
409 ERROR ("rrdtool plugin: malloc failed.");
414 last_up = time (NULL) - 10;
416 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
417 ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
419 new_argv[0] = "create";
420 new_argv[1] = (void *) filename;
422 new_argv[3] = pdp_step_str;
424 new_argv[5] = last_up_str;
426 memcpy (new_argv + 6, argv, argc * sizeof (char *));
427 new_argv[new_argc] = NULL;
429 pthread_mutex_lock (&librrd_lock);
430 optind = 0; /* bug in librrd? */
433 status = rrd_create (new_argc, new_argv);
434 pthread_mutex_unlock (&librrd_lock);
438 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
439 filename, rrd_get_error ());
445 } /* }}} int srrd_create */
446 #endif /* !HAVE_THREADSAFE_LIBRRD */
448 static int lock_file (char const *filename) /* {{{ */
450 async_create_file_t *ptr;
454 pthread_mutex_lock (&async_creation_lock);
456 for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
457 if (strcmp (filename, ptr->filename) == 0)
462 pthread_mutex_unlock (&async_creation_lock);
466 status = stat (filename, &sb);
467 if ((status == 0) || (errno != ENOENT))
469 pthread_mutex_unlock (&async_creation_lock);
473 ptr = malloc (sizeof (*ptr));
476 pthread_mutex_unlock (&async_creation_lock);
480 ptr->filename = strdup (filename);
481 if (ptr->filename == NULL)
483 pthread_mutex_unlock (&async_creation_lock);
488 ptr->next = async_creation_list;
489 async_creation_list = ptr;
491 pthread_mutex_unlock (&async_creation_lock);
494 } /* }}} int lock_file */
496 static int unlock_file (char const *filename) /* {{{ */
498 async_create_file_t *this;
499 async_create_file_t *prev;
502 pthread_mutex_lock (&async_creation_lock);
505 for (this = async_creation_list; this != NULL; this = this->next)
507 if (strcmp (filename, this->filename) == 0)
514 pthread_mutex_unlock (&async_creation_lock);
520 assert (this == async_creation_list);
521 async_creation_list = this->next;
525 assert (this == prev->next);
526 prev->next = this->next;
530 pthread_mutex_unlock (&async_creation_lock);
532 sfree (this->filename);
536 } /* }}} int unlock_file */
538 static void *srrd_create_thread (void *targs) /* {{{ */
540 srrd_create_args_t *args = targs;
541 char tmpfile[PATH_MAX];
544 status = lock_file (args->filename);
547 if (status == EEXIST)
548 NOTICE ("srrd_create_thread: File \"%s\" is already being created.",
551 ERROR ("srrd_create_thread: Unable to lock file \"%s\".",
553 srrd_create_args_destroy (args);
557 ssnprintf (tmpfile, sizeof (tmpfile), "%s.async", args->filename);
559 status = srrd_create (tmpfile, args->pdp_step, args->last_up,
560 args->argc, (void *) args->argv);
563 WARNING ("srrd_create_thread: srrd_create (%s) returned status %i.",
564 args->filename, status);
566 unlock_file (args->filename);
567 srrd_create_args_destroy (args);
571 status = rename (tmpfile, args->filename);
575 ERROR ("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s",
576 tmpfile, args->filename,
577 sstrerror (errno, errbuf, sizeof (errbuf)));
579 unlock_file (args->filename);
580 srrd_create_args_destroy (args);
584 DEBUG ("srrd_create_thread: Successfully created RRD file \"%s\".",
587 unlock_file (args->filename);
588 srrd_create_args_destroy (args);
591 } /* }}} void *srrd_create_thread */
593 static int srrd_create_async (const char *filename, /* {{{ */
594 unsigned long pdp_step, time_t last_up,
595 int argc, const char **argv)
597 srrd_create_args_t *args;
602 DEBUG ("srrd_create_async: Creating \"%s\" in the background.", filename);
604 args = srrd_create_args_create (filename, pdp_step, last_up, argc, argv);
608 status = pthread_attr_init (&attr);
611 srrd_create_args_destroy (args);
615 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
618 pthread_attr_destroy (&attr);
619 srrd_create_args_destroy (args);
623 status = pthread_create (&thread, &attr, srrd_create_thread, args);
627 ERROR ("srrd_create_async: pthread_create failed: %s",
628 sstrerror (status, errbuf, sizeof (errbuf)));
629 pthread_attr_destroy (&attr);
630 srrd_create_args_destroy (args);
634 pthread_attr_destroy (&attr);
635 /* args is freed in srrd_create_thread(). */
637 } /* }}} int srrd_create_async */
642 int cu_rrd_create_file (const char *filename, /* {{{ */
643 const data_set_t *ds, const value_list_t *vl,
644 const rrdcreate_config_t *cfg)
654 unsigned long stepsize;
656 if (check_create_dir (filename))
659 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
661 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
665 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
667 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
671 argc = ds_num + rra_num;
673 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
676 ERROR ("cu_rrd_create_file failed: %s",
677 sstrerror (errno, errbuf, sizeof (errbuf)));
681 memcpy (argv, ds_def, ds_num * sizeof (char *));
682 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
683 argv[ds_num + rra_num] = NULL;
685 last_up = CDTIME_T_TO_TIME_T (vl->time);
687 last_up = time (NULL);
690 if (cfg->stepsize > 0)
691 stepsize = cfg->stepsize;
693 stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
697 status = srrd_create_async (filename, stepsize, last_up,
698 argc, (const char **) argv);
700 WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
701 "returned status %i.",
704 else /* synchronous */
706 status = srrd_create (filename, stepsize, last_up,
707 argc, (const char **) argv);
711 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
716 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
722 ds_free (ds_num, ds_def);
723 rra_free (rra_num, rra_def);
726 } /* }}} int cu_rrd_create_file */
728 /* vim: set sw=2 sts=2 et fdm=marker : */