/*****************************************************************************
- * RRDtool 1.2.99907080300 Copyright by Tobi Oetiker, 1997-2007
+ * RRDtool 1.3rc4 Copyright by Tobi Oetiker, 1997-2008
*****************************************************************************
* rrd_create.c creates new rrds
*****************************************************************************/
+#include <stdlib.h>
+#include <time.h>
#include <locale.h>
#include "rrd_tool.h"
const char *def,
rrd_t *rrd,
int ds_idx);
+long int rra_random_row(
+ rra_def_t *);
int rrd_create(
int argc,
/* static header */
if ((rrd.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) {
rrd_set_error("allocating rrd.stat_head");
- rrd_free(&rrd);
+ free(rrd.stat_head);
return (-1);
}
/* live header */
if ((rrd.live_head = calloc(1, sizeof(live_head_t))) == NULL) {
rrd_set_error("allocating rrd.live_head");
- rrd_free(&rrd);
+ free(rrd.stat_head);
+ free(rrd.live_head);
return (-1);
}
old_size + sizeof(ds_def_t))) ==
NULL) {
rrd_set_error("allocating rrd.ds_def");
- rrd_free(&rrd);
+ free(rrd.stat_head);
+ free(rrd.live_head);
return (-1);
}
memset(&rrd.ds_def[rrd.stat_head->ds_cnt], 0, sizeof(ds_def_t));
rrd_set_error("invalid DS format");
}
if (rrd_test_error()) {
- rrd_free(&rrd);
+ free(rrd.stat_head);
+ free(rrd.live_head);
return -1;
}
}
if (rrd_test_error()) {
- rrd_free(&rrd);
+ free(rrd.stat_head);
+ free(rrd.live_head);
return -1;
}
rrd.stat_head->ds_cnt++;
old_size + sizeof(rra_def_t))) ==
NULL) {
rrd_set_error("allocating rrd.rra_def");
- rrd_free(&rrd);
+ free(rrd.stat_head);
+ free(rrd.live_head);
return (-1);
}
memset(&rrd.rra_def[rrd.stat_head->rra_cnt], 0,
case CF_DEVSEASONAL:
case CF_SEASONAL:
/* optional smoothing window */
- if (sscanf(token, "smoothing-window=%lf",
- &(rrd.rra_def[rrd.stat_head->rra_cnt].
- par[RRA_seasonal_smoothing_window].u_val))) {
- strcpy(rrd.stat_head->version, RRD_VERSION); /* smoothing-window causes Version 4 */
+ if (sscanf(token, "smoothing-window=%lf",
+ &(rrd.rra_def[rrd.stat_head->rra_cnt].
+ par[RRA_seasonal_smoothing_window].
+ u_val))) {
+ strcpy(rrd.stat_head->version, RRD_VERSION); /* smoothing-window causes Version 4 */
if (rrd.rra_def[rrd.stat_head->rra_cnt].
- par[RRA_seasonal_smoothing_window].u_val < 0.0
- || rrd.rra_def[rrd.stat_head->rra_cnt].
- par[RRA_seasonal_smoothing_window].u_val > 1.0) {
- rrd_set_error("Invalid smoothing-window %f: must be between 0 and 1",
- rrd.rra_def[rrd.stat_head->rra_cnt].
- par[RRA_seasonal_smoothing_window].u_val);
+ par[RRA_seasonal_smoothing_window].u_val < 0.0
+ || rrd.rra_def[rrd.stat_head->rra_cnt].
+ par[RRA_seasonal_smoothing_window].u_val >
+ 1.0) {
+ rrd_set_error
+ ("Invalid smoothing-window %f: must be between 0 and 1",
+ rrd.rra_def[rrd.stat_head->rra_cnt].
+ par[RRA_seasonal_smoothing_window].
+ u_val);
}
} else {
rrd_set_error("Invalid option %s", token);
if (rrd_test_error()) {
/* all errors are unrecoverable */
free(argvcopy);
- rrd_free(&rrd);
+ free(rrd.stat_head);
+ free(rrd.live_head);
return (-1);
}
token = strtok_r(NULL, ":", &tokptr);
if (create_hw_contingent_rras(&rrd, period, hashed_name) ==
-1) {
rrd_set_error("creating contingent RRA");
- rrd_free(&rrd);
+ free(rrd.stat_head);
+ free(rrd.live_head);
return -1;
}
}
rrd.stat_head->rra_cnt++;
} else {
rrd_set_error("can't parse argument '%s'", argv[i]);
- rrd_free(&rrd);
+ free(rrd.stat_head);
+ free(rrd.live_head);
return -1;
}
}
if (rrd.stat_head->rra_cnt < 1) {
rrd_set_error("you must define at least one Round Robin Archive");
- rrd_free(&rrd);
+ free(rrd.stat_head);
+ free(rrd.live_head);
return (-1);
}
if (rrd.stat_head->ds_cnt < 1) {
rrd_set_error("you must define at least one Data Source");
- rrd_free(&rrd);
+ free(rrd.stat_head);
+ free(rrd.live_head);
return (-1);
}
return rrd_create_fn(filename, &rrd);
rrd_file_t *rrd_file_dn;
rrd_t rrd_dn;
- if ((rrd_file =
- open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) == NULL) {
+ if ((rrd_file = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
rrd_set_error("creating '%s': %s", file_name, rrd_strerror(errno));
- rrd_free(rrd);
+ free(rrd->stat_head);
+ free(rrd->live_head);
return (-1);
}
if ((rrd->pdp_prep = calloc(1, sizeof(pdp_prep_t))) == NULL) {
rrd_set_error("allocating pdp_prep");
- rrd_free(rrd);
+ free(rrd->stat_head);
+ free(rrd->live_head);
close(rrd_file);
return (-1);
}
- strcpy(rrd->pdp_prep->last_ds, "UNKN");
+ strcpy(rrd->pdp_prep->last_ds, "U");
rrd->pdp_prep->scratch[PDP_val].u_val = 0.0;
rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt =
if ((rrd->cdp_prep = calloc(1, sizeof(cdp_prep_t))) == NULL) {
rrd_set_error("allocating cdp_prep");
- rrd_free(rrd);
+ free(rrd->stat_head);
+ free(rrd->live_head);
close(rrd_file);
return (-1);
}
if ((rrd->rra_ptr = calloc(1, sizeof(rra_ptr_t))) == NULL) {
rrd_set_error("allocating rra_ptr");
- rrd_free(rrd);
+ free(rrd->stat_head);
+ free(rrd->live_head);
close(rrd_file);
return (-1);
}
* would occur for cur_row = 1 because rrd_update increments
* the pointer a priori. */
for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
- rrd->rra_ptr->cur_row = rrd->rra_def[i].row_cnt - 1;
+ rrd->rra_ptr->cur_row = rra_random_row(&rrd->rra_def[i]);
write(rrd_file, rrd->rra_ptr, sizeof(rra_ptr_t));
}
/* write the empty data area */
if ((unknown = (rrd_value_t *) malloc(512 * sizeof(rrd_value_t))) == NULL) {
rrd_set_error("allocating unknown");
- rrd_free(rrd);
+ free(rrd->stat_head);
+ free(rrd->live_head);
close(rrd_file);
return (-1);
}
}
free(unknown);
fdatasync(rrd_file);
- rrd_free(rrd);
+ free(rrd->stat_head);
+ free(rrd->live_head);
if (close(rrd_file) == -1) {
rrd_set_error("creating rrd: %s", rrd_strerror(errno));
return -1;
/* flush all we don't need out of the cache */
rrd_file_dn = rrd_open(file_name, &rrd_dn, RRD_READONLY);
rrd_dontneed(rrd_file_dn, &rrd_dn);
+ rrd_free(&rrd_dn);
rrd_close(rrd_file_dn);
return (0);
}
+
+static int rand_init = 0;
+
+long int rra_random_row(
+ rra_def_t *rra)
+{
+ if (!rand_init) {
+ srandom((unsigned int) time(NULL) + (unsigned int) getpid());
+ rand_init++;
+ }
+
+ return random() % rra->row_cnt;
+}