sbm

simple bandwidth monitor
git clone git://git.2f30.org/sbm
Log | Files | Refs | LICENSE

commit fe27962fac30d17f0a43f3074c4e8206567aa370
parent 9ae3b156057907b01a03863c274c86c1afadc997
Author: sin <sin@2f30.org>
Date:   Fri,  5 Aug 2016 11:13:56 +0100

Print uptime when printing total bandwidth consumed

Diffstat:
Msbm.c | 103++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 63 insertions(+), 40 deletions(-)

diff --git a/sbm.c b/sbm.c @@ -33,6 +33,7 @@ char *argv0; char *ifname; uint64_t origrxbytes, origtxbytes, rxbytes, txbytes; +struct timeval begin; int Bflag; int tflag; @@ -142,6 +143,43 @@ sample(uint64_t *rxbytes, uint64_t *txbytes, } #endif +int +getmonotime(struct timeval *tv) +{ + struct timespec ts; + int ret; + + ret = clock_gettime(CLOCK_MONOTONIC, &ts); + if (ret < 0) + return -1; + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / 1000; + return ret; +} + +void +ms2tv(struct timeval *tv, long ms) +{ + tv->tv_sec = ms / 1000; + tv->tv_usec = (ms % 1000) * 1000; +} + +long +tv2ms(struct timeval *tv) +{ + return (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000); +} + +int +tvsleep(struct timeval *tv) +{ + struct timespec ts; + + ts.tv_sec = tv->tv_sec; + ts.tv_nsec = tv->tv_usec * 1000; + return nanosleep(&ts, NULL); +} + double scale(char **suffix, uint64_t bits) { @@ -188,53 +226,37 @@ print(uint64_t rxbits, uint64_t txbits, uint64_t rxpps, uint64_t txpps) void printrxtx(int sig) { + struct timeval now, uptime; char *rxsuffix, *txsuffix; double rxround, txround; - uint64_t rxbits = (rxbytes - origrxbytes) * 8, - txbits = (txbytes - origtxbytes) * 8; + uint64_t rxbits, txbits; + unsigned int days, hours, minutes; + + getmonotime(&now); + timersub(&now, &begin, &uptime); + uptime.tv_sec /= 60; + minutes = uptime.tv_sec % 60; + uptime.tv_sec /= 60; + hours = uptime.tv_sec % 24; + days = uptime.tv_sec / 24; + rxbits = (rxbytes - origrxbytes) * 8; + txbits = (txbytes - origtxbytes) * 8; rxround = scale(&rxsuffix, rxbits); txround = scale(&txsuffix, txbits); - printf("%*s %6.2f %2s Rx %6.2f %2s Tx\n", + + printf("%*s %6.2f %2s Rx %6.2f %2s Tx in ", (int)strlen(ifname) + 1, "", rxround, rxsuffix, txround, txsuffix); -} - -int -getmonotime(struct timeval *tv) -{ - struct timespec ts; - int ret; - - ret = clock_gettime(CLOCK_MONOTONIC, &ts); - if (ret < 0) - return -1; - tv->tv_sec = ts.tv_sec; - tv->tv_usec = ts.tv_nsec / 1000; - return ret; -} - -void -ms2tv(struct timeval *tv, long ms) -{ - tv->tv_sec = ms / 1000; - tv->tv_usec = (ms % 1000) * 1000; -} - -long -tv2ms(struct timeval *tv) -{ - return (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000); -} - -int -tvsleep(struct timeval *tv) -{ - struct timespec ts; - - ts.tv_sec = tv->tv_sec; - ts.tv_nsec = tv->tv_usec * 1000; - return nanosleep(&ts, NULL); + if (days != 0) + printf("%d day%s, ", days, + days > 1 ? "s" : ""); + if (hours != 0) + printf("%02d:%02d, ", hours, minutes); + else + printf("%d min%s, ", minutes, + minutes > 1 ? "s" : ""); + putchar('\n'); } void @@ -312,6 +334,7 @@ main(int argc, char *argv[]) usage(); } ARGEND + getmonotime(&begin); ifname = ifn; signal(SIGINFO, printrxtx); ms2tv(&tv, delay);