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;
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) {
254 ERROR("rrdtool plugin: calloc failed: %s", STRERRNO);
258 for (ds_num = 0; ds_num < ds->ds_num; ds_num++) {
259 data_source_t *d = ds->ds + ds_num;
263 ds_def[ds_num] = NULL;
265 if (d->type == DS_TYPE_COUNTER)
267 else if (d->type == DS_TYPE_GAUGE)
269 else if (d->type == DS_TYPE_DERIVE)
271 else if (d->type == DS_TYPE_ABSOLUTE)
274 ERROR("rrdtool plugin: Unknown DS type: %i", d->type);
279 sstrncpy(min, "U", sizeof(min));
281 snprintf(min, sizeof(min), "%f", d->min);
284 sstrncpy(max, "U", sizeof(max));
286 snprintf(max, sizeof(max), "%f", d->max);
289 buffer, sizeof(buffer), "DS:%s:%s:%i:%s:%s", d->name, type,
290 (cfg->heartbeat > 0) ? cfg->heartbeat
291 : (int)CDTIME_T_TO_TIME_T(2 * vl->interval),
293 if ((status < 1) || ((size_t)status >= sizeof(buffer)))
296 ds_def[ds_num] = sstrdup(buffer);
297 } /* for ds_num = 0 .. ds->ds_num */
299 if (ds_num != ds->ds_num) {
300 ds_free(ds_num, ds_def);
311 } /* }}} int ds_get */
313 #if HAVE_THREADSAFE_LIBRRD
314 static int srrd_create(const char *filename, /* {{{ */
315 unsigned long pdp_step, time_t last_up, int argc,
320 if ((filename == NULL) || (argv == NULL))
323 /* Some versions of librrd don't have the `const' qualifier for the first
324 * argument, so we have to copy the pointer here to avoid warnings. It sucks,
325 * but what else can we do? :( -octo */
326 filename_copy = strdup(filename);
327 if (filename_copy == NULL) {
328 ERROR("srrd_create: strdup failed.");
332 optind = 0; /* bug in librrd? */
335 status = rrd_create_r(filename_copy, pdp_step, last_up, argc, (void *)argv);
338 WARNING("rrdtool plugin: rrd_create_r (%s) failed: %s", filename,
342 sfree(filename_copy);
345 } /* }}} int srrd_create */
346 /* #endif HAVE_THREADSAFE_LIBRRD */
348 #else /* !HAVE_THREADSAFE_LIBRRD */
349 static int srrd_create(const char *filename, /* {{{ */
350 unsigned long pdp_step, time_t last_up, int argc,
357 char pdp_step_str[16];
358 char last_up_str[16];
361 new_argv = malloc((new_argc + 1) * sizeof(*new_argv));
362 if (new_argv == NULL) {
363 ERROR("rrdtool plugin: malloc failed.");
368 last_up = time(NULL) - 10;
370 snprintf(pdp_step_str, sizeof(pdp_step_str), "%lu", pdp_step);
371 snprintf(last_up_str, sizeof(last_up_str), "%lu", (unsigned long)last_up);
373 new_argv[0] = "create";
374 new_argv[1] = (void *)filename;
376 new_argv[3] = pdp_step_str;
378 new_argv[5] = last_up_str;
380 memcpy(new_argv + 6, argv, argc * sizeof(char *));
381 new_argv[new_argc] = NULL;
383 pthread_mutex_lock(&librrd_lock);
384 optind = 0; /* bug in librrd? */
387 status = rrd_create(new_argc, new_argv);
388 pthread_mutex_unlock(&librrd_lock);
391 WARNING("rrdtool plugin: rrd_create (%s) failed: %s", filename,
398 } /* }}} int srrd_create */
399 #endif /* !HAVE_THREADSAFE_LIBRRD */
401 static int lock_file(char const *filename) /* {{{ */
403 async_create_file_t *ptr;
407 pthread_mutex_lock(&async_creation_lock);
409 for (ptr = async_creation_list; ptr != NULL; ptr = ptr->next)
410 if (strcmp(filename, ptr->filename) == 0)
414 pthread_mutex_unlock(&async_creation_lock);
418 status = stat(filename, &sb);
419 if ((status == 0) || (errno != ENOENT)) {
420 pthread_mutex_unlock(&async_creation_lock);
424 ptr = malloc(sizeof(*ptr));
426 pthread_mutex_unlock(&async_creation_lock);
430 ptr->filename = strdup(filename);
431 if (ptr->filename == NULL) {
432 pthread_mutex_unlock(&async_creation_lock);
437 ptr->next = async_creation_list;
438 async_creation_list = ptr;
440 pthread_mutex_unlock(&async_creation_lock);
443 } /* }}} int lock_file */
445 static int unlock_file(char const *filename) /* {{{ */
447 async_create_file_t *this;
448 async_create_file_t *prev;
450 pthread_mutex_lock(&async_creation_lock);
453 for (this = async_creation_list; this != NULL; this = this->next) {
454 if (strcmp(filename, this->filename) == 0)
460 pthread_mutex_unlock(&async_creation_lock);
465 assert(this == async_creation_list);
466 async_creation_list = this->next;
468 assert(this == prev->next);
469 prev->next = this->next;
473 pthread_mutex_unlock(&async_creation_lock);
475 sfree(this->filename);
479 } /* }}} int unlock_file */
481 static void *srrd_create_thread(void *targs) /* {{{ */
483 srrd_create_args_t *args = targs;
484 char tmpfile[PATH_MAX];
487 status = lock_file(args->filename);
489 if (status == EEXIST)
490 NOTICE("srrd_create_thread: File \"%s\" is already being created.",
493 ERROR("srrd_create_thread: Unable to lock file \"%s\".", args->filename);
494 srrd_create_args_destroy(args);
498 snprintf(tmpfile, sizeof(tmpfile), "%s.async", args->filename);
500 status = srrd_create(tmpfile, args->pdp_step, args->last_up, args->argc,
503 WARNING("srrd_create_thread: srrd_create (%s) returned status %i.",
504 args->filename, status);
506 unlock_file(args->filename);
507 srrd_create_args_destroy(args);
511 status = rename(tmpfile, args->filename);
513 ERROR("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s", tmpfile,
514 args->filename, STRERRNO);
516 unlock_file(args->filename);
517 srrd_create_args_destroy(args);
521 DEBUG("srrd_create_thread: Successfully created RRD file \"%s\".",
524 unlock_file(args->filename);
525 srrd_create_args_destroy(args);
528 } /* }}} void *srrd_create_thread */
530 static int srrd_create_async(const char *filename, /* {{{ */
531 unsigned long pdp_step, time_t last_up, int argc,
533 srrd_create_args_t *args;
538 DEBUG("srrd_create_async: Creating \"%s\" in the background.", filename);
540 args = srrd_create_args_create(filename, pdp_step, last_up, argc, argv);
544 status = pthread_attr_init(&attr);
546 srrd_create_args_destroy(args);
550 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
552 pthread_attr_destroy(&attr);
553 srrd_create_args_destroy(args);
557 status = pthread_create(&thread, &attr, srrd_create_thread, args);
559 ERROR("srrd_create_async: pthread_create failed: %s", STRERROR(status));
560 pthread_attr_destroy(&attr);
561 srrd_create_args_destroy(args);
565 pthread_attr_destroy(&attr);
566 /* args is freed in srrd_create_thread(). */
568 } /* }}} int srrd_create_async */
573 int cu_rrd_create_file(const char *filename, /* {{{ */
574 const data_set_t *ds, const value_list_t *vl,
575 const rrdcreate_config_t *cfg) {
578 char **rra_def = NULL;
580 char **ds_def = NULL;
584 unsigned long stepsize;
586 if (check_create_dir(filename))
589 if ((rra_num = rra_get(&rra_def, vl, cfg)) < 1) {
590 ERROR("cu_rrd_create_file failed: Could not calculate RRAs");
594 if ((ds_num = ds_get(&ds_def, ds, vl, cfg)) < 1) {
595 ERROR("cu_rrd_create_file failed: Could not calculate DSes");
596 rra_free(rra_num, rra_def);
600 argc = ds_num + rra_num;
602 if ((argv = malloc(sizeof(*argv) * (argc + 1))) == NULL) {
603 ERROR("cu_rrd_create_file failed: %s", STRERRNO);
604 rra_free(rra_num, rra_def);
605 ds_free(ds_num, ds_def);
609 memcpy(argv, ds_def, ds_num * sizeof(char *));
610 memcpy(argv + ds_num, rra_def, rra_num * sizeof(char *));
611 argv[ds_num + rra_num] = NULL;
613 last_up = CDTIME_T_TO_TIME_T(vl->time);
615 last_up = time(NULL);
618 if (cfg->stepsize > 0)
619 stepsize = cfg->stepsize;
621 stepsize = (unsigned long)CDTIME_T_TO_TIME_T(vl->interval);
624 status = srrd_create_async(filename, stepsize, last_up, argc,
625 (const char **)argv);
627 WARNING("cu_rrd_create_file: srrd_create_async (%s) "
628 "returned status %i.",
630 } else /* synchronous */
632 status = lock_file(filename);
634 if (status == EEXIST)
635 NOTICE("cu_rrd_create_file: File \"%s\" is already being created.",
638 ERROR("cu_rrd_create_file: Unable to lock file \"%s\".", filename);
641 srrd_create(filename, stepsize, last_up, argc, (const char **)argv);
644 WARNING("cu_rrd_create_file: srrd_create (%s) returned status %i.",
647 DEBUG("cu_rrd_create_file: Successfully created RRD file \"%s\".",
650 unlock_file(filename);
655 ds_free(ds_num, ds_def);
656 rra_free(rra_num, rra_def);
659 } /* }}} int cu_rrd_create_file */