Capstone: assess fleet maintenance
Combine inventory, service and backup evidence into a tested fleet assessment with reviewable recommendations.
Give the next operator a usable starting point
A daily fleet assessment should tell an administrator what was checked, what needs review and what could not be assessed. This project combines inventory, service observations and backup-job history under a small reviewed policy. Its recommendations are data for a human handover, not commands executed against machines.
Validate the required files and policy before replacing any previous report. Individual malformed records are different from a missing export: reject them with source line references and continue with the valid evidence. Reserve identifiers only after validation so a malformed first row does not suppress a corrected observation.
Keep the operational meanings separate
Retired assets remain known for reference validation but do not produce active-fleet recommendations. A missing owner or observation makes an active asset unknown, even if other measurements also need review. Preserve all applicable issues so an incomplete collection does not hide a capacity warning.
For backups, select the latest accepted job before deciding whether it is healthy. An older successful job cannot cancel a recent failure. Define timestamp ties and freshness boundaries explicitly. Compare disk utilisation with original measurements rather than a rounded percentage.
Build for repetition and review
Separate parsing, assessment and publication. Use fixed schemas, stable sorting and derived counts. Stage complete reports and replace them only after a successful run, leaving source exports unchanged. Repeating the same assessment should produce the same bytes.
The supplied records are fictional, and the sandbox does not manage your actual accounts, services or remote computers. This course prepares you to build and test the reporting parts of administration. Live operating-system automation needs an authorised environment and additional operational controls. Finish with a report that makes its evidence limitations visible instead of implying that missing observations are healthy systems.
printf 'Fleet sources:\n'
for name in inventory.tsv services.tsv backups.tsv;do
printf '%s: ' "$name"
awk 'END {printf "%d data rows\n",NR-1}' "fleet/$name"
done
Put it into practice.
Build bash main.sh [input_directory] [output_directory], defaulting to fleet and reports. Read required inventory.tsv, services.tsv, backups.tsv and policy.conf. Use UTF-8, LF records and literal tabs, with no embedded tabs/newlines or ASCII control characters in fields. Identifiers are case sensitive. Preserve all inputs and write reports to a separate safe relative workspace directory. Missing inputs fail nonzero with Missing input on stderr; a missing or incorrect table header fails with Invalid header. Startup failures must not replace previous reports.
Policy contains exactly one of each key: min_memory_gib (1–1024), max_disk_percent (1–100), max_backup_age (1–86400 seconds) and now_second (0–1000000000). Values are canonical nonnegative decimal integers without leading zeros, written key=value with no surrounding spaces. Allow blank lines and lines beginning #. Unknown or duplicate keys, missing keys and invalid values fail with Invalid policy on stderr. Never source or execute policy text.
Inventory header: asset_id<TAB>hostname<TAB>owner<TAB>state<TAB>memory_gib<TAB>used_mib<TAB>total_mib. Reject each row with the first applicable reason: fields (not seven), asset_id (not ^[A-Za-z0-9_-]+$), hostname (empty), state (not active or retired), memory_gib (not canonical 1–1024), used_mib (not canonical 0–1000000), total_mib (not canonical 1–1000000), capacity (used > total), duplicate (asset ID already accepted). Owner may be empty or contain spaces/Unicode. Only valid rows reserve IDs. Retired assets remain known for joins but are excluded from fleet reports and recommendations.
Services header: asset_id<TAB>service<TAB>state. Validate in order: fields (not three), asset_id (invalid identifier), unknown_asset (absent from accepted inventory), service (not ^[A-Za-z0-9_.-]+$), state (not running or stopped), duplicate (accepted asset/service pair). First valid pair wins. Every active asset requires an observation for exactly backup-agent; other services do not fulfil this requirement.
Backups header: job_id<TAB>asset_id<TAB>second<TAB>result. Validate in order: fields (not four), job_id (invalid identifier), asset_id (invalid identifier), unknown_asset, second (not canonical 0–now_second), result (not ok or failed), duplicate (job ID already accepted anywhere). First valid job ID wins. Select each asset’s greatest second, then greatest job ID in LC_ALL=C lexical order. Select the latest job before assessing its result; do not fall back to an older success.
Write fleet_report.tsv with header asset_id<TAB>hostname<TAB>owner<TAB>memory_gib<TAB>disk_percent<TAB>service<TAB>backup_job<TAB>backup_age<TAB>backup_state<TAB>status. Include active assets sorted by asset ID in LC_ALL=C order. Empty owner becomes unassigned. Disk percent is floor(used*100/total), but compare original cross-products for the threshold. Service is the backup-agent observation or missing. No backup gives job -, age - and state missing. Otherwise age is now_second-second; failed latest result gives state failed, successful age > max_backup_age gives stale, otherwise ok. Overall status is unknown if owner, service or backup is missing; otherwise review if any issue below exists, otherwise ok.
Write actions.tsv with header asset_id<TAB>issue<TAB>observed<TAB>expected<TAB>action<TAB>source<TAB>line, sorted by asset ID then issue in LC_ALL=C order. Add one row for each applicable rule: owner_missing uses -, assigned, assign_owner; memory_low (memory < minimum) uses the observed number, >=minimum, review_capacity; disk_high (used*100 >= total*max_disk_percent) uses the floored percent with %, <threshold%, review_capacity; service_missing uses missing, running, investigate_service; service_stopped uses stopped, running, review_service; backup_missing uses missing, recent_ok, investigate_backup; backup_failed uses failed, ok, review_backup; backup_stale uses age seconds, <=max_backup_age, review_backup. The source is inventory.tsv, services.tsv or backups.tsv as appropriate. Line is the accepted source’s physical line number, or - for a missing service/backup. These are recommendations, never executed commands. Include all issues even when the overall asset status is unknown.
Write errors.tsv with header source<TAB>line<TAB>reason for rejected rows, ordered by inventory, services, then backups, retaining input order inside each source. Invalid rows do not abort the assessment. Write summary.txt with these key=value lines in order: inventory_accepted, active_assets, retired_assets, service_records (all accepted service rows), backup_records (all accepted jobs), rejected, ok, review, unknown, actions, then scope=Supplied exports; recommendations only. Counts ok/review/unknown refer to active-asset status, not action rows. Include final newlines and headers even for empty reports. Successful repeated runs replace all four reports and emit no stdout or stderr.
See an explained solution
export LC_ALL=C
input=${1:-fleet};out=${2:-reports}
for name in inventory.tsv services.tsv backups.tsv policy.conf;do
[ -f "$input/$name" ] || { printf 'Missing input: %s\n' "$name" >&2;exit 1; }
done
values=$(awk -F '=' '
/^[[:space:]]*$/ || /^#/ {next}
{if(NF!=2 || ($1!="min_memory_gib" && $1!="max_disk_percent" && $1!="max_backup_age" && $1!="now_second") || $2!~/^(0|[1-9][0-9]*)$/ || length($2)>10 || seen[$1]++) bad=1;v[$1]=$2}
END {if(bad || !seen["min_memory_gib"] || !seen["max_disk_percent"] || !seen["max_backup_age"] || !seen["now_second"] || v["min_memory_gib"]<1 || v["min_memory_gib"]>1024 || v["max_disk_percent"]<1 || v["max_disk_percent"]>100 || v["max_backup_age"]<1 || v["max_backup_age"]>86400 || v["now_second"]<0 || v["now_second"]>1000000000) exit 1;printf "%d %d %d %d\n",v["min_memory_gib"],v["max_disk_percent"],v["max_backup_age"],v["now_second"]}
' "$input/policy.conf") || { printf 'Invalid policy\n' >&2;exit 1; }
read -r min_memory max_disk max_age now <<< "$values"
[ "$(head -n 1 "$input/inventory.tsv")" = $'asset_id\thostname\towner\tstate\tmemory_gib\tused_mib\ttotal_mib' ] || { printf 'Invalid header: inventory.tsv\n' >&2;exit 1; }
[ "$(head -n 1 "$input/services.tsv")" = $'asset_id\tservice\tstate' ] || { printf 'Invalid header: services.tsv\n' >&2;exit 1; }
[ "$(head -n 1 "$input/backups.tsv")" = $'job_id\tasset_id\tsecond\tresult' ] || { printf 'Invalid header: backups.tsv\n' >&2;exit 1; }
tmp=$(mktemp -d "${TMPDIR:-/tmp}/rocks-fleet.XXXXXX") || exit 1
trap 'rm -rf "$tmp"' EXIT
: > "$tmp/fleet.data";: > "$tmp/actions.data"
printf 'source\tline\treason\n' > "$tmp/errors.tsv"
awk -F '\t' -v min_memory="$min_memory" -v max_disk="$max_disk" -v max_age="$max_age" -v now="$now" -v work="$tmp" '
function integer(s,low,high){return s~/^(0|[1-9][0-9]*)$/ && length(s)<=10 && s>=low && s<=high}
function id_ok(s){return s~/^[A-Za-z0-9_-]+$/}
function reject(source,line,reason){printf "%s\t%d\t%s\n",source,line,reason >> (work "/errors.tsv");rejected++}
function action(id,issue,observed,expected,next_step,source,line){printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",id,issue,observed,expected,next_step,source,line >> (work "/actions.data");actions++;issue_count[id]++}
FILENAME==ARGV[1] {
if(FNR==1) next
reason=""
if(NF!=7) reason="fields"
else if(!id_ok($1)) reason="asset_id"
else if($2=="") reason="hostname"
else if($4!="active" && $4!="retired") reason="state"
else if(!integer($5,1,1024)) reason="memory_gib"
else if(!integer($6,0,1000000)) reason="used_mib"
else if(!integer($7,1,1000000)) reason="total_mib"
else if($6+0>$7+0) reason="capacity"
else if($1 in asset_state) reason="duplicate"
if(reason!=""){reject("inventory.tsv",FNR,reason);next}
asset_state[$1]=$4;hostname[$1]=$2;owner[$1]=$3;memory[$1]=$5;used[$1]=$6;total[$1]=$7;asset_line[$1]=FNR;accepted++;next
}
FILENAME==ARGV[2] {
if(FNR==1) next
reason="";key=$1 SUBSEP $2
if(NF!=3) reason="fields"
else if(!id_ok($1)) reason="asset_id"
else if(!($1 in asset_state)) reason="unknown_asset"
else if($2!~/^[A-Za-z0-9_.-]+$/) reason="service"
else if($3!="running" && $3!="stopped") reason="state"
else if(key in services) reason="duplicate"
if(reason!=""){reject("services.tsv",FNR,reason);next}
services[key]=$3;service_line[key]=FNR;service_records++;next
}
FNR>1 {
reason=""
if(NF!=4) reason="fields"
else if(!id_ok($1)) reason="job_id"
else if(!id_ok($2)) reason="asset_id"
else if(!($2 in asset_state)) reason="unknown_asset"
else if(!integer($3,0,now)) reason="second"
else if($4!="ok" && $4!="failed") reason="result"
else if($1 in seen_job) reason="duplicate"
if(reason!=""){reject("backups.tsv",FNR,reason);next}
seen_job[$1]=1;backup_records++;id=$2
if(!(id in job_second) || $3>job_second[id] || ($3==job_second[id] && ("x" $1)>("x" job_id[id]))){job_id[id]=$1;job_second[id]=$3;job_result[id]=$4;job_line[id]=FNR}
}
END {
for(id in asset_state){
if(asset_state[id]=="retired"){retired++;continue}
active++;unknown=0
if(owner[id]==""){action(id,"owner_missing","-","assigned","assign_owner","inventory.tsv",asset_line[id]);unknown=1}
if(memory[id]<min_memory) action(id,"memory_low",memory[id],">=" min_memory,"review_capacity","inventory.tsv",asset_line[id])
percent=int(used[id]*100/total[id])
if(used[id]*100>=total[id]*max_disk) action(id,"disk_high",percent "%","<" max_disk "%","review_capacity","inventory.tsv",asset_line[id])
key=id SUBSEP "backup-agent"
if(!(key in services)){service="missing";action(id,"service_missing","missing","running","investigate_service","services.tsv","-");unknown=1}
else {service=services[key];if(service=="stopped") action(id,"service_stopped","stopped","running","review_service","services.tsv",service_line[key])}
selected_job="-";age="-";backup_state="missing"
if(!(id in job_id)){action(id,"backup_missing","missing","recent_ok","investigate_backup","backups.tsv","-");unknown=1}
else {
selected_job=job_id[id];age=now-job_second[id]
backup_state=(job_result[id]=="failed"?"failed":(age>max_age?"stale":"ok"))
if(backup_state=="failed") action(id,"backup_failed","failed","ok","review_backup","backups.tsv",job_line[id])
else if(backup_state=="stale") action(id,"backup_stale",age,"<=" max_age,"review_backup","backups.tsv",job_line[id])
}
status=(unknown?"unknown":(issue_count[id]>0?"review":"ok"));state_count[status]++
printf "%s\t%s\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%s\n",id,hostname[id],(owner[id]==""?"unassigned":owner[id]),memory[id],percent,service,selected_job,age,backup_state,status >> (work "/fleet.data")
}
printf "inventory_accepted=%d\nactive_assets=%d\nretired_assets=%d\nservice_records=%d\nbackup_records=%d\nrejected=%d\nok=%d\nreview=%d\nunknown=%d\nactions=%d\nscope=Supplied exports; recommendations only.\n",accepted,active,retired,service_records,backup_records,rejected,state_count["ok"],state_count["review"],state_count["unknown"],actions > (work "/summary.txt")
}
' "$input/inventory.tsv" "$input/services.tsv" "$input/backups.tsv" || exit 1
printf 'asset_id\thostname\towner\tmemory_gib\tdisk_percent\tservice\tbackup_job\tbackup_age\tbackup_state\tstatus\n' > "$tmp/fleet_report.tsv"
sort -t $'\t' -k1,1 "$tmp/fleet.data" >> "$tmp/fleet_report.tsv" || exit 1
printf 'asset_id\tissue\tobserved\texpected\taction\tsource\tline\n' > "$tmp/actions.tsv"
sort -t $'\t' -k1,1 -k2,2 "$tmp/actions.data" >> "$tmp/actions.tsv" || exit 1
mkdir -p "$out" || exit 1
for name in fleet_report.tsv actions.tsv errors.tsv summary.txt;do mv "$tmp/$name" "$out/$name" || exit 1;done
The reference separates startup failures from record rejection, validates joins against accepted inventory, and evaluates active assets without losing missing coverage. Deterministic job selection and exact threshold comparisons produce stable recommendations. Source references, independent counts and staged report replacement make the result useful for a repeatable daily handover.