應領導要求,對公司幾個主要站點的域名訪問情況進行監控。下面分享一個監控指令碼,並利用sendemail進行郵件傳送。
監控指令碼如下:
下面是寫了一個多執行緒的網站狀態檢測指令碼,直接從檔案中讀出站點地址,然後用curl去檢測返回碼,發現速度非常好,基本幾秒鐘內就能出結果。
[root@bastion-IDC ~]# cat url-monit.sh
#!/bin/bash
#取出網站資料
data=`cat /root/url.list`
if [ -z "$data" ];then
echo "Faild to connect database!"
exit 1
fi
test -f result.log && rm -f result.log
function delay {
sleep 2
}
tmp_fifofile=/tmp/$$.fifo
mkfifo $tmp_fifofile
exec 6<>$tmp_fifofile
rm $tmp_fifofile
#定義併發執行緒數,需根據vps配置進行調整。
thread=100
for ((i=0 ;i<$thread;i++ ))
do
echo
done>&6
#開始多執行緒迴圈檢測
for url in $data
do
read -u6
{
#curl抓取網站http狀態碼
code=`curl -o /dev/null --retry 3 --retry-max-time 8 -s -w %{http_code} $url`
echo "HTTP Status of $url is $code ">>result.log
#判斷子執行緒是否執行成功,並輸出結果
delay && {
echo "HTTP Status of $url is $code"
} || {
echo "Check thread error!"
}
echo >& 6
}&
done
#等待所有執行緒執行完畢
wait
exec 6>&-
exit 0
[root@bastion-IDC ~]# cat url.list
www.fangfull.com
www.huanqiu.com
erp.fangfull.com
fanghuadmin.huanqiu.com
www.hqsbtime.com
qmjjr.huanqiu.com
admin.huanqiu.com
m.huanqiu.com
fq.huanqiu.com
mfq.huanqiu.com
zc.huanqiu.com
mzc.huanqiu.com
uc.huanqiu.com
fanghu.huanqiu.com
img.huanqiu.com
app.huanqiu.com
www.fangfull.cn
www.huanqiu.wang.com
執行指令碼:
[root@bastion-IDC ~]# sh url-monit.sh
HTTP Status of app.huanqiu.com is 301
HTTP Status of fanghu.huanqiu.com is 301
HTTP Status of www.huanqiu.com is 301
HTTP Status of fanghuadmin.huanqiu.com is 301
HTTP Status of admin.huanqiu.com is 301
HTTP Status of mfq.huanqiu.com is 301
HTTP Status of zc.huanqiu.com is 301
HTTP Status of erp.fangfull.com is 302
HTTP Status of www.fangfull.com is 200
HTTP Status of fq.huanqiu.com is 301
HTTP Status of img.huanqiu.com is 301
HTTP Status of www.hqsbtime.com is 200
HTTP Status of mzc.huanqiu.com is 301
HTTP Status of www.fangfull.cn is 000
HTTP Status of uc.huanqiu.com is 301
HTTP Status of qmjjr.huanqiu.com is 301
HTTP Status of m.huanqiu.com is 301
HTTP Status of www.huanqiu.wang.com is 000
測試利用上面的多執行緒的網站狀態檢測指令碼的執行時間,如下,12s多執行完畢!
[root@bastion-IDC ~]# time sh url-monit.sh
HTTP Status of app.huanqiu.com is 301
HTTP Status of fanghu.huanqiu.com is 301
HTTP Status of www.huanqiu.com is 301
HTTP Status of fanghuadmin.huanqiu.com is 301
HTTP Status of admin.huanqiu.com is 301
HTTP Status of mfq.huanqiu.com is 301
HTTP Status of zc.huanqiu.com is 301
HTTP Status of erp.fangfull.com is 302
HTTP Status of www.fangfull.com is 200
HTTP Status of fq.huanqiu.com is 301
HTTP Status of img.huanqiu.com is 301
HTTP Status of www.hqsbtime.com is 200
HTTP Status of mzc.huanqiu.com is 301
HTTP Status of www.fangfull.cn is 000
HTTP Status of uc.huanqiu.com is 301
HTTP Status of qmjjr.huanqiu.com is 301
HTTP Status of m.huanqiu.com is 301
HTTP Status of www.huanqiu.wang.com is 000
real 0m12.782s
user 0m0.085s
sys 0m0.096s
下面再測試直接curl監測網站狀態的時間:
[root@bastion-IDC ~]# cat testurl-monit.sh
#!/bin/bash
for url in `cat /root/url.list`
do
code=`curl -I -s $url | head -1 | cut -d " " -f2`
echo "HTTP Status of $url is $code "
done
如下,這個指令碼執行時間要30s多!
[root@bastion-IDC ~]# time sh testurl-monit.sh
HTTP Status of www.fangfull.com is 200
HTTP Status of www.huanqiu.com is 301
HTTP Status of erp.fangfull.com is 302
HTTP Status of fanghuadmin.huanqiu.com is 301
HTTP Status of www.hqsbtime.com is 200
HTTP Status of qmjjr.huanqiu.com is 301
HTTP Status of admin.huanqiu.com is 301
HTTP Status of m.huanqiu.com is 301
HTTP Status of fq.huanqiu.com is 301
HTTP Status of mfq.huanqiu.com is 301
HTTP Status of zc.huanqiu.com is 301
HTTP Status of mzc.huanqiu.com is 301
HTTP Status of uc.huanqiu.com is 301
HTTP Status of fanghu.huanqiu.com is 301
HTTP Status of img.huanqiu.com is 301
HTTP Status of app.huanqiu.com is 301
HTTP Status of www.fangfull.cn is
HTTP Status of www.huanqiu.wang.com is
real 0m31.689s
user 0m0.067s
sys 0m0.124s
顯然多執行緒的測試指令碼執行速度要快點!所以保留第一個指令碼url-monit.sh!
-------------------------------------------------------------------------------------------------------
下面是郵件報警設定:
1)先下載安裝包到本地,解壓。
[root@bastion-IDC ~]# cd /usr/local/src/
[root@bastion-IDC src]# wget -c http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
[root@bastion-IDC src]# tar -zvxf sendEmail-v1.56.tar.gz
[root@bastion-IDC src]# cd sendEmail-v1.56
[root@bastion-IDC sendEmail-v1.56]# cp -a sendEmail /usr/local/bin/
[root@bastion-IDC sendEmail-v1.56]# chmod +x /usr/local/bin/sendEmail
[root@bastion-IDC sendEmail-v1.56]# file /usr/local/bin/sendEmail
/usr/local/bin/sendEmail: a /usr/bin/perl -w script text executable
2)安裝下依賴
[root@bastion-IDC sendEmail-v1.56]# yum install perl-Net-SSLeay perl-IO-Socket-SSL -y
3)部署傳送指令碼
這裡由於一些域名做了跳轉,所以如果發現域名訪問後的結果不是200,301,302,那麼就是不能正常訪問狀態,需要傳送報警郵件!
如下,報警郵件傳送給wangshibo@huanqiu.cn和hugang@huanqiu.cn兩個郵箱:
[root@bastion-IDC ~]# cat url-mail.sh
#!/bin/bash
NUM=$(/bin/sh /root/url-monit.sh|grep -v "200"|grep -v "301"|grep -v "302"|wc -l)
DOMAIN=$(/bin/sh /root/url-monit.sh|grep -v "200"|grep -v "301"|grep -v "302"|awk -F" " '{print $4}')
if [ $NUM -ne 0 ];then
for url in $DOMAIN;do
/usr/local/bin/sendEmail -f ops@huanqiu.cn -t wangshibo@huanqiu.cn -s smtp.huanqiu.cn -u "Domain monitoring" -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m "[$url] can not normally access,please deal with it as soon as possible "
/usr/local/bin/sendEmail -f ops@huanqiu.cn -t hugang@huanqiu.cn -s smtp.huanqiu.cn -u "Domain monitoring" -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m "[$url] can not normally access,please deal with it as soon as possible "
done
else
echo "it is OK"
fi
-----------------------------------------------------------------
郵件傳送引數說明:
命令說明:
/usr/local/bin/sendEmail #命令主程式
-f from@uhanqiu.cn #發件人郵箱
-t to@huanqiu.cn #收件人郵箱
-s smtp.huanqi.cn #發件人郵箱的smtp伺服器
-u "....." #郵件的標題
-o message-content-type=html #郵件內容的格式,html表示它是html格式
-o message-charset=utf8 #郵件內容編碼
-xu from@huanqiu.cn #發件人郵箱的使用者名稱
-xp zh@123bj #發件人郵箱密碼
-m "......" #郵件的具體內容
-----------------------------------------------------------------
[root@bastion-IDC ~]# sh -x url-mail.sh
++ /bin/sh /root/url-monit.sh
++ grep -v 200
++ grep -v 301
++ grep -v 302
++ wc -l
+ NUM=2
++ /bin/sh /root/url-monit.sh
++ grep -v 200
++ grep -v 301
++ grep -v 302
++ awk '-F ' '{print $4}'
+ DOMAIN='www.fangfull.cn
www.huanqiu.wang.com'
+ '[' 2 -ne 0 ']'
+ for url in '$DOMAIN'
+ /usr/local/bin/sendEmail -f ops@huanqiu.cn -t wangshibo@huanqiu.cn -s smtp.huanqiu.cn -u 'Domain monitoring' -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m '[www.fangfull.cn] can not normally access,please deal with it as soon as possible '
Oct 25 19:21:43 bastion-idc sendEmail[19668]: Email was sent successfully!
+ for url in '$DOMAIN'
+ /usr/local/bin/sendEmail -f ops@huanqiu.cn -t wangshibo@huanqiu.cn -s smtp.huanqiu.cn -u 'Domain monitoring' -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m '[www.huanqiu.wang.com] can not normally access,please deal with it as soon as possible '
Oct 25 19:21:47 bastion-idc sendEmail[19672]: Email was sent successfully!
+ for url in '$DOMAIN'
+ /usr/local/bin/sendEmail -f ops@huanqiu.cn -t huang@huanqiu.cn -s smtp.huanqiu.cn -u 'Domain monitoring' -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m '[www.fangfull.cn] can not normally access,please deal with it as soon as possible '
Oct 25 19:21:43 bastion-idc sendEmail[19668]: Email was sent successfully!
+ for url in '$DOMAIN'
+ /usr/local/bin/sendEmail -f ops@huanqiu.cn -t hugang@huanqiu.cn -s smtp.huanqiu.cn -u 'Domain monitoring' -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m '[www.huanqiu.wang.com] can not normally access,please deal with it as soon as possible '
Oct 25 19:21:47 bastion-idc sendEmail[19672]: Email was sent successfully!
登陸wangshibo@huanqiu.cn郵箱,發現已經收到報警郵件了!
最後新增計劃任務,每5分鐘執行一次
[root@bastion-IDC ~]# crontab -l
#domain monit
*/5 * * * * /bin/bash -x /root/url-mail.sh >/dev/null 2>&1