END_TEST START_TEST(countercalc_no_change_64bit) { uint64_t a, b; a = b = 0; ck_assert_int_eq(countercalc(&a, &b, 1), 0); a = b = 1; ck_assert_int_eq(countercalc(&a, &b, 1), 0); }
END_TEST START_TEST(countercalc_rollover_with_32bit_starting_over_32bit) { uint64_t a, b; a = MAX32 + 1; b = 0; ck_assert(countercalc(&a, &b, 0) == (MAX64 - MAX32 - 1)); ck_assert(countercalc(&a, &b, -1) == (MAX64 - MAX32 - 1)); }
END_TEST START_TEST(countercalc_small_change_64bit) { uint64_t a, b; a = 0; b = 1; ck_assert_int_eq(countercalc(&a, &b, 1), 1); a = 1; b = 2; ck_assert_int_eq(countercalc(&a, &b, 1), 1); b = 3; ck_assert_int_eq(countercalc(&a, &b, 1), 2); }
END_TEST START_TEST(countercalc_rollover_with_64bit_starting_64bit) { uint64_t a, b; a = MAX64 - 1; b = 0; ck_assert(countercalc(&a, &b, 1) == 1); }
END_TEST START_TEST(countercalc_rollover_with_64bit_2) { uint64_t a, b; a = MAX32 + 1; b = 0; ck_assert(countercalc(&a, &b, 1) == (MAX64 - MAX32 - 1)); }
void livetrafficmeter(char iface[32], int mode) { /* received bytes packets errs drop fifo frame compressed multicast */ /* transmitted bytes packets errs drop fifo colls carrier compressed */ uint64_t rx, tx, rxp, txp, timespent, timeslept; uint64_t rxtotal, txtotal, rxptotal, txptotal; uint64_t rxpmin, txpmin, rxpmax, txpmax; uint64_t rxmin, txmin, rxmax, txmax; uint64_t index = 1; int ratewidth, ppswidth, paddingwidth, json = 0; char buffer[256], buffer2[256]; IFINFO previnfo; if (cfg.qmode == 10) { json = 1; } if (!json) { printf("Monitoring %s... (press CTRL-C to stop)\n\n", iface); if (cfg.ostyle != 4) { printf(" getting traffic..."); fflush(stdout); } } /* enable signal trap */ intsignal = 0; if (signal(SIGINT, sighandler) == SIG_ERR) { perror("signal"); exit(EXIT_FAILURE); } /* set some defaults */ rxtotal = txtotal = rxptotal = txptotal = rxpmax = txpmax = 0; rxpmin = txpmin = rxmin = txmin = MAX64; rxmax = txmax = 0; timeslept = 0; timespent = (uint64_t)time(NULL); /* read /proc/net/dev and get values to the first list */ if (!getifinfo(iface)) { printf("Error: Interface \"%s\" not available, exiting.\n", iface); exit(EXIT_FAILURE); } ratewidth = 15; ppswidth = 5; paddingwidth = 8; /* narrow output mode */ if (cfg.ostyle == 0) { ratewidth = 12; ppswidth = 3; paddingwidth = 4; } if (!json) { cursorhide(); } else { printf("{\"jsonversion\":\"%d\",", JSONVERSION_LIVE); printf("\"vnstatversion\":\"%s\",", getversion()); printf("\"interface\":\"%s\",", iface); printf("\"sampletime\":%d}\n", LIVETIME); } /* loop until user gets bored */ while (intsignal == 0) { timeslept = (uint64_t)time(NULL); /* wait 2 seconds for more traffic */ sleep(LIVETIME); timeslept = (uint64_t)time(NULL) - timeslept; /* break loop without calculations because sleep was probably interrupted */ if (intsignal) { break; } /* use values from previous loop if this isn't the first time */ previnfo.rx = ifinfo.rx; previnfo.tx = ifinfo.tx; previnfo.rxp = ifinfo.rxp; previnfo.txp = ifinfo.txp; /* read those values again... */ if (!getifinfo(iface)) { cursorshow(); printf("Error: Interface \"%s\" not available, exiting.\n", iface); exit(EXIT_FAILURE); } /* calculate traffic and packets seen between updates */ rx = countercalc(&previnfo.rx, &ifinfo.rx, ifinfo.is64bit); tx = countercalc(&previnfo.tx, &ifinfo.tx, ifinfo.is64bit); rxp = countercalc(&previnfo.rxp, &ifinfo.rxp, ifinfo.is64bit); txp = countercalc(&previnfo.txp, &ifinfo.txp, ifinfo.is64bit); /* update totals */ rxtotal += rx; txtotal += tx; rxptotal += rxp; txptotal += txp; /* update min & max */ if (rxmin > rx) { rxmin = rx; } if (txmin > tx) { txmin = tx; } if (rxmax < rx) { rxmax = rx; } if (txmax < tx) { txmax = tx; } if (rxpmin > rxp) { rxpmin = rxp; } if (txpmin > txp) { txpmin = txp; } if (rxpmax < rxp) { rxpmax = rxp; } if (txpmax < txp) { txpmax = txp; } /* show the difference in a readable format or json */ if (!json) { if (mode == 0) { /* packets per second visible */ snprintf(buffer, 128, " rx: %s %*" PRIu64 " p/s", gettrafficrate(rx, LIVETIME, ratewidth), ppswidth, (uint64_t)rxp / LIVETIME); snprintf(buffer2, 128, " %*s tx: %s %*" PRIu64 " p/s", paddingwidth, " ", gettrafficrate(tx, LIVETIME, ratewidth), ppswidth, (uint64_t)txp / LIVETIME); } else { /* total transfer amount visible */ snprintf(buffer, 128, " rx: %s %s", gettrafficrate(rx, LIVETIME, ratewidth), getvalue(rxtotal, 1, RT_Normal)); snprintf(buffer2, 128, " %*s tx: %s %s", paddingwidth, " ", gettrafficrate(tx, LIVETIME, ratewidth), getvalue(txtotal, 1, RT_Normal)); } strcat(buffer, buffer2); if (cfg.ostyle != 4 || !debug) { cursortocolumn(1); eraseline(); } if (cfg.ostyle != 4) { printf("%s", buffer); fflush(stdout); } else { printf("%s\n", buffer); } } else { printf("{\"index\":%" PRIu64 ",", index); printf("\"seconds\":%" PRIu64 ",", (uint64_t)time(NULL) - timespent); printf("\"rx\":{"); printf("\"ratestring\":\"%s\",", gettrafficrate(rx, LIVETIME, 0)); printf("\"bytespersecond\":%" PRIu64 ",", (uint64_t)(rx / LIVETIME)); printf("\"packetspersecond\":%" PRIu64 ",", (uint64_t)(rxp / LIVETIME)); printf("\"bytes\":%" PRIu64 ",", rx); printf("\"packets\":%" PRIu64 ",", rxp); printf("\"totalbytes\":%" PRIu64 ",", rxtotal); printf("\"totalpackets\":%" PRIu64 "", rxptotal); printf("},"); printf("\"tx\":{"); printf("\"ratestring\":\"%s\",", gettrafficrate(tx, LIVETIME, 0)); printf("\"bytespersecond\":%" PRIu64 ",", (uint64_t)(tx / LIVETIME)); printf("\"packetspersecond\":%" PRIu64 ",", (uint64_t)(txp / LIVETIME)); printf("\"bytes\":%" PRIu64 ",", tx); printf("\"packets\":%" PRIu64 ",", txp); printf("\"totalbytes\":%" PRIu64 ",", txtotal); printf("\"totalpackets\":%" PRIu64 "", txptotal); printf("}}\n"); index++; } } timespent = (uint64_t)time(NULL) - timespent - timeslept; if (!json) { cursorshow(); printf("\n\n"); } /* print some statistics if enough time did pass */ if (!json && timespent >= 10) { printf("\n %s / traffic statistics\n\n", iface); printf(" rx | tx\n"); printf("--------------------------------------+------------------\n"); printf(" bytes %s", getvalue(rxtotal, 15, RT_Normal)); printf(" | %s", getvalue(txtotal, 15, RT_Normal)); printf("\n"); printf("--------------------------------------+------------------\n"); printf(" max %s", gettrafficrate(rxmax, LIVETIME, 15)); printf(" | %s\n", gettrafficrate(txmax, LIVETIME, 15)); printf(" average %s", gettrafficrate(rxtotal, (time_t)timespent, 15)); printf(" | %s\n", gettrafficrate(txtotal, (time_t)timespent, 15)); printf(" min %s", gettrafficrate(rxmin, LIVETIME, 15)); printf(" | %s\n", gettrafficrate(txmin, LIVETIME, 15)); printf("--------------------------------------+------------------\n"); printf(" packets %12" PRIu64 " | %12" PRIu64 "\n", rxptotal, txptotal); printf("--------------------------------------+------------------\n"); printf(" max %9" PRIu64 " p/s | %9" PRIu64 " p/s\n", rxpmax / LIVETIME, txpmax / LIVETIME); printf(" average %9" PRIu64 " p/s | %9" PRIu64 " p/s\n", rxptotal / timespent, txptotal / timespent); printf(" min %9" PRIu64 " p/s | %9" PRIu64 " p/s\n", rxpmin / LIVETIME, txpmin / LIVETIME); printf("--------------------------------------+------------------\n"); if (timespent <= 60) { printf(" time %9" PRIu64 " seconds\n", timespent); } else { printf(" time %7.2f minutes\n", timespent / (double)60); } printf("\n"); } else if (json) { printf("{\"seconds\":%" PRIu64 ",", timespent); printf("\"rx\":{"); printf("\"maxratestring\":\"%s\",", gettrafficrate(rxmax, LIVETIME, 0)); printf("\"averageratestring\":\"%s\",", gettrafficrate(rxtotal, (time_t)timespent, 0)); printf("\"minratestring\":\"%s\",", gettrafficrate(rxmin, LIVETIME, 0)); printf("\"totalbytes\":%" PRIu64 ",", rxtotal); printf("\"maxbytes\":%" PRIu64 ",", rxmax); printf("\"minbytes\":%" PRIu64 ",", rxmin); printf("\"totalpackets\":%" PRIu64 ",", rxptotal); printf("\"maxpackets\":%" PRIu64 ",", rxpmax); printf("\"minpackets\":%" PRIu64 "", rxpmin); printf("},"); printf("\"tx\":{"); printf("\"maxratestring\":\"%s\",", gettrafficrate(txmax, LIVETIME, 0)); printf("\"averageratestring\":\"%s\",", gettrafficrate(txtotal, (time_t)timespent, 0)); printf("\"minratestring\":\"%s\",", gettrafficrate(txmin, LIVETIME, 0)); printf("\"totalbytes\":%" PRIu64 ",", txtotal); printf("\"maxbytes\":%" PRIu64 ",", txmax); printf("\"minbytes\":%" PRIu64 ",", txmin); printf("\"totalpackets\":%" PRIu64 ",", txptotal); printf("\"maxpackets\":%" PRIu64 ",", txpmax); printf("\"minpackets\":%" PRIu64 "", txpmin); printf("}}\n"); } }
void trafficmeter(char iface[], unsigned int sampletime) { /* received bytes packets errs drop fifo frame compressed multicast */ /* transmitted bytes packets errs drop fifo colls carrier compressed */ uint64_t rx, tx, rxp, txp; int json = 0; IFINFO firstinfo; char buffer[256]; if (cfg.qmode == 10) { json = 1; } /* less than 2 seconds doesn't produce good results */ if (sampletime < 2) { printf("Error: Time for sampling too short.\n"); exit(EXIT_FAILURE); } /* read interface info and get values to the first list */ if (!getifinfo(iface)) { printf("Error: Interface \"%s\" not available, exiting.\n", iface); exit(EXIT_FAILURE); } firstinfo.rx = ifinfo.rx; firstinfo.tx = ifinfo.tx; firstinfo.rxp = ifinfo.rxp; firstinfo.txp = ifinfo.txp; /* wait sampletime and print some nice dots so that the user thinks something is done :) */ if (!json) { snprintf(buffer, 256, "Sampling %s (%u seconds average)", iface, sampletime); printf("%s", buffer); fflush(stdout); sleep(sampletime / 3); printf("."); fflush(stdout); sleep(sampletime / 3); printf("."); fflush(stdout); sleep(sampletime / 3); printf("."); fflush(stdout); if ((sampletime / 3) * 3 != sampletime) { sleep(sampletime - ((sampletime / 3) * 3)); } cursortocolumn(1); eraseline(); } else { sleep(sampletime); } /* read those values again... */ if (!getifinfo(iface)) { printf("Error: Interface \"%s\" not available, exiting.\n", iface); exit(EXIT_FAILURE); } /* calculate traffic and packets seen between updates */ rx = countercalc(&firstinfo.rx, &ifinfo.rx, ifinfo.is64bit); tx = countercalc(&firstinfo.tx, &ifinfo.tx, ifinfo.is64bit); rxp = countercalc(&firstinfo.rxp, &ifinfo.rxp, ifinfo.is64bit); txp = countercalc(&firstinfo.txp, &ifinfo.txp, ifinfo.is64bit); /* show the difference in a readable format or json */ if (!json) { printf("%" PRIu64 " packets sampled in %d seconds\n", rxp + txp, sampletime); printf("Traffic average for %s\n", iface); printf("\n rx %s %5" PRIu64 " packets/s\n", gettrafficrate(rx, sampletime, 15), (uint64_t)(rxp / sampletime)); printf(" tx %s %5" PRIu64 " packets/s\n\n", gettrafficrate(tx, sampletime, 15), (uint64_t)(txp / sampletime)); } else { printf("{\"jsonversion\":\"%d\",", JSONVERSION_TR); printf("\"vnstatversion\":\"%s\",", getversion()); printf("\"interface\":\"%s\",", iface); printf("\"sampletime\":%u,", sampletime); printf("\"rx\":{"); printf("\"ratestring\":\"%s\",", gettrafficrate(rx, sampletime, 0)); printf("\"bytespersecond\":%" PRIu64 ",", (uint64_t)(rx / sampletime)); printf("\"packetspersecond\":%" PRIu64 ",", (uint64_t)(rxp / sampletime)); printf("\"bytes\":%" PRIu64 ",", rx); printf("\"packets\":%" PRIu64 "", rxp); printf("},"); printf("\"tx\":{"); printf("\"ratestring\":\"%s\",", gettrafficrate(tx, sampletime, 0)); printf("\"bytespersecond\":%" PRIu64 ",", (uint64_t)(tx / sampletime)); printf("\"packetspersecond\":%" PRIu64 ",", (uint64_t)(txp / sampletime)); printf("\"bytes\":%" PRIu64 ",", tx); printf("\"packets\":%" PRIu64 "", txp); printf("}}\n"); } }
void parseifinfo(int newdb) { uint64_t rxchange=0, txchange=0, btime, cc; /* rxchange = rx change in MB */ uint64_t krxchange=0, ktxchange=0, maxtransfer; /* krxchange = rx change in kB */ time_t current, interval; struct tm *d; int day, month, year, hour, min, shift, maxbw; int rxkchange=0, txkchange=0; /* changes in the kB counters */ ifinfo.rxp = ifinfo.txp = 0; current=time(NULL); interval=current-data.lastupdated; btime=getbtime(); /* count traffic only if previous update wasn't too long ago */ if ( interval < (60*MAXUPDATEINTERVAL) ) { /* btime in /proc/stat seems to vary ±1 second so we use btime-BVAR just to be safe */ /* the variation is also slightly different between various kernels... */ if (data.btime < (btime-cfg.bvar)) { data.currx=0; data.curtx=0; if (debug) printf("System has been booted.\n"); } /* process rx & tx */ if (newdb!=1) { cc = countercalc(data.currx, ifinfo.rx); rxchange = cc/1048576; /* 1024/1024 */ rxkchange = (cc/1024)%1024; krxchange = cc/1024; ifinfo.rxp = cc%1024; cc = countercalc(data.curtx, ifinfo.tx); txchange = cc/1048576; /* 1024/1024 */ txkchange = (cc/1024)%1024; ktxchange = cc/1024; ifinfo.txp = cc%1024; } /* get bandwidth limit for current interface */ maxbw = ibwget(data.interface); if (maxbw > 0) { /* calculate maximum possible transfer since last update based on set maximum rate */ /* and add 10% in order to be on the safe side */ maxtransfer = ceil((maxbw/(float)8)*interval*(float)1.1); if (debug) printf("interval: %"PRIu64" maxbw: %d maxrate: %"PRIu64" rxc: %"PRIu64" txc: %"PRIu64"\n", (uint64_t)interval, maxbw, maxtransfer, rxchange, txchange); /* sync counters if traffic is greater than set maximum */ if ( (rxchange > maxtransfer) || (txchange > maxtransfer) ) { snprintf(errorstring, 512, "Traffic rate for \"%s\" higher than set maximum %d Mbit (%"PRIu64"->%"PRIu64", r%"PRIu64" t%"PRIu64"), syncing.", data.interface, maxbw, (uint64_t)interval, maxtransfer, rxchange, txchange); printe(PT_Info); rxchange = krxchange = rxkchange = txchange = ktxchange = txkchange = 0; ifinfo.rxp = ifinfo.txp = 0; } } } else { if (debug) printf("Too much time passed since previous update, syncing. (%"PRIu64" < %d)\n", (uint64_t)interval, 60*MAXUPDATEINTERVAL); } /* keep btime updated in case it drifts slowly */ data.btime = btime; data.currx = ifinfo.rx - ifinfo.rxp; data.curtx = ifinfo.tx - ifinfo.txp; addtraffic(&data.totalrx, &data.totalrxk, rxchange, rxkchange); addtraffic(&data.totaltx, &data.totaltxk, txchange, txkchange); /* update days and months */ addtraffic(&data.day[0].rx, &data.day[0].rxk, rxchange, rxkchange); addtraffic(&data.day[0].tx, &data.day[0].txk, txchange, txkchange); addtraffic(&data.month[0].rx, &data.month[0].rxk, rxchange, rxkchange); addtraffic(&data.month[0].tx, &data.month[0].txk, txchange, txkchange); /* fill some variables from current date & time */ d=localtime(¤t); day=d->tm_mday; month=d->tm_mon; year=d->tm_year; hour=d->tm_hour; min=d->tm_min; shift=hour; /* add traffic to previous hour when update happens at X:00 */ /* and previous update was during previous hour */ d=localtime(&data.lastupdated); if ((min==0) && (d->tm_hour!=hour) && ((current-data.lastupdated)<=3600)) { hour--; if (hour<0) { hour=23; } } /* clean and update hourly */ cleanhours(); data.hour[shift].date=current; /* avoid shifting timestamp */ data.hour[hour].rx+=krxchange; data.hour[hour].tx+=ktxchange; /* rotate days in database if needed */ d=localtime(&data.day[0].date); if ((d->tm_mday!=day) || (d->tm_mon!=month) || (d->tm_year!=year)) { /* make a new entry only if there's something to remember (configuration dependent) */ if ( (data.day[0].rx==0) && (data.day[0].tx==0) && (data.day[0].rxk==0) && (data.day[0].txk==0) && (cfg.traflessday==0) ) { data.day[0].date=current; } else { rotatedays(); } } /* rotate months in database if needed */ d=localtime(&data.month[0].month); if ((d->tm_mon!=month) && (day>=cfg.monthrotate)) { rotatemonths(); } }