X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=program%2Fsrc%2Frrd_tool.c;h=4dd4cae1f1c6524cbe6b56a8cf74e7a1681ed96b;hb=12485f69aa59dc0eaed9e33024d152384667552b;hp=cc2aede9e7b2207063f53e2324c5ae542b16e01b;hpb=d4dc1cff0c1bcb36ba0c9317334ec9dcb809b76f;p=rrdtool-all.git diff --git a/program/src/rrd_tool.c b/program/src/rrd_tool.c index cc2aede9..4dd4cae1 100644 --- a/program/src/rrd_tool.c +++ b/program/src/rrd_tool.c @@ -4,10 +4,12 @@ * rrd_tool.c Startup wrapper *****************************************************************************/ -#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(HAVE_CONFIG_H) +#if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(HAVE_CONFIG_H) #include "../win32/config.h" #include #include +#include +#include #else #ifdef HAVE_CONFIG_H #include "../rrd_config.h" @@ -205,7 +207,7 @@ void PrintUsage( N_("* xport - generate XML dump from one or several RRD\n\n" "\trrdtool xport [-s|--start seconds] [-e|--end seconds]\n" "\t\t[-m|--maxrows rows]\n" "\t\t[--step seconds]\n" - "\t\t[--enumds]\n" "\t\t[DEF:vname=rrd:ds-name:CF]\n" + "\t\t[--enumds] [--json]\n" "\t\t[DEF:vname=rrd:ds-name:CF]\n" "\t\t[CDEF:vname=rpn-expression]\n" "\t\t[XPORT:vname:legend]\n"); const char *help_quit = @@ -414,6 +416,12 @@ int main( according to localeconv(3) */ setlocale(LC_ALL, ""); +#if defined(WIN32) && !defined(__CYGWIN__) + setmode(fileno(stdout), O_BINARY); + setmode(fileno(stdin), O_BINARY); +#endif + + #if defined(HAVE_LIBINTL_H) && defined(BUILD_LIBINTL) bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR); textdomain(GETTEXT_PACKAGE); @@ -677,7 +685,7 @@ int HandleInputLine( if (rrd_fetch (argc - 1, &argv[1], &start, &end, &step, &ds_cnt, &ds_namv, - &data) != -1) { + &data) == 0) { datai = data; printf(" "); for (i = 0; i < ds_cnt; i++) @@ -695,6 +703,7 @@ int HandleInputLine( free(data); } } else if (strcmp("xport", argv[1]) == 0) { +#ifdef HAVE_RRD_GRAPH int xxsize; unsigned long int j = 0; time_t start, end, ti; @@ -702,6 +711,7 @@ int HandleInputLine( rrd_value_t *data, *ptr; char **legend_v; int enumds = 0; + int json = 0; int i; size_t vtag_s = strlen(COL_DATA_TAG) + 10; char *vtag = malloc(vtag_s); @@ -709,69 +719,136 @@ int HandleInputLine( for (i = 2; i < argc; i++) { if (strcmp("--enumds", argv[i]) == 0) enumds = 1; + if (strcmp("--json", argv[i]) == 0) + json = 1; } if (rrd_xport (argc - 1, &argv[1], &xxsize, &start, &end, &step, &col_cnt, - &legend_v, &data) != -1) { + &legend_v, &data) == 0) { char *old_locale = setlocale(LC_NUMERIC,NULL); setlocale(LC_NUMERIC, "C"); row_cnt = (end - start) / step; ptr = data; - printf("\n\n", - XML_ENCODING); - printf("<%s>\n", ROOT_TAG); - printf(" <%s>\n", META_TAG); - printf(" <%s>%lld\n", META_START_TAG, - (long long int) start + step, META_START_TAG); - printf(" <%s>%lu\n", META_STEP_TAG, step, META_STEP_TAG); - printf(" <%s>%lld\n", META_END_TAG, (long long int) end, - META_END_TAG); - printf(" <%s>%lu\n", META_ROWS_TAG, row_cnt, - META_ROWS_TAG); - printf(" <%s>%lu\n", META_COLS_TAG, col_cnt, - META_COLS_TAG); - printf(" <%s>\n", LEGEND_TAG); + if (json == 0){ + printf("\n\n", + XML_ENCODING); + printf("<%s>\n", ROOT_TAG); + printf(" <%s>\n", META_TAG); + } + else { + printf("{ about: 'RRDtool xport JSON output',\n meta: {\n"); + } + + +#define pXJV(indent,fmt,tag,value) \ + if (json) { \ + printf(indent "\"%s\": " fmt ",\n",tag,value); \ + } else { \ + printf(indent "<%s>" fmt "\n",tag,value,tag); \ + } + + pXJV(" ","%lld",META_START_TAG,(long long int) start + step); + pXJV(" ","%lu", META_STEP_TAG, step); + pXJV(" ","%lld",META_END_TAG,(long long int) start + step); + if (! json){ + pXJV(" ","%lu", META_ROWS_TAG, row_cnt); + pXJV(" ","%lu", META_COLS_TAG, col_cnt); + } + + if (json){ + printf(" \"%s\": [\n", LEGEND_TAG); + } + else { + printf(" <%s>\n", LEGEND_TAG); + } for (j = 0; j < col_cnt; j++) { char *entry = NULL; - entry = legend_v[j]; - printf(" <%s>%s\n", LEGEND_ENTRY_TAG, entry, + if (json){ + printf(" '%s'", entry); + if (j < col_cnt -1){ + printf(","); + } + printf("\n"); + } + else { + printf(" <%s>%s\n", LEGEND_ENTRY_TAG, entry, LEGEND_ENTRY_TAG); + } free(entry); } free(legend_v); - printf(" \n", LEGEND_TAG); - printf(" \n", META_TAG); - printf(" <%s>\n", DATA_TAG); + if (json){ + printf(" ]\n },\n"); + } + else { + printf(" \n", LEGEND_TAG); + printf(" \n", META_TAG); + } + + if (json){ + printf(" \"%s\": [\n",DATA_TAG); + } else { + printf(" <%s>\n", DATA_TAG); + } for (ti = start + step; ti <= end; ti += step) { - printf(" <%s>", DATA_ROW_TAG); - printf("<%s>%lld", COL_TIME_TAG, (long long int)ti, COL_TIME_TAG); + if (json){ + printf(" [ "); + } + else { + printf(" <%s>", DATA_ROW_TAG); + printf("<%s>%lld", COL_TIME_TAG, (long long int)ti, COL_TIME_TAG); + } for (j = 0; j < col_cnt; j++) { rrd_value_t newval = DNAN; - - if (enumds == 1) - - snprintf(vtag, vtag_s, "%s%lu", COL_DATA_TAG, j); - else - snprintf(vtag, vtag_s, "%s", COL_DATA_TAG); newval = *ptr; - if (isnan(newval)) { - printf("<%s>NaN", vtag, vtag); - } else { - printf("<%s>%0.10e", vtag, newval, vtag); - }; + if (json){ + if (isnan(newval)){ + printf("null"); + } else { + printf("%0.10e",newval); + } + if (j < col_cnt -1){ + printf(", "); + } + } + else { + if (enumds == 1) + snprintf(vtag, vtag_s, "%s%lu", COL_DATA_TAG, j); + else + snprintf(vtag, vtag_s, "%s", COL_DATA_TAG); + if (isnan(newval)) { + printf("<%s>NaN", vtag, vtag); + } else { + printf("<%s>%0.10e", vtag, newval, vtag); + }; + } ptr++; + } + if (json){ + printf(ti < end ? " ],\n" : " ]\n"); + } + else { + printf("\n", DATA_ROW_TAG); } - printf("\n", DATA_ROW_TAG); } free(data); - printf(" \n", DATA_TAG); - printf("\n", ROOT_TAG); + if (json){ + printf(" ]\n}\n"); + } + else { + printf(" \n", DATA_TAG); + printf("\n", ROOT_TAG); + } setlocale(LC_NUMERIC, old_locale); } free(vtag); +#else + rrd_set_error("the instance of rrdtool has been compiled without graphics"); +#endif } else if (strcmp("graph", argv[1]) == 0) { +#ifdef HAVE_RRD_GRAPH char **calcpr; #ifdef notused /*XXX*/ @@ -792,7 +869,7 @@ int HandleInputLine( } if (rrd_graph (argc - 1, &argv[1], &calcpr, &xsize, &ysize, NULL, &ymin, - &ymax) != -1) { + &ymax) == 0) { if (!tostdout && !imginfo) printf("%dx%d\n", xsize, ysize); if (calcpr) { @@ -805,7 +882,11 @@ int HandleInputLine( } } +#else + rrd_set_error("the instance of rrdtool has been compiled without graphics"); +#endif } else if (strcmp("graphv", argv[1]) == 0) { +#ifdef HAVE_RRD_GRAPH rrd_info_t *grinfo = NULL; /* 1 to distinguish it from the NULL that rrd_graph sends in */ grinfo = rrd_graph_v(argc - 1, &argv[1]); @@ -813,7 +894,9 @@ int HandleInputLine( rrd_info_print(grinfo); rrd_info_free(grinfo); } - +#else + rrd_set_error("the instance of rrdtool has been compiled without graphics"); +#endif } else if (strcmp("tune", argv[1]) == 0) rrd_tune(argc - 1, &argv[1]); #ifndef WIN32