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 ss = (cfg->stepsize > 0) ? cfg->stepsize : vl->interval;
113 /* Use the configured timespans or fall back to the built-in defaults */
114 if (cfg->timespans_num != 0)
116 rts = cfg->timespans;
117 rts_num = cfg->timespans_num;
122 rts_num = rra_timespans_num;
125 rra_max = rts_num * rra_types_num;
127 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
129 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
133 for (i = 0; i < rts_num; i++)
137 if ((span / ss) < cfg->rrarows)
138 span = ss * cfg->rrarows;
143 cdp_len = (int) floor (((double) span)
144 / ((double) (cfg->rrarows * ss)));
146 cdp_num = (int) ceil (((double) span)
147 / ((double) (cdp_len * ss)));
149 for (j = 0; j < rra_types_num; j++)
151 if (rra_num >= rra_max)
154 if (ssnprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
155 rra_types[j], cfg->xff,
156 cdp_len, cdp_num) >= sizeof (buffer))
158 ERROR ("rra_get: Buffer would have been truncated.");
162 rra_def[rra_num++] = sstrdup (buffer);
168 } /* }}} int rra_get */
170 static void ds_free (int ds_num, char **ds_def) /* {{{ */
174 for (i = 0; i < ds_num; i++)
175 if (ds_def[i] != NULL)
178 } /* }}} void ds_free */
180 static int ds_get (char ***ret, /* {{{ */
181 const data_set_t *ds, const value_list_t *vl,
182 const rrdcreate_config_t *cfg)
191 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
195 ERROR ("rrdtool plugin: malloc failed: %s",
196 sstrerror (errno, errbuf, sizeof (errbuf)));
199 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
201 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
203 data_source_t *d = ds->ds + ds_num;
207 ds_def[ds_num] = NULL;
209 if (d->type == DS_TYPE_COUNTER)
211 else if (d->type == DS_TYPE_GAUGE)
215 ERROR ("rrdtool plugin: Unknown DS type: %i",
222 sstrncpy (min, "U", sizeof (min));
225 ssnprintf (min, sizeof (min), "%lf", d->min);
229 sstrncpy (max, "U", sizeof (max));
232 ssnprintf (max, sizeof (max), "%lf", d->max);
234 status = ssnprintf (buffer, sizeof (buffer),
237 (cfg->heartbeat > 0) ? cfg->heartbeat : (2 * vl->interval),
239 if ((status < 1) || (status >= sizeof (buffer)))
242 ds_def[ds_num] = sstrdup (buffer);
243 } /* for ds_num = 0 .. ds->ds_num */
245 if (ds_num != ds->ds_num)
247 ds_free (ds_num, ds_def);
253 } /* }}} int ds_get */
255 #if HAVE_THREADSAFE_LIBRRD
256 static int srrd_create (const char *filename, /* {{{ */
257 unsigned long pdp_step, time_t last_up,
258 int argc, const char **argv)
263 if ((filename == NULL) || (argv == NULL))
266 /* Some versions of librrd don't have the `const' qualifier for the first
267 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
268 * but what else can we do? :( -octo */
269 filename_copy = strdup (filename);
270 if (filename_copy == NULL)
272 ERROR ("srrd_create: strdup failed.");
276 optind = 0; /* bug in librrd? */
279 status = rrd_create_r (filename_copy, pdp_step, last_up,
280 argc, (void *) argv);
284 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
285 filename, rrd_get_error ());
288 sfree (filename_copy);
291 } /* }}} int srrd_create */
292 /* #endif HAVE_THREADSAFE_LIBRRD */
294 #else /* !HAVE_THREADSAFE_LIBRRD */
295 static int srrd_create (const char *filename, /* {{{ */
296 unsigned long pdp_step, time_t last_up,
297 int argc, const char **argv)
304 char pdp_step_str[16];
305 char last_up_str[16];
308 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
309 if (new_argv == NULL)
311 ERROR ("rrdtool plugin: malloc failed.");
316 last_up = time (NULL) - 10;
318 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
319 ssnprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);
321 new_argv[0] = "create";
322 new_argv[1] = (void *) filename;
324 new_argv[3] = pdp_step_str;
326 new_argv[5] = last_up_str;
328 memcpy (new_argv + 6, argv, argc * sizeof (char *));
329 new_argv[new_argc] = NULL;
331 pthread_mutex_lock (&librrd_lock);
332 optind = 0; /* bug in librrd? */
335 status = rrd_create (new_argc, new_argv);
336 pthread_mutex_unlock (&librrd_lock);
340 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
341 filename, rrd_get_error ());
347 } /* }}} int srrd_create */
348 #endif /* !HAVE_THREADSAFE_LIBRRD */
353 int cu_rrd_create_file (const char *filename, /* {{{ */
354 const data_set_t *ds, const value_list_t *vl,
355 const rrdcreate_config_t *cfg)
365 if (check_create_dir (filename))
368 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
370 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
374 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
376 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
380 argc = ds_num + rra_num;
382 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
385 ERROR ("cu_rrd_create_file failed: %s",
386 sstrerror (errno, errbuf, sizeof (errbuf)));
390 memcpy (argv, ds_def, ds_num * sizeof (char *));
391 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
392 argv[ds_num + rra_num] = NULL;
394 assert (vl->time > 10);
395 status = srrd_create (filename,
396 (cfg->stepsize > 0) ? cfg->stepsize : vl->interval,
398 argc, (const char **) argv);
401 ds_free (ds_num, ds_def);
402 rra_free (rra_num, rra_def);
406 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
411 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
416 } /* }}} int cu_rrd_create_file */
418 /* vim: set sw=2 sts=2 et fdm=marker : */