监控apache脚本:
1、每十分钟检查apache是否正常运行
分析:进程在运行如何判断
1)lockfile是否存在
2)pid是在后台存在
3)能否正常访问页面
2、如果apache运行不正常(进程死亡、页面访问也不正常等情况),
需要脚本能够通过重启apache进程来恢复正常。
分析:
1)先恢复后报警
2)如何恢复
3)如何报警
3、当apache故障时能通过邮件及短信报警,当apache故障恢复后能够通知管理员
1)重新检查apache是否正常
2)如果正常发信息告诉管理员已经自动恢复成功
3)如果apache还是不正常,发信息告知管理上线手工修复
bash
#!/bin/bash
if [ -f /var/lock/subsys/httpd ]
then
export pid=`cat /var/run/httpd/httpd.pid`
ps -p $pid | grep httpd &> /dev/null
REL1=$?
elinks -dump http://localhost/ok.html &> /dev/null
REL2=$?
if [ 0 -eq $REL1 ] && [ 0 -eq $REL2 ]
then
:
else
pkill -9 httpd
service httpd restart &> /dev/null
REL3=$?
echo "httpd is not OK" | mail -s "httpd is not ok" root@localhost
sleep 3s
if [ $REL3 -eq 0 ]
then
echo "httpd is recovered OK" | mail -s "httpd is recovered ok" root@localhost
else
echo "httpd is recovered not OK,please login your server and recover by yourself" | mail -s "httpd is recovered not ok" root@localhost
fi
fi
else
service httpd start &> /dev/null
REL4=$?
echo "httpd is not OK" | mail -s "httpd is not ok" root@localhost
sleep 3s
if [ $REL4 -eq 0 ]
then
echo "httpd is recovered OK" | mail -s "httpd is recovered ok" root@localhost
else
echo "httpd is recovered not OK,please login your server and recover by yourself" | mail -s "httpd is recovered not ok" root@localhost
fi
fi
bash
*/10 * * * * sh httpd.sh