Nginx配置nginx-module-vts
用Prometheus進行nginx的監控可以自動的對相關server_name和upstream進行監控,你也可以自定義Prometheus的數據標簽,實現對不同機房和不同項目的nginx進行監控。
監控Nginx主要用到以下三個模塊:
nginx-module-vts:Nginx的監控模塊,能夠提供JSON格式的數據產出。
nginx-vts-exporter:主要用於收集Nginx的監控數據,並給Prometheus提供監控接口,默認端口號9913。
Prometheus:監控Nginx-vts-exporter提供的Nginx數據,並存儲在時序數據庫中,可以使用PromQL對時序數據進行查詢和聚合。
nginx-module-vts模塊的編譯
nginx_vts_exporter依賴nginx-module-vts模塊,安裝此模塊無需任何其他依賴。
現網nging需重新編譯,步驟如下:
1、查看當前編譯參數:
#執行:
cd /usr/local/nginx/
sbin/nginx -V
#結果如下(例):
nginx version: nginx/1.10.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-pcre=/home/soft/pcre-8.36/ --with-http_ssl_module --with-http_stub_status_module
2、在nginx編譯時添加vts模塊
軟件包存放在/home/soft下,因此編譯時加入/home/soft/nginx-module-vts-master/
具體如下:
# 找到nginx源碼存放路徑/home/soft/nginx-1.10.1,重新編譯:
cd /home/soft/nginx-1.10.1
./configure --prefix=/usr/local/nginx --with-pcre=/home/soft/pcre-8.36/ --with-http_ssl_module --with-http_stub_status_module --add-module=/home/soft/nginx-module-vts-master/
#編譯執行完成后,確認結果中存在
adding module in /home/soft/nginx-module-vts-master/
+ ngx_http_vhost_traffic_status_module was configured
表示configure正確
# 執行編譯
make
# make完成后,備份並替換現網的nginx可執行文件
cd /usr/local/nginx/sbin
cp nginx nginx.bak
cp /home/soft/nginx-1.10.1/objs/nginx ./
Nginx編譯后,新的二進制文件會存放在objs目錄下
Nginx配置
更改Nginx Conf的配置,添加監控接口/status/:
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
監控數據的查看
安裝完vts模塊后,可以通過nginx status接口進行監控數據的查看,比如:http://127.0.0.1:90/status:
在頁面的最下方可以指定監控頁面刷新的時間間隔,點擊JSON,可以轉為JSON格式輸出。
nginx-vts-exporter的使用
exporter的安裝參考https://www.sumaott.com/doc/static/pms/index.html
Nginx的監控數據類型
nginx-vts-exporter的數據類型命名空間默認以“nginx”開頭,主要有如下9個:
HELP是對監控條目的解釋,TYPE的格式是監控條目名稱+Prometheus數據類型
# HELP nginx_server_bytes request/response bytes
# TYPE nginx_server_bytes counter
# HELP nginx_server_cache cache counter
# TYPE nginx_server_cache counter
# HELP nginx_server_connections nginx connections# TYPE nginx_server_connections gauge
# HELP nginx_server_requestMsec average of request processing timesin milliseconds
# TYPE nginx_server_requestMsec gauge
# HELP nginx_server_requests requests counter,可以區分狀態碼
# TYPE nginx_server_requests counter
# HELP nginx_upstream_bytes request/response bytes
# TYPE nginx_upstream_bytes counter
# HELP nginx_upstream_requestMsec average of request processing timesin milliseconds
# TYPE nginx_upstream_requestMsec gauge
# HELP nginx_upstream_requests requests counter,可以區分狀態碼
# TYPE nginx_upstream_requests counter
# HELP nginx_upstream_responseMsec average of only upstream/backend response processing timesin milliseconds
# TYPE nginx_upstream_responseMsec gauge
Nginx監控在Prometheus的數據匯總
常用監控匯總表達式:
DomainName對應nginx conf里的server_name,這里可以根據不同的server_name和upstream分別進行qps、2xx/3xx/4xx/5xx的狀態碼監控,另外也可以監控nginx每台后端server的qps和后端接口響應時間。
如果不需要區分server_name,可以把表達式里的$DomainName改為星號,“*****”代表所有;
求Nginx的QPS:
sum(irate(nginx_server_requests{code="total",host=~"$DomainName"}[5m]))
求4xx萬分率(5xx類似,code=“5xx”):
(sum(irate(nginx_server_requests{code="4xx",host=~"$DomainName"}[5m])) / sum(irate(nginx_server_requests{code="total",host=~"$DomainName"}[5m]))) * 10000
求upstream的QPS(示例求group1的qps):
sum(irate(nginx_upstream_requests{code="total",upstream="group1"}[5m]))
求upstream后端server的響應時間(示例求group1的后端響應時間):
nginx_upstream_responseMsec{upstream=“group1”}