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]);
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 = malloc (sizeof (*args));
119 ERROR ("srrd_create_args_create: malloc failed.");
122 memset (args, 0, sizeof (*args));
123 args->filename = NULL;
124 args->pdp_step = pdp_step;
125 args->last_up = last_up;
128 args->filename = strdup (filename);
129 if (args->filename == NULL)
131 ERROR ("srrd_create_args_create: strdup failed.");
132 srrd_create_args_destroy (args);
136 args->argv = calloc ((size_t) (argc + 1), sizeof (*args->argv));
137 if (args->argv == NULL)
139 ERROR ("srrd_create_args_create: calloc failed.");
140 srrd_create_args_destroy (args);
144 for (args->argc = 0; args->argc < argc; args->argc++)
146 args->argv[args->argc] = strdup (argv[args->argc]);
147 if (args->argv[args->argc] == NULL)
149 ERROR ("srrd_create_args_create: strdup failed.");
150 srrd_create_args_destroy (args);
154 assert (args->argc == argc);
155 args->argv[args->argc] = NULL;
158 } /* srrd_create_args_t *srrd_create_args_create */
163 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
164 const rrdcreate_config_t *cfg)
178 /* The stepsize we use here: If it is user-set, use it. If not, use the
179 * interval of the value-list. */
182 if (cfg->rrarows <= 0)
188 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
194 if (cfg->stepsize > 0)
197 ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
204 /* Use the configured timespans or fall back to the built-in defaults */
205 if (cfg->timespans_num != 0)
207 rts = cfg->timespans;
208 rts_num = cfg->timespans_num;
213 rts_num = rra_timespans_num;
216 rra_max = rts_num * rra_types_num;
217 assert (rra_max > 0);
219 if ((rra_def = malloc ((rra_max + 1) * sizeof (*rra_def))) == NULL)
221 memset (rra_def, 0, (rra_max + 1) * sizeof (*rra_def));
225 for (i = 0; i < rts_num; i++)
229 if ((span / ss) < cfg->rrarows)
230 span = ss * cfg->rrarows;
235 cdp_len = (int) floor (((double) span)
236 / ((double) (cfg->rrarows * ss)));
238 cdp_num = (int) ceil (((double) span)
239 / ((double) (cdp_len * ss)));
241 for (j = 0; j < rra_types_num; j++)
246 if (rra_num >= rra_max)
249 status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
250 rra_types[j], cfg->xff, cdp_len, cdp_num);
252 if ((status < 0) || ((size_t) status >= sizeof (buffer)))
254 ERROR ("rra_get: Buffer would have been truncated.");
258 rra_def[rra_num++] = sstrdup (buffer);
270 } /* }}} int rra_get */
272 static void ds_free (int ds_num, char **ds_def) /* {{{ */
276 for (i = 0; i < ds_num; i++)
277 if (ds_def[i] != NULL)
280 } /* }}} void ds_free */
282 static int ds_get (char ***ret, /* {{{ */
283 const data_set_t *ds, const value_list_t *vl,
284 const rrdcreate_config_t *cfg)
293 assert (ds->ds_num > 0);
295 ds_def = malloc (ds->ds_num * sizeof (*ds_def));
299 ERROR ("rrdtool plugin: malloc failed: %s",
300 sstrerror (errno, errbuf, sizeof (errbuf)));
303 memset (ds_def, 0, ds->ds_num * sizeof (*ds_def));
305 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
307 data_source_t *d = ds->ds + ds_num;
311 ds_def[ds_num] = NULL;
313 if (d->type == DS_TYPE_COUNTER)
315 else if (d->type == DS_TYPE_GAUGE)
317 else if (d->type == DS_TYPE_DERIVE)
319 else if (d->type == DS_TYPE_ABSOLUTE)
323 ERROR ("rrdtool plugin: Unknown DS type: %i",
330 sstrncpy (min, "U", sizeof (min));
333 ssnprintf (min, sizeof (min), "%f", d->min);
337 sstrncpy (max, "U", sizeof (max));
340 ssnprintf (max, sizeof (max), "%f", d->max);
342 status = ssnprintf (buffer, sizeof (buffer),
347 : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
349 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
352 ds_def[ds_num] = sstrdup (buffer);
353 } /* for ds_num = 0 .. ds->ds_num */
355 if (ds_num != ds->ds_num)
357 ds_free (ds_num, ds_def);
369 } /* }}} int ds_get */
371 #if HAVE_THREADSAFE_LIBRRD
372 static int srrd_create (const char *filename, /* {{{ */
373 unsigned long pdp_step, time_t last_up,
374 int argc, const char **argv)
379 if ((filename == NULL) || (argv == NULL))
382 /* Some versions of librrd don't have the `const' qualifier for the first
383 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
384 * but what else can we do? :( -octo */
385 filename_copy = strdup (filename);
386 if (filename_copy == NULL)
388 ERROR ("srrd_create: strdup failed.");
392 optind = 0; /* bug in librrd? */
395 status = rrd_create_r (filename_copy, pdp_step, last_up,
396 argc, (void *) argv);
400 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
401 filename, rrd_get_error ());
404 sfree (filename_copy);
407 } /* }}} int srrd_create */
408 /* #endif HAVE_THREADSAFE_LIBRRD */
410 #else /* !HAVE_THREADSAFE_LIBRRD */
411 static int srrd_create (const char *filename, /* {{{ */
412 unsigned long pdp_step, time_t last_up,
413 int argc, const char **argv)
420 char pdp_step_str[16];
421 char last_up_str[16];
424 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
425 if (new_argv == NULL)
427 ERROR ("rrdtool plugin: malloc failed.");
432 last_up = time (NULL) - 10;
434 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
435 ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
437 new_argv[0] = "create";
438 new_argv[1] = (void *) filename;
440 new_argv[3] = pdp_step_str;
442 new_argv[5] = last_up_str;
444 memcpy (new_argv + 6, argv, argc * sizeof (char *));
445 new_argv[new_argc] = NULL;
447 pthread_mutex_lock (&librrd_lock);
448 optind = 0; /* bug in librrd? */
451 status = rrd_create (new_argc, new_argv);
452 pthread_mutex_unlock (&librrd_lock);
456 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
457 filename, rrd_get_error ());
463 } /* }}} int srrd_create */
464 #endif /* !HAVE_THREADSAFE_LIBRRD */
466 static int lock_file (char const *filename) /* {{{ */
468 async_create_file_t *ptr;
472 pthread_mutex_lock (&async_creation_lock);
474 for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
475 if (strcmp (filename, ptr->filename) == 0)
480 pthread_mutex_unlock (&async_creation_lock);
484 status = stat (filename, &sb);
485 if ((status == 0) || (errno != ENOENT))
487 pthread_mutex_unlock (&async_creation_lock);
491 ptr = malloc (sizeof (*ptr));
494 pthread_mutex_unlock (&async_creation_lock);
498 ptr->filename = strdup (filename);
499 if (ptr->filename == NULL)
501 pthread_mutex_unlock (&async_creation_lock);
506 ptr->next = async_creation_list;
507 async_creation_list = ptr;
509 pthread_mutex_unlock (&async_creation_lock);
512 } /* }}} int lock_file */
514 static int unlock_file (char const *filename) /* {{{ */
516 async_create_file_t *this;
517 async_create_file_t *prev;
520 pthread_mutex_lock (&async_creation_lock);
523 for (this = async_creation_list; this != NULL; this = this->next)
525 if (strcmp (filename, this->filename) == 0)
532 pthread_mutex_unlock (&async_creation_lock);
538 assert (this == async_creation_list);
539 async_creation_list = this->next;
543 assert (this == prev->next);
544 prev->next = this->next;
548 pthread_mutex_unlock (&async_creation_lock);
550 sfree (this->filename);
554 } /* }}} int unlock_file */
556 static void *srrd_create_thread (void *targs) /* {{{ */
558 srrd_create_args_t *args = targs;
559 char tmpfile[PATH_MAX];
562 status = lock_file (args->filename);
565 if (status == EEXIST)
566 NOTICE ("srrd_create_thread: File \"%s\" is already being created.",
569 ERROR ("srrd_create_thread: Unable to lock file \"%s\".",
571 srrd_create_args_destroy (args);
575 ssnprintf (tmpfile, sizeof (tmpfile), "%s.async", args->filename);
577 status = srrd_create (tmpfile, args->pdp_step, args->last_up,
578 args->argc, (void *) args->argv);
581 WARNING ("srrd_create_thread: srrd_create (%s) returned status %i.",
582 args->filename, status);
584 unlock_file (args->filename);
585 srrd_create_args_destroy (args);
589 status = rename (tmpfile, args->filename);
593 ERROR ("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s",
594 tmpfile, args->filename,
595 sstrerror (errno, errbuf, sizeof (errbuf)));
597 unlock_file (args->filename);
598 srrd_create_args_destroy (args);
602 DEBUG ("srrd_create_thread: Successfully created RRD file \"%s\".",
605 unlock_file (args->filename);
606 srrd_create_args_destroy (args);
609 } /* }}} void *srrd_create_thread */
611 static int srrd_create_async (const char *filename, /* {{{ */
612 unsigned long pdp_step, time_t last_up,
613 int argc, const char **argv)
615 srrd_create_args_t *args;
620 DEBUG ("srrd_create_async: Creating \"%s\" in the background.", filename);
622 args = srrd_create_args_create (filename, pdp_step, last_up, argc, argv);
626 status = pthread_attr_init (&attr);
629 srrd_create_args_destroy (args);
633 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
636 pthread_attr_destroy (&attr);
637 srrd_create_args_destroy (args);
641 status = pthread_create (&thread, &attr, srrd_create_thread, args);
645 ERROR ("srrd_create_async: pthread_create failed: %s",
646 sstrerror (status, errbuf, sizeof (errbuf)));
647 pthread_attr_destroy (&attr);
648 srrd_create_args_destroy (args);
652 pthread_attr_destroy (&attr);
653 /* args is freed in srrd_create_thread(). */
655 } /* }}} int srrd_create_async */
660 int cu_rrd_create_file (const char *filename, /* {{{ */
661 const data_set_t *ds, const value_list_t *vl,
662 const rrdcreate_config_t *cfg)
666 char **rra_def = NULL;
668 char **ds_def = NULL;
672 unsigned long stepsize;
674 if (check_create_dir (filename))
677 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
679 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
683 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
685 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
686 rra_free (rra_num, rra_def);
690 argc = ds_num + rra_num;
692 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
695 ERROR ("cu_rrd_create_file failed: %s",
696 sstrerror (errno, errbuf, sizeof (errbuf)));
697 rra_free (rra_num, rra_def);
698 ds_free (ds_num, ds_def);
702 memcpy (argv, ds_def, ds_num * sizeof (char *));
703 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
704 argv[ds_num + rra_num] = NULL;
706 last_up = CDTIME_T_TO_TIME_T (vl->time);
708 last_up = time (NULL);
711 if (cfg->stepsize > 0)
712 stepsize = cfg->stepsize;
714 stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
718 status = srrd_create_async (filename, stepsize, last_up,
719 argc, (const char **) argv);
721 WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
722 "returned status %i.",
725 else /* synchronous */
727 status = lock_file (filename);
730 if (status == EEXIST)
731 NOTICE ("cu_rrd_create_file: File \"%s\" is already being created.",
734 ERROR ("cu_rrd_create_file: Unable to lock file \"%s\".",
739 status = srrd_create (filename, stepsize, last_up,
740 argc, (const char **) argv);
744 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
749 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
752 unlock_file (filename);
757 ds_free (ds_num, ds_def);
758 rra_free (rra_num, rra_def);
761 } /* }}} int cu_rrd_create_file */
763 /* vim: set sw=2 sts=2 et fdm=marker : */