Prometheus监控之nginx-vts-exporter

时间:2022-12-09 18:02:30

一、搭建

1、下载
nginx-vts-exporter下载地址:https://github.com/hnlq715/nginx-vts-exporter/releases
Nginx下载地址:http://nginx.org/download/
nginx-module-vts下载地址:https://github.com/vozlt/nginx-module-vts
安装nginx-vts-exporter之前,nginx必须安装第三方的nginx virtual host traffic status模块,提供对虚拟主机状态信息的访问,它包含
当前状态,如服务器、upstreams、缓存,即进行模块加载操作!

2、安装nginx服务
#安装依赖软件
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
tar -zxvf nginx-1.9.6.tar.gz
cd nginx-1.9.6
#如果是之前安装过的,可以nginx -V看一下安装添加的编译参数
./configure --prefix=/usr/local/nginx --with-stream --add-module=/usr/local/nginx-module-vts/
make && make install

3、配置nginx
vi /usr/local/nginx/conf/nginx.conf
vhost_traffic_status_zone;
server {
location /status {
root html;
index index.html index.htm;
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
/usr/local/nginx/sbin/nginx

4、安装nginx-vts-exporter
tar -zxvf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
cp -r nginx-vts-exporter-0.10.3.linux-amd64 /usr/local/nginx-vts-exporter

5、开启nginx-vts-exporter服务
cd /usr/local/nginx-vts-exporter/
./nginx-vts-exporter -nginx.scrape_uri http://10.10.10.10:8080/status/format/json \
-telemetry.address 192.168.10.131:9913 &>nginx-vts-exporter.log &

6、添加系统服务开机启动配置文件nginx-vts-exporter.service
vi /usr/lib/systemd/system/nginx-vts-exporter.service
[Unit]
Description=Prometheus Nginx VTS Exporter
After=network.target
User=nginx
Group=nginx

[Service]
Type=simple
Restart=always
ExecStart=/usr/local/nginx-vts-exporter/nginx-vts-exporter \
-nginx.scrape_uri http://localhost/status/format/json

[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl restart nginx-vts-exporter

7、与prometheus集成
vi /usr/local/prometheus/prometheus.yml
- job_name: 'nginx-vts-exporter'
scrape_interval: 10s
static_configs:
- targets: ['192.168.1.20:9913']
#添加一个job
systemctl restart prometheus

8、web测试
数学命令行:nginx_server_connections
accepts:接收到的客户端发来的连接数
Active connections:当前的活动连接数,包含处于等待状态的连接。
handled:已经处理完成的连接数,一般情况下它的accepts值相同,如果不同说明nginx性能出现瓶颈。
reading:正在读取请求头信息的连接数
requests:客户端请求总数
waiting:处于闲置状态正等待客户端发送请求的连接数
writing:正在发送相应报文的连接数