nginx-vts-exporter
安装包下载,注意下载nginx版本需要和生产环境保持一致
wget https://github.com/vozlt/nginx-module-vts/archive/refs/tags/v0.2.2.tar.gz wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz wget http://nginx.org/download/nginx-1.26.2.tar.gz
|
解压nginx-module-vts
tar xf nginx-module-vts-0.2.3.tar.gz -C /usr/local/src/
|
查看运行nginx编译参数
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-stream --with-http_v2_module --with-http_stub_status_module
|
创建nginx用户
useradd -r -M -s /usr/sbin/nologin nginx
|
安装编译环境
yum install pcre-devel openssl-devel libxml2-devel libxslt-devel gd-devel gperftools gperftools-devel
|
解压nginx源码
tar xf nginx-1.26.2.tar.gz
|
再原来编译参数基础上添加vts模块,进行编译
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-stream --with-http_v2_module --with-http_stub_status_module --add-module=/usr/local/src/nginx-module-vts-0.2.2/ make
|
备份nginx配置文件
cd /usr/local/nginx/conf cp nginx.conf nginx.conf.bak
|
nginx配置文件添加配置
vhost_traffic_status_filter_by_host on; vhost_traffic_status_zone; server { listen 8089; location /status { vhost_traffic_status_display; vhost_traffic_status_display_format html; } }
|
将so文件替换
cd /usr/lib64/nginx/modules/ mkdir backup mv ./* backup/ cp /root/nginx-1.26.2/objs/*.so .
|
替换nginx二进制文件,并启动nginx
cd /usr/local/nginx/sbin cp nginx nginx.20241206
systemctl stop nginx
cp /root/nginx-1.20.2/nginx-1.20.2/objs/nginx .
./nginx -t
systemctl start nginx
|
部署exporter
tar -xf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz cp /opt/nginx-vts-exporter-0.10.3.linux-amd64/nginx-vts-exporter /usr/local/bin/ cat > /etc/systemd/system/nginx_vts_exporter.service << EOF [Unit] Description=nginx_exporter After=network.target [Service] Type=simple ExecStart=/usr/local/bin/nginx-vts-exporter -telemetry.address :9145 -nginx.scrape_uri=http://192.168.20.3:8089/status/format/json Restart=on-failure [Install] WantedBy=multi-user.target EOF systemctl start nginx_vts_exporter systemctl enable nginx_vts_exporter
|
测试插件
curl http://x.x.x.x:9145/metrics
|
配置Prometheus job
- job_name: nginx-exporter scrape_interval: 30s scrape_timeout: 30s static_configs: - targets: - 'x.x.x.x:9145' labels: instance: nginx-master group: "nginx-master" - targets: - 'x.x.x.x:9145' labels: instance: nginx-slave group: "nginx-slave" metrics_path: 'metrics' scheme: 'http'
|
prometheus-nginxlog-exporter
通过读取nginx日志文件,解析日志,生成监控指标,并提供http接口供Prometheus拉取。
安装
创建目录
mkdir -p /usr/local/nginxlog-exporter
|
下载安装包
wget https://github.com/martin-helmich/prometheus-nginxlog-exporter/releases/download/v1.11.0/prometheus-nginxlog-exporter_1.11.0_linux_amd64.tar.gz
|
安装服务
tar xf prometheus-nginxlog-exporter_1.11.0_linux_amd64.tar.gz mv prometheus-nginxlog-exporter /usr/local/bin/
|
创建配置文件/usr/local/nginxlog-exporter/nginxlog_exporter.yml
cat > /usr/local/nginxlog-exporter/nginxlog_exporter.yml << 'EOF' listen: port: 4040 address: "0.0.0.0"
consul: enable: false
namespaces: - name: metis relabel_configs: - target_label: request_uri from: request split: 2 separator: ' ' format: '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"' source: files: - /var/log/nginx/access.log labels: service: "metis" environment: "prod" histogram_buckets: [.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10] EOF
|
创建systemd服务
cat > /etc/systemd/system/prometheus-nginxlog-exporter.service << EOF [Unit] Description=NGINX metrics exporter for Prometheus After=network-online.target
[Service] ExecStart=/usr/local/bin/prometheus-nginxlog-exporter -config-file /usr/local/nginxlog-exporter/nginxlog_exporter.yml Restart=always ProtectSystem=full CapabilityBoundingSet=
[Install] WantedBy=multi-user.target EOF
|
启动服务
systemctl start prometheus-nginxlog-exporter systemctl enable prometheus-nginxlog-exporter
|
测试
curl http://localhost:4040/metrics
|
配置Prometheus job
- job_name: nginxlog-exporter static_configs: - targets: - 'x.x.x.x:4040' labels: instance: nginx-master group: "nginx-master" - targets: - 'x.x.x.x:4040' labels: instance: nginx-slave group: "nginx-slave" metrics_path: 'metrics' scheme: 'http'
|