From: Teemu Toivola Date: Wed, 21 Mar 2018 22:58:29 +0000 (+0200) Subject: add option to limit the length of --json and --xml outputs X-Git-Tag: v2.0~44 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=99ef5c66d2393447ff2326482e04e1da5e6d37ae;p=vnstat add option to limit the length of --json and --xml outputs --- diff --git a/CHANGES b/CHANGES index 9b39a33..784d5a2 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,7 @@ - Add yearly and 5 minute resolution statistics - Add pebibyte and exbibyte support - Add options the configure the length of each list output + - Add option to limit the length of --json and --xml outputs - Add hourly list output as alternative - Remove weekly output - Remove merge of data from multiple interfaces to one output diff --git a/man/vnstat.1 b/man/vnstat.1 index 17ddf48..a4a68b5 100644 --- a/man/vnstat.1 +++ b/man/vnstat.1 @@ -44,7 +44,7 @@ vnstat \- a console-based network traffic monitor .B \-\-iflist ] [ .B \-\-json -.I mode +.I "mode limit" ] [ .B \-\-live .I mode @@ -90,7 +90,7 @@ vnstat \- a console-based network traffic monitor .B \-\-version ] [ .B \-\-xml -.I mode +.I "mode limit" ] .SH DESCRIPTION @@ -178,15 +178,20 @@ syntax. Show list of currently available interfaces. .TP -.BI "--json " mode +.BI "--json " "mode limit" Show database content for selected interface or all interfaces in json format. All traffic values in the output are in bytes. 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 '5' will output only 5 minute resolution entries, 'h' hours, 'd' days, 'm' months, -'y' years and 't' the top days. This option can also be used in combination with +to 'f' will output only 5 minute resolution entries, 'h' hours, 'd' days, 'm' months, 'y' +years and 't' the top days. An optional +.I limit +parameter can be used to limit the number results to a given number of most recent +entries. The +.BI "--json" +option can be used in combination with .B "-l, --live" and .B "-tr" @@ -313,15 +318,18 @@ option. However, in that case, the countdown before results isn't shown. Show current version. .TP -.BI "--xml " mode +.BI "--xml " "mode limit" Show database content for selected interface or all interfaces in xml format. All traffic values in the output are in bytes. 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 '5' will output only 5 minute resolution entries, 'h' hours, 'd' days, 'm' months, -'y' years and 't' the top days. +to 'f' will output only 5 minute resolution entries, 'h' hours, 'd' days, 'm' months, 'y' +years and 't' the top days. An optional +.I limit +parameter can be used to limit the number results to a given number of most recent +entries. .TP .B "-?, --help" diff --git a/src/cfg.c b/src/cfg.c index c89ffbf..c0eaad7 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -637,6 +637,7 @@ void defaultcfg(void) cfg.listmonths = LISTMONTHS; cfg.listyears = LISTYEARS; cfg.listtop = LISTTOP; + cfg.listjsonxml = LISTJSONXML; cfg.fiveminutehours = FIVEMINUTEHOURS; cfg.hourlydays = HOURLYDAYS; diff --git a/src/common.h b/src/common.h index 6c903e1..d5e6965 100644 --- a/src/common.h +++ b/src/common.h @@ -173,6 +173,7 @@ and most can be changed later from the config file. #define LISTMONTHS 12 #define LISTYEARS 0 #define LISTTOP 10 +#define LISTJSONXML 0 /* data retention defaults */ #define FIVEMINUTEHOURS 48 @@ -279,7 +280,7 @@ typedef struct { int32_t timesyncwait, updateinterval, pollinterval, saveinterval, offsaveinterval, savestatus; int32_t uselogging, createdirs, updatefileowner, bwdetection, bwdetectioninterval, utflocale; int32_t fiveminutehours, hourlydays, dailydays, monthlymonths, yearlyyears, topdayentries; - int32_t listfivemins, listhours, listdays, listmonths, listyears, listtop; + int32_t listfivemins, listhours, listdays, listmonths, listyears, listtop, listjsonxml; } CFG; /* internal interface information structure */ diff --git a/src/dbjson.c b/src/dbjson.c index 40bf686..e1dbfae 100644 --- a/src/dbjson.c +++ b/src/dbjson.c @@ -49,7 +49,7 @@ void showjson(const char *interface, const int dbcount, const char mode) case 'y': jsondump(&info, "year", 4); break; - case '5': + case 'f': jsondump(&info, "fiveminute", 2); break; case 'a': @@ -79,7 +79,7 @@ void jsondump(const interfaceinfo *interface, const char *tablename, const int d dbdatalist *datalist = NULL, *datalist_i = NULL; dbdatalistinfo datainfo; - if (!db_getdata(&datalist, &datainfo, interface->name, tablename, 0)) { + if (!db_getdata(&datalist, &datainfo, interface->name, tablename, (uint32_t)cfg.listjsonxml)) { printf("Error: Failed to fetch %s data.\n", tablename); return; } diff --git a/src/dbxml.c b/src/dbxml.c index b51d008..4097966 100644 --- a/src/dbxml.c +++ b/src/dbxml.c @@ -47,7 +47,7 @@ void showxml(const char *interface, const char mode) case 'y': xmldump(&info, "year", 4); break; - case '5': + case 'f': xmldump(&info, "fiveminute", 2); break; case 'a': @@ -72,7 +72,7 @@ void xmldump(const interfaceinfo *interface, const char *tablename, const int da dbdatalist *datalist = NULL, *datalist_i = NULL; dbdatalistinfo datainfo; - if (!db_getdata(&datalist, &datainfo, interface->name, tablename, 0)) { + if (!db_getdata(&datalist, &datainfo, interface->name, tablename, (uint32_t)cfg.listjsonxml)) { printf("Error: Failed to fetch %s data.\n", tablename); return; } diff --git a/src/vnstat.c b/src/vnstat.c index acee5c8..71d7fcc 100644 --- a/src/vnstat.c +++ b/src/vnstat.c @@ -252,36 +252,52 @@ int main(int argc, char *argv[]) { } } } else if (strcmp(argv[currentarg],"--xml")==0) { - if (currentarg+1