监控 httpd 服务一:
#!/bin/bash
#描述: 秒级别监控 http 服务 while [ 1 -lt 2 ]
do
sleep 10
ai=`netstat -ntl | grep "\<80\>" | wc -l` if [ $ai -eq 0 ]
then
echo "WARNING!!! `date` httpd is stoped" >> /root/http.log
systemctl restart httped >/dev/null 2>> /root/http.log
fi
done
监控 httpd 服务二:
#!/bin/bsah
#监控web服务的状态 timeout=5 #定义wget的访问超时时间,超时就退出
fails=0
success=0
while true
do
wget --timeout=$timeout --tries=1 http://www.qq.com -q -O /dev/null #--tries自动重试 if [ $? -ne 0 } #如果访问不成功,失败次数+1,若成功,成功次数+1
then
let fails=fails+1
else
let success+=1
fi if [ $success -ge 1 ] #如果成功次数大于1,则输出成功信息
then
echo success!
exit 0
fi if [ $falis -ge 2 ] #如果失败次数大于2,则报警
then
critical='sys is down'
echo $critical|tee|mail -s "$critical" 15570361605@163.com
exit 2
fi
done
监控nginx服务(本地 / 远程)
#!/bin/bash
#监控nginx服务 #[`rpm -q nmap | wc -l` -lt 1] && yum install -y nmap &>/dev/null
#num=`nmap 127.0.0.1 -p 80 2>/dev/null | grep open | wc -l` #远程监测端口 num=`netstat -ntlp | grep nginx | wc -l`
if [ $num -gt o ]
then
echo "nginx is running..."
else
echo "nginx is stoped..."
/etc/init.d/nginx start >/dev/null 2>> /root/nginx.log
fi