python監(jiān)控文件并且發(fā)送告警郵件
本文實(shí)例為大家分享了python監(jiān)控文件并發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
一、配置文件
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", # 授權(quán)碼
"rec_account":["xxxx@qq.com"],
"host":"smtp.qq.com",
"port":465,
"sleep_time":60,
}
二、監(jiān)控
#-*- encoding: utf8 -*-
# 騰訊郵箱授權(quán)碼
# 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+"文件超過限制,可能存在異常,請(qǐng)?zhí)幚?。?xiàng)目為:"+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 + "警告發(fā)送成功")
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郵箱設(shè)置開啟POP3/SMTP服務(wù)

四、參考
Python使用QQ郵箱發(fā)送Email的方法實(shí)例
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用正則匹配實(shí)現(xiàn)抓圖代碼分享
本文給大家分享的是個(gè)人的第一個(gè)作品,使用Python正則匹配實(shí)現(xiàn)抓圖代碼,非常的簡(jiǎn)單實(shí)用,推薦給大家,小伙伴們可以自由擴(kuò)展下。2015-04-04
Tornado路由與Application的實(shí)現(xiàn)
本文主要介紹了Tornado路由與Application的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
python 用遞歸實(shí)現(xiàn)通用爬蟲解析器
這篇文章主要介紹了python 用遞歸實(shí)現(xiàn)通用爬蟲解析器的方法,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
Python查看Tensor尺寸及查看數(shù)據(jù)類型的實(shí)現(xiàn)
這篇文章主要介紹了Python查看Tensor尺寸及查看數(shù)據(jù)類型的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Python中np.random.randint()參數(shù)詳解及用法實(shí)例
numpy.random.randint()函數(shù)不僅可以生成一維隨機(jī)數(shù)組,也可以生成多維度的隨機(jī)數(shù)組,下面這篇文章主要給大家介紹了關(guān)于Python中np.random.randint()參數(shù)詳解及用法的相關(guān)資料,需要的朋友可以參考下2022-09-09
對(duì)python3 urllib包與http包的使用詳解
今天小編就為大家分享一篇對(duì)python3 urllib包與http包的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05

