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)
173 /* The stepsize we use here: If it is user-set, use it. If not, use the
174 * interval of the value-list. */
177 if (cfg->rrarows <= 0)
183 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
189 if (cfg->stepsize > 0)
192 ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
199 /* Use the configured timespans or fall back to the built-in defaults */
200 if (cfg->timespans_num != 0)
202 rts = cfg->timespans;
203 rts_num = cfg->timespans_num;
208 rts_num = rra_timespans_num;
211 rra_max = rts_num * rra_types_num;
212 assert (rra_max > 0);
214 if ((rra_def = malloc ((rra_max + 1) * sizeof (*rra_def))) == NULL)
216 memset (rra_def, 0, (rra_max + 1) * sizeof (*rra_def));
220 for (i = 0; i < rts_num; i++)
224 if ((span / ss) < cfg->rrarows)
225 span = ss * cfg->rrarows;
230 cdp_len = (int) floor (((double) span)
231 / ((double) (cfg->rrarows * ss)));
233 cdp_num = (int) ceil (((double) span)
234 / ((double) (cdp_len * ss)));
236 for (j = 0; j < rra_types_num; j++)
241 if (rra_num >= rra_max)
244 status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
245 rra_types[j], cfg->xff, cdp_len, cdp_num);
247 if ((status < 0) || ((size_t) status >= sizeof (buffer)))
249 ERROR ("rra_get: Buffer would have been truncated.");
253 rra_def[rra_num++] = sstrdup (buffer);
265 } /* }}} int rra_get */
267 static void ds_free (int ds_num, char **ds_def) /* {{{ */
271 for (i = 0; i < ds_num; i++)
272 if (ds_def[i] != NULL)
275 } /* }}} void ds_free */
277 static int ds_get (char ***ret, /* {{{ */
278 const data_set_t *ds, const value_list_t *vl,
279 const rrdcreate_config_t *cfg)
288 assert (ds->ds_num > 0);
290 ds_def = malloc (ds->ds_num * sizeof (*ds_def));
294 ERROR ("rrdtool plugin: malloc failed: %s",
295 sstrerror (errno, errbuf, sizeof (errbuf)));
298 memset (ds_def, 0, ds->ds_num * sizeof (*ds_def));
300 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
302 data_source_t *d = ds->ds + ds_num;
306 ds_def[ds_num] = NULL;
308 if (d->type == DS_TYPE_COUNTER)
310 else if (d->type == DS_TYPE_GAUGE)
312 else if (d->type == DS_TYPE_DERIVE)
314 else if (d->type == DS_TYPE_ABSOLUTE)
318 ERROR ("rrdtool plugin: Unknown DS type: %i",
325 sstrncpy (min, "U", sizeof (min));
328 ssnprintf (min, sizeof (min), "%f", d->min);
332 sstrncpy (max, "U", sizeof (max));
335 ssnprintf (max, sizeof (max), "%f", d->max);
337 status = ssnprintf (buffer, sizeof (buffer),
342 : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
344 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
347 ds_def[ds_num] = sstrdup (buffer);
348 } /* for ds_num = 0 .. ds->ds_num */
350 if (ds_num != ds->ds_num)
352 ds_free (ds_num, ds_def);
364 } /* }}} int ds_get */
366 #if HAVE_THREADSAFE_LIBRRD
367 static int srrd_create (const char *filename, /* {{{ */
368 unsigned long pdp_step, time_t last_up,
369 int argc, const char **argv)
374 if ((filename == NULL) || (argv == NULL))
377 /* Some versions of librrd don't have the `const' qualifier for the first
378 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
379 * but what else can we do? :( -octo */
380 filename_copy = strdup (filename);
381 if (filename_copy == NULL)
383 ERROR ("srrd_create: strdup failed.");
387 optind = 0; /* bug in librrd? */
390 status = rrd_create_r (filename_copy, pdp_step, last_up,
391 argc, (void *) argv);
395 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
396 filename, rrd_get_error ());
399 sfree (filename_copy);
402 } /* }}} int srrd_create */
403 /* #endif HAVE_THREADSAFE_LIBRRD */
405 #else /* !HAVE_THREADSAFE_LIBRRD */
406 static int srrd_create (const char *filename, /* {{{ */
407 unsigned long pdp_step, time_t last_up,
408 int argc, const char **argv)
415 char pdp_step_str[16];
416 char last_up_str[16];
419 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
420 if (new_argv == NULL)
422 ERROR ("rrdtool plugin: malloc failed.");
427 last_up = time (NULL) - 10;
429 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
430 ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
432 new_argv[0] = "create";
433 new_argv[1] = (void *) filename;
435 new_argv[3] = pdp_step_str;
437 new_argv[5] = last_up_str;
439 memcpy (new_argv + 6, argv, argc * sizeof (char *));
440 new_argv[new_argc] = NULL;
442 pthread_mutex_lock (&librrd_lock);
443 optind = 0; /* bug in librrd? */
446 status = rrd_create (new_argc, new_argv);
447 pthread_mutex_unlock (&librrd_lock);
451 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
452 filename, rrd_get_error ());
458 } /* }}} int srrd_create */
459 #endif /* !HAVE_THREADSAFE_LIBRRD */
461 static int lock_file (char const *filename) /* {{{ */
463 async_create_file_t *ptr;
467 pthread_mutex_lock (&async_creation_lock);
469 for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
470 if (strcmp (filename, ptr->filename) == 0)
475 pthread_mutex_unlock (&async_creation_lock);
479 status = stat (filename, &sb);
480 if ((status == 0) || (errno != ENOENT))
482 pthread_mutex_unlock (&async_creation_lock);
486 ptr = malloc (sizeof (*ptr));
489 pthread_mutex_unlock (&async_creation_lock);
493 ptr->filename = strdup (filename);
494 if (ptr->filename == NULL)
496 pthread_mutex_unlock (&async_creation_lock);
501 ptr->next = async_creation_list;
502 async_creation_list = ptr;
504 pthread_mutex_unlock (&async_creation_lock);
507 } /* }}} int lock_file */
509 static int unlock_file (char const *filename) /* {{{ */
511 async_create_file_t *this;
512 async_create_file_t *prev;
515 pthread_mutex_lock (&async_creation_lock);
518 for (this = async_creation_list; this != NULL; this = this->next)
520 if (strcmp (filename, this->filename) == 0)
527 pthread_mutex_unlock (&async_creation_lock);
533 assert (this == async_creation_list);
534 async_creation_list = this->next;
538 assert (this == prev->next);
539 prev->next = this->next;
543 pthread_mutex_unlock (&async_creation_lock);
545 sfree (this->filename);
549 } /* }}} int unlock_file */
551 static void *srrd_create_thread (void *targs) /* {{{ */
553 srrd_create_args_t *args = targs;
554 char tmpfile[PATH_MAX];
557 status = lock_file (args->filename);
560 if (status == EEXIST)
561 NOTICE ("srrd_create_thread: File \"%s\" is already being created.",
564 ERROR ("srrd_create_thread: Unable to lock file \"%s\".",
566 srrd_create_args_destroy (args);
570 ssnprintf (tmpfile, sizeof (tmpfile), "%s.async", args->filename);
572 status = srrd_create (tmpfile, args->pdp_step, args->last_up,
573 args->argc, (void *) args->argv);
576 WARNING ("srrd_create_thread: srrd_create (%s) returned status %i.",
577 args->filename, status);
579 unlock_file (args->filename);
580 srrd_create_args_destroy (args);
584 status = rename (tmpfile, args->filename);
588 ERROR ("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s",
589 tmpfile, args->filename,
590 sstrerror (errno, errbuf, sizeof (errbuf)));
592 unlock_file (args->filename);
593 srrd_create_args_destroy (args);
597 DEBUG ("srrd_create_thread: Successfully created RRD file \"%s\".",
600 unlock_file (args->filename);
601 srrd_create_args_destroy (args);
604 } /* }}} void *srrd_create_thread */
606 static int srrd_create_async (const char *filename, /* {{{ */
607 unsigned long pdp_step, time_t last_up,
608 int argc, const char **argv)
610 srrd_create_args_t *args;
615 DEBUG ("srrd_create_async: Creating \"%s\" in the background.", filename);
617 args = srrd_create_args_create (filename, pdp_step, last_up, argc, argv);
621 status = pthread_attr_init (&attr);
624 srrd_create_args_destroy (args);
628 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
631 pthread_attr_destroy (&attr);
632 srrd_create_args_destroy (args);
636 status = pthread_create (&thread, &attr, srrd_create_thread, args);
640 ERROR ("srrd_create_async: pthread_create failed: %s",
641 sstrerror (status, errbuf, sizeof (errbuf)));
642 pthread_attr_destroy (&attr);
643 srrd_create_args_destroy (args);
647 pthread_attr_destroy (&attr);
648 /* args is freed in srrd_create_thread(). */
650 } /* }}} int srrd_create_async */
655 int cu_rrd_create_file (const char *filename, /* {{{ */
656 const data_set_t *ds, const value_list_t *vl,
657 const rrdcreate_config_t *cfg)
661 char **rra_def = NULL;
663 char **ds_def = NULL;
667 unsigned long stepsize;
669 if (check_create_dir (filename))
672 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
674 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
678 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
680 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
681 rra_free (rra_num, rra_def);
685 argc = ds_num + rra_num;
687 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
690 ERROR ("cu_rrd_create_file failed: %s",
691 sstrerror (errno, errbuf, sizeof (errbuf)));
692 rra_free (rra_num, rra_def);
693 ds_free (ds_num, ds_def);
697 memcpy (argv, ds_def, ds_num * sizeof (char *));
698 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
699 argv[ds_num + rra_num] = NULL;
701 last_up = CDTIME_T_TO_TIME_T (vl->time);
703 last_up = time (NULL);
706 if (cfg->stepsize > 0)
707 stepsize = cfg->stepsize;
709 stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
713 status = srrd_create_async (filename, stepsize, last_up,
714 argc, (const char **) argv);
716 WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
717 "returned status %i.",
720 else /* synchronous */
722 status = lock_file (filename);
725 if (status == EEXIST)
726 NOTICE ("cu_rrd_create_file: File \"%s\" is already being created.",
729 ERROR ("cu_rrd_create_file: Unable to lock file \"%s\".",
734 status = srrd_create (filename, stepsize, last_up,
735 argc, (const char **) argv);
739 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
744 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
747 unlock_file (filename);
752 ds_free (ds_num, ds_def);
753 rra_free (rra_num, rra_def);
756 } /* }}} int cu_rrd_create_file */
758 /* vim: set sw=2 sts=2 et fdm=marker : */