This script based on previous NRPE script for monitoring load average. Same purpouse - get NRPE friendly output with minimal usage of non built in commands. In this case wasn't need to count with floating point. So I could omit bc
.
Yes, I read Why is using a shell loop to process text considered bad practice. And there is a lot of true. Bud I am still thinking about these NRPE scripts as about bash scripting trainnig to get more practice.
Desired output:
MEMORY_TOTAL=2041316kB MEMORY_AVAILABLE=1049260kB | MEMORY_IN_PERCENTAGE=51;80;90
#!/usr/bin/env bash
set -o errexit -o pipefail
warning=80
critical=90
while read -a meminfo_row; do
# Remove the colon character.
row_value_lenght=$(( ${#meminfo_row[0]} -1 ))
row_value=${meminfo_row[0]:0:$row_value_lenght}
case $row_value in
MemTotal)
mem_total=${meminfo_row[1]}
mem_apercent=$(( meminfo_row[1] / 100 ))
;;
MemAvailable)
mem_available=${meminfo_row[1]}
;;
esac
done < /proc/meminfo
mem_percentage=$(( mem_available / mem_apercent ))
if [[ -z $mem_percentage ]]; then
returned_text="MEMORY UNKNOWN - check script"
returned_code=3
else
returned_text="MEMORY_TOTAL=${mem_total}kB MEMORY_AVAILABLE=${mem_available}kB | MEMORY_IN_PERCENTAGE=$mem_percentage;$warning;$critical"
if [[ $mem_percentage -gt $critical ]]; then
returned_code=2
elif [[ $mem_percentage -gt $warning ]]; then
returned_code=1
else
returned_code=0
fi
fi
echo "$returned_text"
exit $returned_code