Skip to content

Commit

Permalink
add support for combining -tr and --json, closes #56
Browse files Browse the repository at this point in the history
  • Loading branch information
vergoh committed Feb 14, 2018
1 parent c55faaf commit cc2abeb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
8 changes: 7 additions & 1 deletion man/vnstat.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
76 changes: 52 additions & 24 deletions src/traffic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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)) {
Expand All @@ -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)
Expand Down

0 comments on commit cc2abeb

Please sign in to comment.