Linux shell script for memory threshold email notification

Hello Folks,
I have created the script which use to send email notification when server memory breach the threshold limits. Script works fine, but the issue is sometimes i am receiving mail alerts for lower threshold memory also.

Please let me know why and any updates required in script?

#!/bin/bash
# Shell script to monitor or watch the high Mem-load
# It will send an email to $ADMIN, if the (memroy load is in %) percentage
# of Mem-load is >= 80%
HOSTNAME=`hostname`
LOAD=80.00
CAT=/bin/cat
MAILFILE=/tmp/mailviews
MAILER=/bin/mail
mailto="skrishna1@xxx.com"
MEM_LOAD=`free -t | awk 'FNR == 2 {printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'`
if [[ $MEM_LOAD > $LOAD ]];
then
 PROC=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1`
 echo "Please check your processess on ${HOSTNAME} the value of cpu load is $CPU_LOAD % & 
 $PROC" > $MAILFILE
 echo "$(ps axo %mem,pid,euser,cmd | sort -nr | head -n 10)" > $MAILFILE
 $CAT $MAILFILE | $MAILER -s "Memory Utilization is High > 80%, $MEM_LOAD % on ${HOSTNAME}" $mailto
fi

Bash does not support floating point

LOAD=80.00

Should be:

LOAD=80

Second this line is incorrect:

MEM_LOAD=`free -t | awk 'FNR == 2 {printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'`

Should be:

MEM_LOAD=$(free -t |  awk 'FNR == 2 {printf("%d"), $3/$2*100}')

Update as above and test it again.

Thanks so much, this has fixed the script. Now receiving only high cpu alerts only.

A post was split to a new topic: Linux shell script to monitor / watch the high cpu-load and send email

Still i have question, the memory shows 98%. But still buffers/cache has enough memory which is reclaimable memory.

[root@grst1smsap01 ~]# free -m
total used free shared buffers cached
Mem: 3832 3770 61 1869 212 2239
-/+ buffers/cache: 1319 2513
Swap: 8191 0 8191

Seems still -/+ buffers/cache: 1319 2513 available.

Need a script to calculate including buffer/cache memory also to avoid false alerts.
Please need your help to fix our requirement.


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki