自动监控tomcat+mysql+heartbeat的脚本

该脚本的作用是检测到tomat+mysql+heartbeat服务出现问题的是自动处理并且报警
#!/bin/bash
path=/usr/local/nagios/libexec/
logfile=/var/log/moniter.log
date=`(date +%D--%T)`
hostname=localhost
sleeptime=30
tomcatmessege="the tomcat server down but it up now"
mysqlmessege="the mysql server  down  but it up now"
tomcat_restart_succeed=/usr/local/iSpaceServer/tomcat/context/tomcat_restart_succeed
mysql_restart_succeed=/usr/local/iSpaceServer/tomcat/context/mysql_restart_succeed
heartbeat_restart_failed=/usr/local/iSpaceServer/tomcat/context/heartbeat_restart_failed
tomcat_restart_failed=/usr/local/iSpaceServer/tomcat/context/tomcat_restart_failed
mysql_restart_failed=/usr/local/iSpaceServer/tomcat/context/mysql_restart_failed
email="[email protected]"
q=refused
level=1000
sqlcode=500
echo "the tomcat server down but it up now">/usr/local/iSpaceServer/tomcat/context/tomcat_restart_succeed
echo "the mysql server down but it up now">/usr/local/iSpaceServer/tomcat/context/mysql_restart_succeed
echo "the tomcat server restart failed!">/usr/local/iSpaceServer/tomcat/context/tomcat_restart_failed
echo "the mysql  server restart failed!">/usr/local/iSpaceServer/tomcat/context/mysql_restart_failed
echo "the heartbeat server restart failed!">/usr/local/iSpaceServer/tomcat/context/heartbeat_restart_failed
result=`(ps -eo pid,tty,user,comm,stime,etime | more |grep java|sed 's/://g'| awk '{print $6}' |sed -e 's/://g' -e 's/-//g')`
valuse=`($path/check_http  -w 5 -c 10  -H localhost |awk '{print $4}')`
f=`($path/check_http  -w 5 -c 10  -H localhost |awk '/refused/'|awk  '{print $2}')`
if [ $valuse != 200 ]
then
       if  [ "$f" = "$q" ]
       then
               service heartbeat stop
               killall -9 java
               /etc/init.d/iSpaceServer start
               sleep $sleeptime
               a=`($path/check_http  -w 5 -c 10  -H localhost |awk '{print $4}')`
               if [ $a == 200  ]
               then
                     echo "==============&gt;$date<=====================">&gt;$logfile
                     echo $tomcatmessege&gt;&gt;$logfile
                     mail -s "tomcat_restart_succeed!" $email <$tomcat_restart_succeed
                     sleep $sleeptime
                     service heartbeat start
                     sleep $sleeptime
                     netstat -an |grep udp |grep 694
                     if [ $? == 0 ]
                     then
                         echo "complete!">&gt;$logfile
                     else
                         mail -s "heartbeat_restart_failed!" $email <$heartbeat_restart_failed
                     fi
               else
                     mail -s "tomcat_restart_failed!" $email &lt;$tomcat_restart_failed
               fi
        elif  (( "10#$result" > "$level" ))
        then
               service heartbeat stop
               killall -9 java
               /etc/init.d/iSpaceServer start
               sleep $sleeptime
               b=`($path/check_http  -w 5 -c 10  -H localhost |awk '{print $4}')`
               if [ $b == 200  ]
               then
                     echo "==============&gt;$date<=====================">&gt;$logfile
                     echo $tomcatmessege&gt;&gt;$logfile
                     mail -s "tomcat_restart_succeed!" $email <$tomcat_restart_succeed
                     sleep $sleeptime
                     service heartbeat start
                     sleep $sleeptime
                     netstat -an |grep udp |grep 694
                     if [ $? == 0 ]
                     then
                         echo "complete!">&gt;$logfile
                     else
                         mail -s "heartbeat_restart_failed!" $email <$heartbeat_restart_failed
                     fi
               else
                     mail -s "tomcat_restart_failed!" $email &lt;$tomcat_restart_failed
               fi
        elif   [[ $sqlcode == $valuse ]]
        then
               service heartbeat stop
               killall -9 mysqld
               /etc/init.d/mysqld start
               sleep $sleeptime
               c=`($path/check_http  -w 5 -c 10  -H localhost |awk '{print $4}')`
               if [ $c == 200 ]
               then
                     echo "==============>$date<=====================">&gt;$logfile
                     echo $mysqlmessege&gt;&gt;$logfile
                     mail -s "mysql_restart_succeed!" $email <$mysql_restart_succeed
                     sleep $sleeptime
                     service heartbeat start
                     sleep $sleeptime
                     netstat -an |grep udp |grep 694
                     if [ $? == 0 ]
                     then
                         echo "complete!">&gt;$logfile
                     else
                         mail -s "heartbeat_restart_failed!" $email &lt;$heartbeat_restart_failed
                     fi
               else
                     mail -s "mysql_restart_failed!" $email &lt;$mysql_restart_failed
               fi
        fi
fi

你可能感兴趣的:(tomcat,mysql,脚本,自动,heartbeat)