Shell script-monitor whether the website is abnormal, send emails abnormally

Claim:

Write a shell script and use curl -I to return the status code to determine whether the visited website is normal. For example, when the code status is 200, it is considered normal.
Write a script to send an email

experiment:

(1) Create trigger and email alarm test

[root@localhost ~]# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
[root@localhost ~]# tar -zxvf sendEmail-v1.56.tar.gz
sendEmail-v1.56/
sendEmail-v1.56/CHANGELOG
sendEmail-v1.56/README
sendEmail-v1.56/README-BR.txt
sendEmail-v1.56/TODO
sendEmail-v1.56/sendEmail
sendEmail-v1.56/sendEmail.pl
[root@localhost ~]# cp sendEmail-v1.56/sendEmail /usr/local/bin/
[root@localhost ~]# chmod 755 /usr/local/bin/sendEmail
[root@localhost ~]# vi /opt/sendEmail.sh
[root@localhost ~]# vim /opt/sendEmail.sh
#!/bin/bash
#
# Filename:    SendEmail.sh
# Revision:    1.0
# Date:        2020/08/26
# Author:      Qicheng
# Email:
# Website:     http://51xit.top/
# Description: 邮件告警脚本
# Notes:       使用sendEmail
#

# 脚本的日志文件
LOGFILE="/tmp/Email.log"
:>"$LOGFILE"
exec 1>"$LOGFILE"
exec 2>&1

SMTP_server='smtp.qq.com'                                     # SMTP服务器,变量值需要自行修改
username='[email protected]'                         	# 用户名,变量值需要自行修改
password='password'                                                  # 密码(QQ邮箱用的是授权码),变量值需要自行修改
from_email_address='[email protected]'         	#### 发件人Email地址,变量值需要自行修改
to_email_address="$1"                                               	# 收件人Email地址,传入的第一个参数
message_subject_utf8="$2"                                        # 邮件标题,传入的第二个参数
message_body_utf8="$3"                                          	# 邮件内容,传入的第三个参数

# 转换邮件标题为GB2312,解决邮件标题含有中文,收到邮件显示乱码的问题。
message_subject_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_subject_utf8
EOF`
[ $? -eq 0 ] && message_subject="$message_subject_gb2312" || message_subject="$message_subject_utf8"

# 转换邮件内容为GB2312,解决收到邮件内容乱码
message_body_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_body_utf8
EOF`
[ $? -eq 0 ] && message_body="$message_body_gb2312" || message_body="$message_body_utf8"

# 发送邮件
sendEmail='/usr/local/bin/sendEmail'
set -x
$sendEmail -s "$SMTP_server" -xu "$username" -xp "$password" -f "$from_email_address" -t "$to_email_address" -u "$message_subject" -m "$message_body" -o message-content-type=text -o message-charset=gb2312
[root@localhost ~]# chmod +x /opt/sendEmail.sh 
[root@localhost ~]# /opt/sendEmail.sh [email protected] 测试 测试

Note: The parameters to be modified by the above script are as follows
Insert picture description here

(2) Create a monitoring script

[root@localhost ~]# vim /opt/123.sh
#!/bin/bash
url='www.51xit.top'
sta=`curl -I $url 2>/dev/null |head -1 |awk '{print $2}'`
if [ $sta != "200" ]
then
/opt/sendEmail.sh [email protected] "$url down." "$url down."
fi
[root@localhost ~]# chmod +x /opt/123.sh
[root@localhost ~]# /opt/123.sh

Guess you like

Origin blog.csdn.net/ZG_66/article/details/108238954