Dec 3, 2008

Checking Load of server in Frequent intervals

One of the major problem in linux server is rising of load to high. This can be happened due to various reason. This script will check the load of machine in frequent intervals and inform the administrator through email. This will also send the current server status.


#mkdir -p /opt/scripts
#cd /opt/scripts
#touch server_load.sh
#chmod 755 server_load.sh

Copy paste the following into the server_load.sh

#!/bin/bash
EMAIL="yourname@yourdomain.com"
SUBJECT="Alert $(hostname) load average is $L05"
TEMPFILE="/tmp/$(hostname)"
echo "Load average Crossed allowed limit." >> $TEMPFILE
echo "Hostname: $(hostname)" >> $TEMPFILE
echo "Local Date & Time : $(date)" >> $TEMPFILE
echo "| Uptime status: |" >> $TEMPFILE
echo "-----------------------------------------------------" >> $TEMPFILE
/usr/bin/uptime >> $TEMPFILE
echo "-----------------------------------------------------" >> $TEMPFILE
echo "| Top 20 CPU consuming processes: |" >> $TEMPFILE
ps aux | head -1 >> $TEMPFILE
ps aux --no-headers | sort -rn | head -20 >> $TEMPFILE
echo "| Top 10 memory-consuming processes: |" >> $TEMPFILE
ps aux --no-headers| sort -rn | head >> $TEMPFILE
echo "----------------------------------------------------" >> $TEMPFILE
echo "| Memory and Swap status: |" >> $TEMPFILE
/usr/bin/free -m >> $TEMPFILE
echo "----------------------------------------------------" >> $TEMPFILE
echo "| Active network connection: |" >> $TEMPFILE
echo "----------------------------------------------------" >> $TEMPFILE
/bin/netstat -tnup | grep ESTA >> $TEMPFILE
echo "----------------------------------------------------" >> $TEMPFILE
echo "| Disk Space information: |" >> $TEMPFILE
echo "----------------------------------------------------" >> $TEMPFILE
/bin/df -h >> $TEMPFILE
echo "-----------------THE END----------------------------" >> $TEMPFILE

#Store the Current Load into a variable
L05="$(uptime|awk '{print $(NF-2)}'|cut -d. -f1)"

#Checking whether it goes beyond the limit

if test $L05 -gt 0
then
mail -s "$SUBJECT $L05" "$EMAIL" < $TEMPFILE fi #Remove the Temporary file.
rm -f $TEMPFILE

Create CronJob for this
#vi /etc/crontab
#The following script will run in every minute.
*/1 * * * * root /opt/scripts/server_load.sh

1 comment:

Anonymous said...

Bipin,great post , i stumbled upon this when i was trying to prevent dos attacks on my server. My website is http://www.mobchannel.com
http://www.crimsoncomputing.com.
Create a mobchannel id and i hope we can get to know more as professionals.