首页 > 技术文章 > zabbix脚本发送邮件告警

shipment 2020-07-19 14:05 原文

zabbix邮箱告警

环境说明:

zabbix服务端 192.168.32.136
zabbix客户端 192.168.32.125

Zabbix监控服务端、客户端都已经部署完成,被监控主机已经添加,Zabiix运行正常。

邮件报警有两种情况:

1、Zabbix服务端只是单纯的发送报警邮件到指定邮箱,发送报警邮件的这个邮箱账号是Zabbix服务端的本地邮箱账号user@hostname(例如:root@localhost.localdomain),只能发送,不能接收外部邮件。

2、使用一个可以在互联网上正常收发邮件的邮箱账号(例如:***@qq.com),通过在Zabbix服务端中设置,使其能够发送报警邮件到指定邮箱。

内部使用第三方邮箱发送邮箱告警

#服务端安装mailx
[root@localhost ~]# yum -y install mailx

[root@localhost ~]# vim /etc/mail.rc
#添加一下内容
set from=1197691518@qq.com
set smtp="smtp.qq.com"
set smtp-auth-user=1197691518@qq.com
set smtp-auth-password=yiyyvgswccakfhaj
set smtp-auth=login
[root@localhost ~]# systemctl restart postfix

#发送一封测试邮件,发件人应当是1197691518@qq.com
[root@localhost ~]# echo 'hello' | mail -s 'test mail' even1197@sina.com

zabbix使用第三方邮箱发送告警

这里使用模板中的监控项Checksum of /etc/passwd

创建





触发并查看是否收到邮件

[root@client ~]# useradd zhangsan
[root@client ~]# id zhangsan 
uid=1002(zhangsan) gid=1002(zhangsan) groups=1002(zhangsan)


通过脚本使用第三方邮箱发送邮箱告警

#在服务端配置脚本
[root@client ~]# vim /usr/local/etc/zabbix_server.conf
//找到警告脚本这一行
# AlertScriptsPath=${datadir}/zabbix/alertscripts
//复制更改为
AlertScriptsPath=/usr/local/etc/zabbix/alertscripts

[root@localhost ~]# mkdir -p /usr/local/etc/zabbix/alertscripts

[root@localhost ~]# cd /usr/local/etc/zabbix/alertscripts/
[root@localhost /usr/local/etc/zabbix/alertscripts]# touch sendmail.sh
[root@localhost /usr/local/etc/zabbix/alertscripts]# chmod +x sendmail.sh 
[root@localhost /usr/local/etc/zabbix/alertscripts]# ls
sendmail.sh
[root@localhost /usr/local/etc/zabbix/alertscripts]# chown -R zabbix.zabbix /usr/local/etc/zabbix

[root@localhost /usr/local/etc/zabbix/alertscripts]# vim sendmail.sh
#!/bin/sh
message=$(echo -e $1 | tr "\r\n" "\n")
subject=$(echo -e $2 | tr "\r\n" "\n")
sendto=
echo "$message" | /usr/bin/mail -s "$subject" $3

添加媒介

使用脚本媒介

在动作里修改发送方式为刚刚编辑的脚本

触发

查看收件箱

推荐阅读