基于Python腳本實現(xiàn)郵件報警功能
使用了smtplib等第三方庫,進行發(fā)送郵件,完成郵件報警功能
如下是實例 :
#!/usr/bin/python import glob import operator from optparse import OptionParser import smtplib import email.MIMEText as MIMEText import email.Utils.formadate as formatdate msg = "" #主方法 def main(): global options global msg parser = OptionParser(add_help_option=False) parser.add_option("-m", "--mail", dest="mail", type="str", help="email address to send report result (comma separated)") parser.add_option("-t", "--title", dest="title", type="str", help="email title (default:Error File Count)") parser.add_option("-a", "--admin", dest="admin", type="str", help="set sender address. works with -m option") (options, args) = parser.parse_args() #這里監(jiān)控文件夾下的文件數(shù),超出25個文件即報警 datanum = cntFiles("/data/","csv") if (operator.gt(datanum,25)): msg += " Please be alert : \n the number of files under /data/ path is greater than 25 :" msg += "\n ==========================================" msg += "\n The number of files is : " + str(datanum) sendmsg(options,msg) print("==== end ====") #添加發(fā)送郵件的信息 def sendmsg(options,msg): if options.mail: toAddr = options.mail if options.admin: fromAddr = options.admin else: fromAddr = 'zhangsan@neiyou.cn'#這里是公司的公用SMTP郵箱賬號 if options.title: subject = options.title else: subject = 'File Stacking Alarm' msg += "\n ========================================== \n" print( msg) msg = createMsg(fromAddr, toAddr, subject, msg) print( msg) send(fromAddr, toAddr, msg) else: print( msg) #glob方法,統(tǒng)計文件夾下的文件數(shù) def cntFiles(in_directory, ext): stage = len(glob.glob1(in_directory,"*." + ext)) return stage #創(chuàng)建郵件頭 def createMsg(fromAddr, toAddr, subject, body): msg = MIMEText(body) msg['Subject'] = subject msg['To'] = toAddr msg['From'] = fromAddr msg['Date'] = formatdate() return msg #發(fā)送郵件 def send(fromAddr, toAddr, msg): try: #這里添加公司的SMTP郵箱地址 s = smtplib.SMTP('192.168.12.120') s.sendmail(fromAddr, toAddr.split(','), msg.as_string()) s.close() print("SUCCESS: sending email") except smtplib.SMTPException: print("ERROR: sending email") if __name__ == '__main__': main()
linux上做計劃任務(wù),把指令添加進計劃任務(wù)中:
Errymsfileemail.py -m zhangsan@gongsi.cn -t "[ERROR/$HOST] File Stacking Alarm"
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫
這篇文章主要介紹了Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05淺談Tensorflow由于版本問題出現(xiàn)的幾種錯誤及解決方法
今天小編就為大家分享一篇淺談Tensorflow由于版本問題出現(xiàn)的幾種錯誤及解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06python3之讀取redis數(shù)據(jù)帶有‘b’的問題
這篇文章主要介紹了python3之讀取redis數(shù)據(jù)帶有‘b’的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09數(shù)據(jù)清洗--DataFrame中的空值處理方法
今天小編就為大家分享一篇數(shù)據(jù)清洗--DataFrame中的空值處理方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07pyinstaller打包成無控制臺程序時運行出錯(與popen沖突的解決方法)
這篇文章主要介紹了pyinstaller打包成無控制臺程序時運行出錯(與popen沖突的解決方法),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04