1.nginx状态
配置文件
[root@web01 ~]$ cat /etc/nginx/conf.d/nginx.status.conf
#nginx 状态
server {
listen 80;
server_name nginx.status.com; # 可以替换为你的域名或服务器IP
# 状态监控页面配置
location /nginx_status {
# 启用状态监控
stub_status on;
# 限制访问来源,增强安全性
# allow 127.0.0.1; # 允许本地访问
# allow 192.168.1.0/24; # 允许指定网段访问
# deny all; # 拒绝其他所有来源
# 不记录此页面的访问日志
access_log off;
# 开启错误日志
error_log /var/log/nginx/nginx_status_error.log;
}
# 可选:配置一个简单的首页
location / {
return 200 ‘Nginx is running normally’;
add_header Content-Type text/plain;
}
}
检查重启
[root@web01 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]$ systemctl restart nginx
测试
[root@web01 ~]$ curl nginx.status.com/nginx_status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
解释
1. Active connections: 1
表示当前活跃的连接总数,包括:
- 正在读取客户端请求的连接
- 正在向客户端发送响应的连接
- 处于空闲状态的长连接(keep-alive)当前值为 1,说明服务器此时只有 1 个活跃连接,负载较轻。
2. server accepts handled requests 及后面的数字 5 5 5
这一行表示 Nginx 启动以来的累计统计:
- 第一个数字 5(accepts):Nginx 总共接受的连接数;
- 第二个数字 5(handled):Nginx 总共成功处理的连接数(通常与 accepts 相等,除非服务器资源不足导致无法处理新连接);
- 第三个数字 5(requests):Nginx 总共处理的请求数(一个连接可能包含多个请求,比如通过 keep-alive 长连接复用,这里每个连接恰好处理了 1 个请求)。
3. Reading: 0
表示 Nginx 正在读取客户端请求头的连接数,当前为 0,说明此时没有客户端正在发送请求。
4. Writing: 1
表示 Nginx 正在向客户端发送响应数据的连接数,当前为 1,说明有 1 个连接正在处理并返回数据给客户端。
5. Waiting: 0
表示处于空闲状态的连接数(这些连接是 keep-alive 长连接,未关闭且等待客户端发送新请求),当前为 0,说明没有空闲的长连接。
脚本
[root@web01 ~]$ cat /server/scripts/nginx.status.sh
#!/bin/bash
# 定义Nginx状态页面URL
STATUS_URL=”http://nginx.status.com/nginx_status”
# 获取原始状态信息
STATUS=$(curl -s “$STATUS_URL”)
# 检查状态信息是否获取成功
if [ -z “$STATUS” ]; then
echo “错误:无法连接到Nginx状态页面” >&2
exit 1
fi
# 解析所有状态值
ACTIVE_CONNECTIONS=$(echo “$STATUS” | grep “Active connections” | awk ‘{print $3}’)
ACCEPTS=$(echo “$STATUS” | awk ‘NR==3 {print $1}’)
HANDLED=$(echo “$STATUS” | awk ‘NR==3 {print $2}’)
REQUESTS=$(echo “$STATUS” | awk ‘NR==3 {print $3}’)
READING=$(echo “$STATUS” | grep “Reading” | awk ‘{print $2}’)
WRITING=$(echo “$STATUS” | grep “Writing” | awk ‘{print $4}’)
WAITING=$(echo “$STATUS” | grep “Waiting” | awk ‘{print $6}’)
# 显示帮助信息
show_help() {
echo “用法: $0 [参数]”
echo “参数说明:”
echo ” active – 输出活跃连接数(Active connections)”
echo ” accepts – 输出累计接受连接数”
echo ” handled – 输出累计处理连接数”
echo ” requests – 输出累计请求数”
echo ” reading – 输出正在读取请求数”
echo ” writing – 输出正在发送响应数”
echo ” waiting – 输出空闲连接数”
echo ” all – 输出所有状态值(键值对形式)”
echo ” help – 显示帮助信息”
}
# 根据参数输出对应的值
case “$1” in
active)
echo “$ACTIVE_CONNECTIONS”
;;
accepts)
echo “$ACCEPTS”
;;
handled)
echo “$HANDLED”
;;
requests)
echo “$REQUESTS”
;;
reading)
echo “$READING”
;;
writing)
echo “$WRITING”
;;
waiting)
echo “$WAITING”
;;
all)
echo “active_connections=$ACTIVE_CONNECTIONS”
echo “accepts=$ACCEPTS”
echo “handled=$HANDLED”
echo “requests=$REQUESTS”
echo “reading=$READING”
echo “writing=$WRITING”
echo “waiting=$WAITING”
;;
help|*)
show_help
;;
esac
监控文件
[root@web01 ~]$ tail -1 /etc/zabbix/zabbix_agent2.d/web.conf
UserParameter=nginx.status[*],sh /server/scripts/nginx.status.sh “$1”
权限问题(如有sudo权限就不用操作)
[root@web01 ~]$ ll /server/scripts/nginx.status.sh
-rw-r–r– 1 root root 2067 Sep 25 18:20 /server/scripts/nginx.status.sh
[root@web01 ~]$ chmod +x /server/scripts/nginx.status.sh
[root@web01 ~]$ ll /server/scripts/nginx.status.sh
-rwxr-xr-x 1 root root 2067 Sep 25 18:20 /server/scripts/nginx.status.sh
修改文件记得重启
[root@web01 ~]$ systemctl restart zabbix-agent2.service
解释
1. UserParameter
这是 Zabbix Agent 的核心配置指令,用于定义 自定义监控项的键(Key)和对应的执行命令。格式为:UserParameter=键名[参数],执行命令
2. nginx.status[*]
nginx.status:是自定义监控项的 键名(Key),用于在 Zabbix Server 中标识这个监控项(类似变量名),通常按 “服务。指标类型” 命名(这里表示 “Nginx 的状态指标”)。
[*]:表示这个监控项 支持接收参数(* 是通配符,代表可以接收任意参数)。例如可以传入 active、accepts、requests 等参数,用于获取 Nginx 的不同状态值。
3. sh /server/scripts/nginx.status.sh “$1”
这是监控项被调用时实际执行的命令:
sh:调用 Shell 解释器执行脚本。
/server/scripts/nginx.status.sh:是自定义的 Shell 脚本路径,用于获取 Nginx 的具体状态数据(需要手动创建这个脚本)。
“$1″:表示将 Zabbix 传递的 第一个参数 传递给脚本($1 是 Shell 中的位置参数,对应 nginx.status[参数] 中的 “参数”)。
测试
[root@zabbix ~]$ zabbix_get -s web01 -k nginx.status[accepts]
17
监控