python監(jiān)控文件并且發(fā)送告警郵件
更新時間:2018年06月21日 09:13:42 作者:99zhenzhen
這篇文章主要為大家詳細介紹了python監(jiān)控文件,并且發(fā)送告警郵件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python監(jiān)控文件并發(fā)送郵件的具體代碼,供大家參考,具體內容如下
一、配置文件
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, }
二、監(jiān)控
#-*- 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+"文件超過限制,可能存在異常,請?zhí)幚?。項目為?+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郵箱設置開啟POP3/SMTP服務
四、參考
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Tornado路由與Application的實現(xiàn)
本文主要介紹了Tornado路由與Application的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05Python查看Tensor尺寸及查看數(shù)據(jù)類型的實現(xiàn)
這篇文章主要介紹了Python查看Tensor尺寸及查看數(shù)據(jù)類型的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Python中np.random.randint()參數(shù)詳解及用法實例
numpy.random.randint()函數(shù)不僅可以生成一維隨機數(shù)組,也可以生成多維度的隨機數(shù)組,下面這篇文章主要給大家介紹了關于Python中np.random.randint()參數(shù)詳解及用法的相關資料,需要的朋友可以參考下2022-09-09