目錄
- Nginx 監控模塊(vts模塊)
- 1、上傳nginx-module-vts-master軟件包并解壓
- 2、安裝Nginx依賴環境
- 3、優化路徑及編譯安裝nginx
- 4、備份nginx的備份文件
- 5、修改nginx的配置文件
- 6、監控模塊各字段信
- 7、一鍵安裝vts監控模塊
- 總結
Nginx 監控模塊(vts模塊)
監控Nginx主要用到以下三個模塊:
1、nginx-module-vts:Nginx virtual host traffic status module,Nginx的監控模塊,能夠提供JSON格式的數據產出。
2、nginx-vts-exporter:Simple server that scrapes Nginx vts stats and exports them via HTTP for Prometheus consumption。主要用于收集Nginx的監控數據,并給Prometheus提供監控接口,默認端口號9913。
3、Prometheus:監控Nginx-vts-exporter提供的Nginx數據,并存儲在時序數據庫中,可以使用PromQL對時序數據進行查詢和聚合。
1、上傳nginx-module-vts-master軟件包并解壓
unzip nginx-module-vts-master.zip 解壓
2、安裝Nginx依賴環境
yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
3、優化路徑及編譯安裝nginx
make && make install
優化管理
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
檢查開啟的模塊
nginx -v? 檢查 nginx版本 nginx -V
watch -n 1 nginx 觀察nginx狀態
4、備份nginx的備份文件
5、修改nginx的配置文件
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
進去后再http{ } 中添加如下內容
vhost_traffic_status_zone;?? ??? ??? ?#流量狀態監控? log_format main '{ "@timestamp": "$time_local", ' '"@fields": { ' '"uri":"$request_uri",' '"url":"$uri",' '"upstream_addr":"$upstream_addr",' '"remote_addr": "$remote_addr", ' '"remote_user": "$remote_user", ' '"body_bytes_sent": "$body_bytes_sent", ' '"host":"$host",' '"server_addr":"$server_addr",' '"request_time": "$request_time", ' '"request_time":"$request_time",' '"status":"$status",' '"request": "$request", ' '"request_method": "$request_method", ' '"size":$body_bytes_sent,' '"upstream_time":"$upstream_response_time"' '"http_referrer": "$http_referer", ' '"body_bytes_sent":"$body_bytes_sent", ' '"http_x_forwarded_for": "$http_x_forwarded_for", ' '"http_user_agent": "$http_user_agent" } }';
在server中80端口添加
location /status {<!--{C}%3C!%2D%2D%20%2D%2D%3E--> vhost_traffic_status_display; vhost_traffic_status_display_format html; }
在虛擬機中測試 192.168.100.10/status
6、監控模塊各字段信
監控列表各項信息
Server main 主服務器
**Host:**主機名
**Version:**版本號
**Uptime:**服務器運行時間
Connections active:當前客戶端的連接數 reading:讀取客戶端連接的總數 writing:寫入客戶端連接的總數
Requsts accepted:接收客戶端的連接總數 handled:已處理客戶端的連接總數 Total:請求總數 Req/s:每秒請求的數量
Shared memory:共享內存 name:配置中指定的共享內存名稱 maxSize:配置中指定的共享內存的最大限制 usedSize:共享內存的當前大小 usedNode:共享內存中當前使用的節點數
Server zones 服務器區域
zone:當前區域
Requests Total:請求總數 Req/s:每秒請求數 time:時間
Responses:狀態碼數量 1xx、2xx、3xx、4xx、5xx:表示響應不同狀態碼數量 Total:響應狀態碼的總數
Traffic表示流量 Sent:發送的流量 Rcvd:接收的流量 Sent/s:每秒發送的流量 Rcvd/s:每秒接收的流量
Cache表示緩存 Miss:未命中的緩存數 Bypass:避開的緩存數 Expirde:過期的緩存數 Stale:生效的緩存數 Updating:緩存更新的次數 Revalidated:重新驗證的緩存書 Hit:緩存命中數 Scarce:未達緩存要求的請求次數Total:總數
7、一鍵安裝vts監控模塊
#!/bin/bash echo "提前準備好安裝包如:nginx-1.15.9.tar.gz? nginx-module-vts-master.zip" ##關閉防火墻及核心防護 systemctl stop firewalld systemctl disable firewalld setenforce 0 #刪除原有的nginx rm -rf /var/run/yum.pid ##安裝依賴包 yum -y install gcc gcc-c++ pcre-devel zlib-devel make pcre zlib?openssl openssl-devel #解包 tar zxvf nginx-1.15.9.tar.gz unzip nginx-module-vts-master.zip #創建運行用戶、組 useradd -M -s /sbin/nologin nginx? ##編譯 cd /opt/nginx-1.15.9/ ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --add-module=/usr/local/nginx-module-vts-master/? #vts模塊 #--with-http_stub_status_module? 統計模塊 #安裝? make && make install #優化路徑 ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ #查看nginx安裝信息 nginx -V #把nginx的加入到systemctl管理中 cat<<EOF >/usr/lib/systemd/system/nginx.service [Unit]?? ? Description=nginx?? ??? ??? ??? ??? ??? ??? ? After=network.target?? ??? ??? ??? ??? ??? ? [Service] Type=forking?? ??? ??? ??? ??? ??? ??? ??? ? PIDFile =/usr/local/nginx/logs/nginx.pid?? ? ExecStart=/usr/local/nginx/sbin/nginx?? ??? ? ExecrReload=/bin/kill -s HUP $MAINPID?? ??? ? ExecrStop=/bin/kill -s QUIT $MAINPID?? ??? ? PrivateTmp=true [Install] WantedBy=multi-user.target EOF #給權限 chmod 754 /lib/systemd/system/nginx.service #備份nginx配置文件? cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak #修改nginx的配置文件 sed -i "21i vhost_traffic_status_zone; #流量狀態監控" /usr/local/nginx/conf/nginx.conf sed -i "/#log_format ?main/i log_format main '{ "@timestamp": "$time_local", ' '"@fields": { ' '"uri":"$request_uri",' '"url":"$uri",' '"upstream_addr":"$upstream_addr",' '"remote_addr": "$remote_addr", ' '"remote_user": "$remote_user", ' '"body_bytes_sent": "$body_bytes_sent", ' '"host":"$host",' '"server_addr":"$server_addr",' '"request_time": "$request_time", ' '"request_time":"$request_time",' '"status":"$status",' '"request": "$request", ' '"request_method": "$request_method", ' '"size":$body_bytes_sent,' '"upstream_time":"$upstream_response_time"' '"http_referrer": "$http_referer", ' '"body_bytes_sent":"$body_bytes_sent", ' '"http_x_forwarded_for": "$http_x_forwarded_for", ' '"http_user_agent": "$http_user_agent" } }';" /usr/local/nginx/conf/nginx.conf sed -i "/server_name ?localhost;/a location /status { ????????????vhost_traffic_status_display; ????????????vhost_traffic_status_display_format html; ????????}" /usr/local/nginx/conf/nginx.conf #重啟nginx systemctl restart nginx &> /dev/null #測試 ? 本機ip/status