This shell script for Nagios to check free memory on your Linux machines, this can support to CentOS, Fedora, Ubuntu Families and etc. Previously we had configured Nagios server and Nagios client monitoring causes.


#Find Ram space, Total, Used and Free.

#!/bin/bash
if [ "$1" = "-w" ] && [ "$2" -gt "0" ] && [ "$3" = "-c" ] && [ "$4" -gt "0" ]; then
        MEMTOTAL_MB=`free -m | awk '{print $2}'| head -2 | tail -1`
        MEM_AVAILABLE=`free -m | awk '{print $6}'| head -1| tail -1`

    if [ "$MEM_AVAILABLE" == "MEM_AVAILABLE" ]; then
               MEMFREE_MB=`free -m | awk '{print $7}'| head -2| tail -1`
               MEMUSED_MB=`expr $MEMTOTAL_MB - $MEMFREE_MB`
    else    
              MEMUSED_MB=`free -m | awk '{print $3}'| head -3| tail -1`
              MEMFREE_MB=`free -m | awk '{print $4}'| head -3| tail -1`
    fi    
        
    memUsedPrc=$(( 100*$MEMFREE_MB / $MEMTOTAL_MB ))
        buffer=$((100 - $memUsedPrc ))

    if [ "$buffer" -ge "$4" ]; then
            echo "RAM SPACE: CRITICAL Total:$MEMTOTAL_MB MB-Free:$MEMFREE_MB MB($memUsedPrc%)-Used:$MEMUSED_MB MB($buffer%)"
               exit 2
        elif [ "$buffer" -ge "$2" ]; then
            echo "RAM SPACE: WARNING Total:$MEMTOTAL_MB MB-Free:$MEMFREE_MB MB($memUsedPrc%)-Used:$MEMUSED_MB MB($buffer%)"
            exit 1
    else
            echo "RAM SPACE: OK Total:$MEMTOTAL_MB MB-Free:$MEMFREE_MB MB($memUsedPrc%)-Used:$MEMUSED_MB MB($buffer%)"
            exit 0
    fi

else
         # If inputs are not as expected, print help.
         sName="`echo $0|awk -F '/' '{print $NF}'`"
         echo -e "\n\n\t\t### $ Version 1.1###\n"
         echo -e "# Usage:\t$sName -w <warnlevel> -c <critlevel>"
         echo -e "\t\t= warning level and critical level is percentage value without %\n"
         echo "# EXAMPLE:\t/usr/local/nagios/libexec/ramfree.sh -w 80 -c 90"
         echo -e "\nCopyright (C)2016 thelinuxfaq.com (support@thelinuxfaq.com)\n\n"
         exit
fi

Save this file into /usr/local/nagios/libexec/check_ramfree.sh 

Check your RAM space usage using below commands, (OK, WARNING, CRITICAL)

root@thelinxufaq:~# /usr/local/nagios/libexec/check_ramfree.sh -w 80 -c 90

RAM SPACE: OK Total:15039 MB-Free:14265 MB(94%)-Used:774 MB(6%)

root@thelinxufaq:~# /usr/local/nagios/libexec/check_ramfree.sh -w 5 -c 10

RAM SPACE: WARNING Total:15039 MB-Free:14265 MB(94%)-Used:774 MB(6%)

root@thelinxufaq:~# /usr/local/nagios/libexec/check_ramfree.sh -w 3 -c 5

RAM SPACE: CRITICAL Total:15039 MB-Free:14264 MB(94%)-Used:775 MB(6%)

root@thelinuxfaq:~# free -m
             total       used       free     shared    buffers     cached
Mem:         15039       4417      10622          1        291       3355
-/+ buffers/cache:        770      14269
Swap:         2047          0       2047