]> arthur.barton.de Git - netdata.git/blobdiff - src/rrd2json.c
added support for generating SVG badges with chart data
[netdata.git] / src / rrd2json.c
old mode 100755 (executable)
new mode 100644 (file)
index 7a5f6df..5adaab0
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdint.h>
+#include <math.h>
 
 #include "log.h"
 #include "common.h"
@@ -23,6 +24,7 @@ void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
                "\t\t\t\"name\": \"%s\",\n"
                "\t\t\t\"type\": \"%s\",\n"
                "\t\t\t\"family\": \"%s\",\n"
+               "\t\t\t\"context\": \"%s\",\n"
                "\t\t\t\"title\": \"%s\",\n"
                "\t\t\t\"priority\": %ld,\n"
                "\t\t\t\"enabled\": %s,\n"
@@ -38,6 +40,7 @@ void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb)
                , st->name
                , st->type
                , st->family
+               , st->context
                , st->title
                , st->priority
                , st->enabled?"true":"false"
@@ -122,6 +125,7 @@ unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
                "\t\t\t\"name\": \"%s\",\n"
                "\t\t\t\"type\": \"%s\",\n"
                "\t\t\t\"family\": \"%s\",\n"
+               "\t\t\t\"context\": \"%s\",\n"
                "\t\t\t\"title\": \"%s\",\n"
                "\t\t\t\"priority\": %ld,\n"
                "\t\t\t\"enabled\": %d,\n"
@@ -144,6 +148,7 @@ unsigned long rrd_stats_one_json(RRDSET *st, char *options, BUFFER *wb)
                , st->name
                , st->type
                , st->family
+               , st->context
                , st->title
                , st->priority
                , st->enabled
@@ -259,16 +264,21 @@ void rrd_stats_all_json(BUFFER *wb)
 
 // ----------------------------------------------------------------------------
 
-// RRDR options
+// RRDR dimension options
 #define RRDR_EMPTY     0x01 // the dimension contains / the value is empty (null)
 #define RRDR_RESET     0x02 // the dimension contains / the value is reset
 #define RRDR_HIDDEN    0x04 // the dimension contains / the value is hidden
 #define RRDR_NONZERO   0x08 // the dimension contains / the value is non-zero
 
+// RRDR result options
+#define RRDR_RESULT_OPTION_ABSOLUTE 0x00000001
+#define RRDR_RESULT_OPTION_RELATIVE 0x00000002
 
 typedef struct rrdresult {
        RRDSET *st;                     // the chart this result refers to
 
+       uint32_t result_options;        // RRDR_RESULT_OPTION_*
+
        int d;                                  // the number of dimensions
        long n;                                 // the number of values in the arrays
        long rows;                          // the number of rows used
@@ -672,18 +682,18 @@ static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
                        sq[0] = '"';
                }
                row_annotations = 1;
-               snprintf(pre_date,   100, "             {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
-               snprintf(post_date,  100, "%s}", sq);
-               snprintf(pre_label,  100, ",\n          {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
-               snprintf(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
-               snprintf(pre_value,  100, ",{%sv%s:", kq, kq);
-               snprintf(post_value, 100, "}");
-               snprintf(post_line,  100, "]}");
-               snprintf(data_begin, 100, "\n   ],\n    %srows%s:\n     [\n", kq, kq);
-               snprintf(finish,     100, "\n   ]\n}");
-
-               snprintf(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
-               snprintf(normal_annotation,   200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
+               snprintfz(pre_date,   100, "            {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
+               snprintfz(post_date,  100, "%s}", sq);
+               snprintfz(pre_label,  100, ",\n         {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
+               snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
+               snprintfz(pre_value,  100, ",{%sv%s:", kq, kq);
+               strcpy(post_value,         "}");
+               strcpy(post_line,          "]}");
+               snprintfz(data_begin, 100, "\n  ],\n    %srows%s:\n     [\n", kq, kq);
+               strcpy(finish,             "\n  ]\n}");
+
+               snprintfz(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
+               snprintfz(normal_annotation,   200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
 
                buffer_sprintf(wb, "{\n %scols%s:\n     [\n", kq, kq, kq, kq);
                buffer_sprintf(wb, "            {%sid%s:%s%s,%slabel%s:%stime%s,%spattern%s:%s%s,%stype%s:%sdatetime%s},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq);
@@ -707,18 +717,18 @@ static void rrdr2json(RRDR *r, BUFFER *wb, uint32_t options, int datatable)
                        dates_with_new = 1;
                }
                if( options & RRDR_OPTION_OBJECTSROWS )
-                       snprintf(pre_date,   100, "             { ");
+                       strcpy(pre_date, "              { ");
                else
-                       snprintf(pre_date,   100, "             [ ");
-               snprintf(pre_label,  100, ", \"");
-               snprintf(post_label, 100, "\"");
-               snprintf(pre_value,  100, ", ");
+                       strcpy(pre_date, "              [ ");
+               strcpy(pre_label,  ", \"");
+               strcpy(post_label, "\"");
+               strcpy(pre_value,  ", ");
                if( options & RRDR_OPTION_OBJECTSROWS )
-                       snprintf(post_line,  100, "}");
+                       strcpy(post_line, "}");
                else
-                       snprintf(post_line,  100, "]");
-               snprintf(data_begin, 100, "],\n %sdata%s:\n     [\n", kq, kq);
-               snprintf(finish,     100, "\n   ]\n}");
+                       strcpy(post_line, "]");
+               snprintfz(data_begin, 100, "],\n        %sdata%s:\n     [\n", kq, kq);
+               strcpy(finish,             "\n  ]\n}");
 
                buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
                buffer_sprintf(wb, "%stime%s", sq, sq);
@@ -927,7 +937,7 @@ static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startlin
                        // generate the local date time
                        struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
                        if(!tm) { error("localtime() failed."); continue; }
-                       buffer_date(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
+                       buffer_date(wb, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
                }
 
                if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
@@ -975,104 +985,121 @@ static void rrdr2csv(RRDR *r, BUFFER *wb, uint32_t options, const char *startlin
        //info("RRD2CSV(): %s: END", r->st->id);
 }
 
-static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
-{
-       //info("RRD2SSV(): %s: BEGIN", r->st->id);
-       long c, i;
+inline static calculated_number rrdr2value(RRDR *r, long i, uint32_t options, int *all_values_are_null) {
+       long c;
        RRDDIM *d;
 
-       buffer_strcat(wb, prefix);
-       long start = 0, end = rrdr_rows(r), step = 1;
-       if((options & RRDR_OPTION_REVERSED)) {
-               start = rrdr_rows(r) - 1;
-               end = -1;
-               step = -1;
-       }
+       calculated_number *cn = &r->v[ i * r->d ];
+       uint8_t *co = &r->o[ i * r->d ];
+
+       calculated_number sum = 0, min = 0, max = 0, v;
+       int all_null = 1, init = 1;
 
-       // for each line in the array
        calculated_number total = 1;
-       for(i = start; i != end ;i += step) {
+       if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
+               total = 0;
+               for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
+                       calculated_number n = cn[c];
 
-               calculated_number *cn = &r->v[ i * r->d ];
-               uint8_t *co = &r->o[ i * r->d ];
+                       if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
+                               n = -n;
 
-               calculated_number sum = 0, min = 0, max = 0, v;
-               int all_null = 1, init = 1;
+                       total += n;
+               }
+               // prevent a division by zero
+               if(total == 0) total = 1;
+       }
 
-               if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
-                       total = 0;
-                       for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
-                               calculated_number n = cn[c];
+       // for each dimension
+       for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
+               if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
+               if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
 
-                               if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
-                                       n = -n;
+               calculated_number n = cn[c];
 
-                               total += n;
+               if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
+                       n = -n;
+
+               if(unlikely(options & RRDR_OPTION_PERCENTAGE))
+                       n = n * 100 / total;
+
+               if(unlikely(init)) {
+                       if(n > 0) {
+                               min = 0;
+                               max = n;
                        }
-                       // prevent a division by zero
-                       if(total == 0) total = 1;
+                       else {
+                               min = n;
+                               max = 0;
+                       }
+                       init = 0;
                }
 
-               // for each dimension
-               for(c = 0, d = r->st->dimensions; d && c < r->d ;c++, d = d->next) {
-                       if(unlikely(r->od[c] & RRDR_HIDDEN)) continue;
-                       if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_NONZERO))) continue;
+               if(likely(!(co[c] & RRDR_EMPTY))) {
+                       all_null = 0;
+                       sum += n;
+               }
 
-                       calculated_number n = cn[c];
+               if(n < min) min = n;
+               if(n > max) max = n;
+       }
 
-                       if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
-                               n = -n;
+       if(unlikely(all_null)) {
+               if(likely(*all_values_are_null))
+                       *all_values_are_null = 1;
+               return 0;
+       }
+       else {
+               if(likely(*all_values_are_null))
+                       *all_values_are_null = 0;
+       }
 
-                       if(unlikely(options & RRDR_OPTION_PERCENTAGE))
-                               n = n * 100 / total;
+       if(options & RRDR_OPTION_MIN2MAX)
+               v = max - min;
+       else
+               v = sum;
 
-                       if(unlikely(init)) {
-                               if(n > 0) {
-                                       min = 0;
-                                       max = n;
-                               }
-                               else {
-                                       min = n;
-                                       max = 0;
-                               }
-                               init = 0;
-                       }
+       return v;
+}
 
-                       if(likely(!(co[c] & RRDR_EMPTY))) {
-                               all_null = 0;
-                               sum += n;
-                       }
+static void rrdr2ssv(RRDR *r, BUFFER *wb, uint32_t options, const char *prefix, const char *separator, const char *suffix)
+{
+       //info("RRD2SSV(): %s: BEGIN", r->st->id);
+       long i;
+
+       buffer_strcat(wb, prefix);
+       long start = 0, end = rrdr_rows(r), step = 1;
+       if((options & RRDR_OPTION_REVERSED)) {
+               start = rrdr_rows(r) - 1;
+               end = -1;
+               step = -1;
+       }
 
-                       if(n < min) min = n;
-                       if(n > max) max = n;
+       // for each line in the array
+       for(i = start; i != end ;i += step) {
+               int all_values_are_null = 0;
+               calculated_number v = rrdr2value(r, i, options, &all_values_are_null);
+
+               if(likely(i != start)) {
+                       if(r->min > v) r->min = v;
+                       if(r->max < v) r->max = v;
+               }
+               else {
+                       r->min = v;
+                       r->max = v;
                }
 
                if(likely(i != start))
                        buffer_strcat(wb, separator);
 
-               if(all_null) {
+               if(all_values_are_null) {
                        if(options & RRDR_OPTION_NULL2ZERO)
                                buffer_strcat(wb, "0");
                        else
                                buffer_strcat(wb, "null");
                }
-               else {
-                       if(options & RRDR_OPTION_MIN2MAX)
-                               v = max - min;
-                       else
-                               v = sum;
-
-                       if(likely(i != start)) {
-                               if(r->min > v) r->min = v;
-                               if(r->max < v) r->max = v;
-                       }
-                       else {
-                               r->min = v;
-                               r->max = v;
-                       }
-
+               else
                        buffer_rrd_value(wb, v);
-               }
        }
        buffer_strcat(wb, suffix);
        //info("RRD2SSV(): %s: END", r->st->id);
@@ -1200,6 +1227,7 @@ cleanup:
 RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int group_method)
 {
        int debug = st->debug;
+       int absolute_period_requested = -1;
 
        time_t first_entry_t = rrdset_first_entry_t(st);
        time_t last_entry_t = rrdset_last_entry_t(st);
@@ -1207,14 +1235,22 @@ RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int g
        if(before == 0 && after == 0) {
                before = last_entry_t;
                after = first_entry_t;
+               absolute_period_requested = 0;
        }
 
        // allow relative for before and after
-       if(before <= st->update_every * st->entries)
+       if(before <= st->update_every * st->entries) {
                before = last_entry_t + before;
+               absolute_period_requested = 0;
+       }
 
-       if(after <= st->update_every * st->entries)
+       if(after <= st->update_every * st->entries) {
                after = last_entry_t + after;
+               absolute_period_requested = 0;
+       }
+
+       if(absolute_period_requested == -1)
+               absolute_period_requested = 1;
 
        // make sure they are within our timeframe
        if(before > last_entry_t) before = last_entry_t;
@@ -1314,6 +1350,11 @@ RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int g
                return r;
        }
 
+       if(absolute_period_requested == 1)
+               r->result_options |= RRDR_RESULT_OPTION_ABSOLUTE;
+       else
+               r->result_options |= RRDR_RESULT_OPTION_RELATIVE;
+
        // find how many dimensions we have
        long dimensions = r->d;
 
@@ -1427,7 +1468,7 @@ RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int g
 
                        switch(group_method) {
                                case GROUP_MAX:
-                                       if(unlikely(abs(value) > abs(group_values[c])))
+                                       if(unlikely(fabsl(value) > fabsl(group_values[c])))
                                                group_values[c] = value;
                                        break;
 
@@ -1489,6 +1530,40 @@ RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int g
        return r;
 }
 
+int rrd2value(RRDSET *st, BUFFER *wb, calculated_number *n, BUFFER *dimensions, long points, long long after, long long before, int group_method, uint32_t options, time_t *latest_timestamp, int *value_is_null)
+{
+       RRDR *r = rrd2rrdr(st, points, after, before, group_method);
+       if(!r) {
+               if(value_is_null) *value_is_null = 1;
+               return 500;
+       }
+
+       if(rrdr_rows(r) == 0) {
+               rrdr_free(r);
+               if(value_is_null) *value_is_null = 1;
+               return 400;
+       }
+
+       if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
+               wb->options |= WB_CONTENT_NO_CACHEABLE;
+       else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
+               wb->options |= WB_CONTENT_CACHEABLE;
+
+       options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
+
+       if(dimensions)
+               rrdr_disable_not_selected_dimensions(r, buffer_tostring(dimensions));
+
+       if(latest_timestamp)
+               *latest_timestamp = r->before;
+
+       long i = (options & RRDR_OPTION_REVERSED)?rrdr_rows(r) - 1:0;
+       *n = rrdr2value(r, i, options, value_is_null);
+
+       rrdr_free(r);
+       return 200;
+}
+
 int rrd2format(RRDSET *st, BUFFER *wb, BUFFER *dimensions, uint32_t format, long points, long long after, long long before, int group_method, uint32_t options, time_t *latest_timestamp)
 {
        RRDR *r = rrd2rrdr(st, points, after, before, group_method);
@@ -1497,6 +1572,11 @@ int rrd2format(RRDSET *st, BUFFER *wb, BUFFER *dimensions, uint32_t format, long
                return 500;
        }
 
+       if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
+               wb->options |= WB_CONTENT_NO_CACHEABLE;
+       else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
+               wb->options |= WB_CONTENT_CACHEABLE;
+
        options = rrdr_check_options(r, options, (dimensions)?buffer_tostring(dimensions):NULL);
 
        if(dimensions)
@@ -1714,12 +1794,12 @@ time_t rrd_stats_json(int type, RRDSET *st, BUFFER *wb, long points, long group,
        // -------------------------------------------------------------------------
        // prepare various strings, to speed up the loop
 
-       char overflow_annotation[201]; snprintf(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
-       char normal_annotation[201];   snprintf(normal_annotation,   200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
-       char pre_date[51];             snprintf(pre_date,             50, "             {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
-       char post_date[21];            snprintf(post_date,            20, "%s}", sq);
-       char pre_value[21];            snprintf(pre_value,            20, ",{%sv%s:", kq, kq);
-       char post_value[21];           snprintf(post_value,           20, "}");
+       char overflow_annotation[201]; snprintfz(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
+       char normal_annotation[201];   snprintfz(normal_annotation,   200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
+       char pre_date[51];             snprintfz(pre_date,             50, "            {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
+       char post_date[21];            snprintfz(post_date,            20, "%s}", sq);
+       char pre_value[21];            snprintfz(pre_value,            20, ",{%sv%s:", kq, kq);
+       char post_value[21];           strcpy(post_value,                  "}");
 
 
        // -------------------------------------------------------------------------