Prometheus 监控Nginx nginx-vts-exporter,并设置自启动

2022年8月26日

采用 nginx-vts-exporter 需要nginx添加 nginx-module-vts 模块, 此模块属于第三方,只能通过编译安装 ,本文已编译安装为例:

1、下载 Nginx

http://nginx.org/download/

wget http://nginx.org/download/nginx-1.21.6.tar.gz

需要的关联组合   (非 刚装的系统 ,可能不止,由于之前装过其他的 ,以下是我使用以下编译参数出现的报错解决所需)
apt-get install libgd2-xpm libgd2-xpm-dev libxml2 libxml2-dev libxslt-dev zlib1g.dev  libgd-dev  libgeoip-dev

2、编译 Nginx

./configure --prefix=/usr/local/nginx --http-client-body-temp-path=/usr/local/nginx/client_temp --http-proxy-temp-path=/usr/local/nginx/proxy_temp \

  --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --with-http_v2_module \

  --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_addition_module --with-http_xslt_module \

  --with-http_image_filter_module --with-http_geoip_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module  \

  --with-http_gunzip_module --with-http_gzip_static_module --with-file-aio --with-ipv6 --with-http_degradation_module --with-http_slice_module \

  --with-stream --with-stream_ssl_module --with-stream_realip_module --with-http_sub_module --with-pcre --add-module=/usr/local/src/nginx-module-vts/ 

如果本就是编译安装   添加 ./configure  --with-pcre --add-module=/usr/local/src/nginx-module-vts/  即可

make  

make install

Nginx  配置文件红色部分为新增的  (这里的配置 分为两部分写的 ,不想写两个 , 将  status 的内容写在 http 的模块下方)

user  webapps webapps;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    log_format info escape=json '{'
                '"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
                '"connection": "$connection", ' # connection serial number
                '"connection_requests": "$connection_requests", ' # number of requests made in connection
                '"pid": "$pid", ' # process pid
                '"request_id": "$request_id", ' # the unique request id
                '"request_length": "$request_length", ' # request length (including headers and body)
                '"remote_addr": "$remote_addr", ' # client IP
                '"remote_user": "$remote_user", ' # client HTTP username
                '"remote_port": "$remote_port", ' # client port
                '"time_local": "$time_local", '
                '"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
                '"request": "$request", ' # full path no arguments if the request
                '"request_uri": "$request_uri", ' # full path and arguments if the request
                '"args": "$args", ' # args
                '"status": "$status", ' # response status code
                '"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
                '"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
                '"http_referer": "$http_referer", ' # HTTP referer
                '"http_user_agent": "$http_user_agent", ' # user agent
                '"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
                '"http_host": "$http_host", ' # the request Host: header
                '"server_name": "$server_name", ' # the name of the vhost serving the request
                '"request_time": "$request_time", ' # request processing time in seconds with msec resolution
                '"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
                '"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
                '"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
                '"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
                '"upstream_response_length": "$upstream_response_length", ' # upstream response length
                '"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
                '"ssl_protocol": "$ssl_protocol", ' # TLS protocol
                '"ssl_cipher": "$ssl_cipher", ' # TLS cipher
                '"scheme": "$scheme", ' # http or https
                '"request_method": "$request_method", ' # request method
                '"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
                '"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
                '"gzip_ratio": "$gzip_ratio", '
                '"http_cf_ray": "$http_cf_ray",'
                '"geoip_country_code": "$geoip_country_code",'
                '"geoip_city": "$geoip_city"'
                '}';
    ####  Log  Dir
    access_log /usr/local/nginx/logs/access.log;
    error_log /usr/local/nginx/logs/error.log;
    ####   Prometheus  Nginx vts
    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;

    #access_log  logs/access.log  main;
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;
    types_hash_max_size 2048;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    server_names_hash_max_size 1024;
    server_names_hash_bucket_size   512;
    #gzip  on;
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    include /usr/local/nginx/conf/conf.d/*.conf;
    include /usr/local/nginx/conf/sites-enabled/*.conf;   
}

status.conf

server {
    listen *:8000;
    location /status {
             vhost_traffic_status_display;
             vhost_traffic_status_display_format html;
             allow 127.0.0.1;  #only allow requests from localhost
             deny all;   #deny all other hosts
         }
}

 

这时候 访问  IP:8000/status  查看 ;

 

3、下载  nginx-vts-exporter 

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

tar zxf  nginx-vts-exporter-0.10.3.linux-amd64.tar.gz

mv  nginx-vts-exporter-0.10.3.linux-amd64 nginx-vts-exporter

4、编写 nginx_vts_exporter.service  启动任务

vim /etc/systemd/system/nginx_vts_exporter.service

[Unit]
Description=nginx_exporter
After=network.target
[Service]
Type=simple
User=root
#ExecStart=/home/prometheus/nginx-prometheus-exporter/nginx-vts-exporter -nginx.scrape_uri=http://{45.136.151.107:8000}/status/format/json
ExecStart=/home/prometheus/nginx-vts-exporter/nginx-vts-exporter \
  -nginx.scrape_timeout 10 \
  -nginx.scrape_uri http://IP:8000/status/format/json
Restart=on-failure
[Install]
WantedBy=multi-user.target

5 、 启动  nginx_vts_exporter.service

systemctl daemon-reload

systemctl start nginx_vts_exporter.service

6、配置 prometheus 

      - targets: ['IP:9913']
        labels:
          origin_prometheus: prometheus
          instance: Nginx

7 配置 grafana  导入nginx vts模板  ID:2949

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注