prometheus — nginx-vts-exporter

参考文档:
https://blog.51cto.com/xujpxm/2080146
注: 本文留用自己参考,建议看以上参考文档,更为细致

prometheus 监控 nginx 使用 nginx-vts-exporter 采集数据。同时,需要 nginx 支持 nginx-module-vts 模块获取 nginx 自身的一些数据。

nginx 的模块支持

进入nginx 安装包解压后的目录,下载模块文件

git clone git://github.com/vozlt/nginx-module-vts.git

编译安装,只需要在之前的编译参数中加上 --add-module=/path/to/nginx-module-vts 即可

./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx  --conf-path=/usr/local/nginx/conf/nginx.conf --user=nginx --group=nginx --add-module=./nginx-module-vts
make && make install

修改nginx 配置

http {
    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;   #开启此功能,会根据不同的server_name进行流量的统计,否则默认会把流量全部计算到第一个上。
    ...
    server {
        ...
        location /status {
        vhost_traffic_status_display;    
        vhost_traffic_status_display_format html;
        }
                }
        }

在不想统计流量的server 区域(未规范配置server_name或者无需进行监控的server上)可以禁用 vhost_traffic_status:

server {
vhost_traffic_status off;
...
}

重启nginx后,访问http://127.0.0.1/status,可以得到各种参数

nginx-vts-exporter 安装使用

wget -c https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.9.1/nginx-vts-exporter-0.9.1.linux-amd64.tar.gz
tar -xvf nginx-vts-exporter-0.9.1.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/nginx-vts-exporter-0.9.1.linux-amd64/
./nginx-vts-exporter  -nginx.scrape_uri http://127.0.0.1/status/format/json &

端口为9913,查看数据:

curl http://127.0.0.1:9913/metrics > nginx_data

在Prometheus中添加此target 便可接入。

常用监控规则:

求Nginx的QPS:
sum(irate(nginx_server_requests{code="total",host=~"$DomainName"}[5m]))
求4xx万分率(5xx类似,code=“5xx”):
(sum(irate(nginx_server_requests{code="4xx",host=~"$DomainName"}[5m])) / sum(irate(nginx_server_requests{code="total",host=~"$DomainName"}[5m]))) * 10000
求upstream的QPS(示例求group1的qps):
sum(irate(nginx_upstream_requests{code="total",upstream="group1"}[5m]))
求upstream后端server的响应时间(示例求group1的后端响应时间):
nginx_upstream_responseMsec{upstream=“group1”}

猜你喜欢

转载自www.cnblogs.com/huandada/p/10472031.html
今日推荐