From 4ca796d5159eca8f9d238d0481742d7fda80576d Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 8 Mar 2023 12:16:20 +0100 Subject: [PATCH] motd.sh: add more details to zfs disk usage --- motd.sh | 71 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/motd.sh b/motd.sh index 3c44c9c..98b874e 100755 --- a/motd.sh +++ b/motd.sh @@ -78,11 +78,16 @@ bar_width=50 white="\e[39m" green="\e[1;32m" 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" undim="\e[0m" # 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 DISKS+="\n" @@ -92,7 +97,11 @@ if test $(command -v zfs); then pcent=$(echo "($used/$ttlsize)*100" | bc -l | numfmt --format="%3.0f") gfxsize=$(numfmt --to=iec --format="%9f" $ttlsize) 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) fi printf "\nDisk usage:\n" @@ -102,29 +111,69 @@ while read line; do continue fi # 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/%//') 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 - color=$red + bar+=$red else - color=$green + bar+=$green fi - # print green/red bar until used_width - bar="[${color}" - for ((i=0; i<$used_width; i++)); do + for ((; i<$dds_width; i++)); do bar+="=" 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}" - for ((i=$used_width; i<$bar_width; i++)); do + for ((; i<$bar_width; i++)); do bar+="=" done + + # ] bar+="${undim}]" # 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/^/ /' done <<< $(echo -e "$DISKS")