[root@bogon 22]# cat monitor.sh
#!/bin/bash
while [ 1 ]
do
#set server ipadd
SERVERA=192.168.0.38
STATUS=SERVERA
#set tomcat path
SVRA_TOMCAT_DIR=/opt/service
SVRB_TOMCAT_DIR=/opt/service
#set server port
TOMCAT1=8081
TOMCAT2=8082
TOMCAT3=8083
TOMCAT4=8084
#set mail
[email protected]
[email protected]
##########################################################################
NULL=/dev/null
LOG=/var/log/monitor-tomcat.log
SLEEP="sleep 10"
TIME=`date +"%Y-%m-%d %H:%M:%S"`
>.T1
>.T2
>.T3
>.T4
STARTUP=bin/startup.sh
SHUTDOWN=bin/shutdown.sh
AR1=`grep Restart $LOG |wc -l`
FILE="monitor tomcat status tomcat is alive"
if [ $STATUS = SERVERA ] ; then
T1WEB_ST=`links http://$SERVERA:$TOMCAT1/monitor.jsp > .T1`
T1=`grep "$FILE" .T1 > $NULL ; echo $?`
T1STATUS=`nc -vv -z $SERVERA $TOMCAT1 > $NULL 2>&1 ; echo $?`
if [ $T1 -eq 0 ] ; then
echo $TIME tomcat1 -------- is -------- alive >> $NULL
if [ $T1STATUS -eq 0 ]; then
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME tomcat1 -------- is -------- alive >> $LOG
else
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME "tomcat1 -------- is -------- shutdown" >> $LOG
echo $TIME "tomcat1 ------------------->>> Restart" >> $LOG
PID=`ps aux |grep tomcat1 > $NULL ;echo $?`
if [ $PID -eq 0 ] ; then
$SVRA_TOMCAT_DIR/tomcat1/$SHUTDOWN 2 > $NULL
$SVRA_TOMCAT_DIR/tomcat1/$STARTUP 2 > $NULL
$SLEEP
else
$SVRA_TOMCAT_DIR/tomcat1/$STARTUP 2 > $NULL
$SLEEP
fi
fi
else
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME "tomcat1 -------- is -------- shutdown" >> $LOG
echo $TIME "tomcat1 ------------------->>> Restart" >> $LOG
PID=`ps aux |grep tomcat1 > $NULL ;echo $?`
if [ $PID -eq 0 ] ; then
$SVRA_TOMCAT_DIR/tomcat1/$SHUTDOWN 2 > $NULL
$SVRA_TOMCAT_DIR/tomcat1/$STARTUP 2 > $NULL
$SLEEP
else
$SVRA_TOMCAT_DIR/tomcat1/$STARTUP 2 > $NULL
$SLEEP
fi
fi
T2WEB_ST=`links http://$SERVERA:$TOMCAT2/monitor.jsp > .T2`
T2=`grep "$FILE" .T2 > $NULL ; echo $?`
T2STATUS=`nc -vv -z $SERVERA $TOMCAT2 > $NULL 2>&1 ; echo $?`
if [ $T2 -eq 0 ] ; then
echo $TIME tomcat2 is alive >> $NULL
if [ $T2STATUS -eq 0 ]; then
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME tomcat2 -------- is -------- alive >> $LOG
else
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME "tomcat2 -------- is -------- shutdown" >> $LOG
echo $TIME "tomcat2 ------------------->>> Restart" >> $LOG
PID=`ps aux |grep tomcat2 > $NULL ;echo $?`
if [ $PID -eq 0 ] ; then
$SVRA_TOMCAT_DIR/tomcat2/$SHUTDOWN 2 > $NULL
$SVRA_TOMCAT_DIR/tomcat2/$STARTUP 2 > $NULL
$SLEEP
else
$SVRA_TOMCAT_DIR/tomcat2/$STARTUP 2 > $NULL
$SLEEP
fi
fi
else
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME "tomcat2 -------- is -------- shutdown" >> $LOG
echo $TIME "tomcat2 ------------------->>> Restart" >> $LOG
PID=`ps aux |grep tomcat2 > $NULL ;echo $?`
if [ $PID -eq 0 ] ; then
$SVRA_TOMCAT_DIR/tomcat2/$SHUTDOWN 2 > $NULL
$SVRA_TOMCAT_DIR/tomcat2/$STARTUP 2 > $NULL
$SLEEP
else
$SVRA_TOMCAT_DIR/tomcat2/$STARTUP 2 > $NULL
$SLEEP
fi
fi
T3WEB_ST=`links http://$SERVERA:$TOMCAT3/monitor.jsp > .T3`
T3=`grep "$FILE" .T3 > $NULL ; echo $?`
T3STATUS=`nc -vv -z $SERVERA $TOMCAT3 > $NULL 2>&1 ; echo $?`
if [ $T3 -eq 0 ] ; then
echo $TIME tomcat3 is alive >> $NULL
if [ $T3STATUS -eq 0 ]; then
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME tomcat3 -------- is -------- alive >> $LOG
else
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME "tomcat3 -------- is -------- shutdown" >> $LOG
echo $TIME "tomcat3 ------------------->>> Restart" >> $LOG
PID=`ps aux |grep tomcat3 > $NULL ;echo $?`
if [ $PID -eq 0 ] ; then
$SVRA_TOMCAT_DIR/tomcat3/$SHUTDOWN 2 > $NULL
$SVRA_TOMCAT_DIR/tomcat3/$STARTUP 2 > $NULL
$SLEEP
else
$SVRA_TOMCAT_DIR/tomcat3/$STARTUP 2 > $NULL
$SLEEP
fi
fi
else
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME "tomcat3 -------- is -------- shutdown" >> $LOG
echo $TIME "tomcat3 ------------------->>> Restart" >> $LOG
PID=`ps aux |grep tomcat3 > $NULL ;echo $?`
if [ $PID -eq 0 ] ; then
$SVRA_TOMCAT_DIR/tomcat3/$SHUTDOWN 2 > $NULL
$SVRA_TOMCAT_DIR/tomcat3/$STARTUP 2 > $NULL
$SLEEP
else
$SVRA_TOMCAT_DIR/tomcat3/$STARTUP 2 > $NULL
$SLEEP
fi
fi
T4WEB_ST=`links http://$SERVERA:$TOMCAT1/monitor.jsp > .T4`
T4=`grep "$FILE" .T4 > $NULL ; echo $?`
T4STATUS=`nc -vv -z $SERVERA $TOMCAT4 > $NULL 2>&1 ; echo $?`
if [ $T4 -eq 0 ] ; then
echo $TIME tomcat4 is alive >> $NULL
if [ $T4STATUS -eq 0 ]; then
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME tomcat4 -------- is -------- alive >> $LOG
else
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME "tomcat4 -------- is -------- shutdown" >> $LOG
echo $TIME "tomcat4 ------------------->>> Restart" >> $LOG
PID=`ps aux |grep tomcat4 > $NULL ;echo $?`
if [ $PID -eq 0 ] ; then
$SVRA_TOMCAT_DIR/tomcat4/$SHUTDOWN 2 > $NULL
$SVRA_TOMCAT_DIR/tomcat4/$STARTUP 2 > $NULL
$SLEEP
else
$SVRA_TOMCAT_DIR/tomcat4/$STARTUP 2 > $NULL
$SLEEP
fi
fi
else
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME "tomcat4 -------- is -------- shutdown" >> $LOG
echo $TIME "tomcat4 ------------------->>> Restart" >> $LOG
PID=`ps aux |grep tomcat4 > $NULL ;echo $?`
if [ $PID -eq 0 ] ; then
$SVRA_TOMCAT_DIR/tomcat4/$SHUTDOWN 2 > $NULL
$SVRA_TOMCAT_DIR/tomcat4/$STARTUP 2 > $NULL
$SLEEP
else
$SVRA_TOMCAT_DIR/tomcat4/$STARTUP 2 > $NULL
$SLEEP
fi
fi
fi
sleep 20
TOMCAT='(tomcat1|tomcat2|tomcat3|tomcat4)'
PIDSTATE=` ps aux |grep -v grep | egrep $TOMCAT > /dev/null ;echo $?`
PORTSTATE1=`nc -vv -z $SERVERA $TOMCAT1 > $NULL 2>&1 ; echo $?`
PORTSTATE2=`nc -vv -z $SERVERA $TOMCAT2 > $NULL 2>&1 ; echo $?`
PORTSTATE3=`nc -vv -z $SERVERA $TOMCAT3 > $NULL 2>&1 ; echo $?`
PORTSTATE4=`nc -vv -z $SERVERA $TOMCAT4 > $NULL 2>&1 ; echo $?`
if [ $PIDSTATE -eq 0 ] && [ $PORTSTATE1 -eq 0 ] && [ $PORTSTATE2 -eq 0 ] && [ $PORTSTATE3 -eq 0 ] && [ $PORTSTATE4 -eq 0 ] ; then
$SLEEP
else
$SLEEPMIN
fi
PIDSTATE=` ps aux |grep -v grep | egrep $TOMCAT > /dev/null ;echo $?`
PORTSTATE1=`nc -vv -z $SERVERA $TOMCAT1 > $NULL 2>&1 ; echo $?`
PORTSTATE2=`nc -vv -z $SERVERA $TOMCAT2 > $NULL 2>&1 ; echo $?`
PORTSTATE3=`nc -vv -z $SERVERA $TOMCAT3 > $NULL 2>&1 ; echo $?`
PORTSTATE4=`nc -vv -z $SERVERA $TOMCAT4 > $NULL 2>&1 ; echo $?`
if [ $PIDSTATE -eq 0 ] && [ $PORTSTATE1 -eq 0 ] && [ $PORTSTATE2 -eq 0 ] && [ $PORTSTATE3 -eq 0 ] && [ $PORTSTATE4 -eq 0 ] ; then
$SLEEP
else
$SLEEPMIN
fi
PIDSTATE=` ps aux |grep -v grep | egrep $TOMCAT > /dev/null ;echo $?`
PORTSTATE1=`nc -vv -z $SERVERA $TOMCAT1 > $NULL 2>&1 ; echo $?`
PORTSTATE2=`nc -vv -z $SERVERA $TOMCAT2 > $NULL 2>&1 ; echo $?`
PORTSTATE3=`nc -vv -z $SERVERA $TOMCAT3 > $NULL 2>&1 ; echo $?`
PORTSTATE4=`nc -vv -z $SERVERA $TOMCAT4 > $NULL 2>&1 ; echo $?`
if [ $PIDSTATE -eq 0 ] && [ $PORTSTATE1 -eq 0 ] && [ $PORTSTATE2 -eq 0 ] && [ $PORTSTATE3 -eq 0 ] && [ $PORTSTATE4 -eq 0 ] ; then
$SLEEP
else
$SLEEPMIN
fi
PIDSTATE=` ps aux |grep -v grep | egrep $TOMCAT > /dev/null ;echo $?`
PORTSTATE1=`nc -vv -z $SERVERA $TOMCAT1 > $NULL 2>&1 ; echo $?`
PORTSTATE2=`nc -vv -z $SERVERA $TOMCAT2 > $NULL 2>&1 ; echo $?`
PORTSTATE3=`nc -vv -z $SERVERA $TOMCAT3 > $NULL 2>&1 ; echo $?`
PORTSTATE4=`nc -vv -z $SERVERA $TOMCAT4 > $NULL 2>&1 ; echo $?`
if [ $PIDSTATE -eq 0 ] && [ $PORTSTATE1 -eq 0 ] && [ $PORTSTATE2 -eq 0 ] && [ $PORTSTATE3 -eq 0 ] && [ $PORTSTATE4 -eq 0 ] ; then
$SLEEP
else
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME "tomcat -------- is -------- shutdown" >> $LOG
$SLEEPMIN
fi
AR2=`grep Restart $LOG |wc -l`
LINEB=`echo "$AR2-$AR1"|bc `
MAILSUBJECT=` grep Restart $LOG | tail -n $LINEB `
if [ $LINEB -gt 0 ] ; then
TIME=`date +"%Y-%m-%d %H:%M:%S"`
echo $TIME send mail to $MAILTO >> $LOG
sendmail -t <<EOF
from: $MAILFROM
to: $MAILTO
subject: warning
$MAILSUBJECT
EOF
fi
done