linux下利用curl監控網頁shell腳本


#!/bin/bash

smail() {
mail -s "$1" gjw_apparitor@gmail.com <<EOF
$1
$2
====
report time: `date +"%F %T"`
current user: `whoami`
shell script: `echo $0`
====
EOF
}

ssms() {
/usr/local/feixin/fetion --mobile=150000000 --pwd=******** --to=13810000000 --msg-gb="fx $1"
}

cd /home/maintain/gaojianwei/Script/
File=Monitor_IP.txt
sed -i /.*/d Curl_Out.txt
sed -i /.*/d Curl_Out_1.txt

sed -e '/^#/d;/^$/d' ${File} | while read Ip Port URL
do
/usr/bin/curl --connect-timeout 8 --max-time 12 -o /dev/null -s -w %{time_total}:%{size_download}:%{http_code} http://${URL} -x ${Ip}:${Port} >> Curl_Out.txt
echo ":${Ip}:${URL}" >> Curl_Out.txt
done

awk -F":" '{if(($1*1000<8000)&&($2>0)&&($3=="200"||$3=="301"||$30=="302"||$3=="401")) {} else {print $0 >> "Curl_Out_1.txt"}}' Curl_Out.txt

if [ -s Curl_Out_1.txt ];then
Warning="`awk '{printf("%s#",$0)}' Curl_Out_1.txt`"
ssms ${Warning}
smail CURL_Monitor ${Warning}
fi
 
備注:
curl是一個命令行下的http下載工具,類似wget。與wget相似,它也可以通過發送指定的http header到服務器來判斷服務的狀態。
這里介紹一個使用curl監控頁面可用性的方法。
可以使用下面的命令,來采集頁面的狀態碼。如果這條命令返回結果為200,說明服務正常。如果返回的是其他的頁面,說明異常。
curl -o /dev/null -s -w %{http_code} http://zys.8800.org/
-o 參數,是把下載的所有內容都重定向到/dev/null,-s命令,是屏蔽了curl本身的輸出,而-w參數,是根據我們自己的需要,自定義了curl的輸出格式。
使用這條命令,再配合郵件和短信,就可以實現對頁面的可用性監控。將這個程序部署在全國各地的機器上,就可以對cdn網絡進行可用性監控。

curl只返回服務器響應狀態,不返回內容,返回200是正常的,其它的不正常,簡單的命令如下:

[coomix@localhost ~]$ echo `curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "http://www.coomix.net/index.jsp"`
200
[coomix@localhost ~]$ echo `curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "http://www.coomix.net/index5.jsp"`
404

====================================================

監控機器列表文件:

server.list

server1

server2

server3

 

建立監控腳本: webstatus.sh

 

#!/bin/sh
monitor_dir=/home/admin/monitor/
if [ ! -d $monitor_dir ]; then
    mkdir $monitor_dir
fi
cd $monitor_dir
web_stat_log=web.status
if [ ! -f $web_stat_log ]; then
   touch $web_stat_log
fi
server_list_file=server.list
if [ ! -f $server_list_file ]; then
   echo "`date '+%Y-%m-%d %H:%M:%S'` ERROR:$server_list_file NOT exists!" >>$web_stat_log
exit 1
fi
#total=`wc -l $server_list_file|awk '{print $1}'`
for website in `cat $server_list_file`
do
   url="http://$website/app.htm"
   server_status_code=`curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "$url"`
   if [ "$server_status_code" = "200" ]; then 
        echo "`date '+%Y-%m-%d %H:%M:%S'` visit $website status code 200 OK" >>$web_stat_log
   else 
        echo "`date '+%Y-%m-%d %H:%M:%S'` visit $website error!!! server can't connect at 10s or stop response at 10 s, send alerm sms ..." >>$web_stat_log
        echo "!app alarm @136xxxxxxxx  server:$website can't connect at 10s or stop response at 10s ..." | nc smsserver port &
   fi
done
exit 0

 

主要是利用 curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "$url" 返回狀態碼是否200,如果10s沒有返回200狀態碼,則發警報

 

最后讓linux 定時執行腳本:

crontab -e

*/10 * * * * /home/admin/app/bin/webstatus.sh

這樣每隔10分鍾就會執行一次

這個是另外一種腳本寫法:

#!/bin/bash

while read URL
  do
    echo `date`
    result=`curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code}  $URL`
    test=`echo $result`
    if [[  "$test" = "200"  ]]
      then
        echo "$URL is ok"
    else
      echo "test err"
/usr/sbin/sendmail -t << EOF
From:SD-Detect
To:13918888888@139.com,13800000000@139.com
Subject:Detected $URL
------------------------------
${URL} is err!!
------------------------------
EOF
    fi
  done < /root/jiankong/httplist.txt


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM