File: //lib/nagios/plugins/check_mem.sh1
#!/bin/bash
# Nagios plugin to check memory consumption
# Excludes Swap and Caches so only the real memory consumption is considered
. /usr/lib/nagios/plugins/utils.sh
# Original de Georg M. Sorst (http://blog.vergiss-blackjack.de/2010/04/nagios-plugin-to-check-memory-consumption/(
# Modificado por Javier de Posada para reportar el estado en el mensaje de salida
# http://javierin.com/2010/09/29/plugin-para-nagios-que-ignora-la-cache-de-memoria-check_mem/
WARN=80
CRIT=90
SWARN=80
SCRIT=90
while getopts "c:w:s:S:h" ARG; do
case $ARG in
w) WARN=$OPTARG;;
c) CRIT=$OPTARG;;
s) SWARN=$OPTARG;;
S) SCRIT=$OPTARG;;
h) echo "Usage: $0 -w <ram warning>% -c <ram critical>% -s <swap warning>% -S <swap critical>%"; exit;;
esac
done
MEM_TOTAL=`free | fgrep "Mem:" | awk '{print $2}'`;
MEM_USED=`free | fgrep "/+ buffers/cache" | awk '{print $3}'`;
SWP_TOTAL=`free | fgrep "Swap" | awk '{print $2}'`;
SWP_USED=`free | fgrep "Swap" | awk '{print $3}'`;
RAMPC=$(($MEM_USED*100/$MEM_TOTAL));
SWPPC=$(($SWP_USED*100/$SWP_TOTAL));
if [ $RAMPC -gt $CRIT -o $SWPPC -gt $SCRIT ]; then
echo "CRITICAL: RAM: ${RAMPC}% ($((($MEM_USED)/1024)) of $((MEM_TOTAL/1024)) MB) used";
echo " SWAP: ${SWPPC}% ($((($SWP_USED)/1024)) of $((SWP_TOTAL/1024)) MB) used";
exit $STATE_CRITICAL;
elif [ $RAMPC -gt $WARN -o $SWPPC -gt $SWARN ]; then
echo "WARNING: RAM: ${RAMPC}% ($((($MEM_USED)/1024)) of $((MEM_TOTAL/1024)) MB) used";
echo " SWAP: ${SWPPC}% ($((($SWP_USED)/1024)) of $((SWP_TOTAL/1024)) MB) used";
exit $STATE_WARN;
else
echo "OK: RAM: ${RAMPC}% ($((($MEM_USED)/1024)) of $((MEM_TOTAL/1024)) MB) used";
echo " SWAP: ${SWPPC}% ($((($SWP_USED)/1024)) of $((SWP_TOTAL/1024)) MB) used";
exit $STATE_OK;
fi