]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
graph: when an RRD file is missing, provide an empty one
authorVincent Bernat <vincent.bernat@dailymotion.com>
Mon, 8 Apr 2013 16:16:49 +0000 (18:16 +0200)
committerVincent Bernat <bernat@luffy.cx>
Thu, 20 Jun 2013 18:54:56 +0000 (20:54 +0200)
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.

src/rrd_fetch.c
src/rrd_graph.c
src/rrd_tool.h

index 94e88327a17f50b122f9d3326689b2db4e62816b..f012157aa9d442afef737d242f01f6f33259a042 100644 (file)
@@ -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 ? */
index 7d4a020649b7aea6819a9496b0082846e78358f9..dbcafade10450b3482ae839c89bdca3b4d903315 100644 (file)
@@ -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;
index 7df1971a8daa33dd9698cd848e4fbf6c9275be79..99111979d3d9bf70a664241f4aa85740ce2ae87c 100644 (file)
@@ -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,