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

首頁 > 編程 > Python > 正文

python監控文件并且發送告警郵件

2020-02-15 21:56:41
字體:
來源:轉載
供稿:網友

本文實例為大家分享了python監控文件并發送郵件的具體代碼,供大家參考,具體內容如下

一、配置文件

import time,datetime  TODAY = time.time() TIME_PATH = str(TODAY.year) + "/" + str(TODAY.month) + "/" + str(datetime.datetime.now().date())  MONITOR_CONFIG = {  "monitor_file":[   {"key":"py_distribute-datacollect","path":"/home/vagrant/py_distribute/data/" + TIME_PATH + "_error.txt","max_size":100},  ],  "send_account":"xxxx@qq.com",  "license_code":"feruwfpsiwkuibge", # 授權碼  "rec_account":["xxxx@qq.com"],  "host":"smtp.qq.com",  "port":465,  "sleep_time":60, } 

二、監控

#-*- encoding: utf8 -*- # 騰訊郵箱授權碼 # feruwfpsiwkuibge  import smtplib import logging import time import os from email.mime.text import MIMEText from monitor_config import MONITOR_CONFIG  FORMAT = '[%(asctime)-15s] %(message)s' logging.basicConfig(filename = "monitor.txt", level = logging.DEBUG, filemode = "a", format=FORMAT)  def get_file_size(file_name):  if os.path.exists(file_name):   bytes_size = float(os.path.getsize(file_name))   kb = bytes_size/1024   mb = kb/1024   return mb  return 0  def send_email(file_name,key):  msg = MIMEText(file_name+"文件超過限制,可能存在異常,請處理。項目為:"+key)  msg = [key]  msg["From"]= MONITOR_CONFIG["send_account"]  msg["To"] = MONITOR_CONFIG["rec_account"]  try:   s = smtplib.SMTP_SSL(MONITOR_CONFIG["host"],MONITOR_CONFIG["port"])   s.login(MONITOR_CONFIG["send_account"],MONITOR_CONFIG["license_code"])   s.sendmail(MONITOR_CONFIG["send_account"],MONITOR_CONFIG["rec_account"],msg.as_string())   s.quit()   logging.info(file_name + "警告發送成功")  except Exception as e:   logging.exception(e)  # check while True:  for file in MONITOR_CONFIG["monitor_file"]:   file_size = get_file_size(file["path"])   if file_size > file["max_size"]:    send_email(file["path"],file["key"])  logging.info("檢查完畢")  time.sleep(MONITOR_CONFIG["sleep_time"]) 

三、需在QQ郵箱設置開啟POP3/SMTP服務

四、參考

Python使用QQ郵箱發送Email的方法實例

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安西县| 霍州市| 隆尧县| 株洲县| 石首市| 固原市| 泰安市| 四会市| 平谷区| 东城区| 井陉县| 晋宁县| 隆子县| 蕉岭县| 伊金霍洛旗| 盐津县| 宣恩县| 新巴尔虎左旗| 安平县| 宁强县| 电白县| 汉源县| 新巴尔虎右旗| 墨竹工卡县| 内黄县| 运城市| 即墨市| 措勤县| 柳河县| 平远县| 师宗县| 榆中县| 高陵县| 那曲县| 甘孜| 都安| 三原县| 凤阳县| 灵寿县| 博罗县| 娱乐|