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>
29 #include "utils/common/common.h"
30 #include "utils/rrdcreate/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 P_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 P_ERROR("srrd_create_args_create: strdup failed.");
110 srrd_create_args_destroy(args);
114 args->argv = calloc(argc + 1, sizeof(*args->argv));
115 if (args->argv == NULL) {
116 P_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 P_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 = ssnprintf(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 P_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 P_ERROR("ds_get: 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 P_ERROR("ds_get: Unknown DS type: %i", d->type);
279 sstrncpy(min, "U", sizeof(min));
281 ssnprintf(min, sizeof(min), "%f", d->min);
284 sstrncpy(max, "U", sizeof(max));
286 ssnprintf(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 P_WARNING("srrd_create: 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 P_ERROR("srrd_create: malloc failed.");
368 last_up = time(NULL) - 10;
370 ssnprintf(pdp_step_str, sizeof(pdp_step_str), "%lu", pdp_step);
371 ssnprintf(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 P_WARNING("srrd_create: 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 P_NOTICE("srrd_create_thread: File \"%s\" is already being created.",
493 P_ERROR("srrd_create_thread: Unable to lock file \"%s\".",
495 srrd_create_args_destroy(args);
499 ssnprintf(tmpfile, sizeof(tmpfile), "%s.async", args->filename);
501 status = srrd_create(tmpfile, args->pdp_step, args->last_up, args->argc,
504 P_WARNING("srrd_create_thread: srrd_create (%s) returned status %i.",
505 args->filename, status);
507 unlock_file(args->filename);
508 srrd_create_args_destroy(args);
512 status = rename(tmpfile, args->filename);
514 P_ERROR("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s", tmpfile,
515 args->filename, STRERRNO);
517 unlock_file(args->filename);
518 srrd_create_args_destroy(args);
522 DEBUG("srrd_create_thread: Successfully created RRD file \"%s\".",
525 unlock_file(args->filename);
526 srrd_create_args_destroy(args);
529 } /* }}} void *srrd_create_thread */
531 static int srrd_create_async(const char *filename, /* {{{ */
532 unsigned long pdp_step, time_t last_up, int argc,
534 srrd_create_args_t *args;
539 DEBUG("srrd_create_async: Creating \"%s\" in the background.", filename);
541 args = srrd_create_args_create(filename, pdp_step, last_up, argc, argv);
545 status = pthread_attr_init(&attr);
547 srrd_create_args_destroy(args);
551 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
553 pthread_attr_destroy(&attr);
554 srrd_create_args_destroy(args);
558 status = pthread_create(&thread, &attr, srrd_create_thread, args);
560 P_ERROR("srrd_create_async: pthread_create failed: %s", STRERROR(status));
561 pthread_attr_destroy(&attr);
562 srrd_create_args_destroy(args);
566 pthread_attr_destroy(&attr);
567 /* args is freed in srrd_create_thread(). */
569 } /* }}} int srrd_create_async */
574 int cu_rrd_create_file(const char *filename, /* {{{ */
575 const data_set_t *ds, const value_list_t *vl,
576 const rrdcreate_config_t *cfg) {
579 char **rra_def = NULL;
581 char **ds_def = NULL;
585 unsigned long stepsize;
587 if (check_create_dir(filename))
590 if ((rra_num = rra_get(&rra_def, vl, cfg)) < 1) {
591 P_ERROR("cu_rrd_create_file failed: Could not calculate RRAs");
595 if ((ds_num = ds_get(&ds_def, ds, vl, cfg)) < 1) {
596 P_ERROR("cu_rrd_create_file failed: Could not calculate DSes");
597 rra_free(rra_num, rra_def);
601 argc = ds_num + rra_num;
603 if ((argv = malloc(sizeof(*argv) * (argc + 1))) == NULL) {
604 P_ERROR("cu_rrd_create_file failed: %s", STRERRNO);
605 rra_free(rra_num, rra_def);
606 ds_free(ds_num, ds_def);
610 memcpy(argv, ds_def, ds_num * sizeof(char *));
611 memcpy(argv + ds_num, rra_def, rra_num * sizeof(char *));
612 argv[ds_num + rra_num] = NULL;
614 last_up = CDTIME_T_TO_TIME_T(vl->time);
616 last_up = time(NULL);
619 if (cfg->stepsize > 0)
620 stepsize = cfg->stepsize;
622 stepsize = (unsigned long)CDTIME_T_TO_TIME_T(vl->interval);
625 status = srrd_create_async(filename, stepsize, last_up, argc,
626 (const char **)argv);
628 P_WARNING("cu_rrd_create_file: srrd_create_async (%s) "
629 "returned status %i.",
631 } else /* synchronous */
633 status = lock_file(filename);
635 if (status == EEXIST)
636 P_NOTICE("cu_rrd_create_file: File \"%s\" is already being created.",
639 P_ERROR("cu_rrd_create_file: Unable to lock file \"%s\".", filename);
642 srrd_create(filename, stepsize, last_up, argc, (const char **)argv);
645 P_WARNING("cu_rrd_create_file: srrd_create (%s) returned status %i.",
648 DEBUG("cu_rrd_create_file: Successfully created RRD file \"%s\".",
651 unlock_file(filename);
656 ds_free(ds_num, ds_def);
657 rra_free(rra_num, rra_def);
660 } /* }}} int cu_rrd_create_file */