From: Vincent Bernat Date: Mon, 8 Apr 2013 16:16:49 +0000 (+0200) Subject: graph: when an RRD file is missing, provide an empty one X-Git-Tag: v1.5.0-rc1~163^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fa523817e4152fef770125034b17f47acb2a866b;p=thirdparty%2Frrdtool-1.x.git graph: when an RRD file is missing, provide an empty one Instead of providing a RRD without any DS, we provide one with the expected DS. This allows one to use CDEF with this RRD. The DS contains only NaN values. --- diff --git a/src/rrd_fetch.c b/src/rrd_fetch.c index 94e88327..f012157a 100644 --- a/src/rrd_fetch.c +++ b/src/rrd_fetch.c @@ -205,6 +205,46 @@ int rrd_fetch_r( (filename, cf_idx, start, end, step, ds_cnt, ds_namv, data)); } /* int rrd_fetch_r */ +int rrd_fetch_empty( + time_t *start, + time_t *end, /* which time frame do you want ? */ + unsigned long *step, /* which stepsize do you want? */ + unsigned long *ds_cnt, /* number of data sources in file */ + char *ds_nam, /* wanted data source */ + char ***ds_namv, /* names of data_sources */ + rrd_value_t **data) +{ + unsigned long rows; + + if (((*ds_namv) = + (char **) malloc(sizeof(char *))) == NULL) { + rrd_set_error("malloc fetch ds_namv array"); + return (-1); + } + if ((((*ds_namv)[0]) = (char*)strdup(ds_nam)) == NULL) { + rrd_set_error("malloc fetch ds_namv entry"); + free(*ds_namv); + return (-1); + } + + *ds_cnt = 1; + if (*step == 0) *step = (*end - *start) / 100; + *start -= (*start % *step); + *end += (*step - *end % *step); + rows = (*end - *start) / *step + 1; + + if (((*data) = (rrd_value_t*)malloc(rows * sizeof(rrd_value_t))) == NULL) { + rrd_set_error("malloc fetch data area"); + free((*ds_namv)[0]); + free(*ds_namv); + return (-1); + } + + while (--rows) + (*data)[rows-1] = DNAN; + return (0); +} + int rrd_fetch_fn( const char *filename, /* name of the rrd */ enum cf_en cf_idx, /* which consolidation function ? */ diff --git a/src/rrd_graph.c b/src/rrd_graph.c index 7d4a0206..dbcafade 100644 --- a/src/rrd_graph.c +++ b/src/rrd_graph.c @@ -900,8 +900,14 @@ int data_fetch( if (status != 0) { if (im->extra_flags & ALLOW_MISSING_DS) { rrd_clear_error(); - im->gdes[i].ds_cnt = im->gdes[i].start = im->gdes[i].end = 0; - ft_step = 1; + if (rrd_fetch_empty(&im->gdes[i].start, + &im->gdes[i].end, + &ft_step, + &im->gdes[i].ds_cnt, + im->gdes[i].ds_nam, + &im->gdes[i].ds_namv, + &im->gdes[i].data) == -1) + return -1; } else return (status); } } @@ -918,8 +924,14 @@ int data_fetch( if (im->extra_flags & ALLOW_MISSING_DS) { /* Unable to fetch data, assume fake data */ rrd_clear_error(); - im->gdes[i].ds_cnt = im->gdes[i].start = im->gdes[i].end = 0; - ft_step = 1; + if (rrd_fetch_empty(&im->gdes[i].start, + &im->gdes[i].end, + &ft_step, + &im->gdes[i].ds_cnt, + im->gdes[i].ds_nam, + &im->gdes[i].ds_namv, + &im->gdes[i].data) == -1) + return -1; } else return -1; } } @@ -947,8 +959,7 @@ int data_fetch( im->gdes[i].ds = ii; } } - if ((im->gdes[i].ds == -1) && - !(im->extra_flags & ALLOW_MISSING_DS)) { + if (im->gdes[i].ds == -1) { rrd_set_error("No DS called '%s' in '%s'", im->gdes[i].ds_nam, im->gdes[i].rrd); return -1; diff --git a/src/rrd_tool.h b/src/rrd_tool.h index 7df1971a..99111979 100644 --- a/src/rrd_tool.h +++ b/src/rrd_tool.h @@ -89,6 +89,15 @@ extern "C" { char ***ds_namv, rrd_value_t **data); + int rrd_fetch_empty( + time_t *start, + time_t *end, + unsigned long *step, + unsigned long *ds_cnt, + char *ds_nam, + char ***ds_namv, + rrd_value_t **data); + #ifdef HAVE_LIBDBI int rrd_fetch_fn_libdbi(const char *filename, enum cf_en cf_idx,