利用zabbix监控nginx的状态,

 一、 zabbix-agent端口添加

1、 在nginx服务器上 指定可以得到nginx 状态的uri

# vim  /opt/nginx/vhosts/nginx_status.conf  # 添加
server {
    server_name 0.0.0.0; 
    listen 12222 ;
    access_log off;

    location = /Status {
        stub_status on;
        access_log off;
        allow 10.1.3.0/24;
        allow 127.0.0.1;
        deny all;
    }
   
    location ~ / {
        return 403;
    }   
}

2、 放一个脚本ningx_status.sh到/usr/bin, 并赋予执行的权限

Zabbix 监控Nginx 接口响应_客户端

Zabbix 监控Nginx 接口响应_连接数_02

#!/usr/bin/env bash
# date: 2018/12/20

# 此脚本用于监控nginx的请求相关数据、 url的状态,用于zabbix-server监控和报警

NGINX_STATUS_URL="http://localhost:12222/Status"   # nginx的status状态的url
NGINX_PIDNAME="nginx"                             # nginx进程名
TIMEOUT=20                                        # 超时时间

export NGINX_STATUS_URL NGINX_CHECK_URL NGINX_PIDNAME TIMEOUT

# example: 
# curl --connect-timeout 20 --silent http://localhost:12222/Status
# Active connections: 1 
# server accepts handled requests
# 26008 26008 43578 
# Reading: 0 Writing: 1 Waiting: 0 

# Active connections: 当前nginx活跃连接数,包括等待的连接, 如果重启过nginx将重新计数
# 这个连接是指客户端与nginx服务端建立的tcp连接, 如果建立连接后没有任何请求则在keep-alive时间内,服务器会断开这个连接,如果一直有请求则这个连接不会断开
# 通过ss -tnp可以看到服务端与客户端建立的连接                      

# server accepts handled requests: 自nginx启动以来共接收了26008个连接,处理了26008个连接, 总共处理了43578个客户端请求,如果重启过nginx将重新计数
# 26008 26008 43578 
# Reading: 0 Writing: 1 Waiting: 0 : Reading当前读取到客户端的Header(连接)数、 Writing当前(处理)返回给客户端的Header(连接)数、 Waiting表示当前等待请求的空闲客户端连接数

# 官方文档说明: http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
# Active connections
#     The current number of active client connections including Waiting connections. 当前nginx活跃连接数,包括正在等待的连接数
# accepts: 
#     The total number of accepted client connections. 接收的客户端连接总数
# handled: 
#     The total number of handled connections.         处理的连接总数,通常这个值等于accepts或小于limit资源限制(例如, worker_connections limit)
#     Generally, the parameter value is the same as accepts unless some resource limits have been reached (for example, the worker_connections limit).
# requests:
#     The total number of client requests.             处理的客户端请求总数
# Reading: 
#     The current number of connections where nginx is reading the request header.  当前读取到客户端的Header(连接)数
# Writing: 
#     The current number of connections where nginx is writing the response back to the client. 当前(处理)返回给客户端的Header(连接)数
# Waiting: 
#     The current number of idle client connections waiting for a request. 当前等待请求的空闲客户端连接数。 nginx已经处理完成,正在等候下一次请求指令的驻留连接 
#                                                                        开启keep-alive的情况下,这个值等于Active-(Reading+Writing)

# 注:如果Writing(响应) + Reading(读取) 很大说明nginx请求量已经非常大了, 因为在不停的请求和响应
#     一般请情况下Waiting数比较多是正常 

get_nginx_process() {
   # 检查nginx进程总数,用于判断nginx是否存活
   pgrep ${NGINX_PIDNAME} | wc -l
}

get_Active_connections() {
  # Active connections: The current number of active client connections including Waiting connections.
  # 当前nginx活动连接数,包括等待的连接
  curl --connect-timeout ${TIMEOUT} --silent ${NGINX_STATUS_URL}  | awk '/Active connections:/ {print $NF}'
}

get_accepts() {
  # handled: The current number of active client connections including Waiting connections.
  # nginx启动来接收的客户端连接总数
  curl --connect-timeout ${TIMEOUT} --silent  ${NGINX_STATUS_URL} | awk 'NR==3 {print $1}'
}

get_handled() {
  # handled: The total number of handled connections.
  # nginx启动以来处理的连接总数
  curl --connect-timeout ${TIMEOUT} --silent  ${NGINX_STATUS_URL} | awk 'NR==3 {print $2}'
}

get_requests() {
  # requests: The total number of client requests.             
  # 处理客户端的请求总数
  curl --connect-timeout ${TIMEOUT} --silent  ${NGINX_STATUS_URL} | awk 'NR==3 {print $3}'
}

get_Reading() {
  # Reading: The current number of connections where nginx is reading the request header
  # 读取到客户端的Header(连接)数
  curl --connect-timeout  ${TIMEOUT} --silent  ${NGINX_STATUS_URL} | awk '/Reading:/ {print $2}'
}

get_Writing() {
  # Writing: The current number of connections where nginx is writing the response back to the client
  # 返回给客户端的Header(连接)数
  curl  --connect-timeout  ${TIMEOUT} --silent  ${NGINX_STATUS_URL} | awk '/Writing:/ {print $4}'
}

get_Waiting() {
  # Waiting: The current number of idle client connections waiting for a request
  # 当前等待请求的空闲客户端连接数
  curl  --connect-timeout  ${TIMEOUT} --silent ${NGINX_STATUS_URL} | awk '/Waiting:/ {print $6}'
}

case "$1" in
  active-connections )
    get_Active_connections
    ;;
  accepts )
    get_accepts
    ;;
  handled )
    get_handled
    ;;
  accepts )
    get_accepts
    ;;
  requests )
    get_requests
    ;;
  reading )
    get_Reading
    ;;
  writing )
    get_Writing
    ;;
  waiting )
    get_Waiting
    ;;
  nginx-process )
    get_nginx_process
    ;;
  *)
   echo "Uasge: ${0} {active-connections|accepts|handled|requests|reading|waiting|writing|nginx-process}"
esac
exit $?

nginx_status.sh

3、定义一个配置文件,告诉zabbix-server怎么采集到数据

# vim /etc/zabbix/zabbix_agentd.d/userparameter_nginx_status.conf   #添加
UserParameter=nginx[*], nginx_status.sh $1

 二、 zabbix-server端制作模板

1、 模板已经制作好了,见附件 模板

2、 导入模板

3、配置主机关联模板

最后效果图

Zabbix 监控Nginx 接口响应_nginx_03

5版本自带nginx监控指标,推荐使用,agent端推荐使用zabbix-agent2