1 /**
2 * collectd - src/utils_rrdcreate.c
3 * Copyright (C) 2006-2013 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "utils_rrdcreate.h"
31 #include <pthread.h>
32 #include <rrd.h>
34 struct srrd_create_args_s
35 {
36 char *filename;
37 unsigned long pdp_step;
38 time_t last_up;
39 int argc;
40 char **argv;
41 };
42 typedef struct srrd_create_args_s srrd_create_args_t;
44 struct async_create_file_s;
45 typedef struct async_create_file_s async_create_file_t;
46 struct async_create_file_s
47 {
48 char *filename;
49 async_create_file_t *next;
50 };
52 /*
53 * Private variables
54 */
55 static int rra_timespans[] =
56 {
57 3600,
58 86400,
59 604800,
60 2678400,
61 31622400
62 };
63 static int rra_timespans_num = STATIC_ARRAY_SIZE (rra_timespans);
65 static const char *const rra_types[] =
66 {
67 "AVERAGE",
68 "MIN",
69 "MAX"
70 };
71 static int rra_types_num = STATIC_ARRAY_SIZE (rra_types);
73 #if !defined(HAVE_THREADSAFE_LIBRRD) || !HAVE_THREADSAFE_LIBRRD
74 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
75 #endif
77 static async_create_file_t *async_creation_list = NULL;
78 static pthread_mutex_t async_creation_lock = PTHREAD_MUTEX_INITIALIZER;
80 /*
81 * Private functions
82 */
83 static void rra_free (int rra_num, char **rra_def) /* {{{ */
84 {
85 int i;
87 for (i = 0; i < rra_num; i++)
88 {
89 sfree (rra_def[i]);
90 }
91 sfree (rra_def);
92 } /* }}} void rra_free */
94 static void srrd_create_args_destroy (srrd_create_args_t *args)
95 {
96 if (args == NULL)
97 return;
99 sfree (args->filename);
100 if (args->argv != NULL)
101 {
102 int i;
103 for (i = 0; i < args->argc; i++)
104 sfree (args->argv[i]);
105 sfree (args->argv);
106 }
107 sfree (args);
108 } /* void srrd_create_args_destroy */
110 static srrd_create_args_t *srrd_create_args_create (const char *filename,
111 unsigned long pdp_step, time_t last_up,
112 int argc, const char **argv)
113 {
114 srrd_create_args_t *args;
116 args = calloc (1, sizeof (*args));
117 if (args == NULL)
118 {
119 ERROR ("srrd_create_args_create: calloc failed.");
120 return (NULL);
121 }
122 args->filename = NULL;
123 args->pdp_step = pdp_step;
124 args->last_up = last_up;
125 args->argv = NULL;
127 args->filename = strdup (filename);
128 if (args->filename == NULL)
129 {
130 ERROR ("srrd_create_args_create: strdup failed.");
131 srrd_create_args_destroy (args);
132 return (NULL);
133 }
135 args->argv = calloc ((size_t) (argc + 1), sizeof (*args->argv));
136 if (args->argv == NULL)
137 {
138 ERROR ("srrd_create_args_create: calloc failed.");
139 srrd_create_args_destroy (args);
140 return (NULL);
141 }
143 for (args->argc = 0; args->argc < argc; args->argc++)
144 {
145 args->argv[args->argc] = strdup (argv[args->argc]);
146 if (args->argv[args->argc] == NULL)
147 {
148 ERROR ("srrd_create_args_create: strdup failed.");
149 srrd_create_args_destroy (args);
150 return (NULL);
151 }
152 }
153 assert (args->argc == argc);
154 args->argv[args->argc] = NULL;
156 return (args);
157 } /* srrd_create_args_t *srrd_create_args_create */
159 /* * * * * * * * * *
160 * WARNING: Magic *
161 * * * * * * * * * */
162 static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */
163 const rrdcreate_config_t *cfg)
164 {
165 char **rra_def;
166 int rra_num;
168 int *rts;
169 int rts_num;
171 int rra_max;
173 int cdp_num;
174 int cdp_len;
175 int i, j;
177 /* The stepsize we use here: If it is user-set, use it. If not, use the
178 * interval of the value-list. */
179 int ss;
181 if (cfg->rrarows <= 0)
182 {
183 *ret = NULL;
184 return (-1);
185 }
187 if ((cfg->xff < 0) || (cfg->xff >= 1.0))
188 {
189 *ret = NULL;
190 return (-1);
191 }
193 if (cfg->stepsize > 0)
194 ss = cfg->stepsize;
195 else
196 ss = (int) CDTIME_T_TO_TIME_T (vl->interval);
197 if (ss <= 0)
198 {
199 *ret = NULL;
200 return (-1);
201 }
203 /* Use the configured timespans or fall back to the built-in defaults */
204 if (cfg->timespans_num != 0)
205 {
206 rts = cfg->timespans;
207 rts_num = cfg->timespans_num;
208 }
209 else
210 {
211 rts = rra_timespans;
212 rts_num = rra_timespans_num;
213 }
215 rra_max = rts_num * rra_types_num;
216 assert (rra_max > 0);
218 if ((rra_def = calloc (rra_max + 1, sizeof (*rra_def))) == NULL)
219 return (-1);
220 rra_num = 0;
222 cdp_len = 0;
223 for (i = 0; i < rts_num; i++)
224 {
225 int span = rts[i];
227 if ((span / ss) < cfg->rrarows)
228 span = ss * cfg->rrarows;
230 if (cdp_len == 0)
231 cdp_len = 1;
232 else
233 cdp_len = (int) floor (((double) span)
234 / ((double) (cfg->rrarows * ss)));
236 cdp_num = (int) ceil (((double) span)
237 / ((double) (cdp_len * ss)));
239 for (j = 0; j < rra_types_num; j++)
240 {
241 char buffer[128];
242 int status;
244 if (rra_num >= rra_max)
245 break;
247 status = ssnprintf (buffer, sizeof (buffer), "RRA:%s:%.10f:%u:%u",
248 rra_types[j], cfg->xff, cdp_len, cdp_num);
250 if ((status < 0) || ((size_t) status >= sizeof (buffer)))
251 {
252 ERROR ("rra_get: Buffer would have been truncated.");
253 continue;
254 }
256 rra_def[rra_num++] = sstrdup (buffer);
257 }
258 }
260 if (rra_num <= 0)
261 {
262 sfree (rra_def);
263 return (0);
264 }
266 *ret = rra_def;
267 return (rra_num);
268 } /* }}} int rra_get */
270 static void ds_free (int ds_num, char **ds_def) /* {{{ */
271 {
272 int i;
274 for (i = 0; i < ds_num; i++)
275 if (ds_def[i] != NULL)
276 free (ds_def[i]);
277 free (ds_def);
278 } /* }}} void ds_free */
280 static int ds_get (char ***ret, /* {{{ */
281 const data_set_t *ds, const value_list_t *vl,
282 const rrdcreate_config_t *cfg)
283 {
284 char **ds_def;
285 size_t ds_num;
287 char min[32];
288 char max[32];
289 char buffer[128];
291 assert (ds->ds_num > 0);
293 ds_def = calloc (ds->ds_num, sizeof (*ds_def));
294 if (ds_def == NULL)
295 {
296 char errbuf[1024];
297 ERROR ("rrdtool plugin: calloc failed: %s",
298 sstrerror (errno, errbuf, sizeof (errbuf)));
299 return (-1);
300 }
302 for (ds_num = 0; ds_num < ds->ds_num; ds_num++)
303 {
304 data_source_t *d = ds->ds + ds_num;
305 const char *type;
306 int status;
308 ds_def[ds_num] = NULL;
310 if (d->type == DS_TYPE_COUNTER)
311 type = "COUNTER";
312 else if (d->type == DS_TYPE_GAUGE)
313 type = "GAUGE";
314 else if (d->type == DS_TYPE_DERIVE)
315 type = "DERIVE";
316 else if (d->type == DS_TYPE_ABSOLUTE)
317 type = "ABSOLUTE";
318 else
319 {
320 ERROR ("rrdtool plugin: Unknown DS type: %i",
321 d->type);
322 break;
323 }
325 if (isnan (d->min))
326 {
327 sstrncpy (min, "U", sizeof (min));
328 }
329 else
330 ssnprintf (min, sizeof (min), "%f", d->min);
332 if (isnan (d->max))
333 {
334 sstrncpy (max, "U", sizeof (max));
335 }
336 else
337 ssnprintf (max, sizeof (max), "%f", d->max);
339 status = ssnprintf (buffer, sizeof (buffer),
340 "DS:%s:%s:%i:%s:%s",
341 d->name, type,
342 (cfg->heartbeat > 0)
343 ? cfg->heartbeat
344 : (int) CDTIME_T_TO_TIME_T (2 * vl->interval),
345 min, max);
346 if ((status < 1) || ((size_t) status >= sizeof (buffer)))
347 break;
349 ds_def[ds_num] = sstrdup (buffer);
350 } /* for ds_num = 0 .. ds->ds_num */
352 if (ds_num != ds->ds_num)
353 {
354 ds_free (ds_num, ds_def);
355 return (-1);
356 }
358 if (ds_num <= 0)
359 {
360 sfree (ds_def);
361 return (0);
362 }
364 *ret = ds_def;
365 return (ds_num);
366 } /* }}} int ds_get */
368 #if HAVE_THREADSAFE_LIBRRD
369 static int srrd_create (const char *filename, /* {{{ */
370 unsigned long pdp_step, time_t last_up,
371 int argc, const char **argv)
372 {
373 int status;
374 char *filename_copy;
376 if ((filename == NULL) || (argv == NULL))
377 return (-EINVAL);
379 /* Some versions of librrd don't have the `const' qualifier for the first
380 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
381 * but what else can we do? :( -octo */
382 filename_copy = strdup (filename);
383 if (filename_copy == NULL)
384 {
385 ERROR ("srrd_create: strdup failed.");
386 return (-ENOMEM);
387 }
389 optind = 0; /* bug in librrd? */
390 rrd_clear_error ();
392 status = rrd_create_r (filename_copy, pdp_step, last_up,
393 argc, (void *) argv);
395 if (status != 0)
396 {
397 WARNING ("rrdtool plugin: rrd_create_r (%s) failed: %s",
398 filename, rrd_get_error ());
399 }
401 sfree (filename_copy);
403 return (status);
404 } /* }}} int srrd_create */
405 /* #endif HAVE_THREADSAFE_LIBRRD */
407 #else /* !HAVE_THREADSAFE_LIBRRD */
408 static int srrd_create (const char *filename, /* {{{ */
409 unsigned long pdp_step, time_t last_up,
410 int argc, const char **argv)
411 {
412 int status;
414 int new_argc;
415 char **new_argv;
417 char pdp_step_str[16];
418 char last_up_str[16];
420 new_argc = 6 + argc;
421 new_argv = malloc ((new_argc + 1) * sizeof (*new_argv));
422 if (new_argv == NULL)
423 {
424 ERROR ("rrdtool plugin: malloc failed.");
425 return (-1);
426 }
428 if (last_up == 0)
429 last_up = time (NULL) - 10;
431 ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
432 ssnprintf (last_up_str, sizeof (last_up_str), "%lu", (unsigned long) last_up);
434 new_argv[0] = "create";
435 new_argv[1] = (void *) filename;
436 new_argv[2] = "-s";
437 new_argv[3] = pdp_step_str;
438 new_argv[4] = "-b";
439 new_argv[5] = last_up_str;
441 memcpy (new_argv + 6, argv, argc * sizeof (char *));
442 new_argv[new_argc] = NULL;
444 pthread_mutex_lock (&librrd_lock);
445 optind = 0; /* bug in librrd? */
446 rrd_clear_error ();
448 status = rrd_create (new_argc, new_argv);
449 pthread_mutex_unlock (&librrd_lock);
451 if (status != 0)
452 {
453 WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
454 filename, rrd_get_error ());
455 }
457 sfree (new_argv);
459 return (status);
460 } /* }}} int srrd_create */
461 #endif /* !HAVE_THREADSAFE_LIBRRD */
463 static int lock_file (char const *filename) /* {{{ */
464 {
465 async_create_file_t *ptr;
466 struct stat sb;
467 int status;
469 pthread_mutex_lock (&async_creation_lock);
471 for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
472 if (strcmp (filename, ptr->filename) == 0)
473 break;
475 if (ptr != NULL)
476 {
477 pthread_mutex_unlock (&async_creation_lock);
478 return (EEXIST);
479 }
481 status = stat (filename, &sb);
482 if ((status == 0) || (errno != ENOENT))
483 {
484 pthread_mutex_unlock (&async_creation_lock);
485 return (EEXIST);
486 }
488 ptr = malloc (sizeof (*ptr));
489 if (ptr == NULL)
490 {
491 pthread_mutex_unlock (&async_creation_lock);
492 return (ENOMEM);
493 }
495 ptr->filename = strdup (filename);
496 if (ptr->filename == NULL)
497 {
498 pthread_mutex_unlock (&async_creation_lock);
499 sfree (ptr);
500 return (ENOMEM);
501 }
503 ptr->next = async_creation_list;
504 async_creation_list = ptr;
506 pthread_mutex_unlock (&async_creation_lock);
508 return (0);
509 } /* }}} int lock_file */
511 static int unlock_file (char const *filename) /* {{{ */
512 {
513 async_create_file_t *this;
514 async_create_file_t *prev;
517 pthread_mutex_lock (&async_creation_lock);
519 prev = NULL;
520 for (this = async_creation_list; this != NULL; this = this->next)
521 {
522 if (strcmp (filename, this->filename) == 0)
523 break;
524 prev = this;
525 }
527 if (this == NULL)
528 {
529 pthread_mutex_unlock (&async_creation_lock);
530 return (ENOENT);
531 }
533 if (prev == NULL)
534 {
535 assert (this == async_creation_list);
536 async_creation_list = this->next;
537 }
538 else
539 {
540 assert (this == prev->next);
541 prev->next = this->next;
542 }
543 this->next = NULL;
545 pthread_mutex_unlock (&async_creation_lock);
547 sfree (this->filename);
548 sfree (this);
550 return (0);
551 } /* }}} int unlock_file */
553 static void *srrd_create_thread (void *targs) /* {{{ */
554 {
555 srrd_create_args_t *args = targs;
556 char tmpfile[PATH_MAX];
557 int status;
559 status = lock_file (args->filename);
560 if (status != 0)
561 {
562 if (status == EEXIST)
563 NOTICE ("srrd_create_thread: File \"%s\" is already being created.",
564 args->filename);
565 else
566 ERROR ("srrd_create_thread: Unable to lock file \"%s\".",
567 args->filename);
568 srrd_create_args_destroy (args);
569 return (0);
570 }
572 ssnprintf (tmpfile, sizeof (tmpfile), "%s.async", args->filename);
574 status = srrd_create (tmpfile, args->pdp_step, args->last_up,
575 args->argc, (void *) args->argv);
576 if (status != 0)
577 {
578 WARNING ("srrd_create_thread: srrd_create (%s) returned status %i.",
579 args->filename, status);
580 unlink (tmpfile);
581 unlock_file (args->filename);
582 srrd_create_args_destroy (args);
583 return (0);
584 }
586 status = rename (tmpfile, args->filename);
587 if (status != 0)
588 {
589 char errbuf[1024];
590 ERROR ("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s",
591 tmpfile, args->filename,
592 sstrerror (errno, errbuf, sizeof (errbuf)));
593 unlink (tmpfile);
594 unlock_file (args->filename);
595 srrd_create_args_destroy (args);
596 return (0);
597 }
599 DEBUG ("srrd_create_thread: Successfully created RRD file \"%s\".",
600 args->filename);
602 unlock_file (args->filename);
603 srrd_create_args_destroy (args);
605 return (0);
606 } /* }}} void *srrd_create_thread */
608 static int srrd_create_async (const char *filename, /* {{{ */
609 unsigned long pdp_step, time_t last_up,
610 int argc, const char **argv)
611 {
612 srrd_create_args_t *args;
613 pthread_t thread;
614 pthread_attr_t attr;
615 int status;
617 DEBUG ("srrd_create_async: Creating \"%s\" in the background.", filename);
619 args = srrd_create_args_create (filename, pdp_step, last_up, argc, argv);
620 if (args == NULL)
621 return (-1);
623 status = pthread_attr_init (&attr);
624 if (status != 0)
625 {
626 srrd_create_args_destroy (args);
627 return (-1);
628 }
630 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
631 if (status != 0)
632 {
633 pthread_attr_destroy (&attr);
634 srrd_create_args_destroy (args);
635 return (-1);
636 }
638 status = pthread_create (&thread, &attr, srrd_create_thread, args);
639 if (status != 0)
640 {
641 char errbuf[1024];
642 ERROR ("srrd_create_async: pthread_create failed: %s",
643 sstrerror (status, errbuf, sizeof (errbuf)));
644 pthread_attr_destroy (&attr);
645 srrd_create_args_destroy (args);
646 return (status);
647 }
649 pthread_attr_destroy (&attr);
650 /* args is freed in srrd_create_thread(). */
651 return (0);
652 } /* }}} int srrd_create_async */
654 /*
655 * Public functions
656 */
657 int cu_rrd_create_file (const char *filename, /* {{{ */
658 const data_set_t *ds, const value_list_t *vl,
659 const rrdcreate_config_t *cfg)
660 {
661 char **argv;
662 int argc;
663 char **rra_def = NULL;
664 int rra_num;
665 char **ds_def = NULL;
666 int ds_num;
667 int status = 0;
668 time_t last_up;
669 unsigned long stepsize;
671 if (check_create_dir (filename))
672 return (-1);
674 if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
675 {
676 ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
677 return (-1);
678 }
680 if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
681 {
682 ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
683 rra_free (rra_num, rra_def);
684 return (-1);
685 }
687 argc = ds_num + rra_num;
689 if ((argv = malloc (sizeof (*argv) * (argc + 1))) == NULL)
690 {
691 char errbuf[1024];
692 ERROR ("cu_rrd_create_file failed: %s",
693 sstrerror (errno, errbuf, sizeof (errbuf)));
694 rra_free (rra_num, rra_def);
695 ds_free (ds_num, ds_def);
696 return (-1);
697 }
699 memcpy (argv, ds_def, ds_num * sizeof (char *));
700 memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
701 argv[ds_num + rra_num] = NULL;
703 last_up = CDTIME_T_TO_TIME_T (vl->time);
704 if (last_up <= 0)
705 last_up = time (NULL);
706 last_up -= 1;
708 if (cfg->stepsize > 0)
709 stepsize = cfg->stepsize;
710 else
711 stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);
713 if (cfg->async)
714 {
715 status = srrd_create_async (filename, stepsize, last_up,
716 argc, (const char **) argv);
717 if (status != 0)
718 WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
719 "returned status %i.",
720 filename, status);
721 }
722 else /* synchronous */
723 {
724 status = lock_file (filename);
725 if (status != 0)
726 {
727 if (status == EEXIST)
728 NOTICE ("cu_rrd_create_file: File \"%s\" is already being created.",
729 filename);
730 else
731 ERROR ("cu_rrd_create_file: Unable to lock file \"%s\".",
732 filename);
733 }
734 else
735 {
736 status = srrd_create (filename, stepsize, last_up,
737 argc, (const char **) argv);
739 if (status != 0)
740 {
741 WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
742 filename, status);
743 }
744 else
745 {
746 DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
747 filename);
748 }
749 unlock_file (filename);
750 }
751 }
753 free (argv);
754 ds_free (ds_num, ds_def);
755 rra_free (rra_num, rra_def);
757 return (status);
758 } /* }}} int cu_rrd_create_file */
760 /* vim: set sw=2 sts=2 et fdm=marker : */