zabbix默认邮件报警会将各个报警接收人单独发送邮件,为了使邮件能以群发的方式统一一封邮件发送所有接收人,需要改成脚本的形式:
sendemail.py:
#!/usr/bin/python
import smtplib,sys
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
import os
def send_mail(mail_to, mail_from, subject, text, files, server="localhost"):
assert type(mail_to) == list
assert type(files) == list
msg = MIMEMultipart()
msg['From'] = mail_from
msg['To'] = COMMASPACE.join(mail_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text, _charset='utf-8'))
smtp = smtplib.SMTP(server)
smtp.sendmail(mail_from, mail_to, msg.as_string())
smtp.close()
to_list_1 = ['*']
if __name__ == '__main__':
f = open("/tmp/test.log","a+")
f.write("start")
title = str(sys.argv[2])
cont = str(sys.argv[3])
f.write(title+cont)
send_mail(to_list_1, 'zabbix@phicomm.com', title, cont, [])
f.close()