From 6b16003b170f4cfefa766becb981401fab5e2d73 Mon Sep 17 00:00:00 2001 From: Teemu Toivola Date: Mon, 23 Jun 2014 01:22:40 +0300 Subject: [PATCH] improve daemon startup and debug prints, use ISO YYYY-MM-DD date format timestamps if logfile is used, add some missed changes to CHANGES --- CHANGES | 7 +++++-- src/Makefile | 2 +- src/common.c | 2 +- src/dbcache.c | 16 +++++++++++----- src/vnstatd.c | 28 ++++++++++++++++++++++------ src/vnstatd.h | 1 + tests/Makefile | 2 +- 7 files changed, 42 insertions(+), 16 deletions(-) diff --git a/CHANGES b/CHANGES index c43d772..547af88 100644 --- a/CHANGES +++ b/CHANGES @@ -7,7 +7,7 @@ of command execution - Fix: Live traffic meter occasionally showing higher minimum than average in end statistics (Debian Bug #687812) - - Fix: Cppcheck findings + - Fix: Cppcheck findings (may fix Debian Bug #692330) - Improve traffic meter output accuracy - Add tests and debug compilation target - Remove use of -D parameter in Makefile install commands in order to @@ -16,7 +16,10 @@ - Add support for database import from text file (--importdb) (based on patch by Tilmann Bubeck) - Rename --dumpdb to --exportdb - - Code refactoring + - Add example systemd service file + - Add example launchd plist file for OS X + - Use ISO YYYY-MM-DD date format timestamps if logfile is used + - Improve daemon startup prints 1.11 / 1-Jun-11 diff --git a/src/Makefile b/src/Makefile index ada69e4..2dc1403 100644 --- a/src/Makefile +++ b/src/Makefile @@ -26,7 +26,7 @@ dbxml.o: dbxml.c dbxml.h common.h dbshow.o: dbshow.c dbshow.h misc.h common.h dbaccess.o: dbaccess.c dbaccess.h common.h dbmerge.o: dbmerge.c dbmerge.h dbaccess.h common.h -dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h +dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h cfg.h common.o: common.c common.h misc.o: misc.c misc.h common.h cfg.o: cfg.c cfg.h common.h diff --git a/src/common.c b/src/common.c index 8cb53ea..dc10c61 100644 --- a/src/common.c +++ b/src/common.c @@ -67,7 +67,7 @@ int logprint(PrintType type) } current = time(NULL); - strftime(timestamp, 22, "%Y.%m.%d %H:%M:%S", localtime(¤t)); + strftime(timestamp, 22, "%Y-%m-%d %H:%M:%S", localtime(¤t)); switch (type) { case PT_Info: diff --git a/src/dbcache.c b/src/dbcache.c index 53bf147..c1549b0 100644 --- a/src/dbcache.c +++ b/src/dbcache.c @@ -2,6 +2,7 @@ #include "ifinfo.h" #include "dbaccess.h" #include "dbcache.h" +#include "cfg.h" int cacheadd(const char *iface, int sync) { @@ -151,8 +152,8 @@ void cacheshow(void) void cachestatus(void) { - char buffer[512]; - int b = 13, count = 0; + char buffer[512], bwtemp[8]; + int b = 13, count = 0, bwlimit = 0; datanode *p = dataptr; snprintf(buffer, b, "Monitoring: "); @@ -160,10 +161,15 @@ void cachestatus(void) if (p != NULL) { while (p != NULL) { - if ((b+strlen(p->data.interface)+1) < 508) { + if ((b+strlen(p->data.interface)+8) < 508) { + bwlimit = ibwget(p->data.interface); + if (bwlimit < 0) { + bwlimit = 0; + } + snprintf(bwtemp, 8, " (%d) ", bwlimit); strncat(buffer, p->data.interface, strlen(p->data.interface)); - strcat(buffer, " "); - b = b+strlen(p->data.interface)+1; + strncat(buffer, bwtemp, strlen(bwtemp)); + b += strlen(p->data.interface) + strlen(bwtemp); } else { strcat(buffer, "..."); break; diff --git a/src/vnstatd.c b/src/vnstatd.c index c631bc7..247990b 100644 --- a/src/vnstatd.c +++ b/src/vnstatd.c @@ -117,14 +117,14 @@ int main(int argc, char *argv[]) if (showhelp) { printf(" vnStat daemon %s by Teemu Toivola \n\n", VNSTATVERSION); printf(" -d, --daemon fork process to background\n"); - printf(" -n, --nodaemon stay in foreground attached to the terminal\n"); + printf(" -n, --nodaemon stay in foreground attached to the terminal\n\n"); printf(" -s, --sync sync interface counters on first update\n"); printf(" -D, --debug show additional debug and disable daemon\n"); printf(" -?, --help show this help\n"); printf(" -v, --version show version\n"); printf(" -p, --pidfile select used pid file\n"); - printf(" --config select used config file\n\n"); - printf(" --noadd don't add found interfaces if no dbs are found\n"); + printf(" --config select used config file\n"); + printf(" --noadd don't add found interfaces if no dbs are found\n\n"); printf("See also \"man vnstatd\".\n"); return 0; } @@ -203,6 +203,7 @@ int main(int argc, char *argv[]) updateinterval = cfg.updateinterval; if (debug) { + debugtimestamp(); cacheshow(); } @@ -554,7 +555,7 @@ void daemonize(void) int addinterfaces(const char *dirname) { char *ifacelist, interface[32]; - int index = 0, count = 0; + int index = 0, count = 0, bwlimit = 0; /* get list of currently visible interfaces */ if (getiflist(&ifacelist)==0) { @@ -595,7 +596,12 @@ int addinterfaces(const char *dirname) continue; } count++; - printf("\"%s\" added, %d Mbit bandwidth limit.\n", interface, ibwget(interface)); + bwlimit = ibwget(interface); + if (bwlimit > 0) { + printf("\"%s\" added with %d Mbit bandwidth limit.\n", interface, bwlimit); + } else { + printf("\"%s\" added. Warning: no bandwidth limit has been set.\n", interface); + } } if (count==1) { @@ -605,7 +611,7 @@ int addinterfaces(const char *dirname) } if (count) { - printf(" Limits can be modified using the configuration file."); + printf(" Limits can be modified using the configuration file. See \"man vnstat.conf\"."); } printf("\n"); @@ -613,3 +619,13 @@ int addinterfaces(const char *dirname) free(ifacelist); return count; } + +void debugtimestamp() +{ + time_t now; + char timestamp[22]; + + now = time(NULL); + strftime(timestamp, 22, "%Y-%m-%d %H:%M:%S", localtime(&now)); + printf("%s\n", timestamp); +} diff --git a/src/vnstatd.h b/src/vnstatd.h index c536eaf..14a6688 100644 --- a/src/vnstatd.h +++ b/src/vnstatd.h @@ -3,5 +3,6 @@ void daemonize(void); int addinterfaces(const char *dirname); +void debugtimestamp(); #endif diff --git a/tests/Makefile b/tests/Makefile index 15ca729..42a177f 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -32,7 +32,7 @@ misc_tests.o: misc_tests.c misc_tests.h vnstat_tests.h common.h misc.h common.o: common.c common.h ifinfo.o: ifinfo.c ifinfo.h common.h dbaccess.h misc.h cfg.h dbaccess.o: dbaccess.c dbaccess.h common.h -dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h +dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h cfg.h cfg.o: cfg.c cfg.h common.h misc.o: misc.c misc.h common.h -- 2.40.0