国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

利用Python自動監(jiān)控網站并發(fā)送郵件告警的方法

2019-11-25 16:36:18
字體:
來源:轉載
供稿:網友

前言

因為有一些網站需要每日檢查是否有問題,所以需要一個報警監(jiān)控的機制,這個需要你指定你發(fā)送的郵箱和你接收的郵箱,就可以做到對網站自動監(jiān)控了。

這里用的是python3.5

需要安裝的插件:

      1、smtplib:發(fā)郵件需要用到

      2、pycurl:訪問網站時會需要用到

      3、linecache:在讀取txt網站清單時需要用到

具體思路:

python程序從txt里面批量讀取到網站的信息,通過Curl.py模擬瀏覽器去訪問網站,并且把訪問的結果寫入到以自己的網站名稱-日期.txt格式的文件中記錄;有幾種情況:

1、如果發(fā)現打不開了,直接發(fā)郵件提示網站已經打不開

2、發(fā)現可以打開,讀取文件中上一次訪問的情況(讀取txt文件最后一行),

    1)如果發(fā)現上一次是打不開的,發(fā)郵件提醒網站已經恢復了

    2)如果發(fā)現上一次是打得開的(200的返回碼),只是記錄網站訪問的日志就可以了

總共4個文件

Email.py是郵件類,主要用來發(fā)郵件的時候調用,這里需要按照你的情況改成你的郵箱(msg['From']),郵箱服務器地址(SMTP地址),和你的郵箱密碼(SMTP.login)

Email.py

#!/usr/bin/python#-*- coding:utf-8 -*-import sysimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartclass 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('郵件發(fā)送成功')  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 主要用來執(zhí)行模擬瀏覽器訪問網站并返回結果的文件

#!/usr/bin/python#-*- coding:utf-8 -*-import sysimport pycurlclass 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 這個文件為主程序,主要執(zhí)行調用上面的函數,讀取txt文件中的網站清單,如果網站打不開就發(fā)郵件出來告警

需要注意:

      1、把xxxx@xxxx.com改成你自己的郵箱,

      2、把文件路徑改成自己的真實路徑

#!/usr/bin/python#-*- coding:utf-8 -*-import pycurlimport osimport sysimport linecacheimport time #引入事件類,用來獲取系統當前時間#from ceshi import Studentfrom Email import Email_sendfrom 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 = "監(jiān)控通知----%s url:%s" % (url_1[0], url) + "在" + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())) + "已經恢復"  down_title = "監(jiān)控通知----%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()   #判斷網站如果掛了就發(fā)郵件   stat3 = Email_send(msgTo, data3, down_title)   resole=stat3.sendEmail()   print(resole)   print(data3 + '郵件已經發(fā)送')  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):    #讀取文件最后一行,為了讀取出來最后一次的狀態(tài)值    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 + '郵件已經發(fā)送')     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 = "監(jiān)控通知-測試" + 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="監(jiān)控通知-中心"+ time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())) script(data2,title)

總結

利用Python自動監(jiān)控網站并發(fā)送郵件告警的方法到這就基本結束了,希望對大家的學習工作能有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 临汾市| 冷水江市| 瓮安县| 慈溪市| 休宁县| 禹州市| 新化县| 湛江市| 工布江达县| 衢州市| 沧源| 双峰县| 志丹县| 苏尼特右旗| 霍州市| 昌都县| 高要市| 武汉市| 潜山县| 舞阳县| 贵德县| 孝昌县| 阳城县| 靖边县| 同心县| 辰溪县| 华阴市| 新巴尔虎右旗| 宁强县| 四平市| 鹤峰县| 肥西县| 微山县| 巨野县| 淳安县| 武强县| 会宁县| 宜都市| 区。| 修文县| 定兴县|