Prometheus采集Nginx指标,使用nginx-module-vts进行性能监控

如何使用Prometheus采集Nginx指标

下面步骤从安装nginx到安装采集模块到配置prometheus
1. 下载nginx
wget http://nginx.org/download/nginx-1.20.1.tar.gz
2. 解压
tar -zxvf nginx-1.20.1.tar.gz -C /usr/local
3. 进入nginx安装目录
cd /usr/local/nginx-1.20.1
4. 安装nginx指标采集插件nginx-module-vts

从git上拉取nginx-module-vts源码

git clone git://github.com/vozlt/nginx-module-vts.git
5. 带着nginx-module-vts模块进行编译
./configure --add-module=/usr/local/nginx-1.20.1/nginx-module-vts

如遇见错误

Prometheus采集Nginx指标,使用nginx-module-vts进行性能监控_第1张图片

nginx error: the HTTP rewrite module requires the PCRE library.

执行
yum -y install pcre-devel
yum -y install openssl openssl-devel
6. 进行nginx的源码编译
make && make install
编辑nginx.conf
vim /usr/local/nginx/conf/nginx.conf

编辑nginx.conf,在http和server中加入配置信息。

http {
    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on; # 根据host分别进行流量统计
    ...
    server {
        ...
        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
}

Prometheus采集Nginx指标,使用nginx-module-vts进行性能监控_第2张图片

使用systemctl管理nginx
vim /usr/lib/systemd/system/nginx.service

[Unit]
  Description=nginx - high performance web server
  After=network.target remote-fs.target nss-lookup.target

  [Service]
  Type=forking
  ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  ExecReload=/usr/local/nginx/sbin/nginx -s reload
  ExecStop=/usr/local/nginx/sbin/nginx -s stop

  [Install]
  WantedBy=multi-user.target

设置开机自启动

systemctl daemon-reload
systemctl enable  nginx
systemctl start  nginx
systemctl status  nginx

访问主机ip/status即可访问
Prometheus采集Nginx指标,使用nginx-module-vts进行性能监控_第3张图片

nginx-exporter安装配置

下载

wget  https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.9.1/nginx-vts-exporter-0.9.1.linux-amd64.tar.gz

解压

tar -zxvf nginx-vts-exporter-0.9.1.linux-amd64.tar.gz

修改目录名称

mv nginx-vts-exporter-0.9.1.linux-amd64 /usr/local/nginx-vts-exporter
使用systemctl 管理nginx-vts-exporter进程
vim /usr/lib/systemd/system/nginx_vts_exporter.service
[Unit]
Description=prometheus_nginx_vts
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/nginx-vts-exporter/nginx-vts-exporter  -nginx.scrape_uri http://192.168.1.1/status/format/json
Restart=on-failure

[Install]
WantedBy=multi-user.target
设置开机自启动
systemctl daemon-reload
systemctl enable  nginx_vts_exporter
systemctl start  nginx_vts_exporter
systemctl status  nginx_vts_exporter

prometheus 配置

配置nginx指标节点nginx-vts-exporter
nginx-vts-exporter默认的端口为9913

#添加个节点
vim /usr/local/prometheus/prometheus.yml 
...
- job_name: 'nginx' # 添加node配置
    static_configs:
      - targets:
        - '192.168.1.1:9913'
...

你可能感兴趣的:(prometheus,elk,nginx)