前言
因为有一些网站需要每日检查是否有问题,所以需要一个报警监控的机制,这个需要你指定你发送的邮箱和你接收的邮箱,就可以做到对网站自动监控了。
这里用的是python3.5
需要安装的插件:
1、smtplib:发邮件需要用到
2、pycurl:访问网站时会需要用到
3、linecache:在读取txt网站清单时需要用到
具体思路:
python程序从txt里面批量读取到网站的信息,通过curl.py模拟浏览器去访问网站,并且把访问的结果写入到以自己的网站名称-日期.txt格式的文件中记录;有几种情况:
1、如果发现打不开了,直接发邮件提示网站已经打不开
2、发现可以打开,读取文件中上一次访问的情况(读取txt文件最后一行),
1)如果发现上一次是打不开的,发邮件提醒网站已经恢复了
2)如果发现上一次是打得开的(200的返回码),只是记录网站访问的日志就可以了
总共4个文件
email.py是邮件类,主要用来发邮件的时候调用,这里需要按照你的情况改成你的邮箱(msg[‘from’]),邮箱服务器地址(smtp地址),和你的邮箱密码(smtp.login)
email.py
#!/usr/bin/python
#-*- coding:utf-8 -*-
import sys
import smtplib
from email.mime.text import mimetext
from email.mime.multipart import mimemultipart
class email_send(object):
def __init__(self,msgto,data2,subject):
self.msgto=msgto
self.data2=data2
self.subject=subject
def sendemail(self):
# (attachment,html) = content
msg = mimemultipart()
msg[‘subject’] = self.subject
msg[‘from’] = ‘xxxx@xxxx.com.cn’
msg[‘to’] = self.msgto
html_att = mimetext(self.data2, ‘html’, ‘utf-8’)
#att = mimetext(attachment, ‘plain’, ‘utf-8’)
msg.attach(html_att)
#msg.attach(att)
try:
smtp = smtplib.smtp()
smtp.connect(‘smtp.xxxx.com’, 25)
smtp.login(msg[‘from’], ‘xxxx’) #改成自己的邮箱密码
smtp.sendmail(msg[‘from’], msg[‘to’].split(‘,’), msg.as_string())
return(‘邮件发送成功’)
except exception as e:
print(‘————–sss——‘,e)
def curl(self):
import pycurl
c=pycurl.curl()
#url=”www.luoan.com.cn”
#indexfile=open(os.path.dirname(os.path.realpath(__file__))+”/content.txt”,”wb”)
c.setopt(c.url,url)
c.setopt(c.verbose,1)
c.setopt(c.encoding,”gzip”)
#模拟火狐浏览器
c.setopt(c.useragent,”mozilla/5.0 (windows nt 6.1; rv:35.0) gecko/20100101 firefox/35.0″)
return c
curl.py 主要用来执行模拟浏览器访问网站并返回结果的文件
#!/usr/bin/python
#-*- coding:utf-8 -*-
import sys
import pycurl
class curl(object):
def __init__(self,url):
self.url=url
def curl_site(self):
c=pycurl.curl()
#url=”www.luoan.com.cn”
#indexfile=open(os.path.dirname(os.path.realpath(__file__))+”/content.txt”,”wb”)
c.setopt(c.url,self.url)
c.setopt(c.verbose,1)
c.setopt(c.encoding,”gzip”)
#模拟火狐浏览器
c.setopt(c.useragent,”mozilla/5.0 (windows nt 6.1; rv:35.0) gecko/20100101 firefox/35.0″)
return c
site_moniter.py 这个文件为主程序,主要执行调用上面的函数,读取txt文件中的网站清单,如果网站打不开就发邮件出来告警
需要注意:
1、把xxxx@xxxx.com改成你自己的邮箱,
2、把文件路径改成自己的真实路径
#!/usr/bin/python
#-*- coding:utf-8 -*-
import pycurl
import os
import sys
import linecache
import time #引入事件类,用来获取系统当前时间
#from ceshi import student
from email import email_send
from curl import curl
#bart = student(‘mafei’,59)
#bart.print_score()
def script(urls,type):
msgto = ‘xxxx@xxxx.com’
now_time=time.strftime(“%y-%m-%d %h:%m:%s”,time.localtime(time.time()))
j=1
# data2=[{‘aa’:’aa’}]
for url_split in urls:
#print(url_split)
url_1=url_split.split(‘—‘)
url=url_1[1]
recovery_title = “监控通知—-%s url:%s” % (url_1[0], url) + “在” + time.strftime(“%y-%m-%d %h:%m:%s”,time.localtime(time.time())) + “已经恢复”
down_title = “监控通知—-%s url:%s” % (url_1[0], url) + “在” + time.strftime(“%y-%m-%d %h:%m:%s”,time.localtime(time.time())) + “无法打开”
#print(‘~~~~~~~~~~~~~~~~~~~’)
#print(url)
#引用爬去网站的类,调用结果
url_result = curl(url)
c = url_result.curl_site()
try:
c.perform()
code = str(c.getinfo(c.http_code))
print(code+’aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa’)
except exception as e:
print(‘——–错误信息:——–‘,e)
#indexfile.close()
#c.close()
code = str(c.getinfo(c.http_code))
# print(code+’aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa’)
filename = ‘%s-%s.txt’ % (url_1[0], time.strftime(“%y-%m-%d”, time.localtime(time.time())))
#判断如果在网站无法打开的情况下
if code == ‘0’ or code==’400′ or code==’500′ or code==’404′:
resolvetime = 0
connection_time = 0
transfer_total_time = 0
total_time = 0
# print(‘为000000000000000000000000000000000000000000’)
data3 = ‘网站:%s无法打开%s’ % (url_1[0], url)
# indexfile.close()
# c.close()
#判断网站如果挂了就发邮件
stat3 = email_send(msgto, data3, down_title)
resole=stat3.sendemail()
print(resole)
print(data3 + ‘邮件已经发送’)
else:
#resolvetime = str(c.getinfo(c.namelookup_time) * 1000) + ” ms”
# connection_time=str(float(c.getinfo(c.connect_time)*1000-c.getinfo(c.namelookup_time)*1000))+” ms”
#connection_time = str(c.getinfo(c.connect_time) * 1000 – c.getinfo(c.namelookup_time) * 1000) + ” ms”
# connection_time=round(float(connection_time))
#transfer_total_time = str(c.getinfo(c.total_time) * 1000 – c.getinfo(c.pretransfer_time) * 1000) + ” ms”
#total_time = str(c.getinfo(c.total_time) * 1000) + ” ms”
# data2=data
# data={‘url’:url,’http code’:code,’resolvetime’:resolvetime,’connection_time’:connection_time,’transfer_total_time’:transfer_total_time,’total_time’:total_time}
print(‘网站可以正常打开’)
#f = open(filename, ‘a’,encoding=’utf-8′)
file_exit=os.path.exists(filename)
#print(file_exit)
#判断这个日志文件存不存在
if(file_exit):
#读取文件最后一行,为了读取出来最后一次的状态值
file = open(filename, ‘r’,encoding=’utf-8′)
linecount = len(file.readlines())
data = linecache.getline(filename, linecount)
file.close
if data == ”:
print(‘这是’+data+’为空的数据’)
else:
print(‘其他信息%s’%(data))
explode = data.split(‘—-‘)
#判断如果读取出来的值,最后一次是异常的情况就告警
if explode[3]==’0\n’ or explode[3]==’400\n’ or explode[3]==’500′ or explode[3]==’404′:
data3 = ‘网站:%s在%s已经恢复%s’ % (url_1[0], now_time,url)
stat3 = email_send(msgto, data3, recovery_title)
resole = stat3.sendemail()
print(resole)
print(data3 + ‘邮件已经发送’)
else:
print(‘最后一次记录为其他值:%s’%(explode[3])+’—–‘)
else:
print(‘文件不存在’)
data2 = ‘\n’ + url_1[0] + ‘—-‘ + url + ‘—–‘ + time.strftime(“%h:%m:%s”, time.localtime(time.time())) + ‘——-‘ + code
print(‘data2数据写入成功:’ + data2)
file = open(filename, ‘a’, encoding=’utf-8′)
file.write(data2)
file.close
# bart = student(data2,59)
# bart.print_score()
if __name__ == “__main__”:
type = “监控通知-测试” + time.strftime(“%y-%m-%d %h:%m:%s”, time.localtime(time.time()))
data1=[‘公司门户—www.luoan.com.cn’,’公司平台—yun.luoan.com.cn’]
#script(data1,type)
#中心层面的网站清单
file=open(‘d:\python\site_moniter\zhongxin.txt’)
data2=[]
while 1:
line2 =file.readline()
print(line2)
if not line2:
break
data2.append(line2[0:-1])
#data2=[‘www.luoan.com.cn’,’yun.luoan.com.cn’,’www.qq.com’]
print(data2)
title=”监控通知-中心”+ time.strftime(“%y-%m-%d %h:%m:%s”,time.localtime(time.time()))
script(data2,title)
总结
利用python自动监控网站并发送邮件告警的方法到这就基本结束了,希望对大家的学习工作能有所帮助。
更多python自动监控网站并发送邮件告警相关文章请关注php中文网!