motd.sh: add more details to zfs disk usage

This commit is contained in:
Alice Gaudon 2023-03-08 12:16:20 +01:00
parent b599517c2d
commit 4ca796d515

71
motd.sh
View File

@ -78,11 +78,16 @@ bar_width=50
white="\e[39m" white="\e[39m"
green="\e[1;32m" green="\e[1;32m"
red="\e[1;31m" red="\e[1;31m"
magenta="\e[1;35m"
lightgreen="\e[1;92m"
lightred="\e[1;91m"
yellow="\e[1;33m"
lightyellow="\e[1;93m"
dim="\e[2m" dim="\e[2m"
undim="\e[0m" undim="\e[0m"
# disk usage: ignore zfs, squashfs & tmpfs # disk usage: ignore zfs, squashfs & tmpfs
DISKS=$(df -H -x squashfs -x tmpfs -x devtmpfs -x zfs --output=target,pcent,size,used | tail -n+2) DISKS=$(df -H -x squashfs -x tmpfs -x devtmpfs -x zfs --output=target,pcent,size,used | tail -n+2)" 0 0 0 -"
if test $(command -v zfs); then if test $(command -v zfs); then
DISKS+="\n" DISKS+="\n"
@ -92,7 +97,11 @@ if test $(command -v zfs); then
pcent=$(echo "($used/$ttlsize)*100" | bc -l | numfmt --format="%3.0f") pcent=$(echo "($used/$ttlsize)*100" | bc -l | numfmt --format="%3.0f")
gfxsize=$(numfmt --to=iec --format="%9f" $ttlsize) gfxsize=$(numfmt --to=iec --format="%9f" $ttlsize)
gfxused=$(numfmt --to=iec --format="%9f" $used) gfxused=$(numfmt --to=iec --format="%9f" $used)
DISKS+="$name $pcent% $gfxsize $gfxused\n" usedsnaprecursive=$(zfs get usedbysnapshots -r "$name" -H -p -t filesystem -o value | awk '{s+=$1} END {printf "%.0f", s}')
usedsnappercent=$(echo "($usedsnaprecursive/$ttlsize)*100" | bc -l | numfmt --format="%3.0f")
useddspercent=$(echo "($usedds/$ttlsize)*100" | bc -l | numfmt --format="%3.0f")
usedchildpercent=$(echo "($usedchild/$ttlsize)*100" | bc -l | numfmt --format="%3.0f")
DISKS+="$name $pcent% $gfxsize $gfxused $usedsnappercent $useddspercent $usedchildpercent x$compressratio\n"
done <<< $(zfs list -H -p -d 0 -o name,available,used,usedsnap,usedds,usedchild,compressratio) done <<< $(zfs list -H -p -d 0 -o name,available,used,usedsnap,usedds,usedchild,compressratio)
fi fi
printf "\nDisk usage:\n" printf "\nDisk usage:\n"
@ -102,29 +111,69 @@ while read line; do
continue continue
fi fi
# get disk usage # get disk usage
dds=$(echo "$line" | awk '{print $6}' | sed 's/%//')
dds_width=$((($dds*$bar_width)/100))
child=$(echo "$line" | awk '{print $7}' | sed 's/%//')
child_width=$((($child*$bar_width)/100))
snap=$(echo "$line" | awk '{print $5}' | sed 's/%//')
snap_width=$((($snap*$bar_width)/100))
usage=$(echo "$line" | awk '{print $2}' | sed 's/%//') usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
used_width=$((($usage*$bar_width)/100)) used_width=$((($usage*$bar_width)/100))
# color is green if usage < max_usage, else red #echo "$line"
#echo "$dds,$dds_width|$child,$child_width|$snap,$snap_width|$usage,$used_width"
i=0
# [
bar="["
# DDS
if [ "${usage}" -ge "${max_usage}" ]; then if [ "${usage}" -ge "${max_usage}" ]; then
color=$red bar+=$red
else else
color=$green bar+=$green
fi fi
# print green/red bar until used_width for ((; i<$dds_width; i++)); do
bar="[${color}"
for ((i=0; i<$used_width; i++)); do
bar+="=" bar+="="
done done
# print dimmmed bar until end
# CHILDREN
if [ "${usage}" -ge "${max_usage}" ]; then
bar+=$lightred
else
bar+=$lightgreen
fi
for ((; i<(($dds_width + $child_width)); i++)); do
bar+="="
done
# SNAP
bar+=$lightyellow
for ((; i<(($dds_width + $child_width + $snap_width)); i++)); do
bar+="="
done
# TOTAL USAGE
if [ "${usage}" -ge "${max_usage}" ]; then
bar+=$red
else
bar+=$green
fi
for ((; i<$used_width; i++)); do
bar+="="
done
# FREE SPACE
bar+="${white}${dim}" bar+="${white}${dim}"
for ((i=$used_width; i<$bar_width; i++)); do for ((; i<$bar_width; i++)); do
bar+="=" bar+="="
done done
# ]
bar+="${undim}]" bar+="${undim}]"
# print usage line & bar # print usage line & bar
echo "${line}" | awk '{ printf("%-36s%+4s/%+4s [%+3s]\n", $1, $4, $3, $2); }' | sed -e 's/^/ /' echo "${line}" | awk '{ printf("%-19scompratio %+5s %+4s/%+4s [%+3s]\n", $1, $8, $4, $3, $2); }' | sed -e 's/^/ /'
echo -e "${bar}" | sed -e 's/^/ /' echo -e "${bar}" | sed -e 's/^/ /'
done <<< $(echo -e "$DISKS") done <<< $(echo -e "$DISKS")