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)
182 /* The stepsize we use here: If it is user-set, use it. If not, use the
183 * interval of the value-list. */
186 if (cfg->rrarows <= 0)
192 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
198 if (cfg->stepsize > 0)
201 ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
208 /* Use the configured timespans or fall back to the built-in defaults */
209 if (cfg->timespans_num != 0)
211 rts = cfg->timespans;
212 rts_num = cfg->timespans_num;
217 rts_num = rra_timespans_num;
220 rra_max = rts_num * rra_types_num;
222 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
224 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
228 for (i = 0; i < rts_num; i++)
232 if ((span / ss) < cfg->rrarows)
233 span = ss * cfg->rrarows;
238 cdp_len = (int) floor (((double) span)
239 / ((double) (cfg->rrarows * ss)));
241 cdp_num = (int) ceil (((double) span)
242 / ((double) (cdp_len * ss)));
244 for (j = 0; j < rra_types_num; j++)
248 if (rra_num >= rra_max)
251 status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
252 rra_types[j], cfg->xff, cdp_len, cdp_num);
254 if ((status < 0) || ((size_t) status >= sizeof (buffer)))
256 ERROR ("rra_get: Buffer would have been truncated.");
260 rra_def[rra_num++] = sstrdup (buffer);
266 } /* }}} int rra_get */
268 static void ds_free (int ds_num, char **ds_def) /* {{{ */
272 for (i = 0; i < ds_num; i++)
273 if (ds_def[i] != NULL)
276 } /* }}} void ds_free */
278 static int ds_get (char ***ret, /* {{{ */
279 const data_set_t *ds, const value_list_t *vl,
280 const rrdcreate_config_t *cfg)
289 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
293 ERROR ("rrdtool plugin: malloc failed: %s",
294 sstrerror (errno, errbuf, sizeof (errbuf)));
297 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
299 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
301 data_source_t *d = ds->ds + ds_num;
305 ds_def[ds_num] = NULL;
307 if (d->type == DS_TYPE_COUNTER)
309 else if (d->type == DS_TYPE_GAUGE)
311 else if (d->type == DS_TYPE_DERIVE)
313 else if (d->type == DS_TYPE_ABSOLUTE)
317 ERROR ("rrdtool plugin: Unknown DS type: %i",
324 sstrncpy (min, "U", sizeof (min));
327 ssnprintf (min, sizeof (min), "%f", d->min);
331 sstrncpy (max, "U", sizeof (max));
334 ssnprintf (max, sizeof (max), "%f", d->max);
336 status = ssnprintf (buffer, sizeof (buffer),
341 : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
343 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
346 ds_def[ds_num] = sstrdup (buffer);
347 } /* for ds_num = 0 .. ds->ds_num */
349 if (ds_num != ds->ds_num)
351 ds_free (ds_num, ds_def);
357 } /* }}} int ds_get */
359 #if HAVE_THREADSAFE_LIBRRD
360 static int srrd_create (const char *filename, /* {{{ */
361 unsigned long pdp_step, time_t last_up,
362 int argc, const char **argv)
367 if ((filename == NULL) || (argv == NULL))
370 /* Some versions of librrd don't have the `const' qualifier for the first
371 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
372 * but what else can we do? :( -octo */
373 filename_copy = strdup (filename);
374 if (filename_copy == NULL)
376 ERROR ("srrd_create: strdup failed.");
380 optind = 0; /* bug in librrd? */
383 status = rrd_create_r (filename_copy, pdp_step, last_up,
384 argc, (void *) argv);
388 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
389 filename, rrd_get_error ());
392 sfree (filename_copy);
395 } /* }}} int srrd_create */
396 /* #endif HAVE_THREADSAFE_LIBRRD */
398 #else /* !HAVE_THREADSAFE_LIBRRD */
399 static int srrd_create (const char *filename, /* {{{ */
400 unsigned long pdp_step, time_t last_up,
401 int argc, const char **argv)
408 char pdp_step_str[16];
409 char last_up_str[16];
412 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
413 if (new_argv == NULL)
415 ERROR ("rrdtool plugin: malloc failed.");
420 last_up = time (NULL) - 10;
422 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
423 ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
425 new_argv[0] = "create";
426 new_argv[1] = (void *) filename;
428 new_argv[3] = pdp_step_str;
430 new_argv[5] = last_up_str;
432 memcpy (new_argv + 6, argv, argc * sizeof (char *));
433 new_argv[new_argc] = NULL;
435 pthread_mutex_lock (&librrd_lock);
436 optind = 0; /* bug in librrd? */
439 status = rrd_create (new_argc, new_argv);
440 pthread_mutex_unlock (&librrd_lock);
444 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
445 filename, rrd_get_error ());
451 } /* }}} int srrd_create */
452 #endif /* !HAVE_THREADSAFE_LIBRRD */
454 static int lock_file (char const *filename) /* {{{ */
456 async_create_file_t *ptr;
460 pthread_mutex_lock (&async_creation_lock);
462 for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
463 if (strcmp (filename, ptr->filename) == 0)
468 pthread_mutex_unlock (&async_creation_lock);
472 status = stat (filename, &sb);
473 if ((status == 0) || (errno != ENOENT))
475 pthread_mutex_unlock (&async_creation_lock);
479 ptr = malloc (sizeof (*ptr));
482 pthread_mutex_unlock (&async_creation_lock);
486 ptr->filename = strdup (filename);
487 if (ptr->filename == NULL)
489 pthread_mutex_unlock (&async_creation_lock);
494 ptr->next = async_creation_list;
495 async_creation_list = ptr;
497 pthread_mutex_unlock (&async_creation_lock);
500 } /* }}} int lock_file */
502 static int unlock_file (char const *filename) /* {{{ */
504 async_create_file_t *this;
505 async_create_file_t *prev;
508 pthread_mutex_lock (&async_creation_lock);
511 for (this = async_creation_list; this != NULL; this = this->next)
513 if (strcmp (filename, this->filename) == 0)
520 pthread_mutex_unlock (&async_creation_lock);
526 assert (this == async_creation_list);
527 async_creation_list = this->next;
531 assert (this == prev->next);
532 prev->next = this->next;
536 pthread_mutex_unlock (&async_creation_lock);
538 sfree (this->filename);
542 } /* }}} int unlock_file */
544 static void *srrd_create_thread (void *targs) /* {{{ */
546 srrd_create_args_t *args = targs;
547 char tmpfile[PATH_MAX];
550 status = lock_file (args->filename);
553 if (status == EEXIST)
554 NOTICE ("srrd_create_thread: File \"%s\" is already being created.",
557 ERROR ("srrd_create_thread: Unable to lock file \"%s\".",
559 srrd_create_args_destroy (args);
563 ssnprintf (tmpfile, sizeof (tmpfile), "%s.async", args->filename);
565 status = srrd_create (tmpfile, args->pdp_step, args->last_up,
566 args->argc, (void *) args->argv);
569 WARNING ("srrd_create_thread: srrd_create (%s) returned status %i.",
570 args->filename, status);
572 unlock_file (args->filename);
573 srrd_create_args_destroy (args);
577 status = rename (tmpfile, args->filename);
581 ERROR ("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s",
582 tmpfile, args->filename,
583 sstrerror (errno, errbuf, sizeof (errbuf)));
585 unlock_file (args->filename);
586 srrd_create_args_destroy (args);
590 DEBUG ("srrd_create_thread: Successfully created RRD file \"%s\".",
593 unlock_file (args->filename);
594 srrd_create_args_destroy (args);
597 } /* }}} void *srrd_create_thread */
599 static int srrd_create_async (const char *filename, /* {{{ */
600 unsigned long pdp_step, time_t last_up,
601 int argc, const char **argv)
603 srrd_create_args_t *args;
608 DEBUG ("srrd_create_async: Creating \"%s\" in the background.", filename);
610 args = srrd_create_args_create (filename, pdp_step, last_up, argc, argv);
614 status = pthread_attr_init (&attr);
617 srrd_create_args_destroy (args);
621 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
624 pthread_attr_destroy (&attr);
625 srrd_create_args_destroy (args);
629 status = pthread_create (&thread, &attr, srrd_create_thread, args);
633 ERROR ("srrd_create_async: pthread_create failed: %s",
634 sstrerror (status, errbuf, sizeof (errbuf)));
635 pthread_attr_destroy (&attr);
636 srrd_create_args_destroy (args);
640 pthread_attr_destroy (&attr);
641 /* args is freed in srrd_create_thread(). */
643 } /* }}} int srrd_create_async */
648 int cu_rrd_create_file (const char *filename, /* {{{ */
649 const data_set_t *ds, const value_list_t *vl,
650 const rrdcreate_config_t *cfg)
660 unsigned long stepsize;
662 if (check_create_dir (filename))
665 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
667 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
671 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
673 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
677 argc = ds_num + rra_num;
679 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
682 ERROR ("cu_rrd_create_file failed: %s",
683 sstrerror (errno, errbuf, sizeof (errbuf)));
687 memcpy (argv, ds_def, ds_num * sizeof (char *));
688 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
689 argv[ds_num + rra_num] = NULL;
691 last_up = CDTIME_T_TO_TIME_T (vl->time);
693 last_up = time (NULL);
696 if (cfg->stepsize > 0)
697 stepsize = cfg->stepsize;
699 stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
703 status = srrd_create_async (filename, stepsize, last_up,
704 argc, (const char **) argv);
706 WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
707 "returned status %i.",
710 else /* synchronous */
712 status = srrd_create (filename, stepsize, last_up,
713 argc, (const char **) argv);
717 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
722 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
728 ds_free (ds_num, ds_def);
729 rra_free (rra_num, rra_def);
732 } /* }}} int cu_rrd_create_file */
734 /* vim: set sw=2 sts=2 et fdm=marker : */