Read-only system inventory
A read-only inventory reports what a system contains without changing it. A good report declares its input format, measurement units and failure policy. In a production Linux environment, df describes filesystem capacity, du estimates file space, and service managers expose service state. These are different measurements: apparent bytes are not necessarily allocated blocks, and a service that is active is not necessarily healthy. Do not run privileged probes on an unapproved host.
Separate collection from reporting
Collect records with a dedicated adapter and normalize them into a small contract. In this lesson the disk adapter emits label used total with integer values in the same unit; labels contain no whitespace. The service adapter emits name state with a fixed state vocabulary. Never interpret a failed collector as an empty successful report. Mark unavailable fields explicitly, preserve useful successful sections, send diagnostics to stderr, and return a nonzero status for incomplete collection. The fixtures below use no actual disk capacity or service queries.
Use a declared threshold (here 80 percent, inclusive) to classify usage. Reject zero capacity and invalid records before calculating. The sample operates on fixed small integers; a general collector must also limit numeric ranges and handle filesystem names and paths safely. Report the observation time and source in real inventory, and avoid collecting usernames, secrets or unnecessary host identifiers. df and du need separate adapters; don't assume human-readable units are suitable for parsing. The disk parser may emit valid rows before reporting a malformed row with status 2. Treat that stdout as incomplete; its integer percentage is truncated, while the threshold compares the original ratio.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Classify synthetic disk capacity
printf '%s\n' 'data 80 100' 'cache 20 100' > disk.fixture || exit 1awk '{ if (NF != 3 || $1 !~ /^[a-z]+$/ || $2 !~ /^[0-9]+$/ || $3 !~ /^[0-9]+$/ || length($2) > 3 || length($3) > 3 || $3 <= 0 || $2 > $3) { bad=1; next } printf "%s %d%% %s\n", $1, 100*$2/$3, ($2*100 >= $3*80 ? "alert" : "ok") } END { if (bad) exit 2 }' disk.fixtureExpected stdout:
data 80% alertcache 20% okThe comparison includes 80%. The data is authored, not a live filesystem reading.
Measure apparent bytes in a fixture
printf 'abc' > sample.txt || exit 1du -b -- sample.txtExpected stdout:
3 sample.txtGNU du -b reports apparent bytes. Actual allocated space may differ.
Keep partial report and failure status
report() { local rc=0 printf 'disk=available\n' if (exit 7); then printf 'service=active\n' else printf 'service=unknown\n' printf 'service collector failed\n' >&2 rc=1 fi return "$rc"}reportrc=$?[[ $rc == 1 ]] || exit 1printf 'report_status=%s\n' "$rc"Expected stdout:
disk=availableservice=unknownreport_status=1Expected stderr:
service collector failedThe fixed child simulates a collector failure. The wrapper prints its status for learning; a real entry point must propagate it.
Common mistakes
A missing report is not a healthy system. du -b is GNU-specific apparent size, not free capacity. Check every collector status; never repair a service merely because a report says inactive.
Practice
Extend the fixed disk records with 79%, 80% and an unavailable section. Predict the report and decide whether to return nonzero. Do not query the host.
Next: Log summaries and maintenance