群里有朋友问怎么样监控自己的网站运行情况,我这里把我用的分享给大家,使用Python运行哦。
# -*- coding: utf8 -*-
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/..")
import logging
import json
import requests
from email.mime.text import MIMEText
from email.header import Header
import smtplib
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# Third-party SMTP service for sending alert emails. 第三方 SMTP 服务,用于发送告警邮件
mail_host = "smtp.exmail.qq.com" #SMTP服务器,如QQ邮箱,需要在账户里开启SMTP服务
mail_user = "***@hipyt.cn" # Username 用户名
mail_pass = "****" # Password, SMTP service password. 口令,SMTP服务密码
mail_port = 465 # SMTP service port. SMTP服务端口
# The URL address need to dial test. 需要拨测的URL地址
test_url_list = [
"https://www.hipyt.cn",
"https://***",
"https://tebk.cn",
"https://***"
]
# The notification list of alert emails. 告警邮件通知列表
email_notify_list = {
"**@**.cn",
"***@gmail.com"
}
def sendEmail(fromAddr, toAddr, subject, content):
sender = fromAddr
receivers = [toAddr]
message = MIMEText(content, 'plain', 'utf-8')
message['From'] = Header(fromAddr, 'utf-8')
message['To'] = Header(toAddr, 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP_SSL(mail_host, mail_port)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("send email success")
return True
except smtplib.SMTPException as e:
print(e)
print("Error: send email fail")
return False
def test_url(url_list):
errorinfo = []
for url in url_list:
resp = None
try:
resp = requests.get(url, timeout=3)
print (resp)
except (
requests.exceptions.Timeout, requests.exceptions.ConnectionError, requests.exceptions.ConnectTimeout) as e:
logger.warn("request exceptions:" + str(e))
errorinfo.append("Access " + url + " timeout")
else:
if resp.status_code >= 400:
logger.warn("response status code fail:" + str(resp.status_code))
errorinfo.append("Access " + url + " fail, status code:" + str(resp.status_code))
if len(errorinfo) != 0:
body = "\r\n".join(errorinfo)
subject = "Please note: PlayCheck Error"
for toAddr in email_notify_list:
print ("send message [%s] to [%s]" % (body, toAddr))
sendEmail(mail_user, toAddr, subject, body)
def main_handler(event, context):
test_url(test_url_list)
if __name__ == '__main__':
main_handler("", "")
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。