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"
31 static int rra_timespans[] =
39 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
41 static char *rra_types[] =
47 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
52 static void rra_free (int rra_num, char **rra_def) /* {{{ */
56 for (i = 0; i < rra_num; i++)
61 } /* }}} void rra_free */
66 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
67 const rrdcreate_config_t *cfg)
85 /* The stepsize we use here: If it is user-set, use it. If not, use the
86 * interval of the value-list. */
89 if (cfg->rrarows <= 0)
95 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
101 ss = (cfg->stepsize > 0) ? cfg->stepsize : vl->interval;
108 /* Use the configured timespans or fall back to the built-in defaults */
109 if (cfg->timespans_num != 0)
111 rts = cfg->timespans;
112 rts_num = cfg->timespans_num;
117 rts_num = rra_timespans_num;
120 rra_max = rts_num * rra_types_num;
122 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
124 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
128 for (i = 0; i < rts_num; i++)
132 if ((span / ss) < cfg->rrarows)
133 span = ss * cfg->rrarows;
138 cdp_len = (int) floor (((double) span)
139 / ((double) (cfg->rrarows * ss)));
141 cdp_num = (int) ceil (((double) span)
142 / ((double) (cdp_len * ss)));
144 for (j = 0; j < rra_types_num; j++)
146 if (rra_num >= rra_max)
149 if (ssnprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
150 rra_types[j], cfg->xff,
151 cdp_len, cdp_num) >= sizeof (buffer))
153 ERROR ("rra_get: Buffer would have been truncated.");
157 rra_def[rra_num++] = sstrdup (buffer);
163 } /* }}} int rra_get */
165 static void ds_free (int ds_num, char **ds_def) /* {{{ */
169 for (i = 0; i < ds_num; i++)
170 if (ds_def[i] != NULL)
173 } /* }}} void ds_free */
175 static int ds_get (char ***ret, /* {{{ */
176 const data_set_t *ds, const value_list_t *vl,
177 const rrdcreate_config_t *cfg)
186 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
190 ERROR ("rrdtool plugin: malloc failed: %s",
191 sstrerror (errno, errbuf, sizeof (errbuf)));
194 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
196 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
198 data_source_t *d = ds->ds + ds_num;
202 ds_def[ds_num] = NULL;
204 if (d->type == DS_TYPE_COUNTER)
206 else if (d->type == DS_TYPE_GAUGE)
210 ERROR ("rrdtool plugin: Unknown DS type: %i",
217 sstrncpy (min, "U", sizeof (min));
220 ssnprintf (min, sizeof (min), "%lf", d->min);
224 sstrncpy (max, "U", sizeof (max));
227 ssnprintf (max, sizeof (max), "%lf", d->max);
229 status = ssnprintf (buffer, sizeof (buffer),
232 (cfg->heartbeat > 0) ? cfg->heartbeat : (2 * vl->interval),
234 if ((status < 1) || (status >= sizeof (buffer)))
237 ds_def[ds_num] = sstrdup (buffer);
238 } /* for ds_num = 0 .. ds->ds_num */
240 if (ds_num != ds->ds_num)
242 ds_free (ds_num, ds_def);
248 } /* }}} int ds_get */
250 #if HAVE_THREADSAFE_LIBRRD
251 static int srrd_create (const char *filename, /* {{{ */
252 unsigned long pdp_step, time_t last_up,
253 int argc, const char **argv)
257 optind = 0; /* bug in librrd? */
260 status = rrd_create_r (filename, pdp_step, last_up, argc, (void *) argv);
264 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
265 filename, rrd_get_error ());
269 } /* }}} int srrd_create */
270 /* #endif HAVE_THREADSAFE_LIBRRD */
272 #else /* !HAVE_THREADSAFE_LIBRRD */
273 static int srrd_create (const char *filename, /* {{{ */
274 unsigned long pdp_step, time_t last_up,
275 int argc, const char **argv)
282 char pdp_step_str[16];
283 char last_up_str[16];
286 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
287 if (new_argv == NULL)
289 ERROR ("rrdtool plugin: malloc failed.");
294 last_up = time (NULL) - 10;
296 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
297 ssnprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);
299 new_argv[0] = "create";
300 new_argv[1] = filename;
302 new_argv[3] = pdp_step_str;
304 new_argv[5] = last_up_str;
306 memcpy (new_argv + 6, argv, argc * sizeof (char *));
307 new_argv[new_argc] = NULL;
309 pthread_mutex_lock (&librrd_lock);
310 optind = 0; /* bug in librrd? */
313 status = rrd_create (new_argc, new_argv);
314 pthread_mutex_unlock (&librrd_lock);
318 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
319 filename, rrd_get_error ());
325 } /* }}} int srrd_create */
326 #endif /* !HAVE_THREADSAFE_LIBRRD */
331 int cu_rrd_create_file (const char *filename, /* {{{ */
332 const data_set_t *ds, const value_list_t *vl,
333 const rrdcreate_config_t *cfg)
343 if (check_create_dir (filename))
346 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
348 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
352 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
354 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
358 argc = ds_num + rra_num;
360 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
363 ERROR ("cu_rrd_create_file failed: %s",
364 sstrerror (errno, errbuf, sizeof (errbuf)));
368 memcpy (argv, ds_def, ds_num * sizeof (char *));
369 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
370 argv[ds_num + rra_num] = NULL;
372 assert (vl->time > 10);
373 status = srrd_create (filename,
374 (cfg->stepsize > 0) ? cfg->stepsize : vl->interval,
376 argc, (const char **) argv);
379 ds_free (ds_num, ds_def);
380 rra_free (rra_num, rra_def);
384 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
389 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
394 } /* }}} int cu_rrd_create_file */
396 /* vim: set sw=2 sts=2 et fdm=marker : */