shell脚本---web监控脚本系列

本文介绍了一系列Shell脚本,用于监控Web服务的状态,包括检查本地httpd服务的端口和进程,通过URL远程监控,根据HTTP状态码判断服务状态,以及创建带有颜色和缩进的CRT界面。此外,还提到了一些常见的HTTP状态码及其含义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.log
httpd_init=/etc/init.d/httpd

if [ $httpd_port -eq 1 ] && [ $httpd_process -eq 9 ]
  then
  echo "httpd is running"
else
  echo "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 ]
    then
    while true
    do
      killall httpd
      [ $? -ne 0 ] && break
       sleep 1
    done
 
$httpd_init start >>$httpd_log echo && echo "success"||echo "fail"
  fi
fi

如果监测远程主机的情况可以将变量$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/null
if [ $? -eq 0 ]
  then
  echo "http is running"
else
  echo "http is not running"
  /etc/init.d/httpd start
fi

这个脚本是比较简单 不规范的,只做举例用。
运用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 1
httpcode=`curl -I -s 192.168.1.220|head -1|cut -d " " -f2`
if [ "$httpcode" == "200" ]
  then
  action "http is running" /bin/true
else
  action "http is not running" /bin/false
  sleep 1
  /etc/init.d/httpd start
fi

运行效果:
[root@jason ~]# sh check_http01.sh 
http 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(网关超时),一般是网关服务器请求后端服务器,后端服务器并没有在特定时间内完成服务





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值