Monitoring server load on Linux


I wrote a simple bash script to monitor the server load, once the server load reaches the threshold it will send out an e-mail.

Once you have received an e-mail from this monitoring script, you have to reset the $fileCHECK file to ‘0’:

# echo ‘0’ >  $fileCHECK

# In this example script it should be:
# echo ‘0’ > /root/monitor/values/load

The script is very basic and simple, I will keep this script updated if necessary.

Script:

#!/bin/bash

fileCHECK=’/root/monitor/values/load’
fileVALUE=`/bin/cat $fileCHECK`

# On what load average an e-mail should be sent
# Example: 5
topTHRESHOLD=’1′

# Grep load average from /proc/loadavg
loadAVERAGE=`cat /proc/loadavg | /usr/bin/cut -d ” ” -f 1 | /usr/bin/cut -d “.” -f 1`

if [ “$fileVALUE” -eq “0” ]
then
        # No e-mail or SMS has been sent yet
        # Now check if average is above threshold
        if (( “$loadAVERAGE” >= “$topTHRESHOLD” ))
        then
                # Load average is higher than $topTHRESHOLD
                # We should send an alert
                /bin/echo echo “Load averag abose threshold: $loadAVERAGE” | /usr/bin/mail -s “Load average above threshold” “info@how2forge.info”
                /bin/echo ‘1’ > $fileCHECK
        else
                # Load average is lower
                # Load is OK, do nothing
                exit
        fi
else
        # E-mail has already been sent, do nothing
        exit
fi