Last active
October 31, 2022 14:29
-
-
Save bcatubig/c63af404081dfc0a510f to your computer and use it in GitHub Desktop.
Linux Memory Check Script in Bash. Works with Nagios and Icinga/Icinga 2
This file contains hidden or 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 | |
#13:16 $ ./check_memory.sh | |
#OK - Used: 197/3953 (4%);|'Memory Used'=197MB;3360;3755;4053 | |
#Swap Usage: 0/3905 (0%);|'Swap Used'=0MB;3319;3709;4005 | |
MEMTOTAL=$(free | awk '/Mem/ {printf("%d",$2/1024)}') | |
SWAPTOTAL=$(free | awk '/Swap/ {printf("%d", $2 / 1024)}') | |
# Get the memory usage in % | |
MEMUSED=$(free | awk '/Mem/ {printf("%d",($3-$6-$7)/1024)}') | |
MEMUSEDPERCENT=$(free | awk '/Mem/ {printf("%d", ($3-$6-$7)/$2 * 100)}') | |
# Get swap usage in % | |
SWAPUSED=$(free | awk '/Swap/ {printf("%d", $3 / 1024)}') | |
SWAPUSEDPERCENT=$(free | awk '/Swap/ {if($3 == 0){printf("%d", 0)} else{printf("%d", $3/$2 * 100)}}') | |
# Total Values (for perf data) | |
mem_warn=$(echo | awk -v memtotal=$MEMTOTAL '{printf("%d", memtotal * .85)}') | |
mem_crit=$(echo | awk -v memtotal=$MEMTOTAL '{printf("%d", memtotal * .95)}') | |
mem_max=$(expr $MEMTOTAL + 100) | |
swap_warn=$(echo | awk -v swaptotal=$SWAPTOTAL '{printf("%d", swaptotal * .85)}') | |
swap_crit=$(echo | awk -v swaptotal=$SWAPTOTAL '{printf("%d", swaptotal * .95)}') | |
swap_max=$(expr $SWAPTOTAL + 100) | |
output="Used: $MEMUSED/$MEMTOTAL ($MEMUSEDPERCENT%);|'Memory Used'=${MEMUSED}MB;${mem_warn};${mem_crit};${mem_max}" | |
swap_output="Swap Usage: $SWAPUSED/$SWAPTOTAL (${SWAPUSEDPERCENT}%);|'Swap Used'=${SWAPUSED}MB;${swap_warn};${swap_crit};${swap_max} " | |
# OK | |
if [ $MEMUSEDPERCENT -le 85 ] | |
then | |
echo "OK - $output" | |
echo "$swap_output" | |
exit 0 | |
elif [ $MEMUSEDPERCENT -ge 85 ] && [ $MEMUSEDPERCENT -le 95 ] | |
then | |
echo "WARNING - $output" | |
echo "$swap_output" | |
exit 1 | |
elif [ $MEMUSEDPERCENT -gt 95 ] | |
then | |
echo "CRITICAL - $output" | |
echo "$swap_output" | |
exit 2 | |
else | |
echo "UNKNOWN - $output" | |
echo "$swap_output" | |
exit 3 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment