Skip to content

Commit

Permalink
add --limit as an alternative way for controlling the length of outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
vergoh committed Jan 18, 2020
1 parent 21acf67 commit 8402207
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
of limiting the maximum number of shown entries in queries
- Summary image output would segmentation fault when database existed but
didn't yet contain any daily or monthly data for the selected interface
- New
- Add --limit as an alternative way for controlling the length of outputs


2.5 / 14-Jan-2020
Expand Down
20 changes: 20 additions & 0 deletions man/vnstat.1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ vnstat \- a console-based network traffic monitor
.RB [ \-\-json
.RI [ mode ]
.RI [ limit ]]
.RB [ \-\-limit
.IR limit ]
.RB [ \-\-live
.RI [ mode ]]
.RB [ \-\-locale
Expand Down Expand Up @@ -229,6 +231,24 @@ names or structures of previously existing content gets changed. In comparison,
.B "vnstatversion"
field exists only as extra information.

.TP
.BI "--limit " limit
Set the maximum number of entries in list outputs to
.IR limit .
Usage of
.B "--limit"
overrides the default list entry limit values and the optional
.I limit
parameter given directly for a list query. All entries stored in the database will be shown if
.I limit
is set to 0.
.B "--limit"
can also be used to control the length of
.B "--json"
and
.B "--xml"
outputs.

.TP
.BI "-l, --live " [mode]
Display current transfer rate for the selected interface in real time
Expand Down
14 changes: 14 additions & 0 deletions man/vnstati.1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ vnstati \- png image output support for vnStat
.IR interface ]
.RB [ \-\-iface
.IR interface ]
.RB [ \-\-limit
.IR limit ]
.RB [ \-\-locale
.IR locale ]
.RB [ \-\-months
Expand Down Expand Up @@ -191,6 +193,18 @@ information of two or more interfaces using the
syntax. All provided interfaces must be unique and must exist in the database
when the merge syntax is used.

.TP
.BI "--limit " limit
Set the maximum number of entries in list outputs to
.IR limit .
Usage of
.B "--limit"
overrides the default list entry limit values and the optional
.I limit
parameter given directly for a list query. All entries stored in the database will be shown if
.I limit
is set to 0.

.TP
.BI "--locale " locale
Use
Expand Down
18 changes: 18 additions & 0 deletions src/vnstat_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void initparams(PARAMS *p)
p->removeiface = 0;
p->renameiface = 0;
p->livemode = 0;
p->limit = -1;
p->ifacelist = NULL;
p->interface[0] = '\0';
p->alias[0] = '\0';
Expand Down Expand Up @@ -113,6 +114,7 @@ void showlonghelp(PARAMS *p)
printf(" -tr, --traffic [time] calculate traffic\n");
printf(" -l, --live [mode] show transfer rate in real time\n");
printf(" -ru, --rateunit [mode] swap configured rate unit\n");
printf(" --limit <limit> set output entry limit\n");
printf(" --style <mode> select output style (0-4)\n");
printf(" --iflist show list of available interfaces\n");
printf(" --dbdir <directory> select database directory\n");
Expand Down Expand Up @@ -454,6 +456,18 @@ void parseargs(PARAMS *p, int argc, char **argv)
}
free(p->ifacelist);
exit(EXIT_SUCCESS);
} else if (strcmp(argv[currentarg], "--limit") == 0) {
if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) {
p->limit = atoi(argv[currentarg + 1]);
if (p->limit < 0) {
printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg + 1], argv[currentarg]);
exit(EXIT_FAILURE);
}
currentarg++;
} else {
printf("Error: Invalid or missing parameter for %s.\n", argv[currentarg]);
exit(EXIT_FAILURE);
}
} else if ((strcmp(argv[currentarg], "-v") == 0) || (strcmp(argv[currentarg], "--version") == 0)) {
printf("vnStat %s by Teemu Toivola <tst at iki dot fi>\n", getversion());
exit(EXIT_SUCCESS);
Expand All @@ -462,6 +476,10 @@ void parseargs(PARAMS *p, int argc, char **argv)
exit(EXIT_FAILURE);
}
}

if (p->limit != -1) {
cfg.listfivemins = cfg.listhours = cfg.listdays = cfg.listmonths = cfg.listyears = cfg.listtop = cfg.listjsonxml = p->limit;
}
}

void handleremoveinterface(PARAMS *p)
Expand Down
1 change: 1 addition & 0 deletions src/vnstat_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ typedef struct {
int query, setalias;
int addiface, force, traffic;
int livetraffic, defaultiface, removeiface, renameiface, livemode;
int32_t limit;
uint64_t dbifcount;
char interface[32], alias[32], newifname[32], filename[512];
char definterface[32], cfgfile[512], *ifacelist, jsonmode, xmlmode;
Expand Down
18 changes: 18 additions & 0 deletions src/vnstati.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void initiparams(IPARAMS *p)
p->cfgfile[0] = '\0';
p->cache = 0;
p->help = 0;
p->limit = -1;
}

void showihelp(IPARAMS *p)
Expand Down Expand Up @@ -125,6 +126,7 @@ void showihelp(IPARAMS *p)
printf(" -?, --help this help\n");
printf(" -D, --debug show some additional debug information\n");
printf(" -v, --version show version\n");
printf(" --limit <limit> set list entry limit\n");
printf(" --dbdir <directory> select database directory\n");
printf(" --style <mode> select output style (0-3)\n");
printf(" --locale <locale> set locale\n");
Expand Down Expand Up @@ -362,6 +364,18 @@ void parseargs(IPARAMS *p, IMAGECONTENT *ic, int argc, char **argv)
printf("Error: Date of format YYYY-MM-DD HH:MM or YYYY-MM-DD for %s missing.\n", argv[currentarg]);
exit(EXIT_FAILURE);
}
} else if (strcmp(argv[currentarg], "--limit") == 0) {
if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) {
p->limit = atoi(argv[currentarg + 1]);
if (p->limit < 0) {
printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg + 1], argv[currentarg]);
exit(EXIT_FAILURE);
}
currentarg++;
} else {
printf("Error: Invalid or missing parameter for %s.\n", argv[currentarg]);
exit(EXIT_FAILURE);
}
} else if ((strcmp(argv[currentarg], "-v") == 0) || (strcmp(argv[currentarg], "--version")) == 0) {
printf("vnStat image output %s by Teemu Toivola <tst at iki dot fi>\n", getversion());
exit(EXIT_SUCCESS);
Expand All @@ -375,6 +389,10 @@ void parseargs(IPARAMS *p, IMAGECONTENT *ic, int argc, char **argv)
showihelp(p);
exit(EXIT_SUCCESS);
}

if (p->limit != -1) {
cfg.listfivemins = cfg.listhours = cfg.listdays = cfg.listmonths = cfg.listyears = cfg.listtop = cfg.listjsonxml = p->limit;
}
}

void validateinput(IPARAMS *p)
Expand Down
1 change: 1 addition & 0 deletions src/vnstati.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

typedef struct {
int cache, help;
int32_t limit;
char interface[32], filename[512], cfgfile[512];
FILE *pngout;
} IPARAMS;
Expand Down
92 changes: 92 additions & 0 deletions tests/parseargs_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,93 @@ START_TEST(vnstat_parseargs_rename_requires_parameter)
}
END_TEST

START_TEST(vnstat_parseargs_limit_requires_parameter)
{
PARAMS p;
char *argv[] = {"vnstat", "--limit", NULL};
int argc = sizeof(argv) / sizeof(char *) - 1;

initparams(&p);
suppress_output();
parseargs(&p, argc, argv);
}
END_TEST

START_TEST(vnstat_parseargs_limit_cannot_be_negative)
{
PARAMS p;
char *argv[] = {"vnstat", "--limit", "-1", NULL};
int argc = sizeof(argv) / sizeof(char *) - 1;

initparams(&p);
suppress_output();
parseargs(&p, argc, argv);
}
END_TEST

START_TEST(vnstat_parseargs_limit_changes_defaults)
{
PARAMS p;
char *argv[] = {"vnstat", "--limit", "1234", NULL};
int argc = sizeof(argv) / sizeof(char *) - 1;

defaultcfg();
initparams(&p);
suppress_output();
parseargs(&p, argc, argv);

ck_assert_int_eq(cfg.listfivemins, 1234);
ck_assert_int_eq(cfg.listhours, 1234);
ck_assert_int_eq(cfg.listdays, 1234);
ck_assert_int_eq(cfg.listmonths, 1234);
ck_assert_int_eq(cfg.listyears, 1234);
ck_assert_int_eq(cfg.listtop, 1234);
ck_assert_int_eq(cfg.listjsonxml, 1234);
}
END_TEST

START_TEST(vnstat_parseargs_limit_overrides)
{
PARAMS p;
char *argv[] = {"vnstat", "-d", "5", "--limit", "234", NULL};
int argc = sizeof(argv) / sizeof(char *) - 1;

defaultcfg();
initparams(&p);
suppress_output();
parseargs(&p, argc, argv);

ck_assert_int_eq(cfg.listfivemins, 234);
ck_assert_int_eq(cfg.listhours, 234);
ck_assert_int_eq(cfg.listdays, 234);
ck_assert_int_eq(cfg.listmonths, 234);
ck_assert_int_eq(cfg.listyears, 234);
ck_assert_int_eq(cfg.listtop, 234);
ck_assert_int_eq(cfg.listjsonxml, 234);
}
END_TEST

START_TEST(vnstat_parseargs_limit_overrides_regardless_of_position)
{
PARAMS p;
char *argv[] = {"vnstat", "--limit", "345", "-d", "5", NULL};
int argc = sizeof(argv) / sizeof(char *) - 1;

defaultcfg();
initparams(&p);
suppress_output();
parseargs(&p, argc, argv);

ck_assert_int_eq(cfg.listfivemins, 345);
ck_assert_int_eq(cfg.listhours, 345);
ck_assert_int_eq(cfg.listdays, 345);
ck_assert_int_eq(cfg.listmonths, 345);
ck_assert_int_eq(cfg.listyears, 345);
ck_assert_int_eq(cfg.listtop, 345);
ck_assert_int_eq(cfg.listjsonxml, 345);
}
END_TEST

void add_parseargs_tests(Suite *s)
{
TCase *tc_pa = tcase_create("ParseArgs");
Expand Down Expand Up @@ -520,5 +607,10 @@ void add_parseargs_tests(Suite *s)
tcase_add_exit_test(tc_pa, vnstat_parseargs_setalias_requires_parameter, 1);
tcase_add_test(tc_pa, vnstat_parseargs_setalias_still_supports_nick);
tcase_add_exit_test(tc_pa, vnstat_parseargs_rename_requires_parameter, 1);
tcase_add_exit_test(tc_pa, vnstat_parseargs_limit_requires_parameter, 1);
tcase_add_exit_test(tc_pa, vnstat_parseargs_limit_cannot_be_negative, 1);
tcase_add_test(tc_pa, vnstat_parseargs_limit_changes_defaults);
tcase_add_test(tc_pa, vnstat_parseargs_limit_overrides);
tcase_add_test(tc_pa, vnstat_parseargs_limit_overrides_regardless_of_position);
suite_add_tcase(s, tc_pa);
}

0 comments on commit 8402207

Please sign in to comment.