2 * collectd - src/utils_rrdcreate.c
3 * Copyright (C) 2006-2013 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
30 #include "utils_rrdcreate.h"
35 struct srrd_create_args_s
38 unsigned long pdp_step;
43 typedef struct srrd_create_args_s srrd_create_args_t;
45 struct async_create_file_s;
46 typedef struct async_create_file_s async_create_file_t;
47 struct async_create_file_s
50 async_create_file_t *next;
56 static int rra_timespans[] =
64 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
66 static const char *const rra_types[] =
72 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
74 #if !defined(HAVE_THREADSAFE_LIBRRD)
75 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
78 static async_create_file_t *async_creation_list = NULL;
79 static pthread_mutex_t async_creation_lock = PTHREAD_MUTEX_INITIALIZER;
84 static void rra_free (int rra_num, char **rra_def) /* {{{ */
86 for (int i = 0; i < rra_num; i++)
91 } /* }}} void rra_free */
93 static void srrd_create_args_destroy (srrd_create_args_t *args)
98 sfree (args->filename);
99 if (args->argv != NULL)
101 for (int i = 0; i < args->argc; i++)
102 sfree (args->argv[i]);
106 } /* void srrd_create_args_destroy */
108 static srrd_create_args_t *srrd_create_args_create (const char *filename,
109 unsigned long pdp_step, time_t last_up,
110 int argc, const char **argv)
112 srrd_create_args_t *args;
114 args = calloc (1, sizeof (*args));
117 ERROR ("srrd_create_args_create: calloc failed.");
120 args->filename = NULL;
121 args->pdp_step = pdp_step;
122 args->last_up = last_up;
125 args->filename = strdup (filename);
126 if (args->filename == NULL)
128 ERROR ("srrd_create_args_create: strdup failed.");
129 srrd_create_args_destroy (args);
133 args->argv = calloc ((size_t) (argc + 1), sizeof (*args->argv));
134 if (args->argv == NULL)
136 ERROR ("srrd_create_args_create: calloc failed.");
137 srrd_create_args_destroy (args);
141 for (args->argc = 0; args->argc < argc; args->argc++)
143 args->argv[args->argc] = strdup (argv[args->argc]);
144 if (args->argv[args->argc] == NULL)
146 ERROR ("srrd_create_args_create: strdup failed.");
147 srrd_create_args_destroy (args);
151 assert (args->argc == argc);
152 args->argv[args->argc] = NULL;
155 } /* srrd_create_args_t *srrd_create_args_create */
160 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
161 const rrdcreate_config_t *cfg)
174 /* The stepsize we use here: If it is user-set, use it. If not, use the
175 * interval of the value-list. */
178 if (cfg->rrarows <= 0)
184 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
190 if (cfg->stepsize > 0)
193 ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
200 /* Use the configured timespans or fall back to the built-in defaults */
201 if (cfg->timespans_num != 0)
203 rts = cfg->timespans;
204 rts_num = cfg->timespans_num;
209 rts_num = rra_timespans_num;
212 rra_max = rts_num * rra_types_num;
213 assert (rra_max > 0);
215 if ((rra_def = calloc (rra_max + 1, sizeof (*rra_def))) == NULL)
220 for (int 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 (int 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) /* {{{ */
269 for (int i = 0; i < ds_num; i++)
270 if (ds_def[i] != NULL)
273 } /* }}} void ds_free */
275 static int ds_get (char ***ret, /* {{{ */
276 const data_set_t *ds, const value_list_t *vl,
277 const rrdcreate_config_t *cfg)
286 assert (ds->ds_num > 0);
288 ds_def = calloc (ds->ds_num, sizeof (*ds_def));
292 ERROR ("rrdtool plugin: calloc failed: %s",
293 sstrerror (errno, errbuf, sizeof (errbuf)));
297 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
299 data_source_t *d = ds->ds + ds_num;
303 ds_def[ds_num] = NULL;
305 if (d->type == DS_TYPE_COUNTER)
307 else if (d->type == DS_TYPE_GAUGE)
309 else if (d->type == DS_TYPE_DERIVE)
311 else if (d->type == DS_TYPE_ABSOLUTE)
315 ERROR ("rrdtool plugin: Unknown DS type: %i",
322 sstrncpy (min, "U", sizeof (min));
325 ssnprintf (min, sizeof (min), "%f", d->min);
329 sstrncpy (max, "U", sizeof (max));
332 ssnprintf (max, sizeof (max), "%f", d->max);
334 status = ssnprintf (buffer, sizeof (buffer),
339 : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
341 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
344 ds_def[ds_num] = sstrdup (buffer);
345 } /* for ds_num = 0 .. ds->ds_num */
347 if (ds_num != ds->ds_num)
349 ds_free (ds_num, ds_def);
361 } /* }}} int ds_get */
363 #if HAVE_THREADSAFE_LIBRRD
364 static int srrd_create (const char *filename, /* {{{ */
365 unsigned long pdp_step, time_t last_up,
366 int argc, const char **argv)
371 if ((filename == NULL) || (argv == NULL))
374 /* Some versions of librrd don't have the `const' qualifier for the first
375 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
376 * but what else can we do? :( -octo */
377 filename_copy = strdup (filename);
378 if (filename_copy == NULL)
380 ERROR ("srrd_create: strdup failed.");
384 optind = 0; /* bug in librrd? */
387 status = rrd_create_r (filename_copy, pdp_step, last_up,
388 argc, (void *) argv);
392 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
393 filename, rrd_get_error ());
396 sfree (filename_copy);
399 } /* }}} int srrd_create */
400 /* #endif HAVE_THREADSAFE_LIBRRD */
402 #else /* !HAVE_THREADSAFE_LIBRRD */
403 static int srrd_create (const char *filename, /* {{{ */
404 unsigned long pdp_step, time_t last_up,
405 int argc, const char **argv)
412 char pdp_step_str[16];
413 char last_up_str[16];
416 new_argv = malloc ((new_argc + 1) * sizeof (*new_argv));
417 if (new_argv == NULL)
419 ERROR ("rrdtool plugin: malloc failed.");
424 last_up = time (NULL) - 10;
426 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
427 ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
429 new_argv[0] = "create";
430 new_argv[1] = (void *) filename;
432 new_argv[3] = pdp_step_str;
434 new_argv[5] = last_up_str;
436 memcpy (new_argv + 6, argv, argc * sizeof (char *));
437 new_argv[new_argc] = NULL;
439 pthread_mutex_lock (&librrd_lock);
440 optind = 0; /* bug in librrd? */
443 status = rrd_create (new_argc, new_argv);
444 pthread_mutex_unlock (&librrd_lock);
448 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
449 filename, rrd_get_error ());
455 } /* }}} int srrd_create */
456 #endif /* !HAVE_THREADSAFE_LIBRRD */
458 static int lock_file (char const *filename) /* {{{ */
460 async_create_file_t *ptr;
464 pthread_mutex_lock (&async_creation_lock);
466 for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
467 if (strcmp (filename, ptr->filename) == 0)
472 pthread_mutex_unlock (&async_creation_lock);
476 status = stat (filename, &sb);
477 if ((status == 0) || (errno != ENOENT))
479 pthread_mutex_unlock (&async_creation_lock);
483 ptr = malloc (sizeof (*ptr));
486 pthread_mutex_unlock (&async_creation_lock);
490 ptr->filename = strdup (filename);
491 if (ptr->filename == NULL)
493 pthread_mutex_unlock (&async_creation_lock);
498 ptr->next = async_creation_list;
499 async_creation_list = ptr;
501 pthread_mutex_unlock (&async_creation_lock);
504 } /* }}} int lock_file */
506 static int unlock_file (char const *filename) /* {{{ */
508 async_create_file_t *this;
509 async_create_file_t *prev;
512 pthread_mutex_lock (&async_creation_lock);
515 for (this = async_creation_list; this != NULL; this = this->next)
517 if (strcmp (filename, this->filename) == 0)
524 pthread_mutex_unlock (&async_creation_lock);
530 assert (this == async_creation_list);
531 async_creation_list = this->next;
535 assert (this == prev->next);
536 prev->next = this->next;
540 pthread_mutex_unlock (&async_creation_lock);
542 sfree (this->filename);
546 } /* }}} int unlock_file */
548 static void *srrd_create_thread (void *targs) /* {{{ */
550 srrd_create_args_t *args = targs;
551 char tmpfile[PATH_MAX];
554 status = lock_file (args->filename);
557 if (status == EEXIST)
558 NOTICE ("srrd_create_thread: File \"%s\" is already being created.",
561 ERROR ("srrd_create_thread: Unable to lock file \"%s\".",
563 srrd_create_args_destroy (args);
567 ssnprintf (tmpfile, sizeof (tmpfile), "%s.async", args->filename);
569 status = srrd_create (tmpfile, args->pdp_step, args->last_up,
570 args->argc, (void *) args->argv);
573 WARNING ("srrd_create_thread: srrd_create (%s) returned status %i.",
574 args->filename, status);
576 unlock_file (args->filename);
577 srrd_create_args_destroy (args);
581 status = rename (tmpfile, args->filename);
585 ERROR ("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s",
586 tmpfile, args->filename,
587 sstrerror (errno, errbuf, sizeof (errbuf)));
589 unlock_file (args->filename);
590 srrd_create_args_destroy (args);
594 DEBUG ("srrd_create_thread: Successfully created RRD file \"%s\".",
597 unlock_file (args->filename);
598 srrd_create_args_destroy (args);
601 } /* }}} void *srrd_create_thread */
603 static int srrd_create_async (const char *filename, /* {{{ */
604 unsigned long pdp_step, time_t last_up,
605 int argc, const char **argv)
607 srrd_create_args_t *args;
612 DEBUG ("srrd_create_async: Creating \"%s\" in the background.", filename);
614 args = srrd_create_args_create (filename, pdp_step, last_up, argc, argv);
618 status = pthread_attr_init (&attr);
621 srrd_create_args_destroy (args);
625 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
628 pthread_attr_destroy (&attr);
629 srrd_create_args_destroy (args);
633 status = pthread_create (&thread, &attr, srrd_create_thread, args);
637 ERROR ("srrd_create_async: pthread_create failed: %s",
638 sstrerror (status, errbuf, sizeof (errbuf)));
639 pthread_attr_destroy (&attr);
640 srrd_create_args_destroy (args);
644 pthread_attr_destroy (&attr);
645 /* args is freed in srrd_create_thread(). */
647 } /* }}} int srrd_create_async */
652 int cu_rrd_create_file (const char *filename, /* {{{ */
653 const data_set_t *ds, const value_list_t *vl,
654 const rrdcreate_config_t *cfg)
658 char **rra_def = NULL;
660 char **ds_def = NULL;
664 unsigned long stepsize;
666 if (check_create_dir (filename))
669 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
671 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
675 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
677 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
678 rra_free (rra_num, rra_def);
682 argc = ds_num + rra_num;
684 if ((argv = malloc (sizeof (*argv) * (argc + 1))) == NULL)
687 ERROR ("cu_rrd_create_file failed: %s",
688 sstrerror (errno, errbuf, sizeof (errbuf)));
689 rra_free (rra_num, rra_def);
690 ds_free (ds_num, ds_def);
694 memcpy (argv, ds_def, ds_num * sizeof (char *));
695 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
696 argv[ds_num + rra_num] = NULL;
698 last_up = CDTIME_T_TO_TIME_T (vl->time);
700 last_up = time (NULL);
703 if (cfg->stepsize > 0)
704 stepsize = cfg->stepsize;
706 stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
710 status = srrd_create_async (filename, stepsize, last_up,
711 argc, (const char **) argv);
713 WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
714 "returned status %i.",
717 else /* synchronous */
719 status = lock_file (filename);
722 if (status == EEXIST)
723 NOTICE ("cu_rrd_create_file: File \"%s\" is already being created.",
726 ERROR ("cu_rrd_create_file: Unable to lock file \"%s\".",
731 status = srrd_create (filename, stepsize, last_up,
732 argc, (const char **) argv);
736 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
741 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
744 unlock_file (filename);
749 ds_free (ds_num, ds_def);
750 rra_free (rra_num, rra_def);
753 } /* }}} int cu_rrd_create_file */
755 /* vim: set sw=2 sts=2 et fdm=marker : */