2 * collectd - src/utils_rrdcreate.c
3 * Copyright (C) 2006-2008 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 verplant.org>
24 #include "utils_rrdcreate.h"
32 static int rra_timespans[] =
40 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
42 static char *rra_types[] =
48 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
50 #if !defined(HAVE_THREADSAFE_LIBRRD) || !HAVE_THREADSAFE_LIBRRD
51 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
57 static void rra_free (int rra_num, char **rra_def) /* {{{ */
61 for (i = 0; i < rra_num; i++)
66 } /* }}} void rra_free */
71 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
72 const rrdcreate_config_t *cfg)
90 /* The stepsize we use here: If it is user-set, use it. If not, use the
91 * interval of the value-list. */
94 if (cfg->rrarows <= 0)
100 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
106 if (cfg->stepsize > 0)
109 ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
116 /* Use the configured timespans or fall back to the built-in defaults */
117 if (cfg->timespans_num != 0)
119 rts = cfg->timespans;
120 rts_num = cfg->timespans_num;
125 rts_num = rra_timespans_num;
128 rra_max = rts_num * rra_types_num;
130 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
132 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
136 for (i = 0; i < rts_num; i++)
140 if ((span / ss) < cfg->rrarows)
141 span = ss * cfg->rrarows;
146 cdp_len = (int) floor (((double) span)
147 / ((double) (cfg->rrarows * ss)));
149 cdp_num = (int) ceil (((double) span)
150 / ((double) (cdp_len * ss)));
152 for (j = 0; j < rra_types_num; j++)
156 if (rra_num >= rra_max)
159 status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
160 rra_types[j], cfg->xff, cdp_len, cdp_num);
162 if ((status < 0) || ((size_t) status >= sizeof (buffer)))
164 ERROR ("rra_get: Buffer would have been truncated.");
168 rra_def[rra_num++] = sstrdup (buffer);
174 } /* }}} int rra_get */
176 static void ds_free (int ds_num, char **ds_def) /* {{{ */
180 for (i = 0; i < ds_num; i++)
181 if (ds_def[i] != NULL)
184 } /* }}} void ds_free */
186 static int ds_get (char ***ret, /* {{{ */
187 const data_set_t *ds, const value_list_t *vl,
188 const rrdcreate_config_t *cfg)
197 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
201 ERROR ("rrdtool plugin: malloc failed: %s",
202 sstrerror (errno, errbuf, sizeof (errbuf)));
205 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
207 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
209 data_source_t *d = ds->ds + ds_num;
213 ds_def[ds_num] = NULL;
215 if (d->type == DS_TYPE_COUNTER)
217 else if (d->type == DS_TYPE_GAUGE)
219 else if (d->type == DS_TYPE_DERIVE)
221 else if (d->type == DS_TYPE_ABSOLUTE)
225 ERROR ("rrdtool plugin: Unknown DS type: %i",
232 sstrncpy (min, "U", sizeof (min));
235 ssnprintf (min, sizeof (min), "%f", d->min);
239 sstrncpy (max, "U", sizeof (max));
242 ssnprintf (max, sizeof (max), "%f", d->max);
244 status = ssnprintf (buffer, sizeof (buffer),
249 : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
251 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
254 ds_def[ds_num] = sstrdup (buffer);
255 } /* for ds_num = 0 .. ds->ds_num */
257 if (ds_num != ds->ds_num)
259 ds_free (ds_num, ds_def);
265 } /* }}} int ds_get */
267 #if HAVE_THREADSAFE_LIBRRD
268 static int srrd_create (const char *filename, /* {{{ */
269 unsigned long pdp_step, time_t last_up,
270 int argc, const char **argv)
275 if ((filename == NULL) || (argv == NULL))
278 /* Some versions of librrd don't have the `const' qualifier for the first
279 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
280 * but what else can we do? :( -octo */
281 filename_copy = strdup (filename);
282 if (filename_copy == NULL)
284 ERROR ("srrd_create: strdup failed.");
288 optind = 0; /* bug in librrd? */
291 status = rrd_create_r (filename_copy, pdp_step, last_up,
292 argc, (void *) argv);
296 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
297 filename, rrd_get_error ());
300 sfree (filename_copy);
303 } /* }}} int srrd_create */
304 /* #endif HAVE_THREADSAFE_LIBRRD */
306 #else /* !HAVE_THREADSAFE_LIBRRD */
307 static int srrd_create (const char *filename, /* {{{ */
308 unsigned long pdp_step, time_t last_up,
309 int argc, const char **argv)
316 char pdp_step_str[16];
317 char last_up_str[16];
320 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
321 if (new_argv == NULL)
323 ERROR ("rrdtool plugin: malloc failed.");
328 last_up = time (NULL) - 10;
330 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
331 ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
333 new_argv[0] = "create";
334 new_argv[1] = (void *) filename;
336 new_argv[3] = pdp_step_str;
338 new_argv[5] = last_up_str;
340 memcpy (new_argv + 6, argv, argc * sizeof (char *));
341 new_argv[new_argc] = NULL;
343 pthread_mutex_lock (&librrd_lock);
344 optind = 0; /* bug in librrd? */
347 status = rrd_create (new_argc, new_argv);
348 pthread_mutex_unlock (&librrd_lock);
352 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
353 filename, rrd_get_error ());
359 } /* }}} int srrd_create */
360 #endif /* !HAVE_THREADSAFE_LIBRRD */
365 int cu_rrd_create_file (const char *filename, /* {{{ */
366 const data_set_t *ds, const value_list_t *vl,
367 const rrdcreate_config_t *cfg)
377 unsigned long stepsize;
379 if (check_create_dir (filename))
382 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
384 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
388 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
390 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
394 argc = ds_num + rra_num;
396 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
399 ERROR ("cu_rrd_create_file failed: %s",
400 sstrerror (errno, errbuf, sizeof (errbuf)));
404 memcpy (argv, ds_def, ds_num * sizeof (char *));
405 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
406 argv[ds_num + rra_num] = NULL;
408 last_up = CDTIME_T_TO_TIME_T (vl->time);
410 last_up = time (NULL);
413 if (cfg->stepsize > 0)
414 stepsize = cfg->stepsize;
416 stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
418 status = srrd_create (filename, stepsize, last_up,
419 argc, (const char **) argv);
422 ds_free (ds_num, ds_def);
423 rra_free (rra_num, rra_def);
427 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
432 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
437 } /* }}} int cu_rrd_create_file */
439 /* vim: set sw=2 sts=2 et fdm=marker : */