#!/bin/bash
FILE=loadwatch.`date +%F.%X`
DIR=/root/loadwatch
#Load Threshold for doing a dump.
THRESH=8
HOSTNAME=`hostname`
LOAD=`cat /proc/loadavg | awk '{print $1}' | awk -F '.' '{print $1}'`
#function for blank lines
function blank {
        for x in `seq 1 "$1"`
        do
                echo >> $DIR/$FILE
        done
        }

echo `date +%F.%X` - Load: $LOAD >> $DIR/checklog

if [ $LOAD -gt $THRESH ]
then
        echo Loadwatch tripped, dumping info to $DIR/$FILE >> $DIR/checklog
        #echo "Please forward this to a member of the SUPPORT Team, the Loadwatch script on this server has sent this email." >> $DIR/$FILE
        blank 2
        cat /proc/loadavg | awk '{print $1" "$2" "$3}'
        blank 2
        ps auxf >> $DIR/$FILE
        blank 2
        mysqladmin processlisti stat >> $DIR/$FILE
        blank 2
        ps aux | grep httpd | grep -v grep -c >> $DIR/$FILE
        blank 2
        /usr/bin/lynx -dump -width 500 http://127.0.0.1/whm-server-status >> $DIR/$FILE
        blank 2
        netstat -tn 2>/dev/null | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head >> $DIR/$FILE
        blank 2
        sar >> $DIR/$FILE
        blank 2
        sar -r >> $DIR/$FILE
        cat $DIR/$FILE | mail -s "High Load on $HOSTNAME, LOADWATCH" support@liquidweb.com
        killall -9 httpd
        killall -9 httpd
        /etc/init.d/httpd startssl
fi

