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