Merge pull request #1596 from rubenk/fix-a-few-more-prototypes
[collectd.git] / src / utils_rrdcreate.c
1 /**
2  * collectd - src/utils_rrdcreate.c
3  * Copyright (C) 2006-2013  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 collectd.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 struct srrd_create_args_s
30 {
31   char *filename;
32   unsigned long pdp_step;
33   time_t last_up;
34   int argc;
35   char **argv;
36 };
37 typedef struct srrd_create_args_s srrd_create_args_t;
38
39 struct async_create_file_s;
40 typedef struct async_create_file_s async_create_file_t;
41 struct async_create_file_s
42 {
43   char *filename;
44   async_create_file_t *next;
45 };
46
47 /*
48  * Private variables
49  */
50 static int rra_timespans[] =
51 {
52   3600,
53   86400,
54   604800,
55   2678400,
56   31622400
57 };
58 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
59
60 static char *rra_types[] =
61 {
62   "AVERAGE",
63   "MIN",
64   "MAX"
65 };
66 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
67
68 #if !defined(HAVE_THREADSAFE_LIBRRD) || !HAVE_THREADSAFE_LIBRRD
69 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
70 #endif
71
72 static async_create_file_t *async_creation_list = NULL;
73 static pthread_mutex_t async_creation_lock = PTHREAD_MUTEX_INITIALIZER;
74
75 /*
76  * Private functions
77  */
78 static void rra_free (int rra_num, char **rra_def) /* {{{ */
79 {
80   int i;
81
82   for (i = 0; i < rra_num; i++)
83   {
84     sfree (rra_def[i]);
85   }
86   sfree (rra_def);
87 } /* }}} void rra_free */
88
89 static void srrd_create_args_destroy (srrd_create_args_t *args)
90 {
91   if (args == NULL)
92     return;
93
94   sfree (args->filename);
95   if (args->argv != NULL)
96   {
97     int i;
98     for (i = 0; i < args->argc; i++)
99       sfree (args->argv[i]);
100     sfree (args->argv);
101   }
102   sfree (args);
103 } /* void srrd_create_args_destroy */
104
105 static srrd_create_args_t *srrd_create_args_create (const char *filename,
106     unsigned long pdp_step, time_t last_up,
107     int argc, const char **argv)
108 {
109   srrd_create_args_t *args;
110
111   args = malloc (sizeof (*args));
112   if (args == NULL)
113   {
114     ERROR ("srrd_create_args_create: malloc failed.");
115     return (NULL);
116   }
117   memset (args, 0, sizeof (*args));
118   args->filename = NULL;
119   args->pdp_step = pdp_step;
120   args->last_up = last_up;
121   args->argv = NULL;
122
123   args->filename = strdup (filename);
124   if (args->filename == NULL)
125   {
126     ERROR ("srrd_create_args_create: strdup failed.");
127     srrd_create_args_destroy (args);
128     return (NULL);
129   }
130
131   args->argv = calloc ((size_t) (argc + 1), sizeof (*args->argv));
132   if (args->argv == NULL)
133   {
134     ERROR ("srrd_create_args_create: calloc failed.");
135     srrd_create_args_destroy (args);
136     return (NULL);
137   }
138
139   for (args->argc = 0; args->argc < argc; args->argc++)
140   {
141     args->argv[args->argc] = strdup (argv[args->argc]);
142     if (args->argv[args->argc] == NULL)
143     {
144       ERROR ("srrd_create_args_create: strdup failed.");
145       srrd_create_args_destroy (args);
146       return (NULL);
147     }
148   }
149   assert (args->argc == argc);
150   args->argv[args->argc] = NULL;
151
152   return (args);
153 } /* srrd_create_args_t *srrd_create_args_create */
154
155 /* * * * * * * * * *
156  * WARNING:  Magic *
157  * * * * * * * * * */
158 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
159     const rrdcreate_config_t *cfg)
160 {
161   char **rra_def;
162   int rra_num;
163
164   int *rts;
165   int  rts_num;
166
167   int rra_max;
168
169   int cdp_num;
170   int cdp_len;
171   int i, j;
172
173   /* The stepsize we use here: If it is user-set, use it. If not, use the
174    * interval of the value-list. */
175   int ss;
176
177   if (cfg->rrarows <= 0)
178   {
179     *ret = NULL;
180     return (-1);
181   }
182
183   if ((cfg->xff < 0) || (cfg->xff >= 1.0))
184   {
185     *ret = NULL;
186     return (-1);
187   }
188
189   if (cfg->stepsize > 0)
190     ss = cfg->stepsize;
191   else
192     ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
193   if (ss <= 0)
194   {
195     *ret = NULL;
196     return (-1);
197   }
198
199   /* Use the configured timespans or fall back to the built-in defaults */
200   if (cfg->timespans_num != 0)
201   {
202     rts = cfg->timespans;
203     rts_num = cfg->timespans_num;
204   }
205   else
206   {
207     rts = rra_timespans;
208     rts_num = rra_timespans_num;
209   }
210
211   rra_max = rts_num * rra_types_num;
212   assert (rra_max > 0);
213
214   if ((rra_def = malloc ((rra_max + 1) * sizeof (*rra_def))) == NULL)
215     return (-1);
216   memset (rra_def, 0, (rra_max + 1) * sizeof (*rra_def));
217   rra_num = 0;
218
219   cdp_len = 0;
220   for (i = 0; i < rts_num; i++)
221   {
222     int span = rts[i];
223
224     if ((span / ss) < cfg->rrarows)
225       span = ss * cfg->rrarows;
226
227     if (cdp_len == 0)
228       cdp_len = 1;
229     else
230       cdp_len = (int) floor (((double) span)
231           / ((double) (cfg->rrarows * ss)));
232
233     cdp_num = (int) ceil (((double) span)
234         / ((double) (cdp_len * ss)));
235
236     for (j = 0; j < rra_types_num; j++)
237     {
238       char buffer[128];
239       int status;
240
241       if (rra_num >= rra_max)
242         break;
243
244       status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
245           rra_types[j], cfg->xff, cdp_len, cdp_num);
246
247       if ((status < 0) || ((size_t) status >= sizeof (buffer)))
248       {
249         ERROR ("rra_get: Buffer would have been truncated.");
250         continue;
251       }
252
253       rra_def[rra_num++] = sstrdup (buffer);
254     }
255   }
256
257   if (rra_num <= 0)
258   {
259     sfree (rra_def);
260     return (0);
261   }
262
263   *ret = rra_def;
264   return (rra_num);
265 } /* }}} int rra_get */
266
267 static void ds_free (int ds_num, char **ds_def) /* {{{ */
268 {
269   int i;
270
271   for (i = 0; i < ds_num; i++)
272     if (ds_def[i] != NULL)
273       free (ds_def[i]);
274   free (ds_def);
275 } /* }}} void ds_free */
276
277 static int ds_get (char ***ret, /* {{{ */
278     const data_set_t *ds, const value_list_t *vl,
279     const rrdcreate_config_t *cfg)
280 {
281   char **ds_def;
282   int ds_num;
283
284   char min[32];
285   char max[32];
286   char buffer[128];
287
288   assert (ds->ds_num > 0);
289
290   ds_def = malloc (ds->ds_num * sizeof (*ds_def));
291   if (ds_def == NULL)
292   {
293     char errbuf[1024];
294     ERROR ("rrdtool plugin: malloc failed: %s",
295         sstrerror (errno, errbuf, sizeof (errbuf)));
296     return (-1);
297   }
298   memset (ds_def, 0, ds->ds_num * sizeof (*ds_def));
299
300   for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
301   {
302     data_source_t *d = ds->ds + ds_num;
303     char *type;
304     int status;
305
306     ds_def[ds_num] = NULL;
307
308     if (d->type == DS_TYPE_COUNTER)
309       type = "COUNTER";
310     else if (d->type == DS_TYPE_GAUGE)
311       type = "GAUGE";
312     else if (d->type == DS_TYPE_DERIVE)
313       type = "DERIVE";
314     else if (d->type == DS_TYPE_ABSOLUTE)
315       type = "ABSOLUTE";
316     else
317     {
318       ERROR ("rrdtool plugin: Unknown DS type: %i",
319           d->type);
320       break;
321     }
322
323     if (isnan (d->min))
324     {
325       sstrncpy (min, "U", sizeof (min));
326     }
327     else
328       ssnprintf (min, sizeof (min), "%f", d->min);
329
330     if (isnan (d->max))
331     {
332       sstrncpy (max, "U", sizeof (max));
333     }
334     else
335       ssnprintf (max, sizeof (max), "%f", d->max);
336
337     status = ssnprintf (buffer, sizeof (buffer),
338         "DS:%s:%s:%i:%s:%s",
339         d->name, type,
340         (cfg->heartbeat > 0)
341         ? cfg->heartbeat
342         : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
343         min, max);
344     if ((status < 1) || ((size_t) status >= sizeof (buffer)))
345       break;
346
347     ds_def[ds_num] = sstrdup (buffer);
348   } /* for ds_num = 0 .. ds->ds_num */
349
350   if (ds_num != ds->ds_num)
351   {
352     ds_free (ds_num, ds_def);
353     return (-1);
354   }
355
356   if (ds_num <= 0)
357   {
358     sfree (ds_def);
359     return (0);
360   }
361
362   *ret = ds_def;
363   return (ds_num);
364 } /* }}} int ds_get */
365
366 #if HAVE_THREADSAFE_LIBRRD
367 static int srrd_create (const char *filename, /* {{{ */
368     unsigned long pdp_step, time_t last_up,
369     int argc, const char **argv)
370 {
371   int status;
372   char *filename_copy;
373
374   if ((filename == NULL) || (argv == NULL))
375     return (-EINVAL);
376
377   /* Some versions of librrd don't have the `const' qualifier for the first
378    * argument, so we have to copy the pointer here to avoid warnings. It sucks,
379    * but what else can we do? :(  -octo */
380   filename_copy = strdup (filename);
381   if (filename_copy == NULL)
382   {
383     ERROR ("srrd_create: strdup failed.");
384     return (-ENOMEM);
385   }
386
387   optind = 0; /* bug in librrd? */
388   rrd_clear_error ();
389
390   status = rrd_create_r (filename_copy, pdp_step, last_up,
391       argc, (void *) argv);
392
393   if (status != 0)
394   {
395     WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
396         filename, rrd_get_error ());
397   }
398
399   sfree (filename_copy);
400
401   return (status);
402 } /* }}} int srrd_create */
403 /* #endif HAVE_THREADSAFE_LIBRRD */
404
405 #else /* !HAVE_THREADSAFE_LIBRRD */
406 static int srrd_create (const char *filename, /* {{{ */
407     unsigned long pdp_step, time_t last_up,
408     int argc, const char **argv)
409 {
410   int status;
411
412   int new_argc;
413   char **new_argv;
414
415   char pdp_step_str[16];
416   char last_up_str[16];
417
418   new_argc = 6 + argc;
419   new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
420   if (new_argv == NULL)
421   {
422     ERROR ("rrdtool plugin: malloc failed.");
423     return (-1);
424   }
425
426   if (last_up == 0)
427     last_up = time (NULL) - 10;
428
429   ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
430   ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
431
432   new_argv[0] = "create";
433   new_argv[1] = (void *) filename;
434   new_argv[2] = "-s";
435   new_argv[3] = pdp_step_str;
436   new_argv[4] = "-b";
437   new_argv[5] = last_up_str;
438
439   memcpy (new_argv + 6, argv, argc * sizeof (char *));
440   new_argv[new_argc] = NULL;
441
442   pthread_mutex_lock (&librrd_lock);
443   optind = 0; /* bug in librrd? */
444   rrd_clear_error ();
445
446   status = rrd_create (new_argc, new_argv);
447   pthread_mutex_unlock (&librrd_lock);
448
449   if (status != 0)
450   {
451     WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
452         filename, rrd_get_error ());
453   }
454
455   sfree (new_argv);
456
457   return (status);
458 } /* }}} int srrd_create */
459 #endif /* !HAVE_THREADSAFE_LIBRRD */
460
461 static int lock_file (char const *filename) /* {{{ */
462 {
463   async_create_file_t *ptr;
464   struct stat sb;
465   int status;
466
467   pthread_mutex_lock (&async_creation_lock);
468
469   for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
470     if (strcmp (filename, ptr->filename) == 0)
471       break;
472
473   if (ptr != NULL)
474   {
475     pthread_mutex_unlock (&async_creation_lock);
476     return (EEXIST);
477   }
478
479   status = stat (filename, &sb);
480   if ((status == 0) || (errno != ENOENT))
481   {
482     pthread_mutex_unlock (&async_creation_lock);
483     return (EEXIST);
484   }
485
486   ptr = malloc (sizeof (*ptr));
487   if (ptr == NULL)
488   {
489     pthread_mutex_unlock (&async_creation_lock);
490     return (ENOMEM);
491   }
492
493   ptr->filename = strdup (filename);
494   if (ptr->filename == NULL)
495   {
496     pthread_mutex_unlock (&async_creation_lock);
497     sfree (ptr);
498     return (ENOMEM);
499   }
500
501   ptr->next = async_creation_list;
502   async_creation_list = ptr;
503
504   pthread_mutex_unlock (&async_creation_lock);
505
506   return (0);
507 } /* }}} int lock_file */
508
509 static int unlock_file (char const *filename) /* {{{ */
510 {
511   async_create_file_t *this;
512   async_create_file_t *prev;
513
514
515   pthread_mutex_lock (&async_creation_lock);
516
517   prev = NULL;
518   for (this = async_creation_list; this != NULL; this = this->next)
519   {
520     if (strcmp (filename, this->filename) == 0)
521       break;
522     prev = this;
523   }
524
525   if (this == NULL)
526   {
527     pthread_mutex_unlock (&async_creation_lock);
528     return (ENOENT);
529   }
530
531   if (prev == NULL)
532   {
533     assert (this == async_creation_list);
534     async_creation_list = this->next;
535   }
536   else
537   {
538     assert (this == prev->next);
539     prev->next = this->next;
540   }
541   this->next = NULL;
542
543   pthread_mutex_unlock (&async_creation_lock);
544
545   sfree (this->filename);
546   sfree (this);
547
548   return (0);
549 } /* }}} int unlock_file */
550
551 static void *srrd_create_thread (void *targs) /* {{{ */
552 {
553   srrd_create_args_t *args = targs;
554   char tmpfile[PATH_MAX];
555   int status;
556
557   status = lock_file (args->filename);
558   if (status != 0)
559   {
560     if (status == EEXIST)
561       NOTICE ("srrd_create_thread: File \"%s\" is already being created.",
562           args->filename);
563     else
564       ERROR ("srrd_create_thread: Unable to lock file \"%s\".",
565           args->filename);
566     srrd_create_args_destroy (args);
567     return (0);
568   }
569
570   ssnprintf (tmpfile, sizeof (tmpfile), "%s.async", args->filename);
571
572   status = srrd_create (tmpfile, args->pdp_step, args->last_up,
573       args->argc, (void *) args->argv);
574   if (status != 0)
575   {
576     WARNING ("srrd_create_thread: srrd_create (%s) returned status %i.",
577         args->filename, status);
578     unlink (tmpfile);
579     unlock_file (args->filename);
580     srrd_create_args_destroy (args);
581     return (0);
582   }
583
584   status = rename (tmpfile, args->filename);
585   if (status != 0)
586   {
587     char errbuf[1024];
588     ERROR ("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s",
589         tmpfile, args->filename,
590         sstrerror (errno, errbuf, sizeof (errbuf)));
591     unlink (tmpfile);
592     unlock_file (args->filename);
593     srrd_create_args_destroy (args);
594     return (0);
595   }
596
597   DEBUG ("srrd_create_thread: Successfully created RRD file \"%s\".",
598       args->filename);
599
600   unlock_file (args->filename);
601   srrd_create_args_destroy (args);
602
603   return (0);
604 } /* }}} void *srrd_create_thread */
605
606 static int srrd_create_async (const char *filename, /* {{{ */
607     unsigned long pdp_step, time_t last_up,
608     int argc, const char **argv)
609 {
610   srrd_create_args_t *args;
611   pthread_t thread;
612   pthread_attr_t attr;
613   int status;
614
615   DEBUG ("srrd_create_async: Creating \"%s\" in the background.", filename);
616
617   args = srrd_create_args_create (filename, pdp_step, last_up, argc, argv);
618   if (args == NULL)
619     return (-1);
620
621   status = pthread_attr_init (&attr);
622   if (status != 0)
623   {
624     srrd_create_args_destroy (args);
625     return (-1);
626   }
627
628   status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
629   if (status != 0)
630   {
631     pthread_attr_destroy (&attr);
632     srrd_create_args_destroy (args);
633     return (-1);
634   }
635
636   status = pthread_create (&thread, &attr, srrd_create_thread, args);
637   if (status != 0)
638   {
639     char errbuf[1024];
640     ERROR ("srrd_create_async: pthread_create failed: %s",
641         sstrerror (status, errbuf, sizeof (errbuf)));
642     pthread_attr_destroy (&attr);
643     srrd_create_args_destroy (args);
644     return (status);
645   }
646
647   pthread_attr_destroy (&attr);
648   /* args is freed in srrd_create_thread(). */
649   return (0);
650 } /* }}} int srrd_create_async */
651
652 /*
653  * Public functions
654  */
655 int cu_rrd_create_file (const char *filename, /* {{{ */
656     const data_set_t *ds, const value_list_t *vl,
657     const rrdcreate_config_t *cfg)
658 {
659   char **argv;
660   int argc;
661   char **rra_def = NULL;
662   int rra_num;
663   char **ds_def = NULL;
664   int ds_num;
665   int status = 0;
666   time_t last_up;
667   unsigned long stepsize;
668
669   if (check_create_dir (filename))
670     return (-1);
671
672   if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
673   {
674     ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
675     return (-1);
676   }
677
678   if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
679   {
680     ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
681     rra_free (rra_num, rra_def);
682     return (-1);
683   }
684
685   argc = ds_num + rra_num;
686
687   if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
688   {
689     char errbuf[1024];
690     ERROR ("cu_rrd_create_file failed: %s",
691         sstrerror (errno, errbuf, sizeof (errbuf)));
692     rra_free (rra_num, rra_def);
693     ds_free (ds_num, ds_def);
694     return (-1);
695   }
696
697   memcpy (argv, ds_def, ds_num * sizeof (char *));
698   memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
699   argv[ds_num + rra_num] = NULL;
700
701   last_up = CDTIME_T_TO_TIME_T (vl->time);
702   if (last_up <= 0)
703     last_up = time (NULL);
704   last_up -= 1;
705
706   if (cfg->stepsize > 0)
707     stepsize = cfg->stepsize;
708   else
709     stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
710
711   if (cfg->async)
712   {
713     status = srrd_create_async (filename, stepsize, last_up,
714         argc, (const char **) argv);
715     if (status != 0)
716       WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
717           "returned status %i.",
718           filename, status);
719   }
720   else /* synchronous */
721   {
722     status = lock_file (filename);
723     if (status != 0)
724     {
725       if (status == EEXIST)
726         NOTICE ("cu_rrd_create_file: File \"%s\" is already being created.",
727             filename);
728       else
729         ERROR ("cu_rrd_create_file: Unable to lock file \"%s\".",
730             filename);
731     }
732     else
733     {
734       status = srrd_create (filename, stepsize, last_up,
735           argc, (const char **) argv);
736
737       if (status != 0)
738       {
739         WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
740             filename, status);
741       }
742       else
743       {
744         DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
745             filename);
746       }
747       unlock_file (filename);
748     }
749   }
750
751   free (argv);
752   ds_free (ds_num, ds_def);
753   rra_free (rra_num, rra_def);
754
755   return (status);
756 } /* }}} int cu_rrd_create_file */
757
758 /* vim: set sw=2 sts=2 et fdm=marker : */