Last active
April 6, 2024 14:06
-
-
Save Sparrow1029/0241d9b48d1f3c72da25d43d97a9b895 to your computer and use it in GitHub Desktop.
Use /proc/meminfo file to check available memory for system and warn based on given argument thresholds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set thresholds from script args | |
WARN_THRESHOLD=$1 | |
CRIT_THRESHOLD=$2 | |
# Find current memory status from /proc/meminfo | |
current=$(head -n 3 /proc/meminfo) | |
memtotal=$(expr $(echo $current | awk '{ print $2 }') / 1024) | |
memfree=$(expr $(echo $current | awk '{ print $5 }') / 1024) | |
memused=$(expr ${memtotal} - ${memfree}) | |
# Use bc to do floating-point calculation in bash | |
MemPercentDecimal=$(echo "scale=2; ${memused} / ${memtotal} * 100" | bc) | |
# Use string formatting to convert back to integer for comparison operators | |
MemPercent=$(echo ${MemPercentDecimal%%.*}) | |
# Check exit status of boolean comparison commands to set flags | |
[[ $MemPercent -ge $WARN_THRESHOLD && $MemPercent -lt $CRIT_THRESHOLD ]]; warning=$? | |
[[ $MemPercent -ge $CRIT_THRESHOLD ]]; critical=$? | |
if [ $warning -eq 0 ]; then | |
echo "WARNING: ${MemPercent}% ${memused}/${memtotal}MB RAM Used" | |
exit 2; | |
elif [ $critical -eq 0 ]; then | |
echo "CRITICAL: ${MemPercent}% ${memused}/${memtotal}MB RAM Used" | |
exit 1; | |
else | |
echo "OK: ${MemPercent}% ${memused}/${memtotal}MB RAM Used" | |
exit 0; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment