环境
名称 | 主机名 | IP |
---|---|---|
服务端 | wangyitong | 192.168.232.128 |
客户端 | wyt3 | 192.168.232.132 |
安装软件包
[root@wangyitong ~]# yum -y install mailx //服务端安装
[root@wyt3 ~]# yum -y install mailx //客服端安装
测试是否收到邮件
添加媒介
添加动作
触发
[root@wyt3 ~]# echo 'fjdlajf' >> /tmp/hello
通过脚本用系统账户发邮件告警
服务端创建脚本
[root@wangyitong ~]# cd /usr/local/etc
[root@wangyitong etc]# vim zabbix_server.conf
AlertScriptsPath=/usr/local/etc/zabbix/alertscripts //添加这一行
[root@wangyitong etc]# mkdir -p /usr/local/etc/zabbix/alertscripts //创建脚本目录
[root@wangyitong etc]# ls
zabbix zabbix_agentd.conf.d zabbix_server.conf.d
zabbix_agentd.conf zabbix_server.conf
[root@wangyitong etc]# cd zabbix/alertscripts/
[root@wangyitong alertscripts]# touch sendmail.sh //创建脚本
[root@wangyitong alertscripts]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 20 18:04 sendmail.sh
[root@wangyitong alertscripts]# chmod +x sendmail.sh //添加执行权限
[root@wangyitong alertscripts]# ll
total 0
-rwxr-xr-x 1 root root 0 Jul 20 18:04 sendmail.sh
写脚本
[root@wangyitong alertscripts]# vim sendmail.sh
#!/bin/bash
SUBJECT=$(echo -e $2 |tr "\r\n" "\n")
MESSAGE=$(echo -e $1 |tr "\r\n" "\n")
echo "$message" | /usr/bin/mail -s "$subject" $3
[root@wangyitong alertscripts]# ./sendmail.sh "hi ni \r\nhi" "123" 1256765524@qq.com //执行脚本
[root@wangyitong alertscripts]# cd /usr/local/etc/
[root@wangyitong etc]# chown -R zabbix.zabbix zabbix //修改属组
[root@wangyitong etc]# ll zabbix
total 0
drwxr-xr-x 2 zabbix zabbix 25 Jul 20 18:24 alertscripts
web界面配置发邮件告警
添加媒介
添加动作
触发
[root@wyt3 ~]# echo 'fjdla' >> /tmp/hello
通过脚本用第三方邮箱发邮件告警
[root@wangyitong ~]# vim /etc/mail.rc
set from=1256765524@qq.com
set smtp="smtp.qq.com"
set smtp-auth-user=1256765524@qq.com
set smtp-auth-password=rqbjoepukrurggdh
set smtp-auth=login
[root@wangyitong ~]# su - zabbix -s /bin/bash
Last login: Mon Jul 20 20:09:00 CST 2020 on pts/1
su: warning: cannot change directory to /home/zabbix: No such file or directory
-bash-4.2$ /usr/local/etc/zabbix/alertscripts/sendmail.sh aa bb 15387089129@189.cn