]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
the beginnings of a callback interface
authorTobias Oetiker <tobi@oetiker.ch>
Wed, 28 May 2014 16:46:38 +0000 (18:46 +0200)
committerTobias Oetiker <tobi@oetiker.ch>
Wed, 28 May 2014 16:46:38 +0000 (18:46 +0200)
bindings/perl-shared/RRDs.pm
src/Makefile.am
src/librrd.sym.in.in
src/rrd_fetch.c
src/rrd_fetch_cb.c [new file with mode: 0644]
src/rrd_tool.h

index bb7d6becb3bb1937752b2cd16408e1493927c7ca..74c202df9403d3d55ee9b05c07663be5d90e7c42 100644 (file)
@@ -34,6 +34,7 @@ RRDs - Access RRDtool as a shared module
   RRDs::dump ...
   RRDs::restore ...
   RRDs::flushcached ...
+  RRDs::register_fetch_cb ...
   $RRDs::VERSION
 
 =head1 DESCRIPTION
@@ -147,6 +148,45 @@ epoch (1970-01-01) for the supplied "start" and "end" arguments, respectively.
 
 See the examples directory for more ways to use this extension.
 
+=head2 Fetch Callback Function
+
+Normally when using graph, xport or fetch the data you see will come from an
+actual rrd file.  Some people who like the look of rrd charts, therefore
+export their data from a database and then load it into an rrd file just to
+be able to call rrdgraph on it. Using a custom callback, you can supply your own
+code for handling the data requests from graph, xport and fetch.
+
+Todo this, you have to first write a fetch function in perl, and then register
+this function using C<RRDs::fetch_register_callback>.
+
+Finally you can use the pseudo path name B<cb//>[I<filename>] to tell
+rrdtool to use your callback routine instead of the normal rrdtool fetch function
+to organize the data required.
+
+The callback function must look like this:
+
+  sub fetch_callback {
+      my $args_hash = shift;
+      # {
+      #  filename => 'cb//somefilename',
+      #  cd => 'AVERAGE',
+      #  start => 1401295291,   
+      #  end => 1401295591,
+      #  step => 300 }
+
+      # do some clever thing to get that data ready    
+
+      return {
+          start => $unix_timestamp,
+          step => $step_width,
+          data => {
+              dsName1 => [ value1, value2, ... ],
+              dsName2 => [ value1, value2, ... ],
+              dsName3 => [ value1, value2, ... ],
+          }
+     };
+  }
+
 =head1 NOTE
 
 If you are manipulating the TZ variable you should also call the POSIX
index 974b9cbdf828201f94d0f05e6314af4f7f554d89..4f68b238778b2c41b8010cb61ee1f0129c910866 100644 (file)
@@ -44,6 +44,7 @@ RRD_C_FILES =         \
        rrd_dump.c      \
        rrd_flushcached.c \
        rrd_fetch.c     \
+       rrd_fetch_cb.c  \
        rrd_resize.c \
        rrd_tune.c      \
        rrd_modify.c
index a0e0e7b52b0bf38bc91fec2e6b30a81d374cb2d7..ba4975954e358bf06e298caa068f3c84a1084da8 100644 (file)
@@ -13,6 +13,7 @@ rrd_dump_r
 rrd_dump_cb_r
 rrd_fetch
 rrd_fetch_r
+rrd_fetch_cb_register
 rrd_flushcached
 rrd_first
 rrd_first_r
index af74a2f642f24c2d4de938fc04b0de3b7f72b5a1..45e2a09eb049196a5100c2b0102e1fedb7f504c6 100644 (file)
@@ -292,6 +292,9 @@ int rrd_fetch_fn(
        return rrd_fetch_fn_libdbi(filename,cf_idx,start,end,step,ds_cnt,ds_namv,data);
     }
 #endif
+    if (strncmp("cb//",filename,5)==0) {
+       return rrd_fetch_fn_cb(filename,cf_idx,start,end,step,ds_cnt,ds_namv,data);
+    }
 
     rrd_init(&rrd);
     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
diff --git a/src/rrd_fetch_cb.c b/src/rrd_fetch_cb.c
new file mode 100644 (file)
index 0000000..b7dde7e
--- /dev/null
@@ -0,0 +1,35 @@
+#include "rrd_tool.h"
+#include "unused.h"
+#include <time.h>
+
+
+static rrd_fetch_cb_t callback = NULL;
+
+int rrd_fetch_cb_register(rrd_fetch_cb_t cb){
+     callback = cb;
+     /* if we find a way to figure out of cb provided is to our
+        liking or not, we could return 0 if there is a problem */
+     return 1;
+}
+
+int
+rrd_fetch_fn_cb(
+    const char     *filename,  /* name of the rrd */
+    enum cf_en     cf_idx, /* consolidation function */
+    time_t         *start,
+    time_t         *end,       /* which time frame do you want ?
+                               * will be changed to represent reality */
+    unsigned long  *step,      /* which stepsize do you want? 
+                               * will be changed to represent reality */
+    unsigned long  *ds_cnt,    /* number of data sources in file */
+    char           ***ds_namv, /* names of data_sources */
+    rrd_value_t    **data)     /* two dimensional array containing the data */
+{
+    if (callback){
+        return callback(filename,cf_idx,start,end,step,ds_cnt,ds_namv,data);
+    }
+    else {
+        rrd_set_error("use rrd_fetch_cb_register to register your callback prior to calling rrd_fetch_fn_cb");
+        return -1;   
+    }
+}
index d772d07f3237934a74f888a3196b3925e9497b92..dfe8e6ed632a95f0bb7851f6e40e574766362a51 100644 (file)
@@ -100,7 +100,7 @@ extern    "C" {
 
 
 #ifdef HAVE_LIBDBI
-int rrd_fetch_fn_libdbi(const char *filename, enum cf_en cf_idx,
+    int rrd_fetch_fn_libdbi(const char *filename, enum cf_en cf_idx,
                        time_t *start,time_t *end,
                        unsigned long *step,
                        unsigned long *ds_cnt,
@@ -108,6 +108,30 @@ int rrd_fetch_fn_libdbi(const char *filename, enum cf_en cf_idx,
                        rrd_value_t **data);
 #endif
 
+typedef int (*rrd_fetch_cb_t)(
+    const char     *filename,  /* name of the rrd */
+    enum cf_en     cf_idx, /* consolidation function */
+    time_t         *start,
+    time_t         *end,       /* which time frame do you want ?
+                                * will be changed to represent reality */
+    unsigned long  *step,      /* which stepsize do you want? 
+                                * will be changed to represent reality */
+    unsigned long  *ds_cnt,    /* number of data sources in file */
+    char           ***ds_namv, /* names of data_sources */
+    rrd_value_t    **data      /* two dimensional array containing the data */
+);
+                                                                                                
+
+    int rrd_fetch_fn_cb(const char *filename, enum cf_en cf_idx,
+        time_t *start,time_t *end,
+        unsigned long *step,
+        unsigned long *ds_cnt,
+        char        ***ds_namv,
+        rrd_value_t **data);
+
+    int rrd_fetch_cb_register(rrd_fetch_cb_t cb);
+
+
 #define RRD_READONLY    (1<<0)
 #define RRD_READWRITE   (1<<1)
 #define RRD_CREAT       (1<<2)