]> granicus.if.org Git - vnstat/commitdiff
add optional mode parameter also to --xml
authorTeemu Toivola <git@humdi.net>
Sat, 31 Jan 2015 15:21:19 +0000 (17:21 +0200)
committerTeemu Toivola <git@humdi.net>
Sat, 31 Jan 2015 15:21:19 +0000 (17:21 +0200)
CHANGES
man/vnstat.1
src/dbxml.c
src/dbxml.h
src/vnstat.c
src/vnstat.h

diff --git a/CHANGES b/CHANGES
index 6e6faf8ce1733ff7312e6db72aaa54bed5804f4d..f1f2715fd77b10673865f6e132a66bde27a42a6c 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -2,8 +2,8 @@
 
  - Fix: JSON output syntax during first day of newly created databases
    (pull request by Stefan Merettig)
- - Add optional mode parameter to --json for limiting the output to only
-   selected information
+ - Add optional mode parameter to --json and --xml for limiting the output
+   to only selected information
 
 
 1.13 / 18-Jan-2015
index 709111e564ca3a238c9368f0b8ecfeb1123ba3bb..ef5c5f3d58b47fb6477d280a84b25cc0f015079d 100644 (file)
@@ -102,6 +102,7 @@ vnstat \- a console-based network traffic monitor
 .B \-\-weeks
 ] [
 .B \-\-xml
+.I mode
 ]
 
 .SH DESCRIPTION
@@ -401,9 +402,14 @@ Show current version.
 Show traffic for 7 days, current and previous week.
 
 .TP
-.B "--xml"
+.BI "--xml " mode
 Show database content for selected interface or all interfaces in xml format. All
-traffic values in the output are in KiB.
+traffic values in the output are in KiB. An optional
+.I mode
+parameter can be used for limiting the output to only selected information.
+Everything is shown by default. Setting
+.I mode
+to 'h' will output only hours, 'd' days, 'm' months and 't' the top 10.
 
 .TP
 .B "-?, --help"
index f615677d294ab94f61d6a57cf94a3e1c44bd19b1..fcec85fa1034ab0bc4cdb901132f1a7dedeac2bf 100644 (file)
@@ -1,7 +1,7 @@
 #include "common.h"
 #include "dbxml.h"
 
-void showxml(void)
+void showxml(char mode)
 {
        printf(" <interface id=\"%s\">\n", data.interface);
 
@@ -18,10 +18,30 @@ void showxml(void)
        printf("  <traffic>\n");
        printf("   <total><rx>%"PRIu64"</rx><tx>%"PRIu64"</tx></total>\n", (data.totalrx*1024)+data.totalrxk, (data.totaltx*1024)+data.totaltxk);
 
-       xmldays();
-       xmlmonths();
-       xmltops();
-       xmlhours();
+       switch (mode) {
+               case 'd':
+                       xmldays();
+                       break;
+               case 'm':
+                       xmlmonths();
+                       break;
+               case 't':
+                       xmltops();
+                       break;
+               case 'h':
+                       xmlhours();
+                       break;
+               case 'a':
+               default:
+                       xmldays();
+                       printf(",");
+                       xmlmonths();
+                       printf(",");
+                       xmltops();
+                       printf(",");
+                       xmlhours();
+                       break;
+       }
 
        printf("  </traffic>\n");
        printf(" </interface>\n");
index 619684a86ac112789349bfd55c887a86e2414a98..dde05be11f90f5546feae7d9b6a656e2ec8a96e1 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef DBXML_H
 #define DBXML_H
 
-void showxml(void);
+void showxml(char mode);
 void xmldays(void);
 void xmlmonths(void);
 void xmltops(void);
index dca7b7fc1972169bf1ed66c381ae15ea5b70edbb..b337b0453a954dc118c049f137e973e2045759e2 100644 (file)
@@ -210,9 +210,24 @@ int main(int argc, char *argv[]) {
                } else if (strcmp(argv[currentarg],"--oneline")==0) {
                        cfg.qmode=9;
                } else if (strcmp(argv[currentarg],"--xml")==0) {
+                       if (currentarg+1<argc && argv[currentarg+1][0]!='-') {
+                               p.xmlmode = argv[currentarg+1][0];
+                               if (strlen(argv[currentarg+1])!=1 || strchr("ahdmt", p.xmlmode)==NULL) {
+                                       printf("Error: Invalid mode parameter \"%s\" for --xml.\n", argv[currentarg+1]);
+                                       printf(" Valid parameters:\n");
+                                       printf("    a - all (default)\n");
+                                       printf("    h - only hours\n");
+                                       printf("    d - only days\n");
+                                       printf("    m - only months\n");
+                                       printf("    t - only top 10\n");
+                                       return 1;
+                               }
+                               currentarg++;
+                       }
                        cfg.qmode=8;
                } else if (strcmp(argv[currentarg],"--json")==0) {
                        if (currentarg+1<argc && argv[currentarg+1][0]!='-') {
+                               p.jsonmode = argv[currentarg+1][0];
                                if (strlen(argv[currentarg+1])!=1 || strchr("ahdmt", p.jsonmode)==NULL) {
                                        printf("Error: Invalid mode parameter \"%s\" for --json.\n", argv[currentarg+1]);
                                        printf(" Valid parameters:\n");
@@ -223,7 +238,6 @@ int main(int argc, char *argv[]) {
                                        printf("    t - only top 10\n");
                                        return 1;
                                }
-                               p.jsonmode = argv[currentarg+1][0];
                                currentarg++;
                        }
                        cfg.qmode=10;
@@ -415,6 +429,7 @@ void initparams(PARAMS *p)
        p->ifacelist = NULL;
        p->cfgfile[0] = '\0';
        p->jsonmode = 'a';
+       p->xmlmode = 'a';
 }
 
 int synccounters(const char *iface, const char *dirname)
@@ -903,7 +918,7 @@ void handleshowdatabases(PARAMS *p)
                        if (cfg.qmode==0) {
                                showdb(5);
                        } else if (cfg.qmode==8) {
-                               showxml();
+                               showxml(p->xmlmode);
                        } else if (cfg.qmode==10) {
                                showjson(dbcount, p->jsonmode);
                        }
@@ -941,7 +956,7 @@ void showoneinterface(PARAMS *p, const char *interface)
                showdb(cfg.qmode);
        } else if (cfg.qmode==8) {
                xmlheader();
-               showxml();
+               showxml(p->xmlmode);
                xmlfooter();
        } else if (cfg.qmode==10) {
                jsonheader();
index e87e32b4a8f9dc85753ba99db62e5e3b3e0e6487..181e03c6d87ce0418a2b57a77077181fed7d7295 100644 (file)
@@ -6,7 +6,7 @@ typedef struct {
        int create, active, files, force, cleartop, rebuildtotal, traffic;
        int livetraffic, defaultiface, delete, livemode;
        char interface[32], dirname[512], nick[32], filename[512];
-       char definterface[32], cfgfile[512], *ifacelist, jsonmode;
+       char definterface[32], cfgfile[512], *ifacelist, jsonmode, xmlmode;
 } PARAMS;
 
 void initparams(PARAMS *p);