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>
29 #include "utils_rrdcreate.h"
34 struct srrd_create_args_s
37 unsigned long pdp_step;
42 typedef struct srrd_create_args_s srrd_create_args_t;
44 struct async_create_file_s;
45 typedef struct async_create_file_s async_create_file_t;
46 struct async_create_file_s
49 async_create_file_t *next;
55 static int rra_timespans[] =
63 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
65 static const char *const rra_types[] =
71 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
73 #if !defined(HAVE_THREADSAFE_LIBRRD)
74 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
77 static async_create_file_t *async_creation_list = NULL;
78 static pthread_mutex_t async_creation_lock = PTHREAD_MUTEX_INITIALIZER;
83 static void rra_free (int rra_num, char **rra_def) /* {{{ */
87 for (i = 0; i < rra_num; i++)
92 } /* }}} void rra_free */
94 static void srrd_create_args_destroy (srrd_create_args_t *args)
99 sfree (args->filename);
100 if (args->argv != NULL)
103 for (i = 0; i < args->argc; i++)
104 sfree (args->argv[i]);
108 } /* void srrd_create_args_destroy */
110 static srrd_create_args_t *srrd_create_args_create (const char *filename,
111 unsigned long pdp_step, time_t last_up,
112 int argc, const char **argv)
114 srrd_create_args_t *args;
116 args = calloc (1, sizeof (*args));
119 ERROR ("srrd_create_args_create: calloc failed.");
122 args->filename = NULL;
123 args->pdp_step = pdp_step;
124 args->last_up = last_up;
127 args->filename = strdup (filename);
128 if (args->filename == NULL)
130 ERROR ("srrd_create_args_create: strdup failed.");
131 srrd_create_args_destroy (args);
135 args->argv = calloc ((size_t) (argc + 1), sizeof (*args->argv));
136 if (args->argv == NULL)
138 ERROR ("srrd_create_args_create: calloc failed.");
139 srrd_create_args_destroy (args);
143 for (args->argc = 0; args->argc < argc; args->argc++)
145 args->argv[args->argc] = strdup (argv[args->argc]);
146 if (args->argv[args->argc] == NULL)
148 ERROR ("srrd_create_args_create: strdup failed.");
149 srrd_create_args_destroy (args);
153 assert (args->argc == argc);
154 args->argv[args->argc] = NULL;
157 } /* srrd_create_args_t *srrd_create_args_create */
162 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
163 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;
216 assert (rra_max > 0);
218 if ((rra_def = calloc (rra_max + 1, sizeof (*rra_def))) == NULL)
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++)
244 if (rra_num >= rra_max)
247 status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
248 rra_types[j], cfg->xff, cdp_len, cdp_num);
250 if ((status < 0) || ((size_t) status >= sizeof (buffer)))
252 ERROR ("rra_get: Buffer would have been truncated.");
256 rra_def[rra_num++] = sstrdup (buffer);
268 } /* }}} int rra_get */
270 static void ds_free (int ds_num, char **ds_def) /* {{{ */
274 for (i = 0; i < ds_num; i++)
275 if (ds_def[i] != NULL)
278 } /* }}} void ds_free */
280 static int ds_get (char ***ret, /* {{{ */
281 const data_set_t *ds, const value_list_t *vl,
282 const rrdcreate_config_t *cfg)
291 assert (ds->ds_num > 0);
293 ds_def = calloc (ds->ds_num, sizeof (*ds_def));
297 ERROR ("rrdtool plugin: calloc failed: %s",
298 sstrerror (errno, errbuf, sizeof (errbuf)));
302 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
304 data_source_t *d = ds->ds + ds_num;
308 ds_def[ds_num] = NULL;
310 if (d->type == DS_TYPE_COUNTER)
312 else if (d->type == DS_TYPE_GAUGE)
314 else if (d->type == DS_TYPE_DERIVE)
316 else if (d->type == DS_TYPE_ABSOLUTE)
320 ERROR ("rrdtool plugin: Unknown DS type: %i",
327 sstrncpy (min, "U", sizeof (min));
330 ssnprintf (min, sizeof (min), "%f", d->min);
334 sstrncpy (max, "U", sizeof (max));
337 ssnprintf (max, sizeof (max), "%f", d->max);
339 status = ssnprintf (buffer, sizeof (buffer),
344 : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
346 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
349 ds_def[ds_num] = sstrdup (buffer);
350 } /* for ds_num = 0 .. ds->ds_num */
352 if (ds_num != ds->ds_num)
354 ds_free (ds_num, ds_def);
366 } /* }}} int ds_get */
368 #if HAVE_THREADSAFE_LIBRRD
369 static int srrd_create (const char *filename, /* {{{ */
370 unsigned long pdp_step, time_t last_up,
371 int argc, const char **argv)
376 if ((filename == NULL) || (argv == NULL))
379 /* Some versions of librrd don't have the `const' qualifier for the first
380 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
381 * but what else can we do? :( -octo */
382 filename_copy = strdup (filename);
383 if (filename_copy == NULL)
385 ERROR ("srrd_create: strdup failed.");
389 optind = 0; /* bug in librrd? */
392 status = rrd_create_r (filename_copy, pdp_step, last_up,
393 argc, (void *) argv);
397 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
398 filename, rrd_get_error ());
401 sfree (filename_copy);
404 } /* }}} int srrd_create */
405 /* #endif HAVE_THREADSAFE_LIBRRD */
407 #else /* !HAVE_THREADSAFE_LIBRRD */
408 static int srrd_create (const char *filename, /* {{{ */
409 unsigned long pdp_step, time_t last_up,
410 int argc, const char **argv)
417 char pdp_step_str[16];
418 char last_up_str[16];
421 new_argv = malloc ((new_argc + 1) * sizeof (*new_argv));
422 if (new_argv == NULL)
424 ERROR ("rrdtool plugin: malloc failed.");
429 last_up = time (NULL) - 10;
431 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
432 ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
434 new_argv[0] = "create";
435 new_argv[1] = (void *) filename;
437 new_argv[3] = pdp_step_str;
439 new_argv[5] = last_up_str;
441 memcpy (new_argv + 6, argv, argc * sizeof (char *));
442 new_argv[new_argc] = NULL;
444 pthread_mutex_lock (&librrd_lock);
445 optind = 0; /* bug in librrd? */
448 status = rrd_create (new_argc, new_argv);
449 pthread_mutex_unlock (&librrd_lock);
453 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
454 filename, rrd_get_error ());
460 } /* }}} int srrd_create */
461 #endif /* !HAVE_THREADSAFE_LIBRRD */
463 static int lock_file (char const *filename) /* {{{ */
465 async_create_file_t *ptr;
469 pthread_mutex_lock (&async_creation_lock);
471 for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
472 if (strcmp (filename, ptr->filename) == 0)
477 pthread_mutex_unlock (&async_creation_lock);
481 status = stat (filename, &sb);
482 if ((status == 0) || (errno != ENOENT))
484 pthread_mutex_unlock (&async_creation_lock);
488 ptr = malloc (sizeof (*ptr));
491 pthread_mutex_unlock (&async_creation_lock);
495 ptr->filename = strdup (filename);
496 if (ptr->filename == NULL)
498 pthread_mutex_unlock (&async_creation_lock);
503 ptr->next = async_creation_list;
504 async_creation_list = ptr;
506 pthread_mutex_unlock (&async_creation_lock);
509 } /* }}} int lock_file */
511 static int unlock_file (char const *filename) /* {{{ */
513 async_create_file_t *this;
514 async_create_file_t *prev;
517 pthread_mutex_lock (&async_creation_lock);
520 for (this = async_creation_list; this != NULL; this = this->next)
522 if (strcmp (filename, this->filename) == 0)
529 pthread_mutex_unlock (&async_creation_lock);
535 assert (this == async_creation_list);
536 async_creation_list = this->next;
540 assert (this == prev->next);
541 prev->next = this->next;
545 pthread_mutex_unlock (&async_creation_lock);
547 sfree (this->filename);
551 } /* }}} int unlock_file */
553 static void *srrd_create_thread (void *targs) /* {{{ */
555 srrd_create_args_t *args = targs;
556 char tmpfile[PATH_MAX];
559 status = lock_file (args->filename);
562 if (status == EEXIST)
563 NOTICE ("srrd_create_thread: File \"%s\" is already being created.",
566 ERROR ("srrd_create_thread: Unable to lock file \"%s\".",
568 srrd_create_args_destroy (args);
572 ssnprintf (tmpfile, sizeof (tmpfile), "%s.async", args->filename);
574 status = srrd_create (tmpfile, args->pdp_step, args->last_up,
575 args->argc, (void *) args->argv);
578 WARNING ("srrd_create_thread: srrd_create (%s) returned status %i.",
579 args->filename, status);
581 unlock_file (args->filename);
582 srrd_create_args_destroy (args);
586 status = rename (tmpfile, args->filename);
590 ERROR ("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s",
591 tmpfile, args->filename,
592 sstrerror (errno, errbuf, sizeof (errbuf)));
594 unlock_file (args->filename);
595 srrd_create_args_destroy (args);
599 DEBUG ("srrd_create_thread: Successfully created RRD file \"%s\".",
602 unlock_file (args->filename);
603 srrd_create_args_destroy (args);
606 } /* }}} void *srrd_create_thread */
608 static int srrd_create_async (const char *filename, /* {{{ */
609 unsigned long pdp_step, time_t last_up,
610 int argc, const char **argv)
612 srrd_create_args_t *args;
617 DEBUG ("srrd_create_async: Creating \"%s\" in the background.", filename);
619 args = srrd_create_args_create (filename, pdp_step, last_up, argc, argv);
623 status = pthread_attr_init (&attr);
626 srrd_create_args_destroy (args);
630 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
633 pthread_attr_destroy (&attr);
634 srrd_create_args_destroy (args);
638 status = pthread_create (&thread, &attr, srrd_create_thread, args);
642 ERROR ("srrd_create_async: pthread_create failed: %s",
643 sstrerror (status, errbuf, sizeof (errbuf)));
644 pthread_attr_destroy (&attr);
645 srrd_create_args_destroy (args);
649 pthread_attr_destroy (&attr);
650 /* args is freed in srrd_create_thread(). */
652 } /* }}} int srrd_create_async */
657 int cu_rrd_create_file (const char *filename, /* {{{ */
658 const data_set_t *ds, const value_list_t *vl,
659 const rrdcreate_config_t *cfg)
663 char **rra_def = NULL;
665 char **ds_def = NULL;
669 unsigned long stepsize;
671 if (check_create_dir (filename))
674 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
676 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
680 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
682 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
683 rra_free (rra_num, rra_def);
687 argc = ds_num + rra_num;
689 if ((argv = malloc (sizeof (*argv) * (argc + 1))) == NULL)
692 ERROR ("cu_rrd_create_file failed: %s",
693 sstrerror (errno, errbuf, sizeof (errbuf)));
694 rra_free (rra_num, rra_def);
695 ds_free (ds_num, ds_def);
699 memcpy (argv, ds_def, ds_num * sizeof (char *));
700 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
701 argv[ds_num + rra_num] = NULL;
703 last_up = CDTIME_T_TO_TIME_T (vl->time);
705 last_up = time (NULL);
708 if (cfg->stepsize > 0)
709 stepsize = cfg->stepsize;
711 stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
715 status = srrd_create_async (filename, stepsize, last_up,
716 argc, (const char **) argv);
718 WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
719 "returned status %i.",
722 else /* synchronous */
724 status = lock_file (filename);
727 if (status == EEXIST)
728 NOTICE ("cu_rrd_create_file: File \"%s\" is already being created.",
731 ERROR ("cu_rrd_create_file: Unable to lock file \"%s\".",
736 status = srrd_create (filename, stepsize, last_up,
737 argc, (const char **) argv);
741 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
746 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
749 unlock_file (filename);
754 ds_free (ds_num, ds_def);
755 rra_free (rra_num, rra_def);
758 } /* }}} int cu_rrd_create_file */
760 /* vim: set sw=2 sts=2 et fdm=marker : */