Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daniilyar-confyrm/ac16310e93e08d7d5311ee036b2a0655 to your computer and use it in GitHub Desktop.
Save daniilyar-confyrm/ac16310e93e08d7d5311ee036b2a0655 to your computer and use it in GitHub Desktop.
[Nagios check, Linux] Check if physical disk size is bigger enough than total size of it's logical partitions (helps to identify partitions which could be extended with resize2fs util)
#!/bin/bash
set -e
OK=0
WARN=1
CRIT=2
UNKNOWN=3
SENSITIVITY_THRESHOLD_PERCENTS=10
function bytes_to_gb {
result=`echo "scale=3;$1/1024/1024/1024"|bc`
printf ${result}
}
IFS=$'\n'
for disk_info in `lsblk -bn | grep -v ─ | grep disk`; do
disk_name=`echo "$disk_info" | awk '{print $1}'`
disk_size_bytes=`echo "$disk_info" | awk '{print $4}'`
# Calculate total partitions size for current disk
total_partitions_size_bytes=0
for partition_info in `df -a | tail -n +2 | grep $disk_name | awk '{print $1,$2,$3,$4,$5}' | sort | uniq`
do
partition_size=`echo "$partition_info" | awk '{print $2}'`
if [[ "$partition_size" == ?(-)+([0-9]) ]]; then
total_partitions_size_bytes=$(( total_partitions_size_bytes + partition_size * 1024 ))
fi
done
difference_bytes=$(( disk_size_bytes - total_partitions_size_bytes ))
disk_size_gb=`bytes_to_gb $disk_size_bytes`
total_partitions_size_gb=`bytes_to_gb $total_partitions_size_bytes`
difference_gb=`bytes_to_gb $difference_bytes`
#echo "[/dev/$disk_name] disk phisical size ${disk_size_gb}GB, total partitions size is ${total_partitions_size_gb}GB"
#echo "Difference is $(( difference_bytes * 100 / disk_size_bytes )) percents"
if (( difference_bytes * 100 / disk_size_bytes > SENSITIVITY_THRESHOLD_PERCENTS )); then
echo "CRITICAL - one or more partitions at /dev/$disk_name needs to be extended (phisical disk size is ${disk_size_gb}GB, but total size of it's partitions is ${total_partitions_size_gb}GB). Use 'sudo resize2fs /dev/${disk_name}[1-9]' to fix"
exit $CRIT
fi
done
echo "OK - no partitions needs to be resized"
exit $OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment