Re: rrdtool fetch / JSON-Output

Michael Markstaller <[email protected]> Fri, 13 Jan 2012 14:12:55 +0100
Newsgroups gmane.comp.db.rrdtool.devel
Message-ID <[email protected]>
Hi,

thanks for looking at it, maybe I was too nervous on my first post ;)
Now its attached.. (I use this with quilt in the debian-lenny package)
Simply copied the fetch-function with some brackets..


regards

Michael

Am 13.01.2012 08:25, schrieb Tobias Oetiker:
> Hi Michael,
>
> I guess adding json will help many people, so I will add it ...
> (got another patch already from a few months back, but if you can
> send me yours, I will be glad to take it for inspiration too :-)
> (it was not attached)
>
> cheers
> tobi
>
> Yesterday Michael Markstaller wrote:
>
>> Hi,
>>
>> my first post to the list and to be honest: I havent followed this list
>> and rrdtool development for many years though using it widely&  every
>> single day. It just worked to well ;)
>>
>> I created a little patch to make "rrdtool fetchj .." simply output json
>> instead of text, attached.
>>
>> Intentions, the answers to why etc:
>> - Target-Platforms are rather low-end (AMD Geode, OpenWRT Routers), so
>> with rrdtool 1.3, 1.4 was dead slow in graphing on these low-ends during
>> my tests.
>> - avoid (another) shellscript or CGI to wrap "rrdtool fetch" output into
>> JSON what I needed to feed jQuery/Flot in this case.
>> - but "rrdtool fetch" does exactly what I need, with all it's benefits,
>> fast, small data to transfer, right data, autoscaling etc.pp.
>>
>>
>> Please let me know your thoughts, if it makes sense at all.
>> I know for sure that a patch against an rather old version isn't perfect
>> but it was as mentioned chosen precisely..
>>
>> Could also do it for current 1.4/trunk but it's currently not my goal
>> because of serious performance-problems (which might have many other
>> causes than rrdtool itself for sure!)
>>
>>
>> best regards
>>
>> Michael Markstaller
>>
>> _______________________________________________
>> rrd-developers mailing list
>> [email protected]
>> https://lists.oetiker.ch/cgi-bin/listinfo/rrd-developers
>>
>>
>

-- 
Michael Markstaller

Elaborated Networks GmbH
www.elabnet.de - www.wiregate.de
Lise-Meitner-Str. 1, D-85662 Hohenbrunn, Germany
fon: +49-8102-8951-60, fax: +49-8102-8951-80
Geschäftsführer: Stefan Werner, Michael Markstaller
Amtsgericht München HRB 125120, Ust-ID: DE201281054

_______________________________________________
rrd-developers mailing list
[email protected]
https://lists.oetiker.ch/cgi-bin/listinfo/rrd-developers
rrdtool-1.3.1-fetchj.patch (text/x-patch, 3.1 KB)
--- a/src/rrd_tool.c
+++ b/src/rrd_tool.c
@@ -54,7 +54,7 @@
     const char *help_list =
         N_
         ("Valid commands: create, update, updatev, graph, graphv,  dump, restore,\n"
-         "\t\tlast, lastupdate, first, info, fetch, tune,\n"
+         "\t\tlast, lastupdate, first, info, fetch, fetchj, tune,\n"
          "\t\tresize, xport\n\n");
 
     const char *help_listremote =
@@ -115,6 +115,12 @@
            "\t\t[-r|--resolution resolution]\n"
            "\t\t[-s|--start start] [-e|--end end]\n\n");
 
+    const char *help_fetchj =
+        N_("* fetchj - fetch data out of an RRD as JSON\n\n"
+           "\trrdtool fetchj filename.rrd CF\n"
+           "\t\t[-r|--resolution resolution]\n"
+           "\t\t[-s|--start start] [-e|--end end]\n\n");
+
 /* break up very large strings (help_graph, help_tune) for ISO C89 compliance*/
 
     const char *help_graph0 =
@@ -214,7 +220,7 @@
            "Public License Version 2. (www.gnu.org/copyleft/gpl.html)\n\n"
            "For more information read the RRD manpages\n\n");
     enum { C_NONE, C_CREATE, C_DUMP, C_INFO, C_RESTORE, C_LAST,
-        C_LASTUPDATE, C_FIRST, C_UPDATE, C_FETCH, C_GRAPH, C_GRAPHV,
+        C_LASTUPDATE, C_FIRST, C_UPDATE, C_FETCH, C_FETCHJ, C_GRAPH, C_GRAPHV,
         C_TUNE,
         C_RESIZE, C_XPORT, C_QUIT, C_LS, C_CD, C_MKDIR, C_PWD,
         C_UPDATEV
@@ -242,6 +248,8 @@
             help_cmd = C_UPDATEV;
         else if (!strcmp(cmd, "fetch"))
             help_cmd = C_FETCH;
+        else if (!strcmp(cmd, "fetchj"))
+            help_cmd = C_FETCHJ;
         else if (!strcmp(cmd, "graph"))
             help_cmd = C_GRAPH;
         else if (!strcmp(cmd, "graphv"))
@@ -302,6 +310,9 @@
     case C_FETCH:
         fputs(_(help_fetch), stdout);
         break;
+    case C_FETCHJ:
+        fputs(_(help_fetchj), stdout);
+        break;
     case C_GRAPH:
         fputs(_(help_graph0), stdout);
         fputs(_(help_graph1), stdout);
@@ -695,6 +706,37 @@
             free(ds_namv);
             free(data);
         }
+    } else if (strcmp("fetchj", argv[1]) == 0) {
+        time_t    start, end, ti;
+        unsigned long step, ds_cnt, i, ii;
+        rrd_value_t *data, *datai;
+        char    **ds_namv;
+        int written = 0;
+
+        if (rrd_fetch
+            (argc - 1, &argv[1], &start, &end, &step, &ds_cnt, &ds_namv,
+             &data) != -1) {
+            datai = data;
+            // printf("Content-Type: text/plain\n\n");
+            printf("[");
+            for (ti = start + step; ti <= end; ti += step) {
+                if (written==1)
+                       printf(",");
+                printf("[%10lu000,[", ti);
+                for (ii = 0; ii < ds_cnt; ii++) {
+                    if (ii > 0)
+                	printf(",");
+                    printf("\"%0.2f\"", *(datai++));
+                }
+                printf("]]");
+                written = 1;
+            }
+            for (i = 0; i < ds_cnt; i++)
+                free(ds_namv[i]);
+            free(ds_namv);
+            free(data);
+            printf("]\n");
+        }
     } else if (strcmp("xport", argv[1]) == 0) {
         int       xxsize;
         unsigned long int j = 0;