Zabbix监控之邮件告警python脚本

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值