Prometheus系列之监控Nginx服务
prometheus监控nginx有两种方式。一种是通过nginx_exporter监控,需要开启nginx_stub_status,主要是nginx自身的status信息,metrics数据现对较少;另外一种使用nginx-vts-exporter监控,但是需要在编译nginx的时候添加nginx-module-vts模块,监控数据较多,提供了包含server、upstream以及cache的相关监控指标,指标更丰富,所以采用nginx-vts-exporter实现对nginx的监控。
环境信息
Nginx : openresty 1.27.1 Prometheus : 3.2.1 operating system : RockyLinux 9.5 Mini
nginx-vts-exporter
- 下载
nginx-module-vts模块[root@openresty-dev software]# git clone https://github.com/vozlt/nginx-module-vts.git - 编译
nginx添加nginx-module-vts模块支持[root@openresty-dev software]# ./configutre --prefix=/usr/local/openresty ...现有module.. --add-module=/data/software/nginx-module-vts
- 修改vhost配置文件,添加
nginx-module-vts相关配置[root@openresty-dev software]# vim /usr/local/openresty/nginx/conf/nginx.conf vhost_traffic_status_zone; vhost_traffic_status_filter_by_host on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location /status { vhost_traffic_status_display; vhost_traffic_status_display_format html; } ... [root@openresty-dev software]# nginx -t [root@openresty-dev software]# nginx -s reload
重载nginx配置之后,在浏览器中访问http://<ip或者domain>:port/status可以看到如下指标信息就是成功了。
4. 安装nginx-vts-exporter
[root@openresty-dev software]# wget https://github.com/sysulq/nginx-vts-exporter/releases/download/v0.10.8/nginx-vtx-exporter_0.10.8_linux_amd64.tar.gz
[root@openresty-dev software]# mv nginx-vtx-exporter /usr/local/
[root@openresty-dev software]# chmod +x /usr/local/nginx_exporter/nginx-vtx-exporter
- 配置nginx-vts-exporter systemctl
[root@openresty-dev software]# vim /usr/lib/systemd/system/nginx-exporter.service [Unit] Description=nginx_vts_exporter After=network.target [Service] Type=simple ExecStart=/usr/local/nginx_exporter/nginx-vtx-exporter -nginx.scrape_uri http://192.168.3.232/status/format/json Restart=on-failure [Install] WantedBy=multi-user.target [root@openresty-dev software]# systemctl daemon-reload [root@openresty-dev software]# systemctl start nginx-exporter [root@openresty-dev software]# systemctl enable nginx-exporter
- 添加配置到
prometheus.yml然后执行- job_name: 'nginx' static_configs: - targets: ['192.168.3.232:9913'] labels: instance: nginxsystemctl restart prometheus重新启动prometheus。 grafana导入nginx-exporter监控模板 模板连接:https://grafana.com/grafana/dashboards/2949
nginx_stub_status
This module is not built by default, it should be enabled with the --with-http_stub_status_module configuration parameter.
编译模块到
nginx[root@openresty-dev software]# ./configutre --prefix=/usr/local/openresty ...现有module.. --with-http_stub_status_module修改
nginx.conf配置文件... server { .... location /nginx_status { stub_status; } } ... [root@openresty-dev software]# nginx -t && nginx -s reload浏览器中访问
http://<ip或domain>:port/nginx_status
配置
nginx_vts_exporter[Unit] Description=nginx_vts_exporter After=network.target [Service] Type=simple #ExecStart=/usr/local/nginx_exporter/nginx-vtx-exporter -nginx.scrape_uri http://192.168.3.232/status/format/jsonExecStart=/usr/local/nginx_exporter/nginx-vtx-exporter -nginx.scrape_uri http://192.168.3.232/nbginx_status Restart=on-failure [Install] WantedBy=multi-user.target查看
prometheus采集信息
添加配置到
prometheus.yml- job_name: 'nginx' static_configs: - targets: ['192.168.3.232:9913'] labels: instance: nginx然后执行
systemctl restart prometheus重新启动prometheus。进入
grafana导入监控模板,就可以了。