03 / ROUTING AND RECORDED DIAGNOSTICS · LESSON 11

Review active DHCP lease conflicts

Count distinct active clients per address at an explicit observation time.

18 min · learn at your pace

A duplicate row is not necessarily a conflict

Two lease records can refer to the same client and address. That is different from two distinct active clients claiming one address. Count unique clients per address rather than raw rows, and decide which leases are active before grouping them.

The supplied time is a fixed integer second. A lease is active only when its expiry is strictly greater than that time. A lease expiring exactly at the observation time is no longer active. This boundary should be written in the task and covered by a test, rather than inherited accidentally from a greater-than-or-equal operator.

Scope the conclusion to the export

An address with more than one distinct active client needs conflict review. It does not establish which client actually has connectivity or whether a reservation or relay configuration is correct. Those questions require additional evidence from the real network.

Both address and client identifiers are validated in this exercise. The report excludes addresses with no active leases and uses explicit lexical ordering for reproducibility. It neither renews nor revokes a lease. Keeping collection time, filtering and grouping separate is a useful pattern for DHCP, sessions and other data whose meaning changes as time passes.

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

Put it into practice.

leases.tsv has validated lease_id<TAB>address<TAB>client_id<TAB>expires_second rows; identifiers are nonempty and expiry is a nonnegative integer. now.txt contains one canonical observation second. A lease is active only when expires_second > now. Print address<TAB>active_clients<TAB>state for addresses with active leases, sorted lexically by address. Count distinct client IDs per address, not rows. State is conflict for more than one distinct active client, otherwise single. Expiry equal to now is inactive.

See an explained solution
One way to solve it.sh
export LC_ALL=C
read -r now < now.txt
printf 'address\tactive_clients\tstate\n'
awk -F '\t' -v now="$now" 'NR>1 && $4>now {key=$2 SUBSEP $3;if(!(key in seen)){seen[key]=1;count[$2]++}}
END{for(ip in count)printf "%s\t%d\t%s\n",ip,count[ip],(count[ip]>1?"conflict":"single")}' leases.tsv | sort

The observation time is an explicit input, so results can be reproduced later. Filtering first and counting unique clients prevents duplicate collection rows or expired leases from creating false conflicts.