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