From 1ac92c058b74b6c85e2761bea8e68d9469dc9ffe Mon Sep 17 00:00:00 2001 From: oetiker Date: Fri, 24 Feb 2012 07:44:25 +0000 Subject: [PATCH] moved output generation for xport out of rrd_tool.c -- martin sperl git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk@2278 a5681a0c-68f1-0310-ab6d-d61299d08faa --- program/src/rrd_tool.c | 147 ++-------------------------------------- program/src/rrd_xport.c | 56 +++++++++------ 2 files changed, 44 insertions(+), 159 deletions(-) diff --git a/program/src/rrd_tool.c b/program/src/rrd_tool.c index 4dd4cae1..24abcdce 100644 --- a/program/src/rrd_tool.c +++ b/program/src/rrd_tool.c @@ -704,146 +704,13 @@ int HandleInputLine( } } else if (strcmp("xport", argv[1]) == 0) { #ifdef HAVE_RRD_GRAPH - int xxsize; - unsigned long int j = 0; - time_t start, end, ti; - unsigned long step, col_cnt, row_cnt; - 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); - - 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) == 0) { - char *old_locale = setlocale(LC_NUMERIC,NULL); - setlocale(LC_NUMERIC, "C"); - row_cnt = (end - start) / step; - ptr = data; - 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]; - 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); - 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) { - 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; - newval = *ptr; - 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); - } - } - free(data); - if (json){ - printf(" ]\n}\n"); - } - else { - printf(" \n", DATA_TAG); - printf("\n", ROOT_TAG); - } - setlocale(LC_NUMERIC, old_locale); - } - free(vtag); + time_t start, end; + unsigned long step, col_cnt; + rrd_value_t *data; + char **legend_v; + rrd_xport + (argc - 1, &argv[1], NULL, &start, &end, &step, &col_cnt, + &legend_v, &data); #else rrd_set_error("the instance of rrdtool has been compiled without graphics"); #endif diff --git a/program/src/rrd_xport.c b/program/src/rrd_xport.c index c4173e4c..4457b60f 100644 --- a/program/src/rrd_xport.c +++ b/program/src/rrd_xport.c @@ -18,7 +18,6 @@ #include #endif - int rrd_xport( int, char **, @@ -40,7 +39,20 @@ int rrd_xport_fn( rrd_value_t **, int); +/* helper function for buffer handling */ +typedef struct stringbuffer_t { + size_t allocated; + size_t len; + unsigned char* data; + FILE *file; +} stringbuffer_t; +int addToBuffer(stringbuffer_t *,char*,size_t); +void escapeJSON(char*,size_t); +int rrd_graph_xport(image_desc_t *); +int rrd_xport_format_xmljson(int,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*); +int rrd_xport_format_sv(char,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*); +int rrd_xport_format_addprints(int,stringbuffer_t *,image_desc_t *); int rrd_xport( int argc, @@ -79,6 +91,9 @@ int rrd_xport( rrd_parsetime("end-24h", &start_tv); rrd_parsetime("now", &end_tv); + int enumds=0; + int json=0; + while (1) { int option_index = 0; int opt; @@ -93,6 +108,10 @@ int rrd_xport( im.step = atoi(optarg); break; case 262: + enumds=1; + break; + case 263: + json=1; break; case 's': if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) { @@ -179,6 +198,18 @@ int rrd_xport( return -1; } + /* and create the export */ + if (!xsize) { + int flags=0; + if (json) { flags|=1; } + if (enumds) { flags|=4; } + stringbuffer_t buffer={0,0,NULL,stdout}; + rrd_xport_format_xmljson(flags,&buffer,&im, + *start, *end, *step, + *col_cnt, *legend_v, + *data); + } + im_free(&im); return 0; } @@ -341,21 +372,6 @@ int rrd_xport_fn( } -/* helper function for buffer handling */ -typedef struct stringbuffer_t { - size_t allocated; - size_t len; - unsigned char* data; - FILE *file; -} stringbuffer_t; -int addToBuffer(stringbuffer_t *,char*,size_t); -void escapeJSON(char*,size_t); - -int rrd_graph_xport(image_desc_t *); -int rrd_xport_format_xmljson(int,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*); -int rrd_xport_format_sv(char,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*); -int rrd_xport_format_addprints(int,stringbuffer_t *,image_desc_t *); - int rrd_graph_xport(image_desc_t *im) { /* prepare the data for processing */ unsigned long col_cnt=0; @@ -690,9 +706,11 @@ int rrd_xport_format_xmljson(int flags,stringbuffer_t *buffer,image_desc_t *im,t if (rrd_xport_format_addprints(json,buffer,im)) {return -1;} /* if we have got a trailing , then kill it */ - if (buffer->data[buffer->len-2]==',') { - buffer->data[buffer->len-2]=buffer->data[buffer->len-1]; - buffer->len--; + if (buffer->data) { + if (buffer->data[buffer->len-2]==',') { + buffer->data[buffer->len-2]=buffer->data[buffer->len-1]; + buffer->len--; + } } /* end meta */ -- 2.30.2