Management wants a weekly email with disk usage of all servers as an HTML table. How do you build it?
BeginnerScenarioScenarios: text processing and scheduling decisions
Answer
Situation read → Collect data, render HTML with a here-doc, send with a mail tool.
What I'd do →
{
cat <<'HTML'
<html><body><h3>Weekly disk usage</h3><table border="1" cellpadding="4">
<tr><th>Host</th><th>Mount</th><th>Used</th><th>Size</th><th>Use%</th></tr>
HTML
for h in "${hosts[@]}"; do
ssh -n -o ConnectTimeout=5 "$h" 'df -hP -x tmpfs -x devtmpfs' | awk -v h="$h" 'NR > 1 {
style = ($5+0 >= 85) ? " style=\"background:#fdd\"" : ""
printf "<tr%s><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", style, h, $6, $3, $2, $5}'
done
echo '</table></body></html>'
} > /tmp/disk-report.html
mail -a 'Content-Type: text/html' -s "Disk usage $(date +%F)" [email protected] < /tmp/disk-report.html
- The
mail -aheader syntax is for GNU Mailutils; s-nail and msmtp differ, so test on your distro. - Highlight rows above 85% so readers see problems at a glance.
Why → A here-doc plus awk is enough for simple HTML; no templating engine needed.
Interview tip → Mention that a Grafana dashboard with alerts is better than an email nobody reads, but reports still have their place.
More on Scenarios: text processing and scheduling decisions
Users report errors in the last hour. From the Nginx access log, find which endpoints produce the most 5xx responses.BeginnerA MySQL slow log is 3 GB. You need the 10 slowest query patterns. What do you do?IntermediateTwo servers that should be identical behave differently. Compare their installed packages.BeginnerFind which pods were OOMKilled in a namespace, with their container names and restart counts.IntermediateProduce a CSV of running EC2 instances that are missing an `Owner` tag.IntermediateYou have access logs from five web servers and need one timeline sorted by time. How?Intermediate