应领导要求,对公司几个主要站点的域名访问情况进行监控。下面分享一个监控脚本,并利用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

***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************
分类:  Shell, 监控系统
本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/5997804.html ,如需转载请自行联系原作者

shell+curl监控网站页面(域名访问状态),并利用sedemail发送邮件相关推荐

  1. epel源mysql版本_linux增加epel源,yum安装nignx,脚本安装mysql服务端,shell脚本监控网站页面...

    epel是一种yum仓库,里面提供了更多.更丰富linux软件,但是,它不是默认yum仓库,我们需要手工添加这个yum源. 添加epel源 然后,找到fedora-epel, 在列表里找到:6serv ...

  2. linux增加epel源,yum安装nignx,脚本安装mysql服务端,shell脚本监控网站页面

    epel是一种yum仓库,里面提供了更多.更丰富linux软件,但是,它不是默认yum仓库,我们需要手工添加这个yum源. 添加epel源 参考:http://freeloda.blog.51cto. ...

  3. shell批量监控网站状态码

    shell批量监控网站状态码脚本,使用curl很慢.等我学完其他方式,在来更新. #!/bin/bash #GuoYabin yuming=`/bin/cat yuming.txt` for i in ...

  4. html怎么设置虚拟浏览量,网站页面的访问数—PV值

    PV:用户每次访问页面数,在某个时间内被访问的页面总数. PV公式计算:综合浏览量/独立访问次数 PV值有什么意义呢? 一个网页的PV值可以反应出一个网页的内容可读性,可读性越大网站权重就会提升,一个 ...

  5. nginx 根据目录指定root_部署Nginx网站服务实现访问状态统计以及访问控制功能

    Nginx专为性能优化而开发,最知名的优点是它的稳定性和低系统资源消耗,以及对HTTP并发连接的高处理能力,单个物理服务器可支持30000-50000个并发请求. Nginx的安装文件可以从官方网站h ...

  6. shell脚本监控网站是否正常

    #!/bin/bash #20190611 url.txt文件直接填写需要监控的网址 第三版 #QQ450433231 time=`date +"%Y/%m/%d %H:%M.%S" ...

  7. 利用shell脚本监控网站状态

    首先需要有一个已开通飞信的手机号,并把接收警报短信的手机号加为飞信好友(也可以自己给自己发),调用飞信的免费短信接口发送报警短信,也可使用MSN报警,相对短信报警更及时. 编辑脚本http.sh vi ...

  8. 智能安防视频监控平台页面无法访问该如何排查?

    我们的各个平台视频能力丰富且全面.部署灵活,能满足用户的多场景视频监控需求,平台各具特点,可支持多类型的设备.多协议接入,包括国标GB28181协议.RTMP/RTSP/Onvif协议.海康EHOME ...

  9. 服务器间 存活状态,Shell脚本监控LVS后台服务器存活状态

    简介 在生产工作中,后台的服务器并不可能永远都处于正常运行状态,若服务器发生宕机,为了不影响正在进行的业务以及给用户更好的体验,我们需要通过编写监控脚本对LVS的后台主机存活情况进行监控,当有服务器发 ...

最新文章

  1. 2018-3-25论文(Whale Optimizer Algorithm)+(Gery Wolf Optimizer)笔记三---算法部分的对比
  2. 局部变量,静态局部变量,全局变量,静态全局变量在内存中的存放区别(转)...
  3. 小学数学加减法测试软件,儿童数学加法运算火箭(测试版)
  4. tshark mysql_使用tshark抓包分析http请求
  5. 嵌入式根文件系统的移植和制作详解
  6. 人工智能先驱 Nils Nilsson 去世,吴恩达、Yann LeCun 悼念!
  7. html form中多个div,在react里最多只能有几个div
  8. 超神学院暗物质计算机,超神学院之进击的赛亚人
  9. Python实现批量Word转PDF
  10. 商品详情页面html,div+css+JQuery仿京东商品详情界面
  11. 【C/C++】黑盒测试
  12. U盘安装ubuntu系统
  13. 快手也抢先字节出手了…
  14. Typora markdown教程
  15. 《歪笑小说》—— 读后总结
  16. Spring Boot启动器
  17. b、B、KB、Kib、MB、MiB、GB、GiB、TB、TiB的区别
  18. 克鲁斯卡尔算法的基本介绍和实现方法(Java)
  19. Visio绘图软件所需图标总结
  20. Ettercap中间人攻击——DNS劫持、替换网页内容与ARP欺骗

热门文章

  1. MP4学习(一)MP4格式分析
  2. LaTeX支持直接输入希腊字母等符号
  3. Matlab R2016a破解安装教程及下载
  4. 光谷码农·每日新闻(2019-05-10)
  5. yolov5 windows 下训练+ c++ TensorRT 部署在qt (vs+qtcreator) 只要一篇文章即可
  6. [zt] NBA季后赛赛程
  7. 导演乔舒马赫去世享年80 曾执导《永远的蝙蝠侠》
  8. asp.net花店购物商城系统
  9. tcpdump丢包问题分析
  10. 分布式管理系统Git工具的使用