1、安装邮件发送工具mailx
yum -y install mailx
2、查看版本
[root@zabbix-100-101 ~]# mailx -V
12.5 7/5/10
3、配置maix
vi /etc/mail.rc
set from=1111@qq.com
set smtp=smtp.exmail.qq.com
set smtp-auth-user=1111@qq.com
set smtp-auth-password=22222222
set smtp-auth=login
4、测试
echo "zabbix test mail" |mail -s "zabbix" xxx@139.com
5、进入该目录:AlertScriptsPath=/usr/lib/zabbix/alertscripts,并创建mail.py并赋予+x权限。为什么我们会在这个路径下面创建这个脚本呢,这是在我们的zabbix_server.conf文件中配置的AlertScriptsPath=/usr/lib/zabbix/alertscripts
[root@zabbix-100-101 alertscripts]# cat mail.py
#!/usr/bin/env python
#coding: utf-8
import smtplib
import json
import sys
import time
from email.mime.text import MIMEText
reload(sys)
now_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
sys.setdefaultencoding('utf-8')
#HOST = "smtp.qq.com"
HOST = "smtp.exmail.qq.com"
SUBJECT = sys.argv[2]
TO = sys.argv[1]
FROM = "rongkang.wu@ptthink.com"
#FROM = "cnzabbix@ptmind.com"
msg = MIMEText("""
<html>
<head>
</head>
<body>
<table width="800" border="1" cellspacing="0" cellpadding="4">
<tr>
<th bgcolor="#00FFFF" height="1" colspan="4" align="center" style="font-size:15px">中国区告警</th>
</tr>
""" + sys.argv[3] + """
<tr>
<td width="100px" style="font-size:15px" nowrap>发生时间</td>
<td style="font-size:15px">""" + now_time + """</td>
</tr>
</table>
</body>
</html>""","html","utf-8")
msg['Subject'] = sys.argv[2]
msg['From']=FROM
msg['To']=TO
try:
server = smtplib.SMTP()
# server = smtplib.SMTP('localhost')
server.connect(HOST,"25")
server.starttls()
server.login("rongkang.wu@ptthink.com","Password2468")
server.sendmail(FROM, TO, msg.as_string())
server.quit()
print "邮件发送成功!"
except Exception, e:
print "失败:"+str(e)
上面的这个三个参数是接收从web页面中传递过来的,1:收件人邮箱地址;2:邮件标题;3:邮件内容
chown zabbix.zabbix mail.py
chmod +x mail.py
6、创建告警媒介
7、创建用户报警邮件
8、创建动作
9、测试
systemctl stop zabbix-agent
systemctl start zabbix-agent
“动作”处“操作”代码
默认标题:
中国 {HOST.NAME1}: {TRIGGER.NAME}: {TRIGGER.SEVERITY}
消息内容
<tr>
<td width="100px" style="font-size:15px" nowrap >告警区域</td>
<td style="font-size:15px">中国</td>
</tr>
<tr>
<td width="100px" style="font-size:15px" nowrap>主机名称</td>
<td style="font-size:15px">{HOST.NAME1}</td>
</tr>
<tr>
<td width="100px" style="font-size:15px" nowrap>告警项目</td>
<td style="font-size:15px">{TRIGGER.NAME}</td>
</tr>
<tr>
<td width="100px" style="font-size:15px" nowrap>告警级别</td>
<td bgcolor=red style="font-size:15px" > {TRIGGER.SEVERITY}</td>
</tr>
<tr>
<td width="100px" style="font-size:15px" nowrap>告警状态</td>
<td bgcolor=red style="font-size:15px" > {TRIGGER.STATUS}</td>
</tr>
<tr>
<td width="100px" style="font-size:15px" nowrap>详细内容</td>
<td style="font-size:15px"> {ITEM.VALUE1}</td>
</tr>
“动作”处“恢复”代码
默认标题
中国 {HOST.NAME1}: {TRIGGER.NAME} :{TRIGGER.STATUS} 已恢复
消息内容
</tr>
<tr>
<td width="100px" style="font-size:15px" nowrap>告警区域</td>
<td style="font-size:15px">中国</td>
</tr>
<tr>
<td width="100px" style="font-size:15px" nowrap>主机名称</td>
<td style="font-size:15px">{HOST.NAME1}</td>
</tr>
<tr>
<td width="100px" style="font-size:15px" nowrap>告警项目</td>
<td style="font-size:15px">{TRIGGER.NAME}</td>
</tr>
<tr>
<td width="100px" style="font-size:15px" nowrap>告警状态</td>
<td bgcolor=green style="font-size:15px"> {TRIGGER.STATUS}</td>
</tr>
<tr>
<td width="100px" style="font-size:15px" nowrap>详细内容</td>
<td style="font-size:15px"> {ITEM.VALUE1}</td>
</tr>