Step1.安装sendmail来发邮件
# yum -y install sendmail
# /etc/init.d/sendmail start
# chkconfig sendmail on
Step2.安装mutt邮件客户端,并设置相关信息
# yum -y install mutt
# vim /etc/Muttrc
set charset="utf-8" #设置发邮件编码
set envelope_from=yes
set rfc2047_parameters=yes #解决附件乱码问题
set realname="报警" #发件人别名
set use_from=yes #指定是否显示别名
set from=monitor@test.com #发送人地址
Step3.脚本信息
1.url文件如下
# cat url
www.baidu.com
www.sina.com
2.脚本如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#!/bin/bash
#====================================================
# Author: lizhenliang - EMail:zhenliang369@163.com
# Last modified: 2015-2-1
# Filename: web_mon.sh
# Description: wget monitoring web is normal
# blog:lizhenliang.blog.51cto.com
#====================================================
while
true
do
Mail=
"baojingtongzhi@163.com"
FailCount=0
Retval=0
GetUrlStatus() {
for
((i=1;i<=3;i++))
#使用i++判断访问次数,如果wget两次超时则判断网站异常
do
wget -T 3 --tries=1 --spider http:
//
${1} >
/dev/null
2>&1
#-T超时时间,--tries尝试1次,--spider蜘蛛
[ $? -
ne
0 ] &&
let
FailCount+=1;
#访问超时时,$?不等于0,则FailCount加1
done
if
[ $FailCount -gt 1 ];
then
Retval=1
Date=`
date
+%F
" "
%H:%M`
echo
-e
"Date : $Date\nProblem : $url is not running."
| mutt -s
"URL Monitor"
$Mail
else
Retval=0
fi
return
$Retval
#如果返回值为0,就正常退出循环,不为0则继续循环
}
for
url
in
`
cat
url |
sed
'/^#/d'
`
do
#GetUrlStatus $url && echo yes || echo no
GetUrlStatus $url
done
sleep
2m
#死循环,设置没2分钟运行一次
done
|
本文转自 李振良OK 51CTO博客,原文链接:http://blog.51cto.com/lizhenliang/1610408,如需转载请自行联系原作者