Server load monitoring script

Its a small but very helpful bash script to monitor server load. Once the server load reaches the threshold value ( here 5 ) , this script will send a mail to the specified address with the following details ” results of top , free , process which are taking more cpu resources and w ”

1) Create the script

>>> vi /usr/local/scripts/load_monitoring.sh

###################

#!/bin/bash
load=`cat /proc/loadavg  | awk {'print $1}' | cut -d. -f1`
if [ "$load" -ge "5" ]; then
echo -e  " \n#######Process details from Hostname  ######\n"  >> /tmp/topresult
ps  –eo  pid,user,%cpu,args  --sort  %cpu  | tail -20  >> /tmp/topresult
echo -e   " \n#######Server Memory Details ######"  >> /tmp/topresult
free -m  >> /tmp/topresult
echo -e  " \n#######Server Load details######"  >> /tmp/topresult
w >> /tmp/topresult
mail -s "CPU usage high on Hostname"  [email protected] < /tmp/topresult
> /tmp/topresult
fi

###################

2) Add the script in your crontab

>>> crontab -e

#### Load Monitoring ######
*/10 * * * * /bin/bash /usr/local/scripts/load_monitoring.sh > /dev/null 2>&1

Hope this will help you to monitor your servers in critical situations