3.4:Zabbix 监控 Nginx

目的在于了解Nginx运行过程中的相关状态指标,以及如何编写脚本并自定义监控项,通过zabbix监控Nginx运行状态。

Nginx的监控数据来源是状态页,所以需要开启Nginx的状态页。

3.4.1:准备 Nginx 主机

3.4.1.1:安装 Nginx

同样用node111(192.168.1.111)作为Nginx测试主机,为其安装Nginx。

如果是编译安装,需要在编译时加上--with-http_stub_status_module,来支持状态页功能。
root@node111:~# apt install nginx

3.4.1.2:开启状态页

root@node111:~# vim /etc/nginx/nginx.conf 
        server {
                listen 192.168.1.111:80;
                listen 127.0.0.1:80;
                server_name node111.yqc.com;

                location / {
                        root /usr/share/nginx/html;
                        index index.html;
                }

                location /nginx_status {
                        stub_status;
                        allow 192.168.0.0/16;
                        allow 127.0.0.1;
                }
        }

检查配置文件并重启Nginx:


root@node111:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

root@node111:~# systemctl restart nginx

3.4.1.3:验证访问

http://192.168.1.111

在这里插入图片描述

http://192.168.1.111/nginx\_status

在这里插入图片描述

3.4.2:Nginx 状态监测脚本

3.4.2.1:Nginx 状态页信息

状态页完整信息:

Active connections: 2 
server accepts handled requests
 2 2 3 
Reading: 0 Writing: 1 Waiting: 1 
  • Active connections:当前处于活动状态的连接数(包括等待中的空闲连接);
  • accepts:运行至今已接受的客户端请求总数;
  • handled:运行至今已处理完成的客户端请求总数(通常与accepts相等);
  • requests:运行至今客户端发来的请求总数;
  • Reading:正在读取客户端请求报文首部的连接数;
  • Writing:正在向客户端发送响应报文的连接数;
  • Waiting:正在等待客户端发送请求报文的空闲连接数(开启keep-alive的情况下,这个值等于 Active connections - (Reading+Writing))。

3.4.2.2:编写脚本

主要通过curl命令获取到状态页的完整信息,并进行数据提取。

因为Nginx状态页的显示不规范,需要为每个指标都单独编辑提取命令。

脚本内容:

#!/bin/bash
# Description: Nginx状态监测脚本
# Author: yqc

# Variables set
Nginx_Addr=127.0.0.1
Nginx_Port=80
Page_Name=nginx_status
Status_Name=$1

# Define function "Nginx_Status"
Nginx_Status (){
        case ${Status_Name} in
        active)
                Status_Num=$(/usr/bin/curl "http://${Nginx_Addr}:${Nginx_Port}/${Page_Name}" 2> /dev/null | awk '/Active/{print $NF}');
        ;;
        accepts)
                Status_Num=$(/usr/bin/curl "http://${Nginx_Addr}:${Nginx_Port}/${Page_Name}" 2> /dev/null | awk 'NR==3{print $1}');
        ;;
        handled)
                Status_Num=$(/usr/bin/curl "http://${Nginx_Addr}:${Nginx_Port}/${Page_Name}" 2> /dev/null | awk 'NR==3{print $2}');
        ;;
        requests)
                Status_Num=$(/usr/bin/curl "http://${Nginx_Addr}:${Nginx_Port}/${Page_Name}" 2> /dev/null | awk 'NR==3{print $3}');
        ;;
        reading)
                Status_Num=$(/usr/bin/curl "http://${Nginx_Addr}:${Nginx_Port}/${Page_Name}" 2> /dev/null | awk '/Reading/{print $2}');
        ;;
        writing)
                Status_Num=$(/usr/bin/curl "http://${Nginx_Addr}:${Nginx_Port}/${Page_Name}" 2> /dev/null | awk '/Writing/{print $4}');
        ;;
        waiting)
                Status_Num=$(/usr/bin/curl "http://${Nginx_Addr}:${Nginx_Port}/${Page_Name}" 2> /dev/null | awk '/Waiting/{print $6}');
        esac
        echo ${Status_Num}
}

# Use function "Nginx_Status"
Nginx_Status $1

脚本测试:

root@node111:~# bash Nginx-Status.sh active
1
root@node111:~# bash Nginx-Status.sh accepts
29
root@node111:~# bash Nginx-Status.sh handled
30
root@node111:~# bash Nginx-Status.sh requests
30
root@node111:~# bash Nginx-Status.sh reading
0
root@node111:~# bash Nginx-Status.sh writing
1
root@node111:~# bash Nginx-Status.sh waiting
0

3.4.3:Zabbix Agent 添加 UserParameter

3.4.3.1:添加 UserParameter

拷贝脚本并添加执行权限:

root@node111:~# cp /root/Nginx-Status.sh /etc/zabbix/zabbix_agentd.d/
root@node111:~# chmod +x /etc/zabbix/zabbix_agentd.d/Nginx-Status.sh 

编辑配置文件:

root@node111:~# vim /etc/zabbix/zabbix_agentd.d/Nginx-Status.conf
UserParameter=nginx_status[*],/etc/zabbix/zabbix_agentd.d/Nginx-Status.sh "$1"

重启zabbix-agent:

root@node111:~# systemctl restart zabbix-agent

3.4.3.2:Zabbix Server 测试手动获取监控数据

root@zabbix-server:~# zabbix_get -s 192.168.1.111 -p 10050 -k "nginx_status["active"]"               
1
root@zabbix-server:~# zabbix_get -s 192.168.1.111 -p 10050 -k "nginx_status["waiting"]"
0
root@zabbix-server:~# zabbix_get -s 192.168.1.111 -p 10050 -k "nginx_status["writing"]"
1
root@zabbix-server:~# zabbix_get -s 192.168.1.111 -p 10050 -k "nginx_status["handled"]"
38

3.4.4:Zabbix Server 制作 Nginx 状态监测模板

3.4.4.1:创建模板

在这里插入图片描述

3.4.4.2:创建应用集

在这里插入图片描述

3.4.4.3:创建监控项

活动连接数监控项:

在这里插入图片描述

依次克隆并更改,添加其它指标的监控项:

在这里插入图片描述

3.4.5:Zabbix Server 为主机关联模板

3.4.5.1:关联模板

在这里插入图片描述

3.4.5.2:验证监控数据

最新数据:

在这里插入图片描述

标签: nginx, Nginx, root, zabbix, Zabbix, Status

相关文章推荐

添加新评论,含*的栏目为必填