]> granicus.if.org Git - vnstat/commitdiff
add configuration options for defining maximum entry counts for list output, allow...
authorTeemu Toivola <git@humdi.net>
Tue, 31 Jan 2017 19:46:04 +0000 (21:46 +0200)
committerTeemu Toivola <git@humdi.net>
Tue, 31 Jan 2017 19:46:04 +0000 (21:46 +0200)
cfg/vnstat.conf
src/cfg.c
src/common.h
src/dbshow.c
src/image.c
src/vnstat.c
src/vnstati.c

index 895bc81d0defd1f244c558253675e7e07c83b27c..752b094463292a7b8f5ec559b03c75d37ddd5571 100644 (file)
@@ -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
index e1289f728b4dc391126b875fa710f73fce719051..f38b112b73bcfef0e8eb08e81f5bdf3f719bc09f 100644 (file)
--- 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;
index 1791c55369ef33a5f32cb5e39f69bee715d36de4..6a9cc9bdcbe5930f0fff103b3abc156f5298d514 100644 (file)
@@ -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 */
index 743cebfc588de61d911a9e79fd5a7a55cec292bf..2117171ff01ab05d6313385d43b04502ace8fd72 100644 (file)
@@ -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) {
index 0a9e14e9013ebd9c4513ba573a3e67fe3cc4ac9d..ad1b72631dbe9f48a6380f6eed2eebf30f006a74 100644 (file)
@@ -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;
index d23be39f7552d4201e4947a05505248f1bd2be39..10d1fd46a0be58e5471bc0e9d1d9b04dbcb0c7e7 100644 (file)
@@ -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+1<argc && isdigit(argv[currentarg+1][0])) {
+                               cfg.listdays = atoi(argv[currentarg+1]);
+                               if (cfg.listdays < 0) {
+                                       printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg+1], argv[currentarg]);
+                                       return 1;
+                               }
+                               currentarg++;
+                       }
                } else if ((strcmp(argv[currentarg],"-m")==0) || (strcmp(argv[currentarg],"--months")==0)) {
                        cfg.qmode=2;
-               } else if ((strcmp(argv[currentarg],"-t")==0) || (strcmp(argv[currentarg],"--top10")==0)) {
+                       if (currentarg+1<argc && isdigit(argv[currentarg+1][0])) {
+                               cfg.listmonths = atoi(argv[currentarg+1]);
+                               if (cfg.listmonths < 0) {
+                                       printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg+1], argv[currentarg]);
+                                       return 1;
+                               }
+                               currentarg++;
+                       }
+               } else if ((strcmp(argv[currentarg],"-t")==0) || (strcmp(argv[currentarg],"--top")==0)) {
                        cfg.qmode=3;
+                       if (currentarg+1<argc && isdigit(argv[currentarg+1][0])) {
+                               cfg.listtop = atoi(argv[currentarg+1]);
+                               if (cfg.listtop < 0) {
+                                       printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg+1], argv[currentarg]);
+                                       return 1;
+                               }
+                               currentarg++;
+                       }
                } else if ((strcmp(argv[currentarg],"-s")==0) || (strcmp(argv[currentarg],"--short")==0)) {
                        cfg.qmode=5;
                } else if ((strcmp(argv[currentarg],"-y")==0) || (strcmp(argv[currentarg],"--years")==0)) {
                        cfg.qmode=6;
+                       if (currentarg+1<argc && isdigit(argv[currentarg+1][0])) {
+                               cfg.listyears = atoi(argv[currentarg+1]);
+                               if (cfg.listyears < 0) {
+                                       printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg+1], argv[currentarg]);
+                                       return 1;
+                               }
+                               currentarg++;
+                       }
                } else if ((strcmp(argv[currentarg],"-h")==0) || (strcmp(argv[currentarg],"--hours")==0)) {
                        cfg.qmode=7;
                } else if ((strcmp(argv[currentarg],"--exportdb")==0) || (strcmp(argv[currentarg],"--dumpdb")==0)) {
@@ -420,7 +452,7 @@ void showhelp(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("         -i,  --iface          select interface (default: %s)\n", p->definterface);
        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");
index 344fe3f206adefc0109f180a2790de40847cb6b4..9f100a0d1910e2578972397a09ffbf30a52a5c9e 100644 (file)
@@ -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<argc && isdigit(argv[currentarg+1][0])) {
+                               cfg.listdays = atoi(argv[currentarg+1]);
+                               if (cfg.listdays < 0) {
+                                       printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg+1], argv[currentarg]);
+                                       return 1;
+                               }
+                               currentarg++;
+                       }
                } else if ((strcmp(argv[currentarg],"-m")==0) || (strcmp(argv[currentarg],"--months"))==0) {
                        cfg.qmode = 2;
-               } else if ((strcmp(argv[currentarg],"-t")==0) || (strcmp(argv[currentarg],"--top10"))==0) {
+                       if (currentarg+1<argc && isdigit(argv[currentarg+1][0])) {
+                               cfg.listmonths = atoi(argv[currentarg+1]);
+                               if (cfg.listmonths < 0) {
+                                       printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg+1], argv[currentarg]);
+                                       return 1;
+                               }
+                               currentarg++;
+                       }
+               } else if ((strcmp(argv[currentarg],"-t")==0) || (strcmp(argv[currentarg],"--top"))==0) {
                        cfg.qmode = 3;
+                       if (currentarg+1<argc && isdigit(argv[currentarg+1][0])) {
+                               cfg.listtop = atoi(argv[currentarg+1]);
+                               if (cfg.listtop < 0) {
+                                       printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg+1], argv[currentarg]);
+                                       return 1;
+                               }
+                               currentarg++;
+                       }
                } else if ((strcmp(argv[currentarg],"-y")==0) || (strcmp(argv[currentarg],"--years"))==0) {
                        cfg.qmode = 4;
+                       if (currentarg+1<argc && isdigit(argv[currentarg+1][0])) {
+                               cfg.listyears = atoi(argv[currentarg+1]);
+                               if (cfg.listyears < 0) {
+                                       printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg+1], argv[currentarg]);
+                                       return 1;
+                               }
+                               currentarg++;
+                       }
                } else if ((strcmp(argv[currentarg],"-s")==0) || (strcmp(argv[currentarg],"--summary"))==0) {
                        cfg.qmode = 5;
                } else if ((strcmp(argv[currentarg],"-h")==0) || (strcmp(argv[currentarg],"--hours"))==0) {