shell脚本监控网站状态分析

符号 阅读:309 2021-01-14 09:34:20 评论:0

shell脚本监控网站状态

#!/bin/sh 
 
date=`date +"%Y%m%d-%H%M"` 
title="status" 
contentFail="status is not ok:" 
contentSuccess="status is ok:" 
url="https://www.abc.com" 
status=`curl -m 20 -s -I $url | grep HTTP | awk '{print $2}'` 
 
#echo "status: $status" 
 
cd /data/shell 
laststatus=`cat status.log` 
 
if [ "$status" == "200" ] 
    then 
        if [ "$laststatus" != 200 ] 
            then 
                /usr/bin/python /shell/mail.py "$title" "$contentSuccess $url $date $status" 
                echo "200" > status.log 
        fi 
else 
    if [ "$laststatus" == 200 ] 
        then 
            /usr/bin/python /shell/mail.py "$title" "$contentFail $url $date $status" 
            echo "$status" > status.log 
    fi 
fi

 mail.py

from email.header import Header 
from email.mime.text import MIMEText 
import smtplib 
import sys 
 
def sendmail(subject, content): 
    sender = '[email protected]' 
    password = 'abc' 
    recipients = '[email protected]' 
    host = 'smtp.abc.com' 
    msg = MIMEText(content, 'plain', 'utf-8') 
    msg['From'] = sender 
    msg['To'] = recipients 
    msg['Subject'] = Header(subject, 'utf-8').encode() 
    server = smtplib.SMTP_SSL(host, 465) 
    server.login(sender, password) 
    server.sendmail(sender, [recipients], msg.as_string()) 
    server.quit() 
 
 
sendmail(sys.argv[1],sys.argv[2])

 设置定时任务

crontab -e
*/2 * * * * /shell/status.sh

 

 

 

标签:linux
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号