Merge branch 'collectd-4.10' into collectd-5.0
[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 <pthread.h>
27 #include <rrd.h>
28
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);
41
42 static char *rra_types[] =
43 {
44   "AVERAGE",
45   "MIN",
46   "MAX"
47 };
48 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
49
50 #if !defined(HAVE_THREADSAFE_LIBRRD) || !HAVE_THREADSAFE_LIBRRD
51 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
52 #endif
53
54 /*
55  * Private functions
56  */
57 static void rra_free (int rra_num, char **rra_def) /* {{{ */
58 {
59   int i;
60
61   for (i = 0; i < rra_num; i++)
62   {
63     sfree (rra_def[i]);
64   }
65   sfree (rra_def);
66 } /* }}} void rra_free */
67
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;
76
77   int *rts;
78   int  rts_num;
79
80   int rra_max;
81
82   int span;
83
84   int cdp_num;
85   int cdp_len;
86   int i, j;
87
88   char buffer[128];
89
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;
93
94   if (cfg->rrarows <= 0)
95   {
96     *ret = NULL;
97     return (-1);
98   }
99
100   if ((cfg->xff < 0) || (cfg->xff >= 1.0))
101   {
102     *ret = NULL;
103     return (-1);
104   }
105
106   if (cfg->stepsize > 0)
107     ss = cfg->stepsize;
108   else
109     ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
110   if (ss <= 0)
111   {
112     *ret = NULL;
113     return (-1);
114   }
115
116   /* Use the configured timespans or fall back to the built-in defaults */
117   if (cfg->timespans_num != 0)
118   {
119     rts = cfg->timespans;
120     rts_num = cfg->timespans_num;
121   }
122   else
123   {
124     rts = rra_timespans;
125     rts_num = rra_timespans_num;
126   }
127
128   rra_max = rts_num * rra_types_num;
129
130   if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
131     return (-1);
132   memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
133   rra_num = 0;
134
135   cdp_len = 0;
136   for (i = 0; i < rts_num; i++)
137   {
138     span = rts[i];
139
140     if ((span / ss) < cfg->rrarows)
141       span = ss * cfg->rrarows;
142
143     if (cdp_len == 0)
144       cdp_len = 1;
145     else
146       cdp_len = (int) floor (((double) span)
147           / ((double) (cfg->rrarows * ss)));
148
149     cdp_num = (int) ceil (((double) span)
150         / ((double) (cdp_len * ss)));
151
152     for (j = 0; j < rra_types_num; j++)
153     {
154       int status;
155
156       if (rra_num >= rra_max)
157         break;
158
159       status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
160           rra_types[j], cfg->xff, cdp_len, cdp_num);
161
162       if ((status < 0) || ((size_t) status >= sizeof (buffer)))
163       {
164         ERROR ("rra_get: Buffer would have been truncated.");
165         continue;
166       }
167
168       rra_def[rra_num++] = sstrdup (buffer);
169     }
170   }
171
172   *ret = rra_def;
173   return (rra_num);
174 } /* }}} int rra_get */
175
176 static void ds_free (int ds_num, char **ds_def) /* {{{ */
177 {
178   int i;
179
180   for (i = 0; i < ds_num; i++)
181     if (ds_def[i] != NULL)
182       free (ds_def[i]);
183   free (ds_def);
184 } /* }}} void ds_free */
185
186 static int ds_get (char ***ret, /* {{{ */
187     const data_set_t *ds, const value_list_t *vl,
188     const rrdcreate_config_t *cfg)
189 {
190   char **ds_def;
191   int ds_num;
192
193   char min[32];
194   char max[32];
195   char buffer[128];
196
197   ds_def = (char **) malloc (ds->ds_num * sizeof (char *));
198   if (ds_def == NULL)
199   {
200     char errbuf[1024];
201     ERROR ("rrdtool plugin: malloc failed: %s",
202         sstrerror (errno, errbuf, sizeof (errbuf)));
203     return (-1);
204   }
205   memset (ds_def, '\0', ds->ds_num * sizeof (char *));
206
207   for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
208   {
209     data_source_t *d = ds->ds + ds_num;
210     char *type;
211     int status;
212
213     ds_def[ds_num] = NULL;
214
215     if (d->type == DS_TYPE_COUNTER)
216       type = "COUNTER";
217     else if (d->type == DS_TYPE_GAUGE)
218       type = "GAUGE";
219     else if (d->type == DS_TYPE_DERIVE)
220       type = "DERIVE";
221     else if (d->type == DS_TYPE_ABSOLUTE)
222       type = "ABSOLUTE";
223     else
224     {
225       ERROR ("rrdtool plugin: Unknown DS type: %i",
226           d->type);
227       break;
228     }
229
230     if (isnan (d->min))
231     {
232       sstrncpy (min, "U", sizeof (min));
233     }
234     else
235       ssnprintf (min, sizeof (min), "%f", d->min);
236
237     if (isnan (d->max))
238     {
239       sstrncpy (max, "U", sizeof (max));
240     }
241     else
242       ssnprintf (max, sizeof (max), "%f", d->max);
243
244     status = ssnprintf (buffer, sizeof (buffer),
245         "DS:%s:%s:%i:%s:%s",
246         d->name, type,
247         (cfg->heartbeat > 0)
248         ? cfg->heartbeat
249         : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
250         min, max);
251     if ((status < 1) || ((size_t) status >= sizeof (buffer)))
252       break;
253
254     ds_def[ds_num] = sstrdup (buffer);
255   } /* for ds_num = 0 .. ds->ds_num */
256
257   if (ds_num != ds->ds_num)
258   {
259     ds_free (ds_num, ds_def);
260     return (-1);
261   }
262
263   *ret = ds_def;
264   return (ds_num);
265 } /* }}} int ds_get */
266
267 #if HAVE_THREADSAFE_LIBRRD
268 static int srrd_create (const char *filename, /* {{{ */
269     unsigned long pdp_step, time_t last_up,
270     int argc, const char **argv)
271 {
272   int status;
273   char *filename_copy;
274
275   if ((filename == NULL) || (argv == NULL))
276     return (-EINVAL);
277
278   /* Some versions of librrd don't have the `const' qualifier for the first
279    * argument, so we have to copy the pointer here to avoid warnings. It sucks,
280    * but what else can we do? :(  -octo */
281   filename_copy = strdup (filename);
282   if (filename_copy == NULL)
283   {
284     ERROR ("srrd_create: strdup failed.");
285     return (-ENOMEM);
286   }
287
288   optind = 0; /* bug in librrd? */
289   rrd_clear_error ();
290
291   status = rrd_create_r (filename_copy, pdp_step, last_up,
292       argc, (void *) argv);
293
294   if (status != 0)
295   {
296     WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
297         filename, rrd_get_error ());
298   }
299
300   sfree (filename_copy);
301
302   return (status);
303 } /* }}} int srrd_create */
304 /* #endif HAVE_THREADSAFE_LIBRRD */
305
306 #else /* !HAVE_THREADSAFE_LIBRRD */
307 static int srrd_create (const char *filename, /* {{{ */
308     unsigned long pdp_step, time_t last_up,
309     int argc, const char **argv)
310 {
311   int status;
312
313   int new_argc;
314   char **new_argv;
315
316   char pdp_step_str[16];
317   char last_up_str[16];
318
319   new_argc = 6 + argc;
320   new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
321   if (new_argv == NULL)
322   {
323     ERROR ("rrdtool plugin: malloc failed.");
324     return (-1);
325   }
326
327   if (last_up == 0)
328     last_up = time (NULL) - 10;
329
330   ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
331   ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
332
333   new_argv[0] = "create";
334   new_argv[1] = (void *) filename;
335   new_argv[2] = "-s";
336   new_argv[3] = pdp_step_str;
337   new_argv[4] = "-b";
338   new_argv[5] = last_up_str;
339
340   memcpy (new_argv + 6, argv, argc * sizeof (char *));
341   new_argv[new_argc] = NULL;
342
343   pthread_mutex_lock (&librrd_lock);
344   optind = 0; /* bug in librrd? */
345   rrd_clear_error ();
346
347   status = rrd_create (new_argc, new_argv);
348   pthread_mutex_unlock (&librrd_lock);
349
350   if (status != 0)
351   {
352     WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
353         filename, rrd_get_error ());
354   }
355
356   sfree (new_argv);
357
358   return (status);
359 } /* }}} int srrd_create */
360 #endif /* !HAVE_THREADSAFE_LIBRRD */
361
362 /*
363  * Public functions
364  */
365 int cu_rrd_create_file (const char *filename, /* {{{ */
366     const data_set_t *ds, const value_list_t *vl,
367     const rrdcreate_config_t *cfg)
368 {
369   char **argv;
370   int argc;
371   char **rra_def;
372   int rra_num;
373   char **ds_def;
374   int ds_num;
375   int status = 0;
376   time_t last_up;
377   unsigned long stepsize;
378
379   if (check_create_dir (filename))
380     return (-1);
381
382   if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
383   {
384     ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
385     return (-1);
386   }
387
388   if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
389   {
390     ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
391     return (-1);
392   }
393
394   argc = ds_num + rra_num;
395
396   if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
397   {
398     char errbuf[1024];
399     ERROR ("cu_rrd_create_file failed: %s",
400         sstrerror (errno, errbuf, sizeof (errbuf)));
401     return (-1);
402   }
403
404   memcpy (argv, ds_def, ds_num * sizeof (char *));
405   memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
406   argv[ds_num + rra_num] = NULL;
407
408   last_up = CDTIME_T_TO_TIME_T (vl->time);
409   if (last_up <= 10)
410     last_up = time (NULL);
411   last_up -= 10;
412
413   if (cfg->stepsize > 0)
414     stepsize = cfg->stepsize;
415   else
416     stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
417
418   status = srrd_create (filename, stepsize, last_up,
419       argc, (const char **) argv);
420
421   free (argv);
422   ds_free (ds_num, ds_def);
423   rra_free (rra_num, rra_def);
424
425   if (status != 0)
426   {
427     WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
428         filename, status);
429   }
430   else
431   {
432     DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
433         filename);
434   }
435
436   return (status);
437 } /* }}} int cu_rrd_create_file */
438
439 /* vim: set sw=2 sts=2 et fdm=marker : */