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 char *rra_types[] =
71 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
73 #if !defined(HAVE_THREADSAFE_LIBRRD) || !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]);
107 } /* void srrd_create_args_destroy */
109 static srrd_create_args_t *srrd_create_args_create (const char *filename,
110 unsigned long pdp_step, time_t last_up,
111 int argc, const char **argv)
113 srrd_create_args_t *args;
115 args = malloc (sizeof (*args));
118 ERROR ("srrd_create_args_create: malloc failed.");
121 memset (args, 0, sizeof (*args));
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)
181 /* The stepsize we use here: If it is user-set, use it. If not, use the
182 * interval of the value-list. */
185 if (cfg->rrarows <= 0)
191 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
197 if (cfg->stepsize > 0)
200 ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
207 /* Use the configured timespans or fall back to the built-in defaults */
208 if (cfg->timespans_num != 0)
210 rts = cfg->timespans;
211 rts_num = cfg->timespans_num;
216 rts_num = rra_timespans_num;
219 rra_max = rts_num * rra_types_num;
221 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
223 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
227 for (i = 0; i < rts_num; i++)
231 if ((span / ss) < cfg->rrarows)
232 span = ss * cfg->rrarows;
237 cdp_len = (int) floor (((double) span)
238 / ((double) (cfg->rrarows * ss)));
240 cdp_num = (int) ceil (((double) span)
241 / ((double) (cdp_len * ss)));
243 for (j = 0; j < rra_types_num; j++)
247 if (rra_num >= rra_max)
250 status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
251 rra_types[j], cfg->xff, cdp_len, cdp_num);
253 if ((status < 0) || ((size_t) status >= sizeof (buffer)))
255 ERROR ("rra_get: Buffer would have been truncated.");
259 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 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
292 ERROR ("rrdtool plugin: malloc failed: %s",
293 sstrerror (errno, errbuf, sizeof (errbuf)));
296 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
298 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
300 data_source_t *d = ds->ds + ds_num;
304 ds_def[ds_num] = NULL;
306 if (d->type == DS_TYPE_COUNTER)
308 else if (d->type == DS_TYPE_GAUGE)
310 else if (d->type == DS_TYPE_DERIVE)
312 else if (d->type == DS_TYPE_ABSOLUTE)
316 ERROR ("rrdtool plugin: Unknown DS type: %i",
323 sstrncpy (min, "U", sizeof (min));
326 ssnprintf (min, sizeof (min), "%f", d->min);
330 sstrncpy (max, "U", sizeof (max));
333 ssnprintf (max, sizeof (max), "%f", d->max);
335 status = ssnprintf (buffer, sizeof (buffer),
340 : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
342 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
345 ds_def[ds_num] = sstrdup (buffer);
346 } /* for ds_num = 0 .. ds->ds_num */
348 if (ds_num != ds->ds_num)
350 ds_free (ds_num, ds_def);
356 } /* }}} int ds_get */
358 #if HAVE_THREADSAFE_LIBRRD
359 static int srrd_create (const char *filename, /* {{{ */
360 unsigned long pdp_step, time_t last_up,
361 int argc, const char **argv)
366 if ((filename == NULL) || (argv == NULL))
369 /* Some versions of librrd don't have the `const' qualifier for the first
370 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
371 * but what else can we do? :( -octo */
372 filename_copy = strdup (filename);
373 if (filename_copy == NULL)
375 ERROR ("srrd_create: strdup failed.");
379 optind = 0; /* bug in librrd? */
382 status = rrd_create_r (filename_copy, pdp_step, last_up,
383 argc, (void *) argv);
387 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
388 filename, rrd_get_error ());
391 sfree (filename_copy);
394 } /* }}} int srrd_create */
395 /* #endif HAVE_THREADSAFE_LIBRRD */
397 #else /* !HAVE_THREADSAFE_LIBRRD */
398 static int srrd_create (const char *filename, /* {{{ */
399 unsigned long pdp_step, time_t last_up,
400 int argc, const char **argv)
407 char pdp_step_str[16];
408 char last_up_str[16];
411 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
412 if (new_argv == NULL)
414 ERROR ("rrdtool plugin: malloc failed.");
419 last_up = time (NULL) - 10;
421 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
422 ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
424 new_argv[0] = "create";
425 new_argv[1] = (void *) filename;
427 new_argv[3] = pdp_step_str;
429 new_argv[5] = last_up_str;
431 memcpy (new_argv + 6, argv, argc * sizeof (char *));
432 new_argv[new_argc] = NULL;
434 pthread_mutex_lock (&librrd_lock);
435 optind = 0; /* bug in librrd? */
438 status = rrd_create (new_argc, new_argv);
439 pthread_mutex_unlock (&librrd_lock);
443 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
444 filename, rrd_get_error ());
450 } /* }}} int srrd_create */
451 #endif /* !HAVE_THREADSAFE_LIBRRD */
453 static int lock_file (char const *filename) /* {{{ */
455 async_create_file_t *ptr;
459 pthread_mutex_lock (&async_creation_lock);
461 for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
462 if (strcmp (filename, ptr->filename) == 0)
467 pthread_mutex_unlock (&async_creation_lock);
471 status = stat (filename, &sb);
472 if ((status == 0) || (errno != ENOENT))
474 pthread_mutex_unlock (&async_creation_lock);
478 ptr = malloc (sizeof (*ptr));
481 pthread_mutex_unlock (&async_creation_lock);
485 ptr->filename = strdup (filename);
486 if (ptr->filename == NULL)
488 pthread_mutex_unlock (&async_creation_lock);
493 ptr->next = async_creation_list;
494 async_creation_list = ptr;
496 pthread_mutex_unlock (&async_creation_lock);
499 } /* }}} int lock_file */
501 static int unlock_file (char const *filename) /* {{{ */
503 async_create_file_t *this;
504 async_create_file_t *prev;
507 pthread_mutex_lock (&async_creation_lock);
510 for (this = async_creation_list; this != NULL; this = this->next)
512 if (strcmp (filename, this->filename) == 0)
519 pthread_mutex_unlock (&async_creation_lock);
525 assert (this == async_creation_list);
526 async_creation_list = this->next;
530 assert (this == prev->next);
531 prev->next = this->next;
535 pthread_mutex_unlock (&async_creation_lock);
537 sfree (this->filename);
541 } /* }}} int unlock_file */
543 static void *srrd_create_thread (void *targs) /* {{{ */
545 srrd_create_args_t *args = targs;
546 char tmpfile[PATH_MAX];
549 status = lock_file (args->filename);
552 if (status == EEXIST)
553 NOTICE ("srrd_create_thread: File \"%s\" is already being created.",
556 ERROR ("srrd_create_thread: Unable to lock file \"%s\".",
558 srrd_create_args_destroy (args);
562 ssnprintf (tmpfile, sizeof (tmpfile), "%s.async", args->filename);
564 status = srrd_create (tmpfile, args->pdp_step, args->last_up,
565 args->argc, (void *) args->argv);
568 WARNING ("srrd_create_thread: srrd_create (%s) returned status %i.",
569 args->filename, status);
571 unlock_file (args->filename);
572 srrd_create_args_destroy (args);
576 status = rename (tmpfile, args->filename);
580 ERROR ("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s",
581 tmpfile, args->filename,
582 sstrerror (errno, errbuf, sizeof (errbuf)));
584 unlock_file (args->filename);
585 srrd_create_args_destroy (args);
589 DEBUG ("srrd_create_thread: Successfully created RRD file \"%s\".",
592 unlock_file (args->filename);
593 srrd_create_args_destroy (args);
596 } /* }}} void *srrd_create_thread */
598 static int srrd_create_async (const char *filename, /* {{{ */
599 unsigned long pdp_step, time_t last_up,
600 int argc, const char **argv)
602 srrd_create_args_t *args;
607 DEBUG ("srrd_create_async: Creating \"%s\" in the background.", filename);
609 args = srrd_create_args_create (filename, pdp_step, last_up, argc, argv);
613 status = pthread_attr_init (&attr);
616 srrd_create_args_destroy (args);
620 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
623 pthread_attr_destroy (&attr);
624 srrd_create_args_destroy (args);
628 status = pthread_create (&thread, &attr, srrd_create_thread, args);
632 ERROR ("srrd_create_async: pthread_create failed: %s",
633 sstrerror (status, errbuf, sizeof (errbuf)));
634 pthread_attr_destroy (&attr);
635 srrd_create_args_destroy (args);
639 pthread_attr_destroy (&attr);
640 /* args is freed in srrd_create_thread(). */
642 } /* }}} int srrd_create_async */
647 int cu_rrd_create_file (const char *filename, /* {{{ */
648 const data_set_t *ds, const value_list_t *vl,
649 const rrdcreate_config_t *cfg)
659 unsigned long stepsize;
661 if (check_create_dir (filename))
664 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
666 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
670 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
672 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
676 argc = ds_num + rra_num;
678 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
681 ERROR ("cu_rrd_create_file failed: %s",
682 sstrerror (errno, errbuf, sizeof (errbuf)));
686 memcpy (argv, ds_def, ds_num * sizeof (char *));
687 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
688 argv[ds_num + rra_num] = NULL;
690 last_up = CDTIME_T_TO_TIME_T (vl->time);
692 last_up = time (NULL);
695 if (cfg->stepsize > 0)
696 stepsize = cfg->stepsize;
698 stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
702 status = srrd_create_async (filename, stepsize, last_up,
703 argc, (const char **) argv);
705 WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
706 "returned status %i.",
709 else /* synchronous */
711 status = srrd_create (filename, stepsize, last_up,
712 argc, (const char **) argv);
716 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
721 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
727 ds_free (ds_num, ds_def);
728 rra_free (rra_num, rra_def);
731 } /* }}} int cu_rrd_create_file */
733 /* vim: set sw=2 sts=2 et fdm=marker : */