04 / RELIABLE NETWORK TOOLS · LESSON 13

Calculate interface rates

Calculate rates from counter deltas and reject resets instead of inventing traffic.

18 min · learn at your pace

A cumulative counter is not a rate

An interface octet counter records a total. Subtract the earlier reading from the later one, multiply by eight to convert bytes to bits, then divide by elapsed seconds. Comparing raw totals between interfaces says little about their recent traffic.

The input contract gives two readings for the same link and a positive speed in bits per second. Validate the time relationship first. If the end is not later than the start, no rate can be computed. If the counter decreased, label a counter reset rather than assuming a particular wrap width or producing a negative traffic rate.

Decide before rounding the display

A displayed utilisation percentage can hide the actual threshold. Compare cross-products of the original delta, elapsed time and speed to decide whether utilisation is at least 80%. Floor only the displayed rate and percentage. A measurement just below 80% stays ok even if a rounded dashboard might show 80.

All supplied numbers stay within the exact integer range used by this awk calculation. An estimate above 100% is retained for review rather than silently capped; it may reveal timing or collector inconsistencies. The script interprets recorded counters and makes no live interface queries.

Try an example.sh
awk 'BEGIN{delta=100000;seconds=10;printf "%.0f bits per second\n",delta*8/seconds}'
YOUR TURN

Put it into practice.

counters.tsv has validated link_id<TAB>start_second<TAB>end_second<TAB>before_octets<TAB>after_octets<TAB>speed_bps. IDs are unique; seconds are 0–1000000000, any positive elapsed interval is at most 86400 seconds, counters 0–1000000000000, speed 1–1000000000. Print link_id<TAB>bps<TAB>percent<TAB>state sorted by ID. First use invalid_time if end <= start; otherwise counter_reset if after < before, with - for both numeric outputs. Otherwise floor delta*8/elapsed and delta*800/(elapsed*speed). State is review when exact utilisation >=80%, else ok. Do not infer wraparound or cap percentages.

See an explained solution
One way to solve it.sh
export LC_ALL=C
printf 'link_id\tbps\tpercent\tstate\n'
awk -F '\t' 'NR>1{t=$3-$2;d=$5-$4;if(t<=0){printf "%s\t-\t-\tinvalid_time\n",$1;next}if(d<0){printf "%s\t-\t-\tcounter_reset\n",$1;next}state=(d*800>=t*$6*80)?"review":"ok";printf "%s\t%.0f\t%.0f\t%s\n",$1,int(d*8/t),int(d*800/(t*$6)),state}' counters.tsv | sort

Time and reset checks prevent invalid measurements from being disguised as traffic. Exact threshold comparison keeps display rounding out of the decision, while uncapped percentages expose inconsistent or over-capacity observations for review.