zzl001 发表于 2018-8-19 14:58:18

Shell脚本——监控web服务

  1.监控端口
  #!/bin/bash
  ##############################################################
  # File Name: web.sh
  # Version: V7.4
  # Author: feng yu
  # Organization: http://blog.51cto.com/13520761
  # Created Time : 2018-03-27 15:05:09
  # Description:
  ##############################################################
  if [ $(netstat -lntup | grep -w80| awk -F "[ :]+" 'NR==1{print $5}') = 80 ];then
  echo "Nginx is Running"
  else
  echo "Nginx is Stopped"
  mail -s &quot;Nginx is stop&quot; q@163.com < nginx is stop
  fi
  2.监控进程(注意:监控进程,脚本名不要写服务名)
  #!/bin/bash
  ##############################################################
  # File Name: web1.sh
  # Version: V7.4
  # Author: feng yu
  # Organization: http://blog.51cto.com/13520761
  # Created Time : 2018-03-27 15:30:56
  # Description:
  ##############################################################
  if [ $(ps -ef | grep nginx |wc -l) -gt 0 ];then
  echo &quot;Nginx is Running&quot;
  else
  echo &quot;Nginx is stopped&quot;
  mail -s &quot;nginx is stop&quot; q@163.com < nginx is stop
  fi
  3.返回值
  #!/bin/bash
  ##############################################################
  # File Name: web2.sh
  # Version: V7.4
  # Author: feng yu
  # Organization: http://blog.51cto.com/13520761
  # Created Time : 2018-03-27 16:01:40
  # Description:
  ##############################################################
  num=$(curl -I www.baidu.com -s -w %{http_code} -o /dev/null)
  if [ $num = 200 ];then
  echo &quot;网页正常&quot;
  else
  echo &quot;$num&quot;
  mail -s &quot;网页异常&quot; q@163.com < 网页异常,$num
  fi

页: [1]
查看完整版本: Shell脚本——监控web服务