zabbix_sendmail.py
来源:互联网 发布:大数据在审计中的应用 编辑:程序博客网 时间:2024/06/06 08:29
#!/usr/bin/python
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
import os
import argparse
import logging
import datetime
mail_host = 'smtp.163.com'
mail_user = 'monitor_itnihao'
mail_pass = '123456'
mail_postfix = '163.com'
#mail_host='smtp.qq.com'
#mail_user='itnihao'
#mail_pass='123456'
#mail_postfix='qq.com'
def send_mail(mail_to,subject,content):
me = mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = me
msg['to'] = mail_to
global sendstatus
global senderr
try:
smtp = smtplib.SMTP()
smtp.connect(mail_host)
smtp.login(mail_user,mail_pass)
smtp.sendmail(me,mail_to,msg.as_string())
smtp.close()
print 'send ok'
sendstatus = True
except Exception,e:
senderr=str(e)
print senderr
sendstatus = False
def logwrite(sendstatus,mail_to,content):
logpath='/var/log/zabbix/alert'
if not sendstatus:
content = senderr
if not os.path.isdir(logpath):
os.makedirs(logpath)
t=datetime.datetime.now()
daytime=t.strftime('%Y-%m-%d')
daylogfile=logpath+'/'+str(daytime)+'.log'
logging.basicConfig(filename=daylogfile,level=logging.DEBUG)
os.system('chown zabbix.zabbix {0}'.format(daylogfile))
logging.info('*'*130)
logging.debug(str(t)+' mail send to {0},content is :\n {1}'.format(mail_to,content))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Send mail to user for zabbix alerting')
parser.add_argument('mail_to',action="store", help='The address of the E-mail that send to user ')
parser.add_argument('subject',action="store", help='The subject of the E-mail')
parser.add_argument('content',action="store", help='The content of the E-mail')
args = parser.parse_args()
mail_to=args.mail_to
subject=args.subject
content=args.content
send_mail(mail_to,subject,content)
logwrite(sendstatus,mail_to,content)
0 0
- zabbix_sendmail.py
- py
- py
- py
- py
- py
- py
- py
- Py
- bin2hex.py && hex2bin.py
- web.py (url.py)
- [py]py存放家具
- Xctf之调皮的py-py-py
- dir_size.py
- sendEmail.py
- Html.py
- web.py
- mail.py
- 编译型与解释型、动态语言与静态语言、强类型语言与弱类型语言的区别
- UVA - 10613(Storage Keepers(最优解不可组合))
- 【数据结构与算法】【查找】基本概念
- linux 内核移植和根文件系统的制作
- 网络流
- zabbix_sendmail.py
- Android-Universal-Image-Loader 图片加载库 详细分析
- C++隐式的类类型转换和类型转换运算符
- py2exe使用教程(六)——关于压缩文件
- Date()日期创建
- 织梦DedeCMS增加自定义文档属性四部曲
- jQuery中join方法
- android 根据原理分析ListView滑动checkbox状态被清除 不保存
- nodejs扩展log4js正则匹配区分文件(方案一)