colorize usage

This commit is contained in:
2026-07-13 10:26:49 -04:00
parent 76346f4e61
commit 0ae2e0a622
+25 -2
View File
@@ -49,6 +49,28 @@ HTML_NVME=""
HTML_ZFS="" HTML_ZFS=""
HTML_SPACE="" HTML_SPACE=""
# Map a usage percentage to a hex color: green at 0%, amber at 50%,
# red at 100%, linearly interpolated per RGB channel. Endpoints are the
# same dark shades used by state_html so they stay readable on white.
# Done in bash because email clients can't compute colors in CSS.
usage_color() {
local p=$1 r g b
[ "$p" -ge 0 ] 2>/dev/null || { printf '#222222'; return; }
[ "$p" -gt 100 ] && p=100
if [ "$p" -le 50 ]; then
# 1a7f37 (green) -> 9a6700 (amber)
r=$(( 26 + (154 - 26) * p / 50 ))
g=$(( 127 + (103 - 127) * p / 50 ))
b=$(( 55 + (0 - 55) * p / 50 ))
else
# 9a6700 (amber) -> cf222e (red)
r=$(( 154 + (207 - 154) * (p - 50) / 50 ))
g=$(( 103 + (34 - 103) * (p - 50) / 50 ))
b=$(( 0 + (46 - 0) * (p - 50) / 50 ))
fi
printf '#%02x%02x%02x' "$r" "$g" "$b"
}
# Green for healthy, red for anything else. UNKNOWN is amber because it # Green for healthy, red for anything else. UNKNOWN is amber because it
# means smartctl couldn't read the device, not that the device failed. # means smartctl couldn't read the device, not that the device failed.
state_html() { state_html() {
@@ -190,11 +212,12 @@ while IFS= read -r line; do
used=$(echo "$line" | awk '{print $3}') used=$(echo "$line" | awk '{print $3}')
avail=$(echo "$line" | awk '{print $4}') avail=$(echo "$line" | awk '{print $4}')
STATUS+=" $mount: ${usage}% (${used}/${size}, ${avail} free)\n" STATUS+=" $mount: ${usage}% (${used}/${size}, ${avail} free)\n"
usage_cell="${usage}%" weight=""
if [ "$usage" -gt "$SPACE_THRESHOLD" ] 2>/dev/null; then if [ "$usage" -gt "$SPACE_THRESHOLD" ] 2>/dev/null; then
PROBLEMS+="[SPACE] $mount is ${usage}% full\n" PROBLEMS+="[SPACE] $mount is ${usage}% full\n"
usage_cell="<span style=\"color:#cf222e;font-weight:bold\">${usage}%</span>" weight=";font-weight:bold"
fi fi
usage_cell="<span style=\"color:$(usage_color "$usage")$weight\">${usage}%</span>"
HTML_SPACE+="<tr><td $TD>$mount</td><td $TDR>$usage_cell</td><td $TDR>$used</td><td $TDR>$size</td><td $TDR>$avail</td></tr>"$'\n' HTML_SPACE+="<tr><td $TD>$mount</td><td $TDR>$usage_cell</td><td $TDR>$used</td><td $TDR>$size</td><td $TDR>$avail</td></tr>"$'\n'
done < <(df -h --output=source,size,used,avail,pcent,target -x tmpfs -x devtmpfs -x overlay -x efivarfs 2>/dev/null | tail -n +2 || true) done < <(df -h --output=source,size,used,avail,pcent,target -x tmpfs -x devtmpfs -x overlay -x efivarfs 2>/dev/null | tail -n +2 || true)