Zabbix监控nginx accepts及状态码

1. Nginx端配置

1.2 Nginx加入配置

Nginx端需要加入配置文件
location ~ /status{
stub_status on;
access_log off;
}
再到/usr/sbin下 ./nginx -s reload重启nginx

[root@abc zabbix]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /etc/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        location ~ /status{
        stub_status on;
        access_log off;
       }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

   }

1.2 监测是否成功

http://10.5.11.251/status
Active connections: 2
server accepts handled requests
1822 1822 1127
Reading: 0 Writing: 1 Waiting: 1

7个状态量解释

Active connections 当前的活动连接数,包含处于等待状态的连接 The current number of active client connections including Waiting connections.

accepts 接收到的客户端发来的连接数 The total number of accepted client connections.

handled 已经处理完成的连接数,一般情况下它和accepts值相同,如果不同说明nginx性能出现瓶颈 The total number of handled connections. Generally, the parameter value is the same as acceptsunless 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.

Writing  正在发送响应报文的连接数 The current number of connections where nginx is writing the response back to the client.

Waiting  处于闲置状态正等待客户端发送请求的连接数 The current number of idle client connections waiting for a request.

2.编写agent端监控脚本

[root@abc zabbix]# cat chk_nginx_status.sh 
#! /bin/bash
#### Description:zabbix监控nginx性能以及进程状态
#### Note:此脚本需要配置在被监控端,否则ping检测将会得到不符合预期的结果
#主机ip或者域名
HOST="10.5.11.251"
#nginx开放端口
PORT="80"
#### 检测nginx进程是否存在
function ping {
    /sbin/pidof nginx | wc -l
}
#### 检测nginx性能
function active {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
    /usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
#### 执行function
$1

2.1 本地测试

[root@abc zabbix]# sh chk_nginx_status.sh accepts
1513

3 配置zabbix.conf文件

要使用脚本监控需要将zabbixd.conf文件中的
UnsafeUserParameters=1 开启
添加如下内容:

UserParameter=nginx.status[*],/etc/zabbix/chk_nginx_status.sh $1
配置好后重启zabbix-agent

service zabbix-agent restart

4 zabbix服务端用zabbix_get 测试获取数据

[root@zabbix-proxy ~]# zabbix_get -s 10.5.11.251 -k nginx.status[active]
1

5 Zabbix里面监控配置

注意key值和脚本里面的保持一致
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6 Nginx状态码监控

[root@www zabbix]# tail -1 nginx-item.conf
UserParameter=nginx_status_code[*],sh /etc/zabbix/nginx_code.sh “$1”
[root@zabbix]# cat /usr/local/zabbix/etc/zabbix_agentd.conf.d/nginx_code.sh

#!/bin/bash
FILE=/usr/local/nginx/logs/access.log
A=$1
nginx_200(){
cat $FILE | awk ‘{print $9}’| sort | uniq -c | grep $A | awk ‘{print $1}’
}
nginx_301(){
cat $FILE | awk ‘{print $9}’| sort | uniq -c | grep $A | awk ‘{print $1}’
}
nginx_302(){
cat $FILE | awk ‘{print $9}’| sort | uniq -c | grep $A | awk ‘{print $1}’
}
nginx_303(){
cat $FILE | awk ‘{print $9}’| sort | uniq -c | grep $A | awk ‘{print $1}’
}
nginx_304(){
cat $FILE | awk ‘{print $9}’| sort | uniq -c | grep $A | awk ‘{print $1}’
}
nginx_400(){
cat $FILE | awk ‘{print $9}’| sort | uniq -c | grep $A | awk ‘{print $1}’
}
nginx_403(){
cat $FILE | awk ‘{print $9}’| sort | uniq -c | grep $A | awk ‘{print $1}’
}
nginx_404(){
cat $FILE | awk ‘{print $9}’| sort | uniq -c | grep $A | awk ‘{print $1}’
}
nginx_500(){
cat $FILE | awk ‘{print $9}’| sort | uniq -c | grep $A | awk ‘{print $1}’
}
case $A in
200)
nginx_200;
;;
301)
nginx_301;
;;
302)
nginx_302;
;;
303)
nginx_303;
;;
304)
nginx_304;
;;
400)
nginx_400;
;;
403)
nginx_403;
;;
404)
nginx_404;
;;
500)
nginx_500;
;;
*)
esac
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值