diff --git a/yabs.sh b/yabs.sh index 6bffcd0..27836eb 100755 --- a/yabs.sh +++ b/yabs.sh @@ -106,6 +106,45 @@ if [ ! -z "$PRINT_HELP" ]; then exit 0 fi +# format_size +# Purpose: Formats raw disk and memory sizes from kibibytes (KiB) to largest unit +# Parameters: +# 1. RAW - the raw memory size (RAM/Swap) in kibibytes +# Returns: +# Formatted memory size in KiB, MiB, GiB, or TiB +function format_mem { + RAW=$1 # mem size in KiB + RESULT=$RAW + local DENOM=1 + local UNIT="KiB" + + # ensure the raw value is a number, otherwise return blank + re='^[0-9]+$' + if ! [[ $RAW =~ $re ]] ; then + echo "" + return 0 + fi + + if [ "$RAW" -ge 1073741824 ]; then + DENOM=1073741824 + UNIT="TiB" + elif [ "$RAW" -ge 1048576 ]; then + DENOM=1048576 + UNIT="GiB" + elif [ "$RAW" -ge 1024 ]; then + DENOM=1024 + UNIT="MiB" + fi + + # divide the raw result to get the corresponding formatted result (based on determined unit) + RESULT=$(awk -v a="$RESULT" -v b="$DENOM" 'BEGIN { print a / b }') + # shorten the formatted result to two decimal places (i.e. x.x) + RESULT=$(echo $RESULT | awk -F. '{ printf "%0.1f",$1"."substr($2,1,2) }') + # concat formatted result value with units and return result + RESULT="$RESULT $UNIT" + echo $RESULT +} + # gather basic system information (inc. CPU, AES-NI/virt status, RAM + swap + disk size) echo -e echo -e "Basic System Information:" @@ -121,12 +160,12 @@ echo -e "AES-NI : $CPU_AES" CPU_VIRT=$(cat /proc/cpuinfo | grep 'vmx\|svm') [[ -z "$CPU_VIRT" ]] && CPU_VIRT="\xE2\x9D\x8C Disabled" || CPU_VIRT="\xE2\x9C\x94 Enabled" echo -e "VM-x/AMD-V : $CPU_VIRT" -TOTAL_RAM=$(free -h | awk 'NR==2 {print $2}') +TOTAL_RAM=$(format_mem $(free | awk 'NR==2 {print $2}')) echo -e "RAM : $TOTAL_RAM" -TOTAL_SWAP=$(free -h | grep Swap | awk '{ print $2 }') +TOTAL_SWAP=$(format_mem $(free | grep Swap | awk '{ print $2 }')) echo -e "Swap : $TOTAL_SWAP" # total disk size is calculated by adding all partitions of the types listed below (after the -t flags) -TOTAL_DISK=$(df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs -t swap --total -h | grep total | awk '{ print $2 }') +TOTAL_DISK=$(format_mem $(df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs -t swap --total 2>/dev/null | grep total | awk '{ print $2 }')) echo -e "Disk : $TOTAL_DISK" # create a directory in the same location that the script is being run to temporarily store YABS-related files