基于Python脚本实现邮件报警功能
作者:Mr_Wmn 时间:2023-02-04 17:16:41
使用了smtplib等第三方库,进行发送邮件,完成邮件报警功能
如下是实例 :
#!/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()
#这里监控文件夹下的文件数,超出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 ====")
#添加发送邮件的信息
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方法,统计文件夹下的文件数
def cntFiles(in_directory, ext):
stage = len(glob.glob1(in_directory,"*." + ext))
return stage
#创建邮件头
def createMsg(fromAddr, toAddr, subject, body):
msg = MIMEText(body)
msg['Subject'] = subject
msg['To'] = toAddr
msg['From'] = fromAddr
msg['Date'] = formatdate()
return msg
#发送邮件
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上做计划任务,把指令添加进计划任务中:
Errymsfileemail.py -m zhangsan@gongsi.cn -t "[ERROR/$HOST] File Stacking Alarm"
来源:https://www.cnblogs.com/weijiazheng/p/12910061.html
标签:Python,脚本,邮件,报警
0
投稿
猜你喜欢
零基础学习Python爬虫
2021-12-14 12:29:37
mysql时区查看与设置方法
2024-01-20 02:01:46
asp动态页面生成html页面
2008-10-24 09:03:00
SQLite3中文编码 Python的实现
2023-02-03 13:21:38
python中实现字符串翻转的方法
2021-06-08 04:27:59
详解Python requests模块
2021-12-31 21:55:12
如何学习Python time模块
2023-07-30 17:14:59
python的图形用户界面介绍
2023-04-08 19:55:46
Pytorch 之修改Tensor部分值方式
2023-04-11 06:45:12
Python range、enumerate和zip函数用法详解
2021-06-29 22:24:06
一文带你安装opencv与常用库(保姆级教程)
2021-03-11 12:21:19
分析MySQL抛出异常的几种常见解决方式
2024-01-16 14:00:59
javascript 版 Bad Apple 字符动画
2010-01-28 12:19:00
php5.4传引用时报错问题分析
2023-11-14 15:03:17
tensorflow 获取checkpoint中的变量列表实例
2021-06-21 05:47:17
Python编写带选项的命令行程序方法
2023-11-18 20:47:35
Python使用re模块正则提取字符串中括号内的内容示例
2022-06-15 14:20:04
SQL中JOIN和UNION区别、用法及示例介绍
2012-08-21 10:47:22
Python输出\\u编码将其转换成中文的实例
2023-03-30 22:58:02
我的页面制作方法
2008-03-23 13:51:00