2 * collectd - src/utils_rrdcreate.c
3 * Copyright (C) 2006-2013 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
30 #include "utils_rrdcreate.h"
35 struct srrd_create_args_s {
37 unsigned long pdp_step;
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 {
48 async_create_file_t *next;
54 static int rra_timespans[] = {3600, 86400, 604800, 2678400, 31622400};
55 static int rra_timespans_num = STATIC_ARRAY_SIZE(rra_timespans);
57 static const char *const rra_types[] = {"AVERAGE", "MIN", "MAX"};
58 static int rra_types_num = STATIC_ARRAY_SIZE(rra_types);
60 #if !defined(HAVE_THREADSAFE_LIBRRD)
61 static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
64 static async_create_file_t *async_creation_list = NULL;
65 static pthread_mutex_t async_creation_lock = PTHREAD_MUTEX_INITIALIZER;
70 static void rra_free(int rra_num, char **rra_def) /* {{{ */
72 for (int i = 0; i < rra_num; i++) {
76 } /* }}} void rra_free */
78 static void srrd_create_args_destroy(srrd_create_args_t *args) {
82 sfree(args->filename);
83 if (args->argv != NULL) {
84 for (int i = 0; i < args->argc; i++)
89 } /* void srrd_create_args_destroy */
91 static srrd_create_args_t *srrd_create_args_create(const char *filename,
92 unsigned long pdp_step,
93 time_t last_up, int argc,
95 srrd_create_args_t *args;
97 args = calloc(1, sizeof(*args));
99 ERROR("srrd_create_args_create: calloc failed.");
102 args->filename = NULL;
103 args->pdp_step = pdp_step;
104 args->last_up = last_up;
107 args->filename = strdup(filename);
108 if (args->filename == NULL) {
109 ERROR("srrd_create_args_create: strdup failed.");
110 srrd_create_args_destroy(args);
114 args->argv = calloc((size_t)(argc + 1), sizeof(*args->argv));
115 if (args->argv == NULL) {
116 ERROR("srrd_create_args_create: calloc failed.");
117 srrd_create_args_destroy(args);
121 for (args->argc = 0; args->argc < argc; args->argc++) {
122 args->argv[args->argc] = strdup(argv[args->argc]);
123 if (args->argv[args->argc] == NULL) {
124 ERROR("srrd_create_args_create: strdup failed.");
125 srrd_create_args_destroy(args);
129 assert(args->argc == argc);
130 args->argv[args->argc] = NULL;
133 } /* srrd_create_args_t *srrd_create_args_create */
138 static int rra_get(char ***ret, const value_list_t *vl, /* {{{ */
139 const rrdcreate_config_t *cfg) {
151 /* The stepsize we use here: If it is user-set, use it. If not, use the
152 * interval of the value-list. */
155 if (cfg->rrarows <= 0) {
160 if ((cfg->xff < 0) || (cfg->xff >= 1.0)) {
165 if (cfg->stepsize > 0)
168 ss = (int)CDTIME_T_TO_TIME_T(vl->interval);
174 /* Use the configured timespans or fall back to the built-in defaults */
175 if (cfg->timespans_num != 0) {
176 rts = cfg->timespans;
177 rts_num = cfg->timespans_num;
180 rts_num = rra_timespans_num;
183 rra_max = rts_num * rra_types_num;
186 if ((rra_def = calloc(rra_max + 1, sizeof(*rra_def))) == NULL)
191 for (int i = 0; i < rts_num; i++) {
194 if ((span / ss) < cfg->rrarows)
195 span = ss * cfg->rrarows;
200 cdp_len = (int)floor(((double)span) / ((double)(cfg->rrarows * ss)));
202 cdp_num = (int)ceil(((double)span) / ((double)(cdp_len * ss)));
204 for (int j = 0; j < rra_types_num; j++) {
208 if (rra_num >= rra_max)
211 status = snprintf(buffer, sizeof(buffer), "RRA:%s:%.10f:%u:%u",
212 rra_types[j], cfg->xff, cdp_len, cdp_num);
214 if ((status < 0) || ((size_t)status >= sizeof(buffer))) {
215 ERROR("rra_get: Buffer would have been truncated.");
219 rra_def[rra_num++] = sstrdup(buffer);
230 } /* }}} int rra_get */
232 static void ds_free(int ds_num, char **ds_def) /* {{{ */
234 for (int i = 0; i < ds_num; i++)
235 if (ds_def[i] != NULL)
238 } /* }}} void ds_free */
240 static int ds_get(char ***ret, /* {{{ */
241 const data_set_t *ds, const value_list_t *vl,
242 const rrdcreate_config_t *cfg) {
250 assert(ds->ds_num > 0);
252 ds_def = calloc(ds->ds_num, sizeof(*ds_def));
253 if (ds_def == NULL) {
255 ERROR("rrdtool plugin: calloc failed: %s",
256 sstrerror(errno, errbuf, sizeof(errbuf)));
260 for (ds_num = 0; ds_num < ds->ds_num; ds_num++) {
261 data_source_t *d = ds->ds + ds_num;
265 ds_def[ds_num] = NULL;
267 if (d->type == DS_TYPE_COUNTER)
269 else if (d->type == DS_TYPE_GAUGE)
271 else if (d->type == DS_TYPE_DERIVE)
273 else if (d->type == DS_TYPE_ABSOLUTE)
276 ERROR("rrdtool plugin: Unknown DS type: %i", d->type);
281 sstrncpy(min, "U", sizeof(min));
283 snprintf(min, sizeof(min), "%f", d->min);
286 sstrncpy(max, "U", sizeof(max));
288 snprintf(max, sizeof(max), "%f", d->max);
291 buffer, sizeof(buffer), "DS:%s:%s:%i:%s:%s", d->name, type,
292 (cfg->heartbeat > 0) ? cfg->heartbeat
293 : (int)CDTIME_T_TO_TIME_T(2 * vl->interval),
295 if ((status < 1) || ((size_t)status >= sizeof(buffer)))
298 ds_def[ds_num] = sstrdup(buffer);
299 } /* for ds_num = 0 .. ds->ds_num */
301 if (ds_num != ds->ds_num) {
302 ds_free(ds_num, ds_def);
313 } /* }}} int ds_get */
315 #if HAVE_THREADSAFE_LIBRRD
316 static int srrd_create(const char *filename, /* {{{ */
317 unsigned long pdp_step, time_t last_up, int argc,
322 if ((filename == NULL) || (argv == NULL))
325 /* Some versions of librrd don't have the `const' qualifier for the first
326 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
327 * but what else can we do? :( -octo */
328 filename_copy = strdup(filename);
329 if (filename_copy == NULL) {
330 ERROR("srrd_create: strdup failed.");
334 optind = 0; /* bug in librrd? */
337 status = rrd_create_r(filename_copy, pdp_step, last_up, argc, (void *)argv);
340 WARNING("rrdtool plugin: rrd_create_r (%s) failed: %s", filename,
344 sfree(filename_copy);
347 } /* }}} int srrd_create */
348 /* #endif HAVE_THREADSAFE_LIBRRD */
350 #else /* !HAVE_THREADSAFE_LIBRRD */
351 static int srrd_create(const char *filename, /* {{{ */
352 unsigned long pdp_step, time_t last_up, int argc,
359 char pdp_step_str[16];
360 char last_up_str[16];
363 new_argv = malloc((new_argc + 1) * sizeof(*new_argv));
364 if (new_argv == NULL) {
365 ERROR("rrdtool plugin: malloc failed.");
370 last_up = time(NULL) - 10;
372 snprintf(pdp_step_str, sizeof(pdp_step_str), "%lu", pdp_step);
373 snprintf(last_up_str, sizeof(last_up_str), "%lu", (unsigned long)last_up);
375 new_argv[0] = "create";
376 new_argv[1] = (void *)filename;
378 new_argv[3] = pdp_step_str;
380 new_argv[5] = last_up_str;
382 memcpy(new_argv + 6, argv, argc * sizeof(char *));
383 new_argv[new_argc] = NULL;
385 pthread_mutex_lock(&librrd_lock);
386 optind = 0; /* bug in librrd? */
389 status = rrd_create(new_argc, new_argv);
390 pthread_mutex_unlock(&librrd_lock);
393 WARNING("rrdtool plugin: rrd_create (%s) failed: %s", filename,
400 } /* }}} int srrd_create */
401 #endif /* !HAVE_THREADSAFE_LIBRRD */
403 static int lock_file(char const *filename) /* {{{ */
405 async_create_file_t *ptr;
409 pthread_mutex_lock(&async_creation_lock);
411 for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
412 if (strcmp(filename, ptr->filename) == 0)
416 pthread_mutex_unlock(&async_creation_lock);
420 status = stat(filename, &sb);
421 if ((status == 0) || (errno != ENOENT)) {
422 pthread_mutex_unlock(&async_creation_lock);
426 ptr = malloc(sizeof(*ptr));
428 pthread_mutex_unlock(&async_creation_lock);
432 ptr->filename = strdup(filename);
433 if (ptr->filename == NULL) {
434 pthread_mutex_unlock(&async_creation_lock);
439 ptr->next = async_creation_list;
440 async_creation_list = ptr;
442 pthread_mutex_unlock(&async_creation_lock);
445 } /* }}} int lock_file */
447 static int unlock_file(char const *filename) /* {{{ */
449 async_create_file_t *this;
450 async_create_file_t *prev;
452 pthread_mutex_lock(&async_creation_lock);
455 for (this = async_creation_list; this != NULL; this = this->next) {
456 if (strcmp(filename, this->filename) == 0)
462 pthread_mutex_unlock(&async_creation_lock);
467 assert(this == async_creation_list);
468 async_creation_list = this->next;
470 assert(this == prev->next);
471 prev->next = this->next;
475 pthread_mutex_unlock(&async_creation_lock);
477 sfree(this->filename);
481 } /* }}} int unlock_file */
483 static void *srrd_create_thread(void *targs) /* {{{ */
485 srrd_create_args_t *args = targs;
486 char tmpfile[PATH_MAX];
489 status = lock_file(args->filename);
491 if (status == EEXIST)
492 NOTICE("srrd_create_thread: File \"%s\" is already being created.",
495 ERROR("srrd_create_thread: Unable to lock file \"%s\".", args->filename);
496 srrd_create_args_destroy(args);
500 snprintf(tmpfile, sizeof(tmpfile), "%s.async", args->filename);
502 status = srrd_create(tmpfile, args->pdp_step, args->last_up, args->argc,
505 WARNING("srrd_create_thread: srrd_create (%s) returned status %i.",
506 args->filename, status);
508 unlock_file(args->filename);
509 srrd_create_args_destroy(args);
513 status = rename(tmpfile, args->filename);
516 ERROR("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s", tmpfile,
517 args->filename, sstrerror(errno, errbuf, sizeof(errbuf)));
519 unlock_file(args->filename);
520 srrd_create_args_destroy(args);
524 DEBUG("srrd_create_thread: Successfully created RRD file \"%s\".",
527 unlock_file(args->filename);
528 srrd_create_args_destroy(args);
531 } /* }}} void *srrd_create_thread */
533 static int srrd_create_async(const char *filename, /* {{{ */
534 unsigned long pdp_step, time_t last_up, int argc,
536 srrd_create_args_t *args;
541 DEBUG("srrd_create_async: Creating \"%s\" in the background.", filename);
543 args = srrd_create_args_create(filename, pdp_step, last_up, argc, argv);
547 status = pthread_attr_init(&attr);
549 srrd_create_args_destroy(args);
553 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
555 pthread_attr_destroy(&attr);
556 srrd_create_args_destroy(args);
560 status = pthread_create(&thread, &attr, srrd_create_thread, args);
563 ERROR("srrd_create_async: pthread_create failed: %s",
564 sstrerror(status, errbuf, sizeof(errbuf)));
565 pthread_attr_destroy(&attr);
566 srrd_create_args_destroy(args);
570 pthread_attr_destroy(&attr);
571 /* args is freed in srrd_create_thread(). */
573 } /* }}} int srrd_create_async */
578 int cu_rrd_create_file(const char *filename, /* {{{ */
579 const data_set_t *ds, const value_list_t *vl,
580 const rrdcreate_config_t *cfg) {
583 char **rra_def = NULL;
585 char **ds_def = NULL;
589 unsigned long stepsize;
591 if (check_create_dir(filename))
594 if ((rra_num = rra_get(&rra_def, vl, cfg)) < 1) {
595 ERROR("cu_rrd_create_file failed: Could not calculate RRAs");
599 if ((ds_num = ds_get(&ds_def, ds, vl, cfg)) < 1) {
600 ERROR("cu_rrd_create_file failed: Could not calculate DSes");
601 rra_free(rra_num, rra_def);
605 argc = ds_num + rra_num;
607 if ((argv = malloc(sizeof(*argv) * (argc + 1))) == NULL) {
609 ERROR("cu_rrd_create_file failed: %s",
610 sstrerror(errno, errbuf, sizeof(errbuf)));
611 rra_free(rra_num, rra_def);
612 ds_free(ds_num, ds_def);
616 memcpy(argv, ds_def, ds_num * sizeof(char *));
617 memcpy(argv + ds_num, rra_def, rra_num * sizeof(char *));
618 argv[ds_num + rra_num] = NULL;
620 last_up = CDTIME_T_TO_TIME_T(vl->time);
622 last_up = time(NULL);
625 if (cfg->stepsize > 0)
626 stepsize = cfg->stepsize;
628 stepsize = (unsigned long)CDTIME_T_TO_TIME_T(vl->interval);
631 status = srrd_create_async(filename, stepsize, last_up, argc,
632 (const char **)argv);
634 WARNING("cu_rrd_create_file: srrd_create_async (%s) "
635 "returned status %i.",
637 } else /* synchronous */
639 status = lock_file(filename);
641 if (status == EEXIST)
642 NOTICE("cu_rrd_create_file: File \"%s\" is already being created.",
645 ERROR("cu_rrd_create_file: Unable to lock file \"%s\".", filename);
648 srrd_create(filename, stepsize, last_up, argc, (const char **)argv);
651 WARNING("cu_rrd_create_file: srrd_create (%s) returned status %i.",
654 DEBUG("cu_rrd_create_file: Successfully created RRD file \"%s\".",
657 unlock_file(filename);
662 ds_free(ds_num, ds_def);
663 rra_free(rra_num, rra_def);
666 } /* }}} int cu_rrd_create_file */