From e3e01e53f956e3cbdcee686b931db70a493f5759 Mon Sep 17 00:00:00 2001 From: Teemu Toivola Date: Tue, 31 Jan 2017 21:46:04 +0200 Subject: [PATCH] add configuration options for defining maximum entry counts for list output, allow defining list maximum entry count also as command line parameter --- cfg/vnstat.conf | 10 ++++++++- src/cfg.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++- src/common.h | 11 +++++++++ src/dbshow.c | 34 +++++++++++++++++++--------- src/image.c | 24 +++++++++++++++----- src/vnstat.c | 40 +++++++++++++++++++++++++++++---- src/vnstati.c | 34 +++++++++++++++++++++++++++- 7 files changed, 190 insertions(+), 22 deletions(-) diff --git a/cfg/vnstat.conf b/cfg/vnstat.conf index 895bc81..752b094 100644 --- a/cfg/vnstat.conf +++ b/cfg/vnstat.conf @@ -76,7 +76,15 @@ BootVariation 15 # log days without traffic to daily list (1 = enabled, 0 = disabled) TrafficlessDays 1 -# data retention durations (-1 = unlimited, 0 = log disabled) +# default list output entry count (0 = all) +List5Mins 24 +ListHours 24 +ListDays 30 +ListMonths 12 +ListYears 0 +ListTop 10 + +# data retention durations (-1 = unlimited, 0 = feature disabled) 5MinuteHours 48 HourlyDays 2 DailyDays 30 diff --git a/src/cfg.c b/src/cfg.c index e1289f7..f38b112 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -89,7 +89,15 @@ void printcfgfile(void) printf("# log days without traffic to daily list (1 = enabled, 0 = disabled)\n"); printf("TrafficlessDays %d\n\n", cfg.traflessday); - printf("# data retention durations (-1 = unlimited, 0 = log disabled)\n"); + printf("# default list output entry count (0 = all)\n"); + printf("List5Mins %2d\n", cfg.listfivemins); + printf("ListHours %2d\n", cfg.listhours); + printf("ListDays %2d\n", cfg.listdays); + printf("ListMonths %2d\n", cfg.listmonths); + printf("ListYears %2d\n", cfg.listyears); + printf("ListTop %2d\n\n", cfg.listtop); + + printf("# data retention durations (-1 = unlimited, 0 = feature disabled)\n"); printf("5MinuteHours %2d\n", cfg.fiveminutehours); printf("HourlyDays %2d\n", cfg.hourlydays); printf("DailyDays %2d\n", cfg.dailydays); @@ -208,6 +216,12 @@ int loadcfg(const char *cfgfile) { "UseFileLocking", 0, &cfg.flock, 0, 0 }, { "BootVariation", 0, &cfg.bvar, 0, 0 }, { "TrafficlessDays", 0, &cfg.traflessday, 0, 0 }, + { "List5Mins", 0, &cfg.listfivemins, 0, 0 }, + { "ListHours", 0, &cfg.listhours, 0, 0 }, + { "ListDays", 0, &cfg.listdays, 0, 0 }, + { "ListMonths", 0, &cfg.listmonths, 0, 0 }, + { "ListYears", 0, &cfg.listyears, 0, 0 }, + { "ListTop", 0, &cfg.listtop, 0, 0 }, { "5MinuteHours", 0, &cfg.fiveminutehours, 0, 0 }, { "HourlyDays", 0, &cfg.hourlydays, 0, 0 }, { "DailyDays", 0, &cfg.dailydays, 0, 0 }, @@ -476,6 +490,42 @@ void validatecfg(void) printe(PT_Config); } + if (cfg.listfivemins<0) { + cfg.listfivemins = LISTFIVEMINS; + snprintf(errorstring, 512, "%s List5Mins, %s \"%d\".", invalidvalue, resettingto, cfg.listfivemins); + printe(PT_Config); + } + + if (cfg.listhours<0) { + cfg.listhours = LISTHOURS; + snprintf(errorstring, 512, "%s ListHours, %s \"%d\".", invalidvalue, resettingto, cfg.listhours); + printe(PT_Config); + } + + if (cfg.listdays<0) { + cfg.listdays = LISTDAYS; + snprintf(errorstring, 512, "%s ListDays, %s \"%d\".", invalidvalue, resettingto, cfg.listdays); + printe(PT_Config); + } + + if (cfg.listmonths<0) { + cfg.listmonths = LISTMONTHS; + snprintf(errorstring, 512, "%s ListMonths, %s \"%d\".", invalidvalue, resettingto, cfg.listmonths); + printe(PT_Config); + } + + if (cfg.listyears<0) { + cfg.listyears = LISTYEARS; + snprintf(errorstring, 512, "%s ListYears, %s \"%d\".", invalidvalue, resettingto, cfg.listyears); + printe(PT_Config); + } + + if (cfg.listtop<0) { + cfg.listtop = LISTTOP; + snprintf(errorstring, 512, "%s ListTop, %s \"%d\".", invalidvalue, resettingto, cfg.listtop); + printe(PT_Config); + } + if (cfg.fiveminutehours<-1) { cfg.fiveminutehours = FIVEMINUTEHOURS; snprintf(errorstring, 512, "%s 5MinuteHours, %s \"%d\".", invalidvalue, resettingto, cfg.fiveminutehours); @@ -548,6 +598,13 @@ void defaultcfg(void) cfg.traflessday = TRAFLESSDAY; cfg.utflocale = UTFLOCALE; + cfg.listfivemins = LISTFIVEMINS; + cfg.listhours = LISTHOURS; + cfg.listdays = LISTDAYS; + cfg.listmonths = LISTMONTHS; + cfg.listyears = LISTYEARS; + cfg.listtop = LISTTOP; + cfg.fiveminutehours = FIVEMINUTEHOURS; cfg.hourlydays = HOURLYDAYS; cfg.dailydays = DAILYDAYS; diff --git a/src/common.h b/src/common.h index 1791c55..6a9cc9b 100644 --- a/src/common.h +++ b/src/common.h @@ -57,6 +57,8 @@ and most can be changed later from the config file. #define DATABASEDIR "/var/lib/vnstat" #endif #endif + +/* database file name */ #define DATABASEFILE "vnstat.db" /* on which day should months change */ @@ -148,6 +150,14 @@ and most can be changed later from the config file. /* log trafficless days by default */ #define TRAFLESSDAY 1 +/* list outputs */ +#define LISTFIVEMINS 24 +#define LISTHOURS 24 +#define LISTDAYS 30 +#define LISTMONTHS 12 +#define LISTYEARS 0 +#define LISTTOP 10 + /* data retention defaults */ #define FIVEMINUTEHOURS 48 #define HOURLYDAYS 2 @@ -240,6 +250,7 @@ typedef struct { int32_t updateinterval, pollinterval, saveinterval, offsaveinterval, savestatus, uselogging; int32_t createdirs, updatefileowner, bwdetection, bwdetectioninterval, utflocale; int32_t fiveminutehours, hourlydays, dailydays, monthlymonths, yearlyyears, topdayentries; + int32_t listfivemins, listhours, listdays, listmonths, listyears, listtop; } CFG; /* internal interface information structure */ diff --git a/src/dbshow.c b/src/dbshow.c index 743cebf..2117171 100644 --- a/src/dbshow.c +++ b/src/dbshow.c @@ -284,32 +284,51 @@ void showlist(const interfaceinfo *interface, const char *listname) strncpy_nt(colname, listname, 8); snprintf(titlename, 8, "daily"); strncpy_nt(stampformat, cfg.dformat, 64); - limit = 30; + limit = cfg.listdays; } else if (strcmp(listname, "month") == 0) { listtype = 2; strncpy_nt(colname, listname, 8); snprintf(titlename, 8, "monthly"); strncpy_nt(stampformat, cfg.mformat, 64); - limit = 12; + limit = cfg.listmonths; } else if (strcmp(listname, "year") == 0) { listtype = 3; strncpy_nt(colname, listname, 8); snprintf(titlename, 8, "yearly"); strncpy_nt(stampformat, "%Y", 64); - limit = -1; + limit = cfg.listyears; } else if (strcmp(listname, "top") == 0) { listtype = 4; snprintf(colname, 8, "day"); - snprintf(titlename, 8, "top 10"); strncpy_nt(stampformat, cfg.tformat, 64); - limit = 10; + limit = cfg.listtop; offset = 6; } else { return; } + if (limit == 0) { + limit = -1; + } + e_rx = e_tx = e_secs = 0; + if (!db_getdata(&datalist, &datainfo, interface->name, listname, limit)) { + printf("Error: Failed to fetch %s data.\n", titlename); + return; + } + + if (listtype == 4) { + if (limit > 0 && datainfo.count < (uint32_t)limit) { + limit = datainfo.count; + } + if (limit <= 0 || datainfo.count > 999) { + snprintf(titlename, 8, "top"); + } else { + snprintf(titlename, 8, "top %d", limit); + } + } + printf("\n"); if (strcmp(interface->name, interface->alias) == 0 || strlen(interface->alias) == 0) { printf(" %s", interface->name); @@ -355,11 +374,6 @@ void showlist(const interfaceinfo *interface, const char *listname) printf("\n"); } - if (!db_getdata(&datalist, &datainfo, interface->name, listname, limit)) { - printf("Error: Failed to fetch %s data.\n", titlename); - return; - } - datalist_i = datalist; while (datalist_i != NULL) { diff --git a/src/image.c b/src/image.c index 0a9e14e..ad1b726 100644 --- a/src/image.c +++ b/src/image.c @@ -284,36 +284,50 @@ void drawlist(IMAGECONTENT *ic, const char *listname) strncpy_nt(colname, listname, 8); snprintf(titlename, 8, "daily"); strncpy_nt(stampformat, cfg.dformat, 64); - limit = 30; + limit = cfg.listdays; } else if (strcmp(listname, "month") == 0) { listtype = 2; strncpy_nt(colname, listname, 8); snprintf(titlename, 8, "monthly"); strncpy_nt(stampformat, cfg.mformat, 64); - limit = 12; + limit = cfg.listmonths; } else if (strcmp(listname, "year") == 0) { listtype = 3; strncpy_nt(colname, listname, 8); snprintf(titlename, 8, "yearly"); strncpy_nt(stampformat, "%Y", 64); - limit = -1; + limit = cfg.listyears; } else if (strcmp(listname, "top") == 0) { listtype = 4; snprintf(colname, 8, "day"); - snprintf(titlename, 8, "top 10"); strncpy_nt(stampformat, cfg.tformat, 64); - limit = 10; + limit = cfg.listtop; offsetx = 30; offsety = -16; } else { return; } + if (limit == 0) { + limit = -1; + } + if (!db_getdata(&datalist, &datainfo, ic->interface.name, listname, limit)) { printf("Error: Failed to fetch %s data.\n", "day"); return; } + if (listtype == 4) { + if (limit > 0 && datainfo.count < (uint32_t)limit) { + limit = datainfo.count; + } + if (limit <= 0 || datainfo.count > 999) { + snprintf(titlename, 8, "top"); + } else { + snprintf(titlename, 8, "top %d", limit); + } + } + datalist_i = datalist; width = 500; diff --git a/src/vnstat.c b/src/vnstat.c index d23be39..10d1fd4 100644 --- a/src/vnstat.c +++ b/src/vnstat.c @@ -167,21 +167,53 @@ int main(int argc, char *argv[]) { p.query=0; } else if ((strcmp(argv[currentarg],"-u")==0) || (strcmp(argv[currentarg],"--update")==0)) { printf("Error: The \"%s\" parameter is not supported in this version.\n", argv[currentarg]); - exit(EXIT_FAILURE); + return 1; } else if ((strcmp(argv[currentarg],"-q")==0) || (strcmp(argv[currentarg],"--query")==0)) { p.query=1; } else if ((strcmp(argv[currentarg],"-D")==0) || (strcmp(argv[currentarg],"--debug")==0)) { debug=1; } else if ((strcmp(argv[currentarg],"-d")==0) || (strcmp(argv[currentarg],"--days")==0)) { cfg.qmode=1; + if (currentarg+1definterface); printf(" -?, --help short help\n"); @@ -442,7 +474,7 @@ void showlonghelp(PARAMS *p) printf(" -d, --days show days\n"); printf(" -m, --months show months\n"); printf(" -y, --years show years\n"); - printf(" -t, --top10 show top 10 days\n"); + printf(" -t, --top show top days\n"); printf(" -s, --short use short output\n"); printf(" -ru, --rateunit swap configured rate unit\n"); printf(" --oneline show simple parseable format\n"); diff --git a/src/vnstati.c b/src/vnstati.c index 344fe3f..9f100a0 100644 --- a/src/vnstati.c +++ b/src/vnstati.c @@ -173,12 +173,44 @@ int main(int argc, char *argv[]) debug = 1; } else if ((strcmp(argv[currentarg],"-d")==0) || (strcmp(argv[currentarg],"--days"))==0) { cfg.qmode = 1; + if (currentarg+1