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 <rrd.h>
28 /*
29 * Private variables
30 */
31 static int rra_timespans[] =
32 {
33 3600,
34 86400,
35 604800,
36 2678400,
37 31622400
38 };
39 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
41 static char *rra_types[] =
42 {
43 "AVERAGE",
44 "MIN",
45 "MAX"
46 };
47 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
49 /*
50 * Private functions
51 */
52 static void rra_free (int rra_num, char **rra_def) /* {{{ */
53 {
54 int i;
56 for (i = 0; i < rra_num; i++)
57 {
58 sfree (rra_def[i]);
59 }
60 sfree (rra_def);
61 } /* }}} void rra_free */
63 /* * * * * * * * * *
64 * WARNING: Magic *
65 * * * * * * * * * */
66 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
67 const rrdcreate_config_t *cfg)
68 {
69 char **rra_def;
70 int rra_num;
72 int *rts;
73 int rts_num;
75 int rra_max;
77 int span;
79 int cdp_num;
80 int cdp_len;
81 int i, j;
83 char buffer[128];
85 /* The stepsize we use here: If it is user-set, use it. If not, use the
86 * interval of the value-list. */
87 int ss;
89 if (cfg->rrarows <= 0)
90 {
91 *ret = NULL;
92 return (-1);
93 }
95 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
96 {
97 *ret = NULL;
98 return (-1);
99 }
101 ss = (cfg->stepsize > 0) ? cfg->stepsize : vl->interval;
102 if (ss <= 0)
103 {
104 *ret = NULL;
105 return (-1);
106 }
108 /* Use the configured timespans or fall back to the built-in defaults */
109 if (cfg->timespans_num != 0)
110 {
111 rts = cfg->timespans;
112 rts_num = cfg->timespans_num;
113 }
114 else
115 {
116 rts = rra_timespans;
117 rts_num = rra_timespans_num;
118 }
120 rra_max = rts_num * rra_types_num;
122 if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
123 return (-1);
124 memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
125 rra_num = 0;
127 cdp_len = 0;
128 for (i = 0; i < rts_num; i++)
129 {
130 span = rts[i];
132 if ((span / ss) < cfg->rrarows)
133 span = ss * cfg->rrarows;
135 if (cdp_len == 0)
136 cdp_len = 1;
137 else
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++)
145 {
146 if (rra_num >= rra_max)
147 break;
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))
152 {
153 ERROR ("rra_get: Buffer would have been truncated.");
154 continue;
155 }
157 rra_def[rra_num++] = sstrdup (buffer);
158 }
159 }
161 *ret = rra_def;
162 return (rra_num);
163 } /* }}} int rra_get */
165 static void ds_free (int ds_num, char **ds_def) /* {{{ */
166 {
167 int i;
169 for (i = 0; i < ds_num; i++)
170 if (ds_def[i] != NULL)
171 free (ds_def[i]);
172 free (ds_def);
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)
178 {
179 char **ds_def;
180 int ds_num;
182 char min[32];
183 char max[32];
184 char buffer[128];
186 ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
187 if (ds_def == NULL)
188 {
189 char errbuf[1024];
190 ERROR ("rrdtool plugin: malloc failed: %s",
191 sstrerror (errno, errbuf, sizeof (errbuf)));
192 return (-1);
193 }
194 memset (ds_def, '\0', ds->ds_num * sizeof (char *));
196 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
197 {
198 data_source_t *d = ds->ds + ds_num;
199 char *type;
200 int status;
202 ds_def[ds_num] = NULL;
204 if (d->type == DS_TYPE_COUNTER)
205 type = "COUNTER";
206 else if (d->type == DS_TYPE_GAUGE)
207 type = "GAUGE";
208 else
209 {
210 ERROR ("rrdtool plugin: Unknown DS type: %i",
211 d->type);
212 break;
213 }
215 if (isnan (d->min))
216 {
217 sstrncpy (min, "U", sizeof (min));
218 }
219 else
220 ssnprintf (min, sizeof (min), "%lf", d->min);
222 if (isnan (d->max))
223 {
224 sstrncpy (max, "U", sizeof (max));
225 }
226 else
227 ssnprintf (max, sizeof (max), "%lf", d->max);
229 status = ssnprintf (buffer, sizeof (buffer),
230 "DS:%s:%s:%i:%s:%s",
231 d->name, type,
232 (cfg->heartbeat > 0) ? cfg->heartbeat : (2 * vl->interval),
233 min, max);
234 if ((status < 1) || (status >= sizeof (buffer)))
235 break;
237 ds_def[ds_num] = sstrdup (buffer);
238 } /* for ds_num = 0 .. ds->ds_num */
240 if (ds_num != ds->ds_num)
241 {
242 ds_free (ds_num, ds_def);
243 return (-1);
244 }
246 *ret = ds_def;
247 return (ds_num);
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)
254 {
255 int status;
256 char *filename_copy;
258 if ((filename == NULL) || (argv == NULL))
259 return (-EINVAL);
261 /* Some versions of librrd don't have the `const' qualifier for the first
262 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
263 * but what else can we do? :( -octo */
264 filename_copy = strdup (filename);
265 if (filename_copy == NULL)
266 {
267 ERROR ("srrd_create: strdup failed.");
268 return (-ENOMEM);
269 }
271 optind = 0; /* bug in librrd? */
272 rrd_clear_error ();
274 status = rrd_create_r (filename_copy, pdp_step, last_up,
275 argc, (void *) argv);
277 if (status != 0)
278 {
279 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
280 filename, rrd_get_error ());
281 }
283 sfree (filename_copy);
285 return (status);
286 } /* }}} int srrd_create */
287 /* #endif HAVE_THREADSAFE_LIBRRD */
289 #else /* !HAVE_THREADSAFE_LIBRRD */
290 static int srrd_create (const char *filename, /* {{{ */
291 unsigned long pdp_step, time_t last_up,
292 int argc, const char **argv)
293 {
294 int status;
296 int new_argc;
297 char **new_argv;
299 char pdp_step_str[16];
300 char last_up_str[16];
302 new_argc = 6 + argc;
303 new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
304 if (new_argv == NULL)
305 {
306 ERROR ("rrdtool plugin: malloc failed.");
307 return (-1);
308 }
310 if (last_up == 0)
311 last_up = time (NULL) - 10;
313 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
314 ssnprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);
316 new_argv[0] = "create";
317 new_argv[1] = filename;
318 new_argv[2] = "-s";
319 new_argv[3] = pdp_step_str;
320 new_argv[4] = "-b";
321 new_argv[5] = last_up_str;
323 memcpy (new_argv + 6, argv, argc * sizeof (char *));
324 new_argv[new_argc] = NULL;
326 pthread_mutex_lock (&librrd_lock);
327 optind = 0; /* bug in librrd? */
328 rrd_clear_error ();
330 status = rrd_create (new_argc, new_argv);
331 pthread_mutex_unlock (&librrd_lock);
333 if (status != 0)
334 {
335 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
336 filename, rrd_get_error ());
337 }
339 sfree (new_argv);
341 return (status);
342 } /* }}} int srrd_create */
343 #endif /* !HAVE_THREADSAFE_LIBRRD */
345 /*
346 * Public functions
347 */
348 int cu_rrd_create_file (const char *filename, /* {{{ */
349 const data_set_t *ds, const value_list_t *vl,
350 const rrdcreate_config_t *cfg)
351 {
352 char **argv;
353 int argc;
354 char **rra_def;
355 int rra_num;
356 char **ds_def;
357 int ds_num;
358 int status = 0;
360 if (check_create_dir (filename))
361 return (-1);
363 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
364 {
365 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
366 return (-1);
367 }
369 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
370 {
371 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
372 return (-1);
373 }
375 argc = ds_num + rra_num;
377 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
378 {
379 char errbuf[1024];
380 ERROR ("cu_rrd_create_file failed: %s",
381 sstrerror (errno, errbuf, sizeof (errbuf)));
382 return (-1);
383 }
385 memcpy (argv, ds_def, ds_num * sizeof (char *));
386 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
387 argv[ds_num + rra_num] = NULL;
389 assert (vl->time > 10);
390 status = srrd_create (filename,
391 (cfg->stepsize > 0) ? cfg->stepsize : vl->interval,
392 vl->time - 10,
393 argc, (const char **) argv);
395 free (argv);
396 ds_free (ds_num, ds_def);
397 rra_free (rra_num, rra_def);
399 if (status != 0)
400 {
401 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
402 filename, status);
403 }
404 else
405 {
406 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
407 filename);
408 }
410 return (status);
411 } /* }}} int cu_rrd_create_file */
413 /* vim: set sw=2 sts=2 et fdm=marker : */