04 / RELIABLE NETWORK TOOLS · LESSON 14

Summarise link events in a window

Deduplicate link events and apply a half-open time window consistently.

18 min · learn at your pace

A window needs two explicit boundaries

A half-open window includes its start and excludes its end. Consecutive windows can then meet without counting the shared endpoint twice. The collector has already normalised timestamps to integer seconds on one time scale; parsing timezone strings is outside this exercise.

Repeated collection can produce the same event ID more than once. This task keeps the first occurrence in the full file before applying the window. A later duplicate inside the window must not replace an earlier occurrence outside it. Other systems may choose another policy, but whichever policy you choose needs to be explicit and tested.

Separate event counts from final state

Count up and down events for each device/port pair. Determine the final observed state from the greatest timestamp, with lexical event ID as the deterministic tie-break. Do not assume input order is chronological. Two or more down events within the window trigger a flapping review in this teaching policy.

An event count is not a duration. These records do not prove how long a link was down, and ports absent from the report may simply have no events in this window. Retaining this distinction prevents a small report from becoming an unsupported availability claim.

Try an example.sh
awk -F '\t' 'NR>1 {printf "%s at %s: %s\n",$1,$4,$5}' events.tsv
YOUR TURN

Put it into practice.

events.tsv contains validated event_id<TAB>device_id<TAB>port<TAB>second<TAB>state rows, state up or down. IDs start with letters. window.tsv contains start<TAB>end as one numeric row, with start < end. Keep the first occurrence of each event ID in the entire file, then select start <= second < end. Print device_id<TAB>port<TAB>downs<TAB>ups<TAB>last_state<TAB>review sorted by device/port. Last state comes from greatest second, then lexical greatest event ID. Review is flapping if downs >=2, otherwise ok. Omit ports with no selected events.

See an explained solution
One way to solve it.sh
export LC_ALL=C
IFS=$'\t' read -r start end < window.tsv
printf 'device_id\tport\tdowns\tups\tlast_state\treview\n'
awk -F '\t' -v start="$start" -v end="$end" 'NR>1{
 if($1 in seen)next;seen[$1]=1
 if($4<start || $4>=end)next
 k=$2 SUBSEP $3;device[k]=$2;port[k]=$3
 if($5=="down")downs[k]++;else ups[k]++
 if(!(k in latest) || $4>latest[k] || ($4==latest[k] && ("x" $1)>("x" id[k]))){latest[k]=$4;id[k]=$1;state[k]=$5}
}END{for(k in device)printf "%s\t%s\t%d\t%d\t%s\t%s\n",device[k],port[k],downs[k],ups[k],state[k],(downs[k]>=2?"flapping":"ok")}' events.tsv | sort

The report implements a documented deduplication and time-window contract. Timestamp-and-ID selection makes final state independent of row order, and the review label describes repeated events rather than inventing downtime duration.