zabbix自定义监控
自定义监控进程
zabbix配置文件有两种:
- 服务器端配置文件(/usr/local/etc/zabbix_server.conf)
- 客户端配置文件(/usr/local/etc/zabbix_agentd.conf)
- zabbix代理配置文件(/usr/local/etc/zabbix_proxy.conf)
服务器端配置文件zabbix_server.conf常用配置参数:
参数 | 作用 |
---|---|
LogFile | 设置服务端日志文件存放路径 |
ListenIP | 设置服务端监听IP |
ListenPort | 设置服务端监听的端口号 |
PidFile | 设置服务端进程号文件存放路径 |
DBHost | 指定zabbix的数据库服务器IP |
DBName | 指定zabbix使用的数据库库名 |
DBUser | 指定zabbix数据库登录用户 |
DBPassword | 指定zabbix数据库登录密码 |
DBPort | 指定zabbix数据库端口号 |
User | 设置zabbix以什么用户的身份运行 |
AlertScriptsPath | 设置告警脚本存放路径 |
ExternalScripts | 外部脚本存放路径 |
客户端配置文件zabbix_agentd.conf常用配置参数:
参数 | 作用 |
---|---|
Server | 指定zabbix服务器的IP或域名 |
ServerActive | 指定zabbix服务器的IP或域名 |
Hostname | 指定本机的主机名,此项必须与web界面配置项一致 |
UnsafeUserParameters | 是否启用自定义监控项,可选值为{1 | 0} |
UserParameter | 指定自定义监控脚本参数 |
LogFile | 设置客户端日志文件存放路径 |
#这个路径就是配置文件存放的路径,这边先下个服务做一个实验。
[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@localhost etc]# dnf -y install httpd
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 3:33:20 ago on Mon Sep 5 21:55:39 2022.
Dependencies resolved.
=========================================================================
Package Arch Version Repo Size
=========================================================================
Installing:
httpd x86_64 2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1
appstream 1.4 M
Installing dependencies:
#然后启动服务
[root@localhost etc]# systemctl start httpd
[root@localhost etc]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost etc]# mkdir scripts
[root@localhost scripts]# vim check_process.sh
[root@localhost scripts]# cat check_process.sh
#!/bin/bash
count=$(ps -ef|grep -Ev "grep|$0" | grep $1| wc -l)
if [ $count -eq 0 ];then
echo '1'
else
echo '0'
fi
#这里给脚本添加执行权限,然后运行测试一下。
[root@localhost scripts]# chmod +x check_process.sh
#这里显示服务还在的时候输出就是0表示服务在正常运行
[root@localhost scripts]# ./check_process.sh httpd
0
[root@localhost scripts]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost scripts]# systemctl stop httpd
#这里我把服务停了他会报1这就可以在服务出现异常的时候在可以给我发送邮件。当然这里还需要配置网页的触发器和警告。
[root@localhost scripts]# ./check_process.sh httpd
1
#再去修改一下配置文件
[root@localhost etc]# vim zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_process[*], /bin/bash /usr/local/etc/scripts/check_process.sh $1 #加这两行配置
#改了配置文件就需要重启一下
[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd
[root@localhost etc]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost etc]#
#然后这里最后在检查一下刚刚写的脚本有没有执行权限。
[root@localhost scripts]# ll
total 4
-rwxr-xr-x 1 root root 129 Sep 6 01:54 check_process.sh
[root@localhost scripts]# cd ..
[root@localhost etc]# ll
total 20
drwxr-xr-x 2 root root 30 Sep 6 01:55 scripts
-rw-r--r-- 1 root root 17033 Sep 6 01:09 zabbix_agentd.conf
drwxr-xr-x. 2 root root 6 Sep 6 01:08 zabbix_agentd.conf.d
[root@localhost etc]#
配置网页的监控
测试网页的监控
这里可以在服务端上检验一下,然后在把客户端的httpd服务停掉。
[root@localhost ~]# zabbix_get -s 192.168.171.135 -k check_process[httpd]
0
[root@localhost ~]#
[root@localhost etc]# systemctl stop httpd
[root@localhost etc]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost etc]#
自定义监控日志
#从外网上拉过来的python脚本,传到脚本目录下然后给他执行权限
[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# ls
log.py scripts zabbix_agentd.conf zabbix_agentd.conf.d
[root@localhost etc]# mv log.py scripts/
[root@localhost etc]# ls
scripts zabbix_agentd.conf zabbix_agentd.conf.d
[root@localhost etc]# cd scripts/
[root@localhost scripts]# ls
check_process.sh log.py
[root@localhost scripts]# ll
total 8
-rwxr-xr-x 1 root root 124 Sep 6 03:03 check_process.sh
-rw-r--r-- 1 root root 1854 Sep 6 03:20 log.py
[root@localhost scripts]#
[root@localhost scripts]# chmod +x log.py
[root@localhost scripts]# ll
total 8
-rwxr-xr-x 1 root root 124 Sep 6 03:03 check_process.sh
-rwxr-xr-x 1 root root 1854 Sep 6 03:20 log.py
[root@localhost scripts]#
[root@localhost httpd]# ll /var/log/
total 904
drwx------ 2 root root 41 Sep 6 01:30 httpd #这里可以看到httpd目录只有root用户有读的权限,所以这里还需要再改一下
#这里需要在下载一个python的包,不然脚本执行不了。
[root@localhost scripts]# dnf -y install python36
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 1:53:14 ago on Tue Sep 6 01:51:47 2022.
Dependencies resolved.
[root@localhost scripts]# ./log.py /var/log/httpd/error_log
0
#这里往那个错误日志里输入一个错误提示就可以看到脚本触发了
[root@localhost httpd]# echo 'Error' >> /var/log/httpd/error_log
[root@localhost scripts]# ./log.py /var/log/httpd/error_log
1
#注意!这里的文件用户必须是zabbix不然写入不了东西,如果不是就直接把文件删了在用服务端测试生成一个。
[root@localhost scripts]# ll /tmp
-rw-rw-r-- 1 zabbix zabbix 4 Sep 6 04:20 logseek
配置网页监控日志
这里配置好直接默认设置创建
测试自定义监控日志
#这里去服务端测试一下
[root@localhost ~]# zabbix_get -s 192.168.171.135 -k check_logs['/var/log/httpd/error_log']
0
[root@localhost ~]#
#这里故意输入一个报错字符,然后等待触发器触发.
[root@localhost etc]# echo 'Error' >> /var/log/httpd/error_log
[root@localhost etc]#