因爲nginx的快速和高效,生產中的大部分應用都會選擇使用nginx來作反向代理,這時對於nginx的監控就顯得很是有必要,須要可以直觀的顯示出nginx實時的流量,訪問量,響應時間,http狀態碼等指標。html
prometheus具備由度量名稱和鍵/值對標識的時間序列數據的多維數據模型,可以很好的對nginx的這些指標進行收集,並配合grafana進行圖像展現。linux
收集nginx指標的較多采用的有nginx-vts模塊,prometheus-lua兩種方式進行採集,本文采用nginx-vts模塊方式進行數據收集。nginx
一、添加nginx模塊git
nginx -V #configure arguments中能夠查看到當前nginx所包含的模塊cd /root/git clone https://github.com/vozlt/nginx-module-vts #獲取vts源碼wget https://nginx.org/download/nginx-1.14.2.tar.gz #獲取nginx源碼tar -zxvf https://nginx.org/download/nginx-1.14.2.tar.gzcd nginx-1.14.2./configure --add-module=/root/nginx-module-vts --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'#--add-module=/root/nginx-module-vts,並將現有參數所有加上make && make install #編譯好的新nginx文件,能夠直接用來替換掉舊的nginx文件,重啓nginx服務進行升級service nginx restart
二、nginx.conf加入如下配置,經過8088端口展現出數據github
http {web
vhost_traffic_status_zone;json
server {後端
listen 8088;dom
location /status {curl
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
}
從新加載nginx配置後,訪問 IP:8088/status 能夠看到以下頁面,可以獲取到請求時間,後端響應時間,http code等大部分須要的指標了。
三、指標的數據類型轉換
要將獲取到的數據接入prometheus還須要將數據轉爲metrics類型的數據,vts模塊中直接提供了/status/format/prometheus 接口,訪問 IP:8088/status/format/prometheus 便可看到轉換後的數據。
部署nginx-vts-exporter
git clone https://github.com/hnlq715/nginx-vts-exporterchmod +x nginx-vts-exporternginx-vts-exporter -nginx.scrape_uri=http://localhost:8088/status/format/json
啓動後默認端口爲 :9913/metrics
四、經過VTS獲取其它指標
經過以上部署只能拿到默認的指標,生產中可能還會須要監控uri的請求量,監控IP訪問狀況(同一個IP出現大量訪問時可能被攻擊),獲取不一樣agent請求量用於分析等,經過vts模塊的vhost_traffic_status_filter_by_set_key功能能夠自定義須要獲取的指標。此處的指標須要加到對應的server配置中
server { listen 80; server_name nginx.test.com; vhost_traffic_status_filter_by_set_key $uri uri::$server_name; #每一個uri訪問量 vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name; #不一樣國家/區域請求量 vhost_traffic_status_filter_by_set_key $filter_user_agent agent::$server_name; #獲取用戶所使用的agent vhost_traffic_status_filter_by_set_key $status $server_name; #http code統計 vhost_traffic_status_filter_by_set_key $upstream_addr upstream::backend; #後端轉發統計 vhost_traffic_status_filter_by_set_key $remote_port client::ports::$server_name; #請求端口統計 vhost_traffic_status_filter_by_set_key $remote_addr client::addr::$server_name; #請求IP統計 location ~ ^/storage/(.+)/.*$ { set $volume $1; vhost_traffic_status_filter_by_set_key $volume storage::$server_name; #請求路徑統計 } ......}
一、部署prometheus
prometheus server採用go語言編寫,下載後解壓可直接運行
tar xvfz prometheus-2.7.2.linux-amd64.tar.gz
cd prometheus-2.7.2.linux-amd64
/usr/local/prometheus-2.7.2.linux-amd64/prometheus --storage.tsdb.path=/data/prometheus --web.enable-lifecycle >> /data/prometheus/prometheus.log 2>&1 & #–storage.tsdb.path參數指定數據存儲路徑#--web.enable-lifecycle參數容許熱加載,設置該參數後能夠經過 curl XPOST localhost:9090/-/reload 快速加載配置
二、nginx-vts-exporte接入
修改prometheus.yml配置文件,將nginx-vts-exporter對應端口接入prometheus,採用文件發現的方式
- job_name: 'Nginx_vts_exporter' file_sd_configs: - files: ['./file_sd_configs/file_sd_configs_nginx.json'] refresh_interval: 30s
編輯./file_sd_configs/file_sd_configs_nginx.json,nginx數量比較多的狀況下,能夠利用自定義labels對Nginx進行分組,便於後續圖像展現
[ { "targets": [ "10.21.141.25:9913" ], "labels": { "group": "ops-test" } }, { "targets": [ "10.21.141.26:9913" ], "labels": { "group": "ops-test" } }]
在grafana數據源設置中將部署好的prometheus加入,就能夠對獲取到的數據使用圖像展現了
導入nginx-vts-exporter的示例圖
https://grafana.com/dashboards/2949
根據監控需求進行標籤分組,增刪圖像。最終效果以下: