zabbix邮箱报警配置(二)

本文详细介绍了如何使用Zabbix配置自定义监控脚本,监测httpd服务状态,并通过Python脚本检查error_log日志,包括设置ACL权限、安装Python3、配置zabbix_agentd和创建触发器。

自定义监控进程

以httpd服务为例,在客户端中安装httpd

[root@hehe ~]# dnf -y install httpd
[root@hehe ~]# systemctl restart httpd.service 
[root@hehe ~]# systemctl enable httpd.service 
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@hehe ~]# 
[root@hehe ~]#  ps -ef | grep httpd | grep -v grep | wc -l
5
[root@hehe ~]# 

新建脚本存放目录

[root@hehe ~]# mkdir /haha
[root@hehe ~]# cd /haha/
[root@hehe haha]# vim hehe.sh
#!/bin/bash 
count=$(ps -ef | grep -Ev "grep|$0" | grep -c httpd)
if [ $count -eq 0 ];then
echo '1'
else
echo '0'
fi
[root@hehe haha]# chmod +x hehe.sh 
[root@hehe haha]# chown zabbix.zabbix /haha/ hehe.sh 
[root@hehe haha]# ll
总用量 4
-rwxr-xr-x. 1 zabbix zabbix 118 9月   6 01:36 hehe.sh
[root@hehe haha]# 

脚本–0是httpd服务开启,1为关闭

[root@hehe haha]# ./hehe.sh 
0
[root@hehe haha]# systemctl stop httpd.service 
[root@hehe haha]# ./hehe.sh 
1

修改zabbix_agentd.conf文件

[root@hehe haha]# vim /etc/zabbix/zabbix_agentd.conf 
# Default:
 UserParameter=loginusers,who | wc -l
 UserParameter=check_httpd,/bin/bash /haha/hehe.sh
[root@hehe haha]# systemctl restart zabbix-agent.service 
[root@hehe haha]# systemctl enable zabbix-agent.service 
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-agent.service → /usr/lib/systemd/system/zabbix-agent.service.
[root@hehe haha]# 

zabbix 服务端进行测试脚本

[root@hehe haha]# dnf -y install zabbix-get
[root@haha ~]# zabbix_get -s  192.168.40.100 -k check_httpd
0

zabbix web平台配置
新建监控项
在这里插入图片描述
在这里插入图片描述
配置触发器
在这里插入图片描述
在这里插入图片描述
测试–关闭httpd服务,测试告警信息
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

自定义监控日志

下载log.py来协助我们进行测试,以httpd服务为例
将log.py上传到/etc/zabbix/script/目录下,然后给执行权限,修改所有者和所属组为zabbix

# log.py
### 作用:检查日志文件中是否有指定的关键字
#### 第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
#### 第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)
#### 第三个参数为搜索关键字,默认为 Error

[root@hehe haha]# ls
hehe.sh  log.py
[root@hehe haha]# chmod  +x log.py 
[root@hehe haha]# chown zabbix.zabbix log.py 
[root@hehe haha]# 

httpd服务的日志文件在/var/log/httpd/目录下,首先我们需要给这个目录设置一个ACL权限,让zabbix用户有权限去访问该目录

[root@hehe haha]# cd /var/log/httpd/
[root@hehe httpd]# ls
access_log  error_log
[root@hehe httpd]# cd ..
[root@hehe log]# setfacl -m u:zabbix:r-x httpd/
[root@hehe log]# 

下载python3来执行log.py脚本

[root@hehe ~]# dnf -y install python3

修改zabbix_agentd.conf文件,并重启服务

[root@hehe ~]# vim /etc/zabbix/zabbix_agentd.conf
# Default:
 UserParameter=loginusers,who | wc -l
 UserParameter=check_httpd,/bin/bash /haha/hehe.sh
 UserParameter=check_logs[*],/usr/bin/python3 /haha/log.py $1 $2 $3

[root@hehe ~]# systemctl restart zabbix-agent.service 

测试脚本

[root@hehe ~]# cd /haha/
[root@hehe haha]# python3 log.py /var/log/httpd/error_log 
0
[root@hehe haha]# echo 'Error' >>  /var/log/httpd/error_log 
[root@hehe haha]# python3 log.py /var/log/httpd/error_log 
1

0为没有Error日志信息,1为有Error日志信息

测试完成后将写入的Error内容删除,而且因文件/tmp/logseek属于root账户,在web端写入写不进去,所以删除。

[root@hehe]# cd /tmp/
[root@hehe tmp]# rm -rf logseek 

配置监控项
在这里插入图片描述
在这里插入图片描述
创建触发器
在这里插入图片描述
在这里插入图片描述
测试,echo Error >> /var/log/httpd/error_log

[root@hehe ~]# echo 'Error' >>  /var/log/httpd/error_log 

在这里插入图片描述
在这里插入图片描述

### Zabbix 邮件通知配置 #### 修改服务器配置文件 为了使Zabbix能够发送邮件通知,需编辑`zabbix_server.conf`文件来指定报警脚本路径以及外部脚本路径。这一步骤确保了当触发条件满足时,可以执行相应的脚本来实现邮件的通知功能[^2]。 ```bash vim /etc/zabbix/zabbix_server.conf AlertScriptsPath=/usr/lib/zabbix/alertscripts # 报警脚本路径 ExternalScripts=/usr/lib/zabbix/externalscripts # 外部脚本路径 ``` #### 创建邮件发送脚本 接着,在上述定义好的目录下创建用于实际发送电子邮件的具体Shell或其他编程语言编写的程序。此脚本应接受至少两个参数:收件人的地址和消息主体内容,并利用SMTP协议完成邮件投递操作。 对于简单的场景可以直接编写shell命令调用mail工具: ```bash #!/bin/bash # $1 is recipient email address, $2 is subject of the message and $3 contains body text. echo "$3" | mail -s "$2" "$1" ``` 更复杂的情况下可能涉及到Python等高级语言开发更为灵活可靠的解决方案。 #### 设置媒体类型 登录到Web界面下的管理板块中的“Media types”,点击右上角的“Create media type”。选择合适的名称并设定Type为Script;在Script name处填入之前所建立的那个自定义脚本的名字(不带扩展名)。保存更改以便稍后关联给具体的用户账号使用[^1]。 #### 添加动作(Action) 转至Configuration -> Actions菜单项,通过新建Action的方式定义何时何地向谁发出何种形式的消息提醒。这里要特别注意的是Conditions部分用来限定触发器的状态变化情况,Operations里面则指定了接收者及其联系方式——即前面提到过的media type设置。 #### 主机与代理通信准备 确认目标机器上的防火墙策略允许必要的网络连接请求进出,同时禁用SELinux以免其干扰正常的数据交换过程。另外还需保证各节点间可通过预设域名相互解析访问,通常做法是在每台计算机本地hosts表里添加对应记录[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值