From cc2abeb033f5e48d3bc379c0fbde65f8cf646f88 Mon Sep 17 00:00:00 2001 From: Teemu Toivola Date: Wed, 14 Feb 2018 18:32:02 +0200 Subject: [PATCH] add support for combining -tr and --json, closes #56 --- man/vnstat.1 | 8 +++++- src/common.h | 4 +++ src/traffic.c | 76 +++++++++++++++++++++++++++++++++++---------------- 3 files changed, 63 insertions(+), 25 deletions(-) diff --git a/man/vnstat.1 b/man/vnstat.1 index 749f6a5f..252117da 100644 --- a/man/vnstat.1 +++ b/man/vnstat.1 @@ -270,6 +270,9 @@ 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. +This option can also be used in combination with the +.B "-tr" +option. .TP .BI "-l, --live " mode @@ -396,7 +399,10 @@ the given .I time seconds. The .I time -will be 5 seconds if a number parameter isn't specified. +will be 5 seconds if a number parameter isn't specified. The output will +be in json format if used in combination with +.B "--json" +option. However, in that case, the countdown before results isn't shown. .TP .B "-u, --update" diff --git a/src/common.h b/src/common.h index 5a0f0eea..36aa4ffb 100644 --- a/src/common.h +++ b/src/common.h @@ -167,6 +167,10 @@ and most can be changed later from the config file. /* 1 = 1.13- */ #define JSONVERSION 1 +/* json format version, -tr */ +/* 1 = 1.18- */ +#define JSONVERSION_TR 1 + /* --oneline format version */ #define ONELINEVERSION 1 diff --git a/src/traffic.c b/src/traffic.c index 749ea06a..59c37103 100644 --- a/src/traffic.c +++ b/src/traffic.c @@ -5,12 +5,15 @@ void trafficmeter(char iface[], int sampletime) { - /* received bytes packets errs drop fifo frame compressed multicast */ - /* transmitted bytes packets errs drop fifo colls carrier compressed */ uint64_t rx, tx, rxp, txp; + int json = 0; IFINFO firstinfo; char buffer[256]; + if (cfg.qmode == 10) { + json = 1; + } + /* less than 2 seconds doesn't produce good results */ if (sampletime<2) { printf("Error: Time for sampling too short.\n"); @@ -29,24 +32,28 @@ void trafficmeter(char iface[], int sampletime) /* wait sampletime and print some nice dots so that the user thinks something is done :) */ - snprintf(buffer, 256, "Sampling %s (%d seconds average)", iface,sampletime); - printf("%s", buffer); - fflush(stdout); - sleep(sampletime/3); - printf("."); - fflush(stdout); - sleep(sampletime/3); - printf("."); - fflush(stdout); - sleep(sampletime/3); - printf("."); - fflush(stdout); - if ((sampletime/3)*3!=sampletime) { - sleep(sampletime-((sampletime/3)*3)); - } + if (!json) { + snprintf(buffer, 256, "Sampling %s (%d seconds average)", iface,sampletime); + printf("%s", buffer); + fflush(stdout); + sleep(sampletime/3); + printf("."); + fflush(stdout); + sleep(sampletime/3); + printf("."); + fflush(stdout); + sleep(sampletime/3); + printf("."); + fflush(stdout); + if ((sampletime/3)*3!=sampletime) { + sleep(sampletime-((sampletime/3)*3)); + } - cursortocolumn(1); - eraseline(); + cursortocolumn(1); + eraseline(); + } else { + sleep(sampletime); + } /* read those values again... */ if (!getifinfo(iface)) { @@ -60,11 +67,32 @@ void trafficmeter(char iface[], int sampletime) rxp = countercalc(&firstinfo.rxp, &ifinfo.rxp); txp = countercalc(&firstinfo.txp, &ifinfo.txp); - /* show the difference in a readable format */ - printf("%"PRIu64" packets sampled in %d seconds\n", rxp+txp, sampletime); - printf("Traffic average for %s\n", iface); - printf("\n rx %s %5"PRIu64" packets/s\n", gettrafficrate(rx, sampletime, 15), (uint64_t)(rxp/sampletime)); - printf(" tx %s %5"PRIu64" packets/s\n\n", gettrafficrate(tx, sampletime, 15), (uint64_t)(txp/sampletime)); + /* show the difference in a readable format or json */ + if (!json) { + printf("%"PRIu64" packets sampled in %d seconds\n", rxp+txp, sampletime); + printf("Traffic average for %s\n", iface); + printf("\n rx %s %5"PRIu64" packets/s\n", gettrafficrate(rx, sampletime, 15), (uint64_t)(rxp/sampletime)); + printf(" tx %s %5"PRIu64" packets/s\n\n", gettrafficrate(tx, sampletime, 15), (uint64_t)(txp/sampletime)); + } else { + printf("{\"jsonversion\":\"%d\",", JSONVERSION_TR); + printf("\"vnstatversion\":\"%s\",", getversion()); + printf("\"interface\":\"%s\",", iface); + printf("\"sampletime\":%d,", sampletime); + printf("\"rx\":{"); + printf("\"ratestring\":\"%s\",", gettrafficrate(rx, sampletime, 0)); + printf("\"bytespersecond\":%"PRIu64",", (uint64_t)(rx/sampletime)); + printf("\"packetspersecond\":%"PRIu64",", (uint64_t)(rxp/sampletime)); + printf("\"bytes\":%"PRIu64",", rx); + printf("\"packets\":%"PRIu64"", rxp); + printf("},"); + printf("\"tx\":{"); + printf("\"ratestring\":\"%s\",", gettrafficrate(tx, sampletime, 0)); + printf("\"bytespersecond\":%"PRIu64",", (uint64_t)(tx/sampletime)); + printf("\"packetspersecond\":%"PRIu64",", (uint64_t)(txp/sampletime)); + printf("\"bytes\":%"PRIu64",", tx); + printf("\"packets\":%"PRIu64"", txp); + printf("}}\n"); + } } void livetrafficmeter(char iface[32], int mode)