#!/bin/bash
#created by Szymon Machajewski 
#smachaje@grcc.edu, support@dataii.com

cd /root

# change logfile location or pager/email address to fit your needs
logfile="/root/"`hostname`"_KeepOn_restart.log"
pager="your_phone_number@messaging.sprintpcs.com"
Server_Name=java

. ~/.bash_profile > /dev/null

#check if tomcat is running
l=`ps -ef | grep $Server_Name | grep -v grep | wc -l`
        if [ $l -eq 0 ]; then
                ./restart_bb.sh
                date >> $logfile 
                date
                date | mail -s "no $Server_Name auto restart" $pager
                exit

        fi

#check and stop flooding IP
./iptables.sh

#check response time
q=`links -dump "http://localhost/" | wc -l`

if [ $q -eq 0 ]; then

        date >> $logfile 
        netstat -np | grep ":80" | cut -f2 -d ":" | cut -c16-28 | grep -v 127.0.0.1 | sort | uniq -c | sort -n >> $logfile 
        
        ./restart_bb.sh
        date >> $logfile 
        date
        date | mail -s "no response auto restart" $pager

fi

