zabbix监控nginx活动连接

agent 10.0.0.78 编译安装nginx

root@78:/usr/local/src# wget http://nginx.org/download/nginx-1.18.0.tar.gz
root@78:/usr/local/src# tar xzvf nginx-1.18.0.tar.gz
root@78:/usr/local/src# cd nginx-1.18.0/
root@78:/usr/local/src/nginx-1.18.0# ./configure --help | grep stub
  --with-http_stub_status_module     enable ngx_http_stub_status_module
root@78:/usr/local/src/nginx-1.18.0# ./configure --prefix=/apps/nginx --with-http_stub_status_module  打开状态页
root@78:/usr/local/src/nginx-1.18.0# make && make install

配置打开状态页

修改配置文件 Server 配置块中添加:
        location /nginx_status {
            stub_status;
            allow 10.0.0.0/24;
            allow 127.0.0.1;
        }

root@78:/apps/nginx# sbin/nginx -t              测试配置文件
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
root@78:/apps/nginx# sbin/nginx                  启动nginx
root@78:/apps/nginx# ss -ntl | grep 80           确认启动
LISTEN   0         128                 0.0.0.0:80               0.0.0.0:*        users:(("nginx",pid=21482,fd=6),("nginx",pid=21481,fd=6))

root@78:/apps/nginx# curl 10.0.0.78/nginx_status              访问测试
Active connections: 2 
server accepts handled requests
 2 2 3 
Reading: 0 Writing: 1 Waiting: 1

reding:正在接收处理请求的数量
writing:正在响应请求的数量
waiting:处于会话保持的连接数量
active connections:活动连接数量    active conn = reding + writing + waiting

监控脚本

7种状态,使用awk命令获取数据,其中

  1. awk pattern // 中使用变量需要先用单引号再用双引号
  2. -F '[[:space:]]+|:' 空格和:同时作为分隔符,需要调整print $x
root@78:/etc/zabbix/zabbix_agentd.d# cat nginx_status.sh 
#!/bin/bash

PORT=$1
STATUS=$2

case $STATUS in
Active)
	ACTIVE_CONN_NUM=`curl http://10.0.0.78:$PORT/nginx_status 2> /dev/null | awk -F: '/'"$STATUS"'/{print $2}'`
	echo $ACTIVE_CONN_NUM
;;
accepts)
	ACCEPT_NUM=`curl http://10.0.0.78:$PORT/nginx_status 2> /dev/null | awk 'NR==3 {print $1}'`
	echo $ACCEPT_NUM
;;
handled)
	HANDLED_NUM=`curl http://10.0.0.78:$PORT/nginx_status 2> /dev/null | awk 'NR==3 {print $2}'`
	echo $HANDLED_NUM
;;
requests)
	REQUESTS_NUM=`curl http://10.0.0.78:$PORT/nginx_status 2> /dev/null | awk 'NR==3 {print $3}'`
	echo $REQUESTS_NUM
	;;
Reading)
	READING_NUM=`curl http://10.0.0.78:$PORT/nginx_status 2> /dev/null | awk -F '[[:space:]]+|:' 'NR==4 {print $3}'`
	echo $READING_NUM
;;
Writing)
	WRITING_NUM=`curl http://10.0.0.78:$PORT/nginx_status 2> /dev/null | awk -F '[[:space:]]+|:' 'NR==4 {print $6}'`
	echo $WRITING_NUM
;;
Waiting)
	WAITING_NUM=`curl http://10.0.0.78:$PORT/nginx_status 2> /dev/null | awk -F '[[:space:]]+|:' 'NR==4 {print $9}'`
	echo $WAITING_NUM
;;
*)
	echo wrong argutment
;;
esac

zabbix_get测试获取数据

root@zabbix-server:~# zabbix_get -s 10.0.0.78 -p 10050 -k nginx_status[80,Active]
2
root@zabbix-server:~# zabbix_get -s 10.0.0.78 -p 10050 -k nginx_status[80,Reading]
0
root@zabbix-server:~# zabbix_get -s 10.0.0.78 -p 10050 -k nginx_status[80,Waiting]
1
root@zabbix-server:~# zabbix_get -s 10.0.0.78 -p 10050 -k nginx_status[80,requests]
73

zabbix-web中添加监控项——触发器——图形

上一篇:zabbix 安装


下一篇:zabbix采坑系列记录