Use emails to monitor whether the website is abnormal
-
[Shell requirements]
-
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 to write an email script.
-
[Shell analysis]
1. The key issue, the code status is intercepted
2. When writing the shell script, you should first use the curl -I http://www.51xit.top/ command to test under the command, and then intercept it through awk Status code
3. Write the script for sending emails, using sendEmail. The production environment has a matching template
4, judgment and e-mail association
curl -I http://www.51xit.top/
We will have interactive information 200 when we capture packets
-
【Install Mail Components】
-
[root@tang ~]# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz ####下载安装包 [root@tang ~]# tar -zxvf sendEmail-v1.56.tar.gz #####解压安装包 [root@tang ~]# cp sendEmail-v1.56/sendEmail /usr/local/bin/ ####复制文件内容至/usr/local/bin/ [root@tang ~]# chmod 755 /usr/local/bin/sendEmail ####给与权限 [root@tang ~]# vi /opt/sendEmail.sh ####编辑脚本文件
-
[Shell configuration-1, 2]
(Note: After the curl -l command is added to the pipeline, there will be a detailed information about the download process. For example: download percentage, speed and other information, these information are wrong output, so you need to add a 2 >/dev/null)#/bin/bash url='www.51xit.top' sta=`curl -l $url 2> /dev/null | head -1 | awk '{print $2}'` if [ $sta != "200" ] then /opt/sendEmail.sh [email protected] "$url down." "$url down." ###xxxxxxxx为 你想要发送邮件的QQ fi
-
[Shell configuration-3, 4]
#!/bin/bash # #Filename: SendEmail.sh #Revision: 1.0 #Date: 2019/05/29 #Author: Qicheng #Email: #Website: http://51xit.top/ #Description: tang邮件告警脚本 #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地址,tang传入的第一个参数 message_subject_utf8="$2" ##### 邮件标题,tang传入的第二个参数 message_body_utf8="$3" ##### 邮件内容,tang传入的第三个参数 #转换邮件标题为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