q66262 发表于 2018-8-27 06:21:22

shell检测网站状态码和访问时间

# cat test_site.sh  
#!/bin/bash
  

  
SITES=("http://10.0.0.2" "http://www.163.com") # 要监控的网站
  
NOTICE_EMAIL='me@example.com'                                 # 管理员电邮
  
DATE=$(date +%Y-%m-%d' '%H:%M:%S)
  

  
function SENDMAIL()
  
{
  
         echo $1
  
}
  

  

  
function CHECK_SITE_CODE()
  
{
  
# 循环判断每个site
  
      for site in ${SITES
[*]}; do
  

  
            printf "start to access ${site}\n"
  
            site_code=$(curl -o /dev/null -s -w %{http_code} "${site}")
  

  
            printf "$(date '+%Y-%m-%d %H:%M:%S')\n"
  
            printf "site http code return:${site_code}\n\n"
  
            if [ ${site_code} != 200 ];
  
                then
  
                        echo "Subject: ${site} can't access ${DATE}" | SENDMAIL ${NOTICE_EMAIL}
  
            fi
  
done
  
}
  

  
function MAX_ACCESS_TIME()
  
{
  
       for site in ${SITES
[*]}; do
  
      site_access_time=$(curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}" "${site}")
  
      echo"$site"
  
      time_total=${site_access_time##*:}
  
      #echo "$time_total"
  
      printf "site access time\n${time_total}\n"
  
      var=${time_total%.*}
  
                if [ ${var} -ge 2 ];
  
                        then
  
                              echo "Subject: ${site} can't access ${DATE} " | SENDMAIL ${NOTICE_EMAIL}
  
                fi
  
       done
  
}
  

  
function SEND_MAIL()
  
{
  
      /usr/local/python
  
}
  

  

  
echo "check the httpd code"
  
CHECK_SITE_CODE
  
echo "-----------------------------------------"\n
  
echo "check max access time"
  
MAX_ACCESS_TIME


页: [1]
查看完整版本: shell检测网站状态码和访问时间