new command rrdtool xport integrated
[rrdtool.git] / src / rrd_tool.c
index a86e0ed..9a1f795 100644 (file)
@@ -5,6 +5,7 @@
  *****************************************************************************/
 
 #include "rrd_tool.h"
+#include "rrd_xport.h"
 
 void PrintUsage(char *cmd);
 int CountArgs(char *aLine);
@@ -24,7 +25,7 @@ void PrintUsage(char *cmd)
 
     char help_list[] =
           "Valid commands: create, update, graph, dump, restore,\n"
-          "\t\tlast, info, fetch, tune, resize\n\n";
+          "\t\tlast, info, fetch, tune, resize, xport\n\n";
 
     char help_create[] =
           "* create - create a new RRD\n\n"
@@ -113,6 +114,15 @@ void PrintUsage(char *cmd)
           " * resize - alter the lenght of one of the RRAs in an RRD\n\n"
           "\trrdtool resize filename rranum GROW|SHRINK rows\n\n";
 
+    char help_xport[] =
+          "* 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[DEF:vname=rrd:ds-name:CF]\n"
+          "\t\t[CDEF:vname=rpn-expression]\n"
+           "\t\t[XPORT:vname:legend]\n\n";
+
     char help_lic[] =
           "RRDtool is distributed under the Terms of the GNU General\n"
           "Public License Version 2. (www.gnu.org/copyleft/gpl.html)\n\n"
@@ -120,7 +130,7 @@ void PrintUsage(char *cmd)
           "For more information read the RRD manpages\n\n";
 
     enum { C_NONE, C_CREATE, C_DUMP, C_INFO, C_RESTORE, C_LAST,
-          C_UPDATE, C_FETCH, C_GRAPH, C_TUNE, C_RESIZE };
+          C_UPDATE, C_FETCH, C_GRAPH, C_TUNE, C_RESIZE, C_XPORT };
 
     int help_cmd = C_NONE;
 
@@ -146,6 +156,8 @@ void PrintUsage(char *cmd)
                help_cmd = C_TUNE;
            else if (!strcmp(cmd,"resize"))
                help_cmd = C_RESIZE;
+           else if (!strcmp(cmd,"xport"))
+               help_cmd = C_XPORT;
        }
     fputs(help_main, stdout);
     switch (help_cmd)
@@ -183,6 +195,9 @@ void PrintUsage(char *cmd)
            case C_RESIZE:
                fputs(help_resize, stdout);
                break;
+           case C_XPORT:
+               fputs(help_xport, stdout);
+               break;
        }
     fputs(help_lic, stdout);
 }
@@ -347,6 +362,54 @@ int HandleInputLine(int argc, char **argv, FILE* out)
            free(ds_namv);
            free (data);
        }
+    } else if (strcmp("xport", argv[1]) == 0) {
+       int xxsize;
+       int i = 0, j = 0;
+       time_t        start,end;
+       unsigned long step, col_cnt,row_cnt;
+       rrd_value_t   *data,*ptr;
+       char          **legend_v;
+       if(rrd_xport(argc-1, &argv[1], &xxsize,&start,&end,&step,&col_cnt,&legend_v,&data) != -1) {
+         row_cnt = (end-start)/step;
+         ptr = data;
+         printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n\n", XML_ENCODING);
+         printf("<%s>\n", ROOT_TAG);
+         printf("  <%s>\n", META_TAG);
+         printf("    <%s>%lu</%s>\n", META_START_TAG, start+step, META_START_TAG);
+         printf("    <%s>%lu</%s>\n", META_STEP_TAG, step, META_STEP_TAG);
+         printf("    <%s>%lu</%s>\n", META_END_TAG, end, META_END_TAG);
+         printf("    <%s>%lu</%s>\n", META_ROWS_TAG, row_cnt, META_ROWS_TAG);
+         printf("    <%s>%lu</%s>\n", META_COLS_TAG, col_cnt, META_COLS_TAG);
+         printf("    <%s>\n", LEGEND_TAG);
+         for (j = 0; j < col_cnt; j++) {
+           char *entry = NULL;
+           entry = legend_v[j];
+           printf("      <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry, LEGEND_ENTRY_TAG);
+           free(entry);
+         }
+         free(legend_v);
+         printf("    </%s>\n", LEGEND_TAG);
+         printf("  </%s>\n", META_TAG);
+         printf("  <%s>\n", DATA_TAG);
+         for (i = start+step; i <= end; i += step) {
+           printf ("    <%s>", DATA_ROW_TAG);
+           printf ("<%s>%lu</%s>", COL_TIME_TAG, i, COL_TIME_TAG);
+           for (j = 0; j < col_cnt; j++) {
+             rrd_value_t newval = DNAN;
+             newval = *ptr;
+             if(isnan(newval)){
+               printf("<%s>NaN</%s>", COL_DATA_TAG, COL_DATA_TAG);
+             } else {
+               printf("<%s>%0.10e</%s>", COL_DATA_TAG, newval, COL_DATA_TAG);
+             };
+             ptr++;
+           }
+           printf("</%s>\n", DATA_ROW_TAG);
+         }
+         free(data);
+         printf("  </%s>\n", DATA_TAG);
+         printf("</%s>\n", ROOT_TAG);
+       }
     }
     else if (strcmp("graph", argv[1]) == 0) {
        char **calcpr;