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