nginx在生產環境中的應用越來越廣泛,所以需要對nginx的性能狀態做一些監控來發現出來出現的問題。zabbix監控nginx,首先確認nginx的監控指標,主要有:基本活動指標,錯誤指標,性能指標。
nginx處理流程圖具體如下:
註釋:Accepts(接受)、Handled(已處理)、Requests(請求數)是一直在增加的計數器。Active(活躍)、Waiting(等待)、Reading(讀)、Writing(寫)隨着請求量而增減
名稱 | 描述 | |
Accepts(接受) | NGINX 所接受的客戶端連接數 | 資源: 功能 |
Handled(已處理) | 成功的客戶端連接數 | 資源: 功能 |
Active(活躍) | 當前活躍的客戶端連接數 | 資源: 功能 |
Dropped(已丟棄,計算得出) | 丟棄的連接數(接受 - 已處理) | 工作:錯誤* |
Requests(請求數) | 客戶端請求數 | 工作:吞吐量 |
NGINX worker 進程接受 OS 的連接請求時 Accepts 計數器增加,而Handled 是當實際的請求得到連接時(通過建立一個新的連接或重新使用一個空閒的)。這兩個計數器的值通常都是相同的,如果它們有差別則表明連接被Dropped, 往往這是由於資源限制,比如已經達到 NGINX 的worker_connections的限制。
首先nginx需要配置nginx_status 具體步驟是:在 zabbix agentd客戶端上,查看nginx是否加載了with-http_stub_status_module。因爲 zabbix 監控nginx是根據nginx的Stub Status模塊,抓取Status模塊所提供的數據。假如以前沒開啓,現在想啓用StubStatus 模塊,在編譯nginx 的時候要加上參數with-http_stub_status_module,執行./configure && make就可以了,不用make install,一般情況下都是安裝的,具體的安裝配置如下
(一)配置nginx
1,查看nginx_status是否開啓,查看已開啓。
[root@iZ237lzm354Z scripts]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.4.7 built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) TLS SNI support enabled configure arguments: --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_realip_module --with-http_p_w_picpath_filter_module [root@iZ237lzm354Z scripts]#
2,nginx_status開啓的步驟:
[root@iZ237lzm354Z scripts]# vim /usr/local/nginx/conf/nginx.conf server { listen 80 ; server_name www.guojinbao.com; rewrite ^/invitejoin/(.*)\.htm[l]?$ /register.shtml?$1 last; index index.jsp index.html; root /opt/home; location = /nginx-status { stub_status on; access_log off; allow 127.0.0.1; allow 10.253.12.34; ####zabbix服務器端的IP地址一般爲內網IP
3,測試並啓動nginx
[root@iZ237lzm354Z scripts]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@iZ237lzm354Z scripts]# /usr/local/nginx/sbin/nginx -s reload
4,用curl來進行測試:
[root@iZ237lzm354Z scripts]# curl www.guojinbao.com/nginx-status Active connections: 979 server accepts handled requests 756072922 756072922 1136799890 Reading: 0 Writing: 4 Waiting: 975
備註:
Active connections –當前活躍的連接數量
server accepts handled requests — 總共處理了756072922個連接 , 成功創建 756072922次握手, 總共處理了1136799890個請求
reading — 讀取客戶端的連接數.
writing — 響應數據到客戶端的數量
waiting — 開啓 keep-alive 的情況下,這個值等於 active – (reading+writing), 意思就是 Nginx 已經處理完正在等候下一次請求指令的駐留連接.
(二)配置zabbix_agentd
1,編寫腳步來獲取nginx的相關信息
[root@ittestserver1 opt]# vim /usr/local/zabbix/scripts/nginx-check_performance.sh #!/bin/bash ################################## # Zabbix monitoring script # # nginx: # - anything available via nginx stub-status module # ################################## # Contact: # [email protected] # Zabbix requested parameter ZBX_REQ_DATA="$1" ZBX_REQ_DATA_URL="$2" # Nginx defaults NGINX_STATUS_DEFAULT_URL="www.guojinbao.com/nginx-status" #(這裏寫網站的域名) WGET_BIN="/usr/bin/wget" # # Error handling: # - need to be displayable in Zabbix (avoid NOT_SUPPORTED) # - items need to be of type "float" (allow negative + float) # ERROR_NO_ACCESS_FILE="-0.9900" ERROR_NO_ACCESS="-0.9901" ERROR_WRONG_PARAM="-0.9902" ERROR_DATA="-0.9903" # either can not connect / bad host / bad port # Handle host and port if non-default if [ ! -z "$ZBX_REQ_DATA_URL" ]; then URL="$ZBX_REQ_DATA_URL" else URL="$NGINX_STATUS_DEFAULT_URL" fi # save the nginx stats in a variable for future parsing NGINX_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null) # error during retrieve if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then echo $ERROR_DATA exit 1 fi # # Extract data from nginx stats # case $ZBX_REQ_DATA in active_connections) echo "$NGINX_STATS" | head -1 | cut -f3 -d' ';; accepted_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f2 -d' ';; handled_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f3 -d' ';; handled_requests) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f4 -d' ';; reading) echo "$NGINX_STATS" | tail -1 | cut -f2 -d' ';; writing) echo "$NGINX_STATS" | tail -1 | cut -f4 -d' ';; waiting) echo "$NGINX_STATS" | tail -1 | cut -f6 -d' ';; *) echo $ERROR_WRONG_PARAM; exit 1;; esac exit 0 [root@ittestserver1 opt]# chmod +x /usr/local/zabbix/scripts/nginx-check_performance.sh -rw-r--r-x1 root root 1645 2月 4 14:26/usr/local/zabbix/scripts/nginx-check_performance.sh
2,配置zabbix_agentd.conf。啓用UserParameter,並配置相關的參數。
[root@ittestserver1 opt]# vim /usr/local/zabbix/etc/zabbix_agentd.conf ####### USER-DEFINED MONITORED PARAMETERS ####### ### Option: UnsafeUserParameters # Allow all characters to be passed in arguments to user-defined parameters. # The following characters are not allowed: # \ ' " ` * ? [ ] { } ~ $ ! & ; ( ) < > | # @ # Additionally, newline characters are not allowed. # 0 - do not allow # 1 - allow # # Mandatory: no # Range: 0-1 # Default: # UnsafeUserParameters=0 UnsafeUserParameters=1 ### Option: UserParameter # User-defined parameter to monitor. There can be several user-defined parameters. # Format: UserParameter=<key>,<shell command> # See 'zabbix_agentd' directory for examples. # # Mandatory: no # Default: # UserParameter= UserParameter=nginx[*],/usr/local/zabbix/scripts/nginx-check_performance.sh "$1"
3,重啓zabbix_agentd客戶端
[root@zabbix ~]# /etc/init.d/zabbix_agentd restart Shutting down zabbix_agentd: [ OK ] Starting zabbix_agentd: [ OK ] [root@zabbix ~]#
4,在zabbix服務端(server)進行測試。
[root@zabbix ~]# zabbix_get -s 10.253.17.20 -p 10050 -k "nginx[reading]" 0 [root@zabbix ~]#
(三)在網頁上配置nginx模板的相關監控
1,登錄zabbix界面,依次點擊:配置(configuration)---模板(template)---導入(import)
2,給主機添加模板:選擇主機---nginx服務器主機---模板---選擇(剛剛導入的nginx性能狀態的模板)---添加---更新
3,查看nginx監控的最新數據:監控中---圖形---選擇相應的監控類型。
備註:
Active :當前活躍的連接數。
Accepts: 接受的請求數
Handled: 處理的請求數(正常服務器響應,這兩項應該是可以相等的)
Requests: 客戶端處理的請求數。(吞吐量)
Reading: 當接收到請求時,連接離開 Waiting 狀態,並且該請求本身使 Reading 狀態計數增加。在這種狀態下 NGINX 會讀取客戶端請求首部。請求首部是比較小的,因此這通常是一個快速的操作。
Writing: 請求被讀取之後,其使 Writing 狀態計數增加,並保持在該狀態,直到響應返回給客戶端。這意味着,該請求在 Writing 狀態時, 一方面 NGINX 等待來自上游系統的結果(系統放在 NGINX “後面”),另外一方面,NGINX 也在同時響應。請求往往會在 Writing 狀態花費大量的時間。
Waiting: 活躍的連接也可以處 於 Waiting 子狀態,如果有在此刻沒有活躍請求的話。新連接可以繞過這個狀態並直接變爲到 Reading 狀態,最常見的是在使用“accept filter(接受過濾器)” 和 “deferred accept(延遲接受)”時,在這種情況下,NGINX 不會接收 worker 進程的通知,直到它具有足夠的數據纔開始響應。如果連接設置爲 keep-alive ,那麼它在發送響應後將處於等待狀態