zabbix邮箱告警配置
环境准备
服务 | ip | 主机名 |
---|---|---|
服务端 | 192.168.87.129 | server |
客户端 | 192.168.87.133 | agent |
1.关闭防火墙和selinux |
服务端
[root@server ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@server ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
客户端
[root@agent ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@agent ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
2.设置主机名
[root@server ~]# hostnamectl set-hostname server.example.com
[root@server ~]# bash
[root@server ~]# hostname
server.example.com
[root@agent ~]# hostnamectl set-hostname agent.example.com
[root@agent ~]# bash
[root@agent ~]# hostname
agent.example.com
3.修改配置文件/etc/hosts
[root@server ~]# vim /etc/hosts
[root@server ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.87.129 server.example.com server
192.168.87.133 agent.example.com agent
[root@agent yum.repos.d]# vim /etc/hosts
[root@agent yum.repos.d]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.87.129 server.example.com server
192.168.87.133 agent.example.com agent
3、安装postfix软件
[root@server ~]# yum install -y postfix
4、修改postfix的主配置文件,并重启服务和设置下次启动生效
[root@server ~]# vim /etc/postfix/main.cf
mydestination = $myhostname, localhost.$mydomain, localhost,$mydomain
(找到配置信息在后添加$mydomain)
[root@server ~]# systemctl restart postfix.service
[root@server ~]# systemctl enable postfix.service
Created symlink /etc/systemd/system/multi-user.target.wants/postfix.service → /usr/lib/systemd/system/postfix.service.
5.创建用户生成邮箱安装
[root@server ~]# useradd zhengbichao
[root@server ~]# ls /home/
zhengbichao
[root@server ~]# cd /var/spool/mail/
[root@server mail]# ll
total 0
-rw-rw---- 1 zhengbichao mail 0 Sep 5 11:18 zhengbichao
[root@server ~]# yum install -y mailx
一、第三方邮箱
1.注册163邮箱
2.开启SMTP服务和POP3服务,会生成一个授权码,需要记住这个授权码
3、修改报警媒介类型–email
4.配置动作
测试效果一
去到163邮箱查看
测试效果二
如图测试agent连接超过七个时是否还会发送邮箱
[root@agent ~]# who | wc -l
8
去到163邮箱查看
当连接数恢复到小于七个时
[root@agent ~]# who | wc -l
2
查看163邮箱
二、第三方邮箱+脚本
1.改mailx配置文件
[root@server ~]# vim /etc/mail.rc
在文件末尾添加
set from=zbc2992554169@163.com
set smtp=smtp.163.com
set smtp-auth-user=zbc2992554169@163.com
set smtp-auth-password=IZJKSIFSYZUCHZHR
set smtp-auth=login
set ssl-verify=ignore
[root@server ~]# id zabbix
uid=993(zabbix) gid=990(zabbix) groups=990(zabbix)
[root@server ~]# chown -R zabbix.zabbix /etc/mail.rc
2.在zabbix服务端写邮件发送脚本
[root@server alertscripts]# vim mail-send.sh
[root@server alertscripts]# cat mail-send.sh
#!/bin/bash
messages=`echo $3 | tr '\r\n' '\n'`
subject=`echo $2 | tr '\r\n' '\n'`
echo "${messages}" | mailx -s "${subject}" $1
[root@server alertscripts]# chmod +x mail-send.sh
[root@server alertscripts]# chown -R zabbix.zabbix mail-send.sh
[root@server alertscripts]# ll
total 4
-rwxr-xr-x 1 zabbix zabbix 130 Sep 5 12:40 mail-send.sh
3.测试邮箱能否接收邮件
[root@server alertscripts]# ./mail-send.sh zbc2992554169@163.com "zabbix" "hello "
查看163邮箱
4.创建报警媒介类型
测试
[root@agent ~]# who | wc -l
8
查看163邮箱
当用户连接少于七个时检验
[root@agent ~]# who | wc -l
2