2 * collectd - src/utils_rrdcreate.c
3 * Copyright (C) 2006-2013 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at collectd.org>
24 #include "utils_rrdcreate.h"
29 struct srrd_create_args_s
32 unsigned long pdp_step;
37 typedef struct srrd_create_args_s srrd_create_args_t;
42 static int rra_timespans[] =
50 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
52 static char *rra_types[] =
58 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
60 #if !defined(HAVE_THREADSAFE_LIBRRD) || !HAVE_THREADSAFE_LIBRRD
61 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
67 static void rra_free (int rra_num, char **rra_def) /* {{{ */
71 for (i = 0; i < rra_num; i++)
76 } /* }}} void rra_free */
78 static void srrd_create_args_destroy (srrd_create_args_t *args)
83 sfree (args->filename);
84 if (args->argv != NULL)
87 for (i = 0; i < args->argc; i++)
88 sfree (args->argv[i]);
91 } /* void srrd_create_args_destroy */
93 static srrd_create_args_t *srrd_create_args_create (const char *filename,
94 unsigned long pdp_step, time_t last_up,
95 int argc, const char **argv)
97 srrd_create_args_t *args;
99 args = malloc (sizeof (*args));
102 ERROR ("srrd_create_args_create: malloc failed.");
105 memset (args, 0, sizeof (*args));
106 args->filename = NULL;
107 args->pdp_step = pdp_step;
108 args->last_up = last_up;
111 args->filename = strdup (filename);
112 if (args->filename == NULL)
114 ERROR ("srrd_create_args_create: strdup failed.");
115 srrd_create_args_destroy (args);
119 args->argv = calloc ((size_t) (argc + 1), sizeof (*args->argv));
120 if (args->argv == NULL)
122 ERROR ("srrd_create_args_create: calloc failed.");
123 srrd_create_args_destroy (args);
127 for (args->argc = 0; args->argc < argc; args->argc++)
129 args->argv[args->argc] = strdup (argv[args->argc]);
130 if (args->argv[args->argc] == NULL)
132 ERROR ("srrd_create_args_create: strdup failed.");
133 srrd_create_args_destroy (args);
137 assert (args->argc == argc);
138 args->argv[args->argc] = NULL;
141 } /* srrd_create_args_t *srrd_create_args_create */
146 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
147 const rrdcreate_config_t *cfg)
165 /* The stepsize we use here: If it is user-set, use it. If not, use the
166 * interval of the value-list. */
169 if (cfg->rrarows <= 0)
175 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
181 if (cfg->stepsize > 0)
184 ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
191 /* Use the configured timespans or fall back to the built-in defaults */
192 if (cfg->timespans_num != 0)
194 rts = cfg->timespans;
195 rts_num = cfg->timespans_num;
200 rts_num = rra_timespans_num;
203 rra_max = rts_num * rra_types_num;
205 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
207 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
211 for (i = 0; i < rts_num; i++)
215 if ((span / ss) < cfg->rrarows)
216 span = ss * cfg->rrarows;
221 cdp_len = (int) floor (((double) span)
222 / ((double) (cfg->rrarows * ss)));
224 cdp_num = (int) ceil (((double) span)
225 / ((double) (cdp_len * ss)));
227 for (j = 0; j < rra_types_num; j++)
231 if (rra_num >= rra_max)
234 status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
235 rra_types[j], cfg->xff, cdp_len, cdp_num);
237 if ((status < 0) || ((size_t) status >= sizeof (buffer)))
239 ERROR ("rra_get: Buffer would have been truncated.");
243 rra_def[rra_num++] = sstrdup (buffer);
249 } /* }}} int rra_get */
251 static void ds_free (int ds_num, char **ds_def) /* {{{ */
255 for (i = 0; i < ds_num; i++)
256 if (ds_def[i] != NULL)
259 } /* }}} void ds_free */
261 static int ds_get (char ***ret, /* {{{ */
262 const data_set_t *ds, const value_list_t *vl,
263 const rrdcreate_config_t *cfg)
272 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
276 ERROR ("rrdtool plugin: malloc failed: %s",
277 sstrerror (errno, errbuf, sizeof (errbuf)));
280 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
282 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
284 data_source_t *d = ds->ds + ds_num;
288 ds_def[ds_num] = NULL;
290 if (d->type == DS_TYPE_COUNTER)
292 else if (d->type == DS_TYPE_GAUGE)
294 else if (d->type == DS_TYPE_DERIVE)
296 else if (d->type == DS_TYPE_ABSOLUTE)
300 ERROR ("rrdtool plugin: Unknown DS type: %i",
307 sstrncpy (min, "U", sizeof (min));
310 ssnprintf (min, sizeof (min), "%f", d->min);
314 sstrncpy (max, "U", sizeof (max));
317 ssnprintf (max, sizeof (max), "%f", d->max);
319 status = ssnprintf (buffer, sizeof (buffer),
324 : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
326 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
329 ds_def[ds_num] = sstrdup (buffer);
330 } /* for ds_num = 0 .. ds->ds_num */
332 if (ds_num != ds->ds_num)
334 ds_free (ds_num, ds_def);
340 } /* }}} int ds_get */
342 #if HAVE_THREADSAFE_LIBRRD
343 static int srrd_create (const char *filename, /* {{{ */
344 unsigned long pdp_step, time_t last_up,
345 int argc, const char **argv)
350 if ((filename == NULL) || (argv == NULL))
353 /* Some versions of librrd don't have the `const' qualifier for the first
354 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
355 * but what else can we do? :( -octo */
356 filename_copy = strdup (filename);
357 if (filename_copy == NULL)
359 ERROR ("srrd_create: strdup failed.");
363 optind = 0; /* bug in librrd? */
366 status = rrd_create_r (filename_copy, pdp_step, last_up,
367 argc, (void *) argv);
371 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
372 filename, rrd_get_error ());
375 sfree (filename_copy);
378 } /* }}} int srrd_create */
379 /* #endif HAVE_THREADSAFE_LIBRRD */
381 #else /* !HAVE_THREADSAFE_LIBRRD */
382 static int srrd_create (const char *filename, /* {{{ */
383 unsigned long pdp_step, time_t last_up,
384 int argc, const char **argv)
391 char pdp_step_str[16];
392 char last_up_str[16];
395 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
396 if (new_argv == NULL)
398 ERROR ("rrdtool plugin: malloc failed.");
403 last_up = time (NULL) - 10;
405 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
406 ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
408 new_argv[0] = "create";
409 new_argv[1] = (void *) filename;
411 new_argv[3] = pdp_step_str;
413 new_argv[5] = last_up_str;
415 memcpy (new_argv + 6, argv, argc * sizeof (char *));
416 new_argv[new_argc] = NULL;
418 pthread_mutex_lock (&librrd_lock);
419 optind = 0; /* bug in librrd? */
422 status = rrd_create (new_argc, new_argv);
423 pthread_mutex_unlock (&librrd_lock);
427 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
428 filename, rrd_get_error ());
434 } /* }}} int srrd_create */
435 #endif /* !HAVE_THREADSAFE_LIBRRD */
437 static void *srrd_create_thread (void *targs)
439 srrd_create_args_t *args = targs;
442 status = srrd_create (args->filename, args->pdp_step, args->last_up,
443 args->argc, (void *) args->argv);
446 WARNING ("srrd_create_thread: srrd_create (%s) returned status %i.",
447 args->filename, status);
451 DEBUG ("srrd_create_thread: Successfully created RRD file \"%s\".",
455 srrd_create_args_destroy (args);
458 } /* void *srrd_create_thread */
460 static int srrd_create_async (const char *filename,
461 unsigned long pdp_step, time_t last_up,
462 int argc, const char **argv)
464 srrd_create_args_t *args;
469 args = srrd_create_args_create (filename, pdp_step, last_up, argc, argv);
473 status = pthread_attr_init (&attr);
476 srrd_create_args_destroy (args);
480 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
483 pthread_attr_destroy (&attr);
484 srrd_create_args_destroy (args);
488 status = pthread_create (&thread, &attr, srrd_create_thread, args);
492 ERROR ("srrd_create_async: pthread_create failed: %s",
493 sstrerror (status, errbuf, sizeof (errbuf)));
494 pthread_attr_destroy (&attr);
495 srrd_create_args_destroy (args);
499 pthread_attr_destroy (&attr);
500 /* args is freed in srrd_create_thread(). */
507 int cu_rrd_create_file (const char *filename, /* {{{ */
508 const data_set_t *ds, const value_list_t *vl,
509 const rrdcreate_config_t *cfg)
519 unsigned long stepsize;
521 if (check_create_dir (filename))
524 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
526 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
530 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
532 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
536 argc = ds_num + rra_num;
538 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
541 ERROR ("cu_rrd_create_file failed: %s",
542 sstrerror (errno, errbuf, sizeof (errbuf)));
546 memcpy (argv, ds_def, ds_num * sizeof (char *));
547 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
548 argv[ds_num + rra_num] = NULL;
550 last_up = CDTIME_T_TO_TIME_T (vl->time);
552 last_up = time (NULL);
555 if (cfg->stepsize > 0)
556 stepsize = cfg->stepsize;
558 stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
562 status = srrd_create_async (filename, stepsize, last_up,
563 argc, (const char **) argv);
565 WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
566 "returned status %i.",
569 else /* synchronous */
571 status = srrd_create (filename, stepsize, last_up,
572 argc, (const char **) argv);
576 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
581 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
587 ds_free (ds_num, ds_def);
588 rra_free (rra_num, rra_def);
591 } /* }}} int cu_rrd_create_file */
593 /* vim: set sw=2 sts=2 et fdm=marker : */