High Memory threshold notification script

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

I am not a bash guru, but maybe try:

if [ $MEM_LOAD -gt $LOAD ]

and at the same time, get rid of the semicolon at the end of this:

if [[ $MEM_LOAD > $LOAD ]];

There are other here much more qualified than me in bash.

What Linux version are you using?
Check the difference between free memory and available memory.

With older Linux versions check the -/+ buffers/cache: line.

The buffers/cache/slab memory is memory that is used to speed up things, but that is available when an application needs it.
So consider leaving that out when reporting memory usage..

--
When used with [[ .. ]] , the < and > operators sort lexicographically using the current locale. So it is a string comparison which may lead to surprising results.

For numerical comparison use either the test utility :

if [ $MEM_LOAD -gt $LOAD ]

like Neo suggested,
or use bash' s arithmetic comparison

if (( MEM_LOAD > LOAD ))

Note: like the test utility, arithmetic comparison in bash cannot handle floating point. So you need to round down / truncate the numbers used in the comparison. So for example use LOAD=80 rather than LOAD=80.00 . If you need floating point you need to use an external utility like bc .

Servers are running with RHEL7.6 version.

Then you can disregard the stuff about older Linux versions, so check the difference between "free memory" and "available memory", plus the remarks by Neo and I about numerical comparisons..

There are several further mistakes in your script:
Text message in MEM_LOAD (that you cannot compare with a numeric value),
two useless code lines about PROC and CPU_LOAD,
useless use of echo,
useless use of cat,
unnecessary and unsafe use of a /tmp/ file.

2 Likes

Tried it, landing with error.

mem.sh: line 12: [[: Current Memory Utilization is : 39.67%: syntax error in expression (error token is "Memory Utilization is : 39.67%")

see MadeInGermany's suggestions in Post# 6