Prometheus+Grafana+nginx-prometheus-exporter 监控 Nginx

Prometheus Grafana DevOps About 1,987 words

下载 nginx-prometheus-exporter

https://github.com/nginxinc/nginx-prometheus-exporter/releases

前提

Nginx需加载--with-http_stub_status_module模块。

配置 metrics 端口

server { 
    listen 81;
    location /metrics {
        stub_status on;
    }
}

访问数据采集端口

curl localhost:81/metrics

输出:

Active connections: 2 
server accepts handled requests
 2 2 16 
Reading: 0 Writing: 1 Waiting: 1 

备注

docker启动时记得暴露metrics端口给宿主机。

docker run -d \
--name=nginx \
-p 80:80 -p 81:81 \
-v $PWD/conf.d:/etc/nginx/conf.d \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx:1.20.2

后台启动

注意与其他exporter命令行参数两个横杠--不同,Nginx官方开源的exporter使用的是单横杠-

-web.listen-address:指定Prometheus抓取metrics数据的端口。

-nginx.scrape-uri:指定Nginx暴露的stub_status地址。

nohup /root/exporter/nginx-prometheus-exporter_0.10.0/nginx-prometheus-exporter \
-web.listen-address=:9113 \
-nginx.scrape-uri=http://127.0.0.1:81/metrics \
> nginx-prometheus-exporter.out 2>&1 &

开机启动

编辑

vim /etc/systemd/system/nginx-prometheus-exporter.service

内容:ExecStart需要修改为nginx-prometheus-exporter二进制文件的绝对路径

[Unit]
Description=nginx-prometheus-exporter
Documentation=https://github.com/nginxinc/nginx-prometheus-exporter
After=network.target

[Service]
Type=simple
User=root
ExecStart= /root/exporter/nginx-prometheus-exporter_0.10.0/nginx-prometheus-exporter \
-web.listen-address=:9113 \
-nginx.scrape-uri=http://127.0.0.1:81/metrics
Restart=on-failure

[Install]
WantedBy=multi-user.target

设为开机启动

sudo systemctl enable nginx-prometheus-exporter.service

启动nginx-prometheus-exporter

sudo systemctl start nginx-prometheus-exporter.service

nginx-prometheus-exporter 默认端口

http://localhost:9113

修改 Prometheus 配置

scrape_configs下新增job_namenginx(名称随意)

  - job_name: 'nginx'
    static_configs:
      - targets: ['192.168.0.100:9113']

重启 Prometheus

重启后可查看nginx-prometheus-exporter是否上线。

http://localhost:9090/targets

Grafana Dashboard

NGINX exporter: https://grafana.com/grafana/dashboards/12708

开源地址

https://github.com/nginxinc/nginx-prometheus-exporter

Views: 4,938 · Posted: 2022-02-26

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh