python url监控 并邮件报警

Posted wangruwei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python url监控 并邮件报警相关的知识,希望对你有一定的参考价值。

import time
import requests
import smtplib
from email.mime.text import MIMEText
import datetime

def SendEmail(fromAdd,toAdd,subject,text):

_pwd = "[email protected]" #授权码

msg = MIMEText(text)
msg["Subject"] = subject
msg["From"] = fromAdd
msg["To"] = toAdd
try:
s = smtplib.SMTP_SSL("smtp.sudaizhijia.com", 465)
s.login(fromAdd, _pwd)
s.sendmail(fromAdd, toAdd, msg.as_string())
s.quit()
print ("Success!")
except smtplib.SMTPException:
print(‘Falied!‘)
from_="[email protected]"
to_ = ["[email protected]"] #收件人地址
subject = "URL报警信息"
# url_list=["http://api.sudaizhijia.com","http://event.fastloan360.com","http://www.qianzhouzhou.com","https://data.sudaizhijia.com","http://uat.data.sudaizhijia.com"] #url地址
url_list=["https://m.1sudai.com/","http://www.qianzhouzhou.com"]


while True:
time.sleep(20)

for i in url_list:
nowTime = datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)
try:
code = requests.get(i).status_code
v = int(code)
time.sleep(2)
if isinstance(v,int):
if v == 200:
pass
else:
text = "%s %s 发出报警,值为:%d 请及时查看状态" %(nowTime,i,v)
for i in to_:
SendEmail(from_, i, subject, text)

except requests.exceptions.ConnectionError as e:
text = "%s %s 发出报警,服务可能挂掉 如果有ssl证书可能证书到期" %(nowTime,i)
for i in to_:
SendEmail(from_, i, subject, text)




















































以上是关于python url监控 并邮件报警的主要内容,如果未能解决你的问题,请参考以下文章

python 监控日志并发送邮件报警

Python3 实现简易ping监控并发送报警邮件

zabbix 定义触发器,并使用邮件,微信消息报警。

利用python3监控服务器状态进行邮件报警

zabbix4.2添加被监控端主机与设置自定义监控项并邮件报警

使用Grafana 展示Docker容器的监控图表并设置邮件报警规则