今天,我们通过shell脚本来对Nginx进行状态跟踪。 操作步骤: 1)启动Nginx的跟踪服务; 2)编写shell脚本获取;

  1. 启动Nginx的跟踪服务 Nginx 的功能模块中有一个ngx_http_stub_status_module的模块,记录着Nginx的基本访问状态信息,但需要在配置才能启动起来。 注意:yum安装方式也默认已安装该模块;

1. 安装Nginx

[root@web01]# yum install -y nginx

2. 启动跟踪服务

[root@web01 nginx]# cd /etc/nginx [root@web01 nginx]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # 启动状态跟踪 location /nginx_status { stub_status on; #打开状态跟踪的功能 access_log off; #关闭记录访问日志的功能 } } } [root@web01 nginx]# 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 nginx]# nginx [root@web01 nginx]# netstat -lntup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1405/nginx
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1188/sshd
tcp 0 0 :::22 :::* LISTEN 1188/sshd

3. curl检测,获取到当前Nginx的状态信息

[root@web01 nginx]# curl 10.0.0.7/nginx_status Active connections: 1 server accepts handled requests 1 1 1 Reading: 0 Writing: 1 Waiting: 0 Bash Copy 2. 编写脚本获取状态 [root@web01 scripts]# cat nginx_status.sh #!/bin/bash ##############################################################

File Name: nginx_status.sh

Version: V1.0

Author: oldboy

Organization: www.oldboyedu.com

Created Time : 2019-01-19 20:31:06

Description:

############################################################## NGINX_URL=$2 NGINX_PORT=$3

[ ! $NGINX_URL ] && { echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests} url [port]" exit 1 }

[ ! $NGINX_PORT ] && { NGINX_PORT=80 }

NGINX_COMMAND=$1 nginx_active(){ /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk '/Active/ {print $NF}' } nginx_reading(){ /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk '/Reading/ {print $2}' } nginx_writing(){ /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk '/Writing/ {print $4}' } nginx_waiting(){ /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk '/Waiting/ {print $6}' } nginx_accepts(){ /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $1}' } nginx_handled(){ /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $2}' } nginx_requests(){ /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $3}' } case $NGINX_COMMAND in active) nginx_active; ;; reading) nginx_reading; ;; writing) nginx_writing; ;; waiting) nginx_waiting; ;; accepts) nginx_accepts; ;; handled) nginx_handled; ;; requests) nginx_requests; ;; *) echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests} url [port]" esac

2. 我们来测试一把

[root@web01 scripts]# curl 10.0.0.7/nginx_status Active connections: 1 server accepts handled requests 7 7 7 Reading: 0 Writing: 1 Waiting: 0 [root@web01 scripts]# sh nginx_status.sh handled 10.0.0.7 8 [root@web01 scripts]# sh nginx_status.sh active 10.0.0.7 1 [root@web01 scripts]# sh nginx_status.sh requests 10.0.0.7 10 Bash Copy Ngnix状态跟踪指标解析:  active connections >> 对后端发起的活动连接数;  server accepts handled requests >> nginx 总共处理了[accepts]个连接, 成功创建[handled]次握手,总共处理了[requests]个请求  reading >> nginx 读取到客户端的Header信息数;  writing >> nginx 返回给客户端的Header信息数;  waiting >> 开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是Nginx说已经处理完正在等候下一次请求指令的驻留连接;