zabbix监控nginx的状态页面
文章目录
- zabbix监控nginx的状态页面
- 1.环境说明
- 2.所涉及到的知识点
- 3.在nginx主机上安装zabbix_agent
- 4.开启nginx状态显示页面
- 5.进入zabbix的web页面配置主机,监控项,触发器
- 5.1.添加主机
- 5.2.创建监控项
- 5.3.创建触发器
1.环境说明
主机名称 | IP地址 | 充当角色 | 所需软件 |
---|---|---|---|
zabbbix | 192.168.195.130 | zabbix服务端 | zabbix_server、zabbix_agent(需在lamp或lnmp上部署) |
nginx | 192.168.195.133 | nginx、zabbix客户端 | nginx+zabbix_agent |
2.所涉及到的知识点
1.安装部署nginx请阅读源码编译安装部署lnmp
2.安装部署zabbix服务端请阅读监控服务zabbix部署
3.配置邮箱告警和自定义监控请阅读zabbix服务配置邮箱告警(定义媒介、配置动作和zabbix服务自定义监控_碳烤小肥杨…的博客-CSDN博客
3.在nginx主机上安装zabbix_agent
//通过zabbix主机将zabbix源码包发送到nginx主机上
[root@zabbix src]# scp zabbix-6.4.6.tar.gz root@192.168.195.133:/root/
The authenticity of host '192.168.195.133 (192.168.195.133)' can't be established.
ECDSA key fingerprint is SHA256:KtCxYXSsV732izj+QL2uzJDCS2G/X1MOMV+uUkriDno.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.195.133' (ECDSA) to the list of known hosts.
root@192.168.195.133's password:
zabbix-6.4.6.tar.gz 100% 42MB 169.9MB/s 00:00
[root@zabbix src]#
//在客户端安装代理,创建zabbix用户用来运行zabbix
[root@nginx ~]# useradd -r -M -s /sbin/nologin zabbix
//前往nginx主机上查看,并将其解压至/usr/lcoal/目录
[root@nginx ~]# ls
anaconda-ks.cfg nginx-1.24.0.tar.gz php-8.2.11.tar.gz
mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz oniguruma-6.9.4.tar.gz zabbix-6.4.6.tar.gz
[root@nginx ~]# tar xf zabbix-6.4.6.tar.gz -C /usr/local/
[root@nginx ~]# ls /usr/local/ && cd /usr/local/
bin include libexec nginx php8 share
etc lib mysql nginx-1.24.0 php-8.2.11 src
games lib64 mysql-5.7.39-linux-glibc2.12-x86_64 oniguruma-6.9.4 sbin zabbix-6.4.6
[root@nginx local]#
//安装编译安装所需要的软件包
[root@nginx local]# yum -y install gcc gcc-c++ make
//进入zabbix-6.4.6的目录进行编译
[root@nginx local]# cd zabbix-6.4.6/
[root@nginx zabbix-6.4.6]# ./configure --enable-agent
. . .
***********************************************************
* Now run 'make install' *
* *
* Thank you for using Zabbix! *
* <http://www.zabbix.com> *
***********************************************************
最后报这个则表示编译成功,可直接使用make install安装
[root@nginx zabbix-6.4.6]# make install
//修改zabbix客户端的配置文件
[root@nginx zabbix-6.4.6]# cd /usr/local/etc
[root@nginx etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@nginx etc]# vim zabbix_agentd.conf
[root@nginx etc]# grep -A2 '# ServerActive=' zabbix_agentd.conf
# ServerActive=
ServerActive=192.138.195.130 //改为server端的ip
[root@centos2 etc]# grep -A2 '# Server=' zabbix_agentd.conf
# Server=
Server=192.168.195.130 //改为server端的ip
[root@centos2 etc]# grep -A2 '# Hostname=' zabbix_agentd.conf
# Hostname=
Hostname=nginx //修改主机名,必须全局唯一
//设置zabbix_agentd开机自启,将zabbix主机上的service文件传送到nginx主机
[root@zabbix src]# scp /usr/lib/systemd/system/zabbix_agentd.service root@192.168.195.133:/usr/lib/systemd/system/
root@192.168.195.133's password:
zabbix_agentd.service 100% 227 212.3KB/s 00:00
[root@zabbix src]#
[root@nginx ~]# systemctl daemon-reload //重新加载
[root@nginx ~]# systemctl enable --now zabbix_agentd //设置开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix_agentd.service → /usr/lib/systemd/system/zabbix_agentd.service.
[root@nginx ~]# systemctl status zabbix_agentd
● zabbix_agentd.service - zabbix agentd daemon
Loaded: loaded (/usr/lib/systemd/system/zabbix_agentd.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-10-18 22:57:55 CST; 55s ago
Process: 12261 ExecStart=/usr/local/sbin/zabbix_agentd (code=exited, status=0/SUCCESS)
Main PID: 12263 (zabbix_agentd)
Tasks: 6 (limit: 23790)
Memory: 3.1M
CGroup: /system.slice/zabbix_agentd.service
├─12263 /usr/local/sbin/zabbix_agentd
├─12264 /usr/local/sbin/zabbix_agentd: collector [idle 1 sec]
├─12265 /usr/local/sbin/zabbix_agentd: listener #1 [waiting for connection]
├─12266 /usr/local/sbin/zabbix_agentd: listener #2 [waiting for connection]
├─12267 /usr/local/sbin/zabbix_agentd: listener #3 [waiting for connection]
└─12268 /usr/local/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
Oct 18 22:57:55 nginx systemd[1]: Starting zabbix agentd daemon...
Oct 18 22:57:55 nginx systemd[1]: Started zabbix agentd daemon.
//查看10050端口是否存在
[root@nginx ~]# ss -antlp | grep 10050
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:* users:(("zabbix_agentd",pid=12268,fd=7),("zabbix_agentd",pid=12267,fd=7),("zabbix_agentd",pid=12266,fd=7),("zabbix_agentd",pid=12265,fd=7),("zabbix_agentd",pid=12264,fd=7),("zabbix_agentd",pid=12263,fd=7))
4.开启nginx状态显示页面
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf
......
location /status {
stub_status on;
allow 192.168.195.0/24; //放行的ip,要放行本机和zabbix服务端
}
//重启服务后查看nginx状态页面
[root@nginx ~]# nginx -s stop
[root@nginx ~]# nginx
[root@nginx ~]# curl http://192.168.195.133/status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
//为监控nginx状态编写脚本文件
[root@nginx ~]# mkdir /scripts && cd /scripts
[root@nginx scripts]# vim nginx_status.sh
[root@nginx scripts]# chmod +x nginx_status.sh
[root@nginx scripts]# cat nginx_status.sh
#!/bin/bash
if [ $1 == 'reading' ]; then
curl -s 192.168.195.133/status | awk 'NR==4{print $2}'
elif [ $1 == 'writing' ]; then
curl -s 192.168.195.133/status | awk 'NR==4{print $4}'
elif [ $1 == 'waiting' ]; then
curl -s 192.168.195.133/status | awk 'NR==4{print $6}'
fi
[root@nginx scripts]#
//进入配置文件,创建自定义监控任务
[root@nginx ~]# vim /usr/local/etc/zabbix_agentd.conf
[root@nginx ~]# tail -1 /usr/local/etc/zabbix_agentd.conf
UserParameter=check_nginx[*],/scripts/nginx_status.sh $1
//因为我们修改了配置文件,所以需要重启服务,重新读取配置文件内容
[root@nginx ~]# systemctl restart zabbix_agentd.service
//创建自定义监控任务后,我们需要在server端去测试一下是否能接受到被监控端的值
[root@zabbix ~]# zabbix_get -s 192.168.195.133 -k check_nginx['reading']
0
[root@zabbix ~]# zabbix_get -s 192.168.195.133 -k check_nginx['writing']
1
[root@zabbix ~]# zabbix_get -s 192.168.195.133 -k check_nginx['waiting']
0
[root@zabbix ~]# //成功接收到值
主机上的配置完成
5.进入zabbix的web页面配置主机,监控项,触发器
5.1.添加主机
5.2.创建监控项
5.3.创建触发器
另外的writing数值和waiting数值与此相似,数值自己定义