Merge branch 'collectd-4.5'
[collectd.git] / src / utils_rrdcreate.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "utils_rrdcreate.h"
25
26 #include <rrd.h>
27
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);
40
41 static char *rra_types[] =
42 {
43   "AVERAGE",
44   "MIN",
45   "MAX"
46 };
47 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
48
49 /*
50  * Private functions
51  */
52 static void rra_free (int rra_num, char **rra_def) /* {{{ */
53 {
54   int i;
55
56   for (i = 0; i < rra_num; i++)
57   {
58     sfree (rra_def[i]);
59   }
60   sfree (rra_def);
61 } /* }}} void rra_free */
62
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;
71
72   int *rts;
73   int  rts_num;
74
75   int rra_max;
76
77   int span;
78
79   int cdp_num;
80   int cdp_len;
81   int i, j;
82
83   char buffer[128];
84
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;
88
89   if (cfg->rrarows <= 0)
90   {
91     *ret = NULL;
92     return (-1);
93   }
94
95   if ((cfg->xff < 0) || (cfg->xff >= 1.0))
96   {
97     *ret = NULL;
98     return (-1);
99   }
100
101   ss = (cfg->stepsize > 0) ? cfg->stepsize : vl->interval;
102   if (ss <= 0)
103   {
104     *ret = NULL;
105     return (-1);
106   }
107
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   }
119
120   rra_max = rts_num * rra_types_num;
121
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;
126
127   cdp_len = 0;
128   for (i = 0; i < rts_num; i++)
129   {
130     span = rts[i];
131
132     if ((span / ss) < cfg->rrarows)
133       span = ss * cfg->rrarows;
134
135     if (cdp_len == 0)
136       cdp_len = 1;
137     else
138       cdp_len = (int) floor (((double) span)
139           / ((double) (cfg->rrarows * ss)));
140
141     cdp_num = (int) ceil (((double) span)
142         / ((double) (cdp_len * ss)));
143
144     for (j = 0; j < rra_types_num; j++)
145     {
146       if (rra_num >= rra_max)
147         break;
148
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       }
156
157       rra_def[rra_num++] = sstrdup (buffer);
158     }
159   }
160
161   *ret = rra_def;
162   return (rra_num);
163 } /* }}} int rra_get */
164
165 static void ds_free (int ds_num, char **ds_def) /* {{{ */
166 {
167   int i;
168
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 */
174
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;
181
182   char min[32];
183   char max[32];
184   char buffer[128];
185
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 *));
195
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;
201
202     ds_def[ds_num] = NULL;
203
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     }
214
215     if (isnan (d->min))
216     {
217       sstrncpy (min, "U", sizeof (min));
218     }
219     else
220       ssnprintf (min, sizeof (min), "%lf", d->min);
221
222     if (isnan (d->max))
223     {
224       sstrncpy (max, "U", sizeof (max));
225     }
226     else
227       ssnprintf (max, sizeof (max), "%lf", d->max);
228
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;
236
237     ds_def[ds_num] = sstrdup (buffer);
238   } /* for ds_num = 0 .. ds->ds_num */
239
240   if (ds_num != ds->ds_num)
241   {
242     ds_free (ds_num, ds_def);
243     return (-1);
244   }
245
246   *ret = ds_def;
247   return (ds_num);
248 } /* }}} int ds_get */
249
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
257   optind = 0; /* bug in librrd? */
258   rrd_clear_error ();
259
260   status = rrd_create_r (filename, pdp_step, last_up, argc, (void *) argv);
261
262   if (status != 0)
263   {
264     WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
265         filename, rrd_get_error ());
266   }
267
268   return (status);
269 } /* }}} int srrd_create */
270 /* #endif HAVE_THREADSAFE_LIBRRD */
271
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)
276 {
277   int status;
278
279   int new_argc;
280   char **new_argv;
281
282   char pdp_step_str[16];
283   char last_up_str[16];
284
285   new_argc = 6 + argc;
286   new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
287   if (new_argv == NULL)
288   {
289     ERROR ("rrdtool plugin: malloc failed.");
290     return (-1);
291   }
292
293   if (last_up == 0)
294     last_up = time (NULL) - 10;
295
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);
298
299   new_argv[0] = "create";
300   new_argv[1] = filename;
301   new_argv[2] = "-s";
302   new_argv[3] = pdp_step_str;
303   new_argv[4] = "-b";
304   new_argv[5] = last_up_str;
305
306   memcpy (new_argv + 6, argv, argc * sizeof (char *));
307   new_argv[new_argc] = NULL;
308
309   pthread_mutex_lock (&librrd_lock);
310   optind = 0; /* bug in librrd? */
311   rrd_clear_error ();
312
313   status = rrd_create (new_argc, new_argv);
314   pthread_mutex_unlock (&librrd_lock);
315
316   if (status != 0)
317   {
318     WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
319         filename, rrd_get_error ());
320   }
321
322   sfree (new_argv);
323
324   return (status);
325 } /* }}} int srrd_create */
326 #endif /* !HAVE_THREADSAFE_LIBRRD */
327
328 /*
329  * Public functions
330  */
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)
334 {
335   char **argv;
336   int argc;
337   char **rra_def;
338   int rra_num;
339   char **ds_def;
340   int ds_num;
341   int status = 0;
342
343   if (check_create_dir (filename))
344     return (-1);
345
346   if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
347   {
348     ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
349     return (-1);
350   }
351
352   if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
353   {
354     ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
355     return (-1);
356   }
357
358   argc = ds_num + rra_num;
359
360   if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
361   {
362     char errbuf[1024];
363     ERROR ("cu_rrd_create_file failed: %s",
364         sstrerror (errno, errbuf, sizeof (errbuf)));
365     return (-1);
366   }
367
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;
371
372   assert (vl->time > 10);
373   status = srrd_create (filename,
374       (cfg->stepsize > 0) ? cfg->stepsize : vl->interval,
375       vl->time - 10,
376       argc, (const char **) argv);
377
378   free (argv);
379   ds_free (ds_num, ds_def);
380   rra_free (rra_num, rra_def);
381
382   if (status != 0)
383   {
384     WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
385         filename, status);
386   }
387   else
388   {
389     DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
390         filename);
391   }
392
393   return (status);
394 } /* }}} int cu_rrd_create_file */
395
396 /* vim: set sw=2 sts=2 et fdm=marker : */