主要用了 requests 模块, 所以跑监控脚本的 机器要先安装 requests
pip install requests
点击(此处)折叠或打开
-
#!/usr/bin/env python
-
# -*- coding: utf-8 -*-
-
-
import smtplib
from email.mime.text import MIMEText
import sys - import requests
-
-
-
urls=[]
-
urls.append('')
-
urls.append('')
-
-
-
# 报警邮件参数设置, 前三行是用来发件的邮箱的配置
-
mail_host = 'smtp.exmail.qq.com'
-
mail_user = 'xj@abc.cn' #qq邮箱这个地方必须写全!
-
mail_pass = "123456"
-
mail_postfix = 'suibianxie.com'
-
admin_email = 'admin@youremail.com'
-
-
-
-
-
# 邮件发送函数
-
def send_mail(to_list,subject,content):
-
me = mail_user+"<"+mail_user+"@"+mail_postfix+">"
-
msg = MIMEText(content)
-
msg['Subject'] = subject
-
msg['From'] = me
-
msg['to'] = to_list
-
-
-
try:
-
s = smtplib.SMTP()
-
s.connect(mail_host)
-
s.login(mail_user,mail_pass)
-
s.sendmail(me,to_list,msg.as_string())
-
s.close()
-
return True
-
except Exception,e:
-
print str(e)
-
return False
-
-
-
# 主体部分
-
for url in urls:
-
try:
-
r = requests.get(url,timeout=3)
-
if r.status_code >= 500:
-
msg = 'Error: ' + str(r.status_code) + ' ' + url
-
print "send email"
-
send_mail( admin_email,"api error",msg)
-
print "done"
-
except requests.exceptions.ConnectionError as e:
-
print "These aren't the domains we're looking for."
-
except requests.exceptions.ConnectTimeout as e:
-
print "Too slow Mojo!"
-
except requests.exceptions.HTTPError as e:
- print "And you get an HTTPError:", e.message