圆月山庄资源网 Design By www.vgjia.com

使用了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"

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
Python脚本,邮件报警

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。