shell脚本监控网站状态

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 = 'abc@163.com'
    password = 'abc'
    recipients = 'abc@qq.com'
    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

 

 

 

转载于:https://www.cnblogs.com/taiguyiba/p/10521227.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值