0)监控本地httpd服务情况,通过检测端口与进程信息进行监测
[root@jason ~]# cat check_http.sh#!/bin/bash
httpd_port=`netstat -antulp|grep httpd|wc -l`httpd_process=`ps -ef |grep httpd|grep -v grep|wc -l`httpd_log=/tmp/httpd.loghttpd_init=/etc/init.d/httpd
if [ $httpd_port -eq 1 ] && [ $httpd_process -eq 9 ]thenecho "httpd is running"elseecho "httpd is not running"$httpd_init start >$httpd_log
sleep 10
httpd_port=`netstat -antulp|grep httpd|wc -l`httpd_process=`ps -ef |grep httpd|grep -v grep|wc -l`
if [ $httpd_port -ne 1 -a $httpd_process -ne 9 ]thenwhile truedokillall httpd[ $? -ne 0 ] && breaksleep 1done$httpd_init start >>$httpd_log echo && echo "success"||echo "fail"fifi
如果监测远程主机的情况可以将变量$httpd_port改写为:
httpd_port=nmap ip -p 80|grep open|wc -l
这里要对nmap命令有简单的了解
2) 通过url地址远程监控
[root@jason ~]# cat check_http01.sh#!/bin/bash
wget -T 10 -q --spider http://192.168.1.220 >&/dev/nullif [ $? -eq 0 ]thenecho "http is running"elseecho "http is not running"/etc/init.d/httpd startfi
这个脚本是比较简单 不规范的,只做举例用。
运用wget命令 -T 指定10秒 -q是quiet安静模式下 --spider 是爬虫
-q 和--spider参数不加也行
3)通过状态码判断(返回值为200)
用curl命令 脚本应改写为:
[root@jason ~]# cat check_http01.sh
#!/bin/bash
httpcode=`curl -I -s 192.168.1.220|head -1|cut -d " " -f2`
if [ $httpcode == "200" ]
then
echo "http is running"
else
echo "http is not running"
/etc/init.d/httpd start
fi
4)配置crt界面 带有缩进、颜色
[root@jason ~]# cat check_http01.sh#!/bin/bash
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions||exit 1httpcode=`curl -I -s 192.168.1.220|head -1|cut -d " " -f2`if [ "$httpcode" == "200" ]thenaction "http is running" /bin/trueelseaction "http is not running" /bin/falsesleep 1/etc/init.d/httpd startfi
运行效果:
[root@jason ~]# sh check_http01.shhttp is running [ OK ]
ps:一般脚本不会指定唯一ip地址,如果是命令行传参则将脚本中的ip地址改为$1,并切增加对参数个数的if条件判断即可。
5)通过端口,进程,url地址综合判断
生产环境常见的HTTP状态码列表
200--ok,服务器成功返回页面
301--moved permanently(永久跳转),请求页面跳转到新位置
403--forbidden(禁止访问),服务器拒绝请求
404--not found ,服务器找不到请求页面
500--internal server error 内部服务器错误
502--bad gateway 一般是网关服务器请求后端服务器时,后端服务器没有按照http协议正确返回结果
一般长出现在负载均衡时出现
503--service unavailable(服务当前不可用),可能超载或停机维护
504--gateway timeout(网关超时),一般是网关服务器请求后端服务器,后端服务器并没有在特定时间内完成服务