Script to Monitor the Memory Usage on Server




We can also schedule the script via cron to get regular notifications. This script is tested in OEL/RHEL/Centos.

Script:


TOT=`cat /proc/meminfo | grep MemTotal: | awk '{print $2}'`
USED=`cat /proc/meminfo | grep Active: | awk '{print $2}'`
HOSTNAME=`hostname -s`
FREE=$[$TOT - $USED ]
LOG=/tmp/mem_monitor.log
echo > $LOG
SEND=0
if [ "$USED" -gt "0" ]; then
   USEDPERC=$[$USED * 100 / $TOT]
   echo "Used Percentage      : $USEDPERC %"
   TOTMB=$[$TOT / 1024 ]
   USEDMB=$[$USED / 1024 ]
   FREEMB=$[$TOTMB - $USEDMB ]
   if [ "$USEDPERC" -gt "80" ]; then
     SEND=1
     STATUS="Warning"
     echo "------------------------------------------------------------------" >> $LOG
     echo `hostname`   >> $LOG
     echo "------------------------------------------------------------------" >> $LOG
     echo "Total Memory (MB)    : $TOTMB" >> $LOG
     echo "Used Memory (MB)     : $USEDMB" >> $LOG
     echo "Free Memory (MB)     : $FREEMB" >> $LOG
     echo "Used Percentage      : $USEDPERC %"  >> $LOG
     echo "------------------------------------------------------------------" >> $LOG
     if [ "$USEDPERC" -gt "95" ]; then
        STATUS="Critical"
     fi
   fi
fi
if [ "$FREEMB" -eq "0" ]; then
   SEND=1
   STATUS="Fatal"
   echo "------------------------------------------------------------------" >> $LOG
   echo "    No free memory available in " `hostname`    >>$LOG
   echo "------------------------------------------------------------------" >> $LOG
fi
ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 10 >> $LOG

if [ "$SEND" -eq "1" ]; then
  mail -s "$HOSTNAME Memory Alert : $STATUS" support@funoracleapps.com < $LOG
fi





If you like please follow and comment