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

news/2024/5/16 6:05:42/文章来源:https://blog.csdn.net/weixin_33755649/article/details/85826433

 

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_783908.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ruby网站部署到服务器,Ruby China 已迁移到新的服务器,基于 Docker 部署

终于决定要迁移新服务器了&#xff0c;之前那台老机器陪同 Ruby China 运作了 6 年(如果我没记错的话)&#xff0c;系统还是 Ubuntu 12.04 ...昨天下班前还是准备&#xff0c;同步数据库到 UCloud 的 PostgreSQL 服务上(避免以后自己维护和备份)。由于早些时候&#xff0c;我已…

买机电哪个网站好_装修网站哪个好?怎么挑选好的装修网站?

当下人们和父辈都已经有了很大的差异&#xff0c;以前父母亲在装修房子的时候都是直接找装修工人&#xff0c;可是对于现如今人们在装修房子的时候要求是比较高的&#xff0c;首先是找装修网站&#xff0c;这样他们能够设计出来不一样的效果&#xff0c;对于装修网站哪个好是必…

Asp.Net 4.0 SEO增强之 UrlRouting

在.Net 4.0之前我们为了做出搜索引擎友好的&#xff0c;对用户也友好的url都是需要自己实现Url重写&#xff0c;现在不需要了&#xff0c;.Net 4.0为我们做这一切。UrlRouting之所以称之为Routing是因为它不但实现了Url重写还可以通过参数得到重写后的Url在页面上使用。 1. Url…

外链引入css有哪些方式_游戏类网站外链建设需要规避的问题

游戏类网站外链建设需要规避的问题。第一&#xff0c;建设软文外链需要谨慎。要知道软文外链通常是一种较为高质量的外链&#xff0c;百度对此也表示了认可。有的游戏类网站还因此让自己的网站内容得到了更多的转载量。不过这里就存在着一个问题&#xff0c;那就是现在很多游戏…

hao123电脑版主页_Hao123这么简单的网站,百度为什么肯花5000万收购

在知名流量监测网站ALEXA上&#xff0c;Hao123历史上曾经紧随百度和淘宝&#xff0c;排名过第三。是什么让这样一个看似简陋的网站&#xff0c;有这么大的流量&#xff0c;又为什么百度肯花5000万收购。一切都从它的创始人&#xff0c;李兴平说起。1、创始人曾是网瘾少年李兴平…

RequireJs构建网站

requireJS构建网站 一.概要 requireJS添加了对jquery的amd支持&#xff0c;并整合了一个require-jquery.js脚本库&#xff0c;因此当我们需要requirejquery来构建项目的时候仅需要引入require-jquery.js就搞定了。 模块化管理有什么作用&#xff1a; 1.易管理维护。 2.按需加载…

html网页制作需要审美,哪些网站能提高HTML5网站设计师的审美?

很多人在动手做网站的时候&#xff0c;感觉无从下手。一方面是对操作不熟悉&#xff0c;另一方面是设计没灵感。一个网站做得好不好&#xff0c;除了技术关&#xff0c;美工也同样重要。操作上需要大家慢慢积累&#xff0c;而提升设计灵感&#xff0c;你只需要看这10个网站。1、…

利用其他网站的搜索结果_科普一下“网站SEO”,关键字搜索结果排名的艺术

前言什么是SEO呢&#xff1f;SEO是Search Engine Optimization&#xff0c;意为“搜索引擎优化”&#xff0c;一般简称为搜索优化。对于SEO的主要工作就是通过了解各类搜索引擎如何抓取互联网页面&#xff0c;如何进行索引以及如何确定其对某一个特定关键词的搜索结果排名等技术…

用python爬取网站数据期末作业_Python爬虫爬取博客园作业

分析一下他们的代码&#xff0c;我在浏览器中对应位置右键&#xff0c;然后点击检查元素&#xff0c;可以找到对应部分的代码。但是&#xff0c;直接查看当前网页的源码发现&#xff0c;里面并没有对应的代码。我猜测这里是根据服务器上的数据动态生成的这部分代码&#xff0c;…

整理了一周的Python资料,包含各阶段所需网站、项目,收藏了慢慢来

这周应该有不少学校已经开学了&#xff0c;那么同学们都该动起来了&#xff0c;把家里面的那些懒习惯给扔掉了可以。 不知怎么的&#xff0c;最近不少关注我的读者都开始私信我怎么学好python&#xff1f;零基础转行是不是合适&#xff0c;还有希望吗&#xff1f;今年30了&…

linux浏览器老显示安全链接,Chrome浏览器显示“网站连接不安全”怎么解决?解决方法分享...

chrome浏览器是一个非常受欢迎的搜索服务软件&#xff0c;这款软件可以满足用户非常多搜索需求&#xff0c;有很多用户都会使用这款软件进行搜索&#xff0c;功能非常的全面&#xff0c;但是在使用Chrome浏览器的也会遇到一般浏览器都会遇到的问题&#xff0c;今天小编就会与大…

产品展示网站源码_ZBLOG免费网站导航主题 – 简单且支持内页详情目录模板

之前一段时间老蒋和从业网赚类网站的网友闲聊到看到有几个做网赚导航类型的网站盈利情况还是不错的&#xff0c;当然我不熟悉这个领域也没有打算做接触这个行业。不过我看到有不少网友在寻找类似的目录导航类网站源码&#xff0c;而且看到有些简单的源码居然还需要付费购买&…

毕业设计-证券宣传手机微网站的设计与实现

本文介绍基于.net的证券公司宣传微网站手机网页的设计与实现方法。 随着计算机技术的快速发展&#xff0c;基于Web的计算机网络金融、证券宣传或交易网站已成为现代金融理财发展的热点,B/S(Browser/Server)结构的互联网宣传也逐步在各大金融证券中得到了广泛的应用[1]。互联网金…

遗传算法matlab_史上最强的MATLAB自学网站,你收藏了吗???

各位小伙伴可点击此处&#xff0c;即可进入到优化算法交流地官方账号主页&#xff08;推荐&#xff09;&#xff0c;谨防上当受骗。hello&#xff0c;大家好&#xff01;这几天是全国大学生数学建模竞赛的比赛时间&#xff0c;首先预祝各位参赛的同学能够取得好成绩。今天&…

linux更换域名全站301,网站换域名必看,全站301重定向代码分享

网站换域名了&#xff0c;至于为什么要换不多说了&#xff0c;伤心事&#xff01;犹豫了半年&#xff0c;新域名也早买好了&#xff0c;一直没下定决心要换域名&#xff0c;毕竟8年老站&#xff0c;中国不多说了&#xff0c;入题&#xff0c;想把域名换成 http://www.125jz.com…

织梦html标题怎么改,织梦网站seo优化技巧-改栏目名称seo标题即可

你们了解过织梦网站seo优化技巧吗?使用该优化首先你要改栏目名称为seo标题&#xff0c;然后在网站的UR路径上进行拼音的修改&#xff0c;修改完之后要以文章的路径保存好&#xff0c;感兴趣的话就随小编一起来了解下吧!第一步网站标题seo优化技巧:列表页标题优化技巧&#xff…

企业网站建设流程步骤,教你快速建网站

企业网站的重要性不言而喻&#xff0c;很多企业都想建一个企业网站&#xff0c;这样既能提高企业形象&#xff0c;还可以助力网络营销和品牌推广。那么如何参与到企业网站建设流程中呢&#xff1f;其实这个流程分五步&#xff0c;今天就为大家介绍一下。 第一步&#xff1a;确定…

模仿某网站招聘列表

引言 关注到一朋友所在公司的网站&#xff0c;看到招聘信息部分&#xff0c;突然觉的这个效果简单并且可复用&#xff0c;然后自己就练习了一把。 缺点 点击标题展开内容后&#xff0c;文字有点抖动&#xff0c;还没排查出来问题所在。 页面 1 <div classjobs_box>2 …

网狐网站后台发布与部署

发布成功并部署OK是这样子的 在部署前要先启用IIS功能 在控制面板的程序与功能左边可打开启用Windows功能窗口 IIS功能启用后可打开 IIS管理器,默认添加了Default Web Site

java 期刊,JSP/Java的期刊在线投稿系统|网站程序设计|程序源码

视频演示网址&#xff1a;http://www.bysjdq.com/project/show/CBADCFADD5E934B6.shtml?tff(咨询特价)系统功能模块设计1&#xff0e;系统登录&#xff1a;系统登录是用户访问系统的路口&#xff0c;设计了系统登录界面&#xff0c;包括用户名、密码和验证码&#xff0c;然后对…