Skip to content

Commit

Permalink
add 95th percentile option to --json
Browse files Browse the repository at this point in the history
  • Loading branch information
vergoh committed Dec 10, 2023
1 parent b22762c commit 9b20b0e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
59 changes: 59 additions & 0 deletions src/dbjson.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "common.h"
#include "dbsql.h"
#include "percentile.h"
#include "dbjson.h"

void showjson(const char *interface, const int ifcount, const char mode, const char *databegin, const char *dataend)
Expand Down Expand Up @@ -30,6 +31,13 @@ void showjson(const char *interface, const int ifcount, const char mode, const c
jsondate(&info.updated, 2);
printf("},");

if (mode == 'p') {
jsonpercentile(&info);
printf("}");
timeused_debug(__func__, 0);
return;
}

printf("\"traffic\":");
printf("{\"total\":{\"rx\":%" PRIu64 ",\"tx\":%" PRIu64 "},", info.rxtotal, info.txtotal);

Expand Down Expand Up @@ -112,6 +120,57 @@ void jsondump(const interfaceinfo *interface, const char *tablename, const int d
printf("]");
}

void jsonpercentile(const interfaceinfo *interface)
{
percentiledata pdata;

if (!getpercentiledata(&pdata, interface->name)) {
exit(EXIT_FAILURE);
}

printf("\"bandwidth\":{");

printf("\"month\":{");
jsondate(&pdata.monthbegin, 1);
printf("},");

printf("\"data_begin\":{");
jsondate(&pdata.databegin, 2);
printf("},");

printf("\"data_end\":{");
jsondate(&pdata.dataend, 2);
printf("},");

printf("\"entries\":{\"seen\":%" PRIu32 ",\"expected\":%" PRIu32 ",\"missing\":%" PRIu32 "},", pdata.count, pdata.countexpectation, pdata.countexpectation-pdata.count);

printf("\"minimum\":{");
printf("\"rx_bytes_per_second\":%" PRIu64 ",", (uint64_t)(pdata.minrx / (double)300));
printf("\"tx_bytes_per_second\":%" PRIu64 ",", (uint64_t)(pdata.mintx / (double)300));
printf("\"total_bytes_per_second\":%" PRIu64 "", (uint64_t)(pdata.min / (double)300));
printf("},");

printf("\"average\":{");
printf("\"rx_bytes_per_second\":%" PRIu64 ",", (uint64_t)(pdata.sumrx / (double)(pdata.count * 300)));
printf("\"tx_bytes_per_second\":%" PRIu64 ",", (uint64_t)(pdata.sumtx / (double)(pdata.count * 300)));
printf("\"total_bytes_per_second\":%" PRIu64 "", (uint64_t)(pdata.sumrx + pdata.sumtx / (double)(pdata.count * 300)));
printf("},");

printf("\"maximum\":{");
printf("\"rx_bytes_per_second\":%" PRIu64 ",", (uint64_t)(pdata.maxrx / (double)300));
printf("\"tx_bytes_per_second\":%" PRIu64 ",", (uint64_t)(pdata.maxtx / (double)300));
printf("\"total_bytes_per_second\":%" PRIu64 "", (uint64_t)(pdata.max / (double)300));
printf("},");

printf("\"95th_percentile\":{");
printf("\"rx_bytes_per_second\":%" PRIu64 ",", (uint64_t)(pdata.rxpercentile / (double)300));
printf("\"tx_bytes_per_second\":%" PRIu64 ",", (uint64_t)(pdata.txpercentile / (double)300));
printf("\"total_bytes_per_second\":%" PRIu64 "", (uint64_t)(pdata.sumpercentile / (double)300));
printf("}");

printf("}");
}

void jsondate(const time_t *date, const int type)
{
struct tm *d;
Expand Down
1 change: 1 addition & 0 deletions src/dbjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

void showjson(const char *interface, const int ifcount, const char mode, const char *databegin, const char *dataend);
void jsondump(const interfaceinfo *interface, const char *tablename, const int datetype, const char *databegin, const char *dataend);
void jsonpercentile(const interfaceinfo *interface);
void jsondate(const time_t *date, const int type);
void jsonheader(void);
void jsonfooter(void);
Expand Down
1 change: 0 additions & 1 deletion src/dbshow.c
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,6 @@ void showhours(const interfaceinfo *interface)
timeused_debug(__func__, 0);
}

// TODO: json? needs new function due to different format
// TODO: xml? needs new function due to different format
// TODO: alert needs new function due to different format
// TODO: image? output below doesn't have much graphics potential, line in 5 minute graph could be one alternative
Expand Down
1 change: 1 addition & 0 deletions src/percentile.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ int getpercentiledata(percentiledata *pdata, const char *iface)
return 0;
}

pdata->databegin = datainfo.mintime;
pdata->dataend = datainfo.maxtime;
pdata->count = datainfo.count;
pdata->countexpectation = (uint32_t)((pdata->dataend - pdata->monthbegin) / 300 + 1);
Expand Down
2 changes: 1 addition & 1 deletion src/percentile.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define PERCENTILE_H

typedef struct percentiledata {
time_t monthbegin, dataend;
time_t monthbegin, databegin, dataend;
uint32_t count, countexpectation;
uint64_t minrx, mintx;
uint64_t maxrx, maxtx;
Expand Down
5 changes: 3 additions & 2 deletions src/vnstat_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,18 +378,19 @@ void parseargs(PARAMS *p, const int argc, char **argv)
cfg.qmode = 10;
if (currentarg + 1 < argc && (strlen(argv[currentarg + 1]) == 1 || ishelprequest(argv[currentarg + 1]))) {
p->jsonmode = argv[currentarg + 1][0];
if (strlen(argv[currentarg + 1]) != 1 || strchr("asfhdmyt", p->jsonmode) == NULL) {
if (strlen(argv[currentarg + 1]) != 1 || strchr("asfhdmytp", p->jsonmode) == NULL) {
if (!ishelprequest(argv[currentarg + 1]))
printf("Error: Invalid mode parameter \"%s\".\n", argv[currentarg + 1]);
printf(" Valid parameters for --json:\n");
printf(" a - all (default)\n");
printf(" a - all except 95th percentile (default)\n");
printf(" s - summary\n");
printf(" f - only 5 minutes\n");
printf(" h - only hours\n");
printf(" d - only days\n");
printf(" m - only months\n");
printf(" y - only years\n");
printf(" t - only top\n");
printf(" p - 95th percentile\n"); // TODO: documentation
exit(EXIT_FAILURE);
}
if (p->jsonmode == 's') {
Expand Down

0 comments on commit 9b20b0e

Please sign in to comment.