1 /**
2 * collectd - src/utils_rrdcreate.c
3 * Copyright (C) 2006-2008 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "utils_rrdcreate.h"
26 #include <pthread.h>
27 #include <rrd.h>
29 /*
30 * Private variables
31 */
32 static int rra_timespans[] =
33 {
34 3600,
35 86400,
36 604800,
37 2678400,
38 31622400
39 };
40 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
42 static char *rra_types[] =
43 {
44 "AVERAGE",
45 "MIN",
46 "MAX"
47 };
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;
52 #endif
54 /*
55 * Private functions
56 */
57 static void rra_free (int rra_num, char **rra_def) /* {{{ */
58 {
59 int i;
61 for (i = 0; i < rra_num; i++)
62 {
63 sfree (rra_def[i]);
64 }
65 sfree (rra_def);
66 } /* }}} void rra_free */
68 /* * * * * * * * * *
69 * WARNING: Magic *
70 * * * * * * * * * */
71 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
72 const rrdcreate_config_t *cfg)
73 {
74 char **rra_def;
75 int rra_num;
77 int *rts;
78 int rts_num;
80 int rra_max;
82 int span;
84 int cdp_num;
85 int cdp_len;
86 int i, j;
88 char buffer[128];
90 /* The stepsize we use here: If it is user-set, use it. If not, use the
91 * interval of the value-list. */
92 int ss;
94 if (cfg->rrarows <= 0)
95 {
96 *ret = NULL;
97 return (-1);
98 }
100 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
101 {
102 *ret = NULL;
103 return (-1);
104 }
106 ss = (cfg->stepsize > 0) ? cfg->stepsize : vl->interval;
107 if (ss <= 0)
108 {
109 *ret = NULL;
110 return (-1);
111 }
113 /* Use the configured timespans or fall back to the built-in defaults */
114 if (cfg->timespans_num != 0)
115 {
116 rts = cfg->timespans;
117 rts_num = cfg->timespans_num;
118 }
119 else
120 {
121 rts = rra_timespans;
122 rts_num = rra_timespans_num;
123 }
125 rra_max = rts_num * rra_types_num;
127 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
128 return (-1);
129 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
130 rra_num = 0;
132 cdp_len = 0;
133 for (i = 0; i < rts_num; i++)
134 {
135 span = rts[i];
137 if ((span / ss) < cfg->rrarows)
138 span = ss * cfg->rrarows;
140 if (cdp_len == 0)
141 cdp_len = 1;
142 else
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++)
150 {
151 int status;
153 if (rra_num >= rra_max)
154 break;
156 status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
157 rra_types[j], cfg->xff, cdp_len, cdp_num);
159 if ((status < 0) || ((size_t) status >= sizeof (buffer)))
160 {
161 ERROR ("rra_get: Buffer would have been truncated.");
162 continue;
163 }
165 rra_def[rra_num++] = sstrdup (buffer);
166 }
167 }
169 *ret = rra_def;
170 return (rra_num);
171 } /* }}} int rra_get */
173 static void ds_free (int ds_num, char **ds_def) /* {{{ */
174 {
175 int i;
177 for (i = 0; i < ds_num; i++)
178 if (ds_def[i] != NULL)
179 free (ds_def[i]);
180 free (ds_def);
181 } /* }}} void ds_free */
183 static int ds_get (char ***ret, /* {{{ */
184 const data_set_t *ds, const value_list_t *vl,
185 const rrdcreate_config_t *cfg)
186 {
187 char **ds_def;
188 int ds_num;
190 char min[32];
191 char max[32];
192 char buffer[128];
194 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
195 if (ds_def == NULL)
196 {
197 char errbuf[1024];
198 ERROR ("rrdtool plugin: malloc failed: %s",
199 sstrerror (errno, errbuf, sizeof (errbuf)));
200 return (-1);
201 }
202 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
204 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
205 {
206 data_source_t *d = ds->ds + ds_num;
207 char *type;
208 int status;
210 ds_def[ds_num] = NULL;
212 if (d->type == DS_TYPE_COUNTER)
213 type = "COUNTER";
214 else if (d->type == DS_TYPE_GAUGE)
215 type = "GAUGE";
216 else if (d->type == DS_TYPE_DERIVE)
217 type = "DERIVE";
218 else if (d->type == DS_TYPE_ABSOLUTE)
219 type = "ABSOLUTE";
220 else
221 {
222 ERROR ("rrdtool plugin: Unknown DS type: %i",
223 d->type);
224 break;
225 }
227 if (isnan (d->min))
228 {
229 sstrncpy (min, "U", sizeof (min));
230 }
231 else
232 ssnprintf (min, sizeof (min), "%f", d->min);
234 if (isnan (d->max))
235 {
236 sstrncpy (max, "U", sizeof (max));
237 }
238 else
239 ssnprintf (max, sizeof (max), "%f", d->max);
241 status = ssnprintf (buffer, sizeof (buffer),
242 "DS:%s:%s:%i:%s:%s",
243 d->name, type,
244 (cfg->heartbeat > 0) ? cfg->heartbeat : (2 * vl->interval),
245 min, max);
246 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
247 break;
249 ds_def[ds_num] = sstrdup (buffer);
250 } /* for ds_num = 0 .. ds->ds_num */
252 if (ds_num != ds->ds_num)
253 {
254 ds_free (ds_num, ds_def);
255 return (-1);
256 }
258 *ret = ds_def;
259 return (ds_num);
260 } /* }}} int ds_get */
262 #if HAVE_THREADSAFE_LIBRRD
263 static int srrd_create (const char *filename, /* {{{ */
264 unsigned long pdp_step, time_t last_up,
265 int argc, const char **argv)
266 {
267 int status;
268 char *filename_copy;
270 if ((filename == NULL) || (argv == NULL))
271 return (-EINVAL);
273 /* Some versions of librrd don't have the `const' qualifier for the first
274 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
275 * but what else can we do? :( -octo */
276 filename_copy = strdup (filename);
277 if (filename_copy == NULL)
278 {
279 ERROR ("srrd_create: strdup failed.");
280 return (-ENOMEM);
281 }
283 optind = 0; /* bug in librrd? */
284 rrd_clear_error ();
286 status = rrd_create_r (filename_copy, pdp_step, last_up,
287 argc, (void *) argv);
289 if (status != 0)
290 {
291 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
292 filename, rrd_get_error ());
293 }
295 sfree (filename_copy);
297 return (status);
298 } /* }}} int srrd_create */
299 /* #endif HAVE_THREADSAFE_LIBRRD */
301 #else /* !HAVE_THREADSAFE_LIBRRD */
302 static int srrd_create (const char *filename, /* {{{ */
303 unsigned long pdp_step, time_t last_up,
304 int argc, const char **argv)
305 {
306 int status;
308 int new_argc;
309 char **new_argv;
311 char pdp_step_str[16];
312 char last_up_str[16];
314 new_argc = 6 + argc;
315 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
316 if (new_argv == NULL)
317 {
318 ERROR ("rrdtool plugin: malloc failed.");
319 return (-1);
320 }
322 if (last_up == 0)
323 last_up = time (NULL) - 10;
325 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
326 ssnprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);
328 new_argv[0] = "create";
329 new_argv[1] = (void *) filename;
330 new_argv[2] = "-s";
331 new_argv[3] = pdp_step_str;
332 new_argv[4] = "-b";
333 new_argv[5] = last_up_str;
335 memcpy (new_argv + 6, argv, argc * sizeof (char *));
336 new_argv[new_argc] = NULL;
338 pthread_mutex_lock (&librrd_lock);
339 optind = 0; /* bug in librrd? */
340 rrd_clear_error ();
342 status = rrd_create (new_argc, new_argv);
343 pthread_mutex_unlock (&librrd_lock);
345 if (status != 0)
346 {
347 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
348 filename, rrd_get_error ());
349 }
351 sfree (new_argv);
353 return (status);
354 } /* }}} int srrd_create */
355 #endif /* !HAVE_THREADSAFE_LIBRRD */
357 /*
358 * Public functions
359 */
360 int cu_rrd_create_file (const char *filename, /* {{{ */
361 const data_set_t *ds, const value_list_t *vl,
362 const rrdcreate_config_t *cfg)
363 {
364 char **argv;
365 int argc;
366 char **rra_def;
367 int rra_num;
368 char **ds_def;
369 int ds_num;
370 int status = 0;
372 if (check_create_dir (filename))
373 return (-1);
375 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
376 {
377 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
378 return (-1);
379 }
381 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
382 {
383 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
384 return (-1);
385 }
387 argc = ds_num + rra_num;
389 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
390 {
391 char errbuf[1024];
392 ERROR ("cu_rrd_create_file failed: %s",
393 sstrerror (errno, errbuf, sizeof (errbuf)));
394 return (-1);
395 }
397 memcpy (argv, ds_def, ds_num * sizeof (char *));
398 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
399 argv[ds_num + rra_num] = NULL;
401 assert (vl->time > 10);
402 status = srrd_create (filename,
403 (cfg->stepsize > 0) ? cfg->stepsize : vl->interval,
404 vl->time - 10,
405 argc, (const char **) argv);
407 free (argv);
408 ds_free (ds_num, ds_def);
409 rra_free (rra_num, rra_def);
411 if (status != 0)
412 {
413 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
414 filename, status);
415 }
416 else
417 {
418 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
419 filename);
420 }
422 return (status);
423 } /* }}} int cu_rrd_create_file */
425 /* vim: set sw=2 sts=2 et fdm=marker : */