ps: nginx在生產環境中的應用愈來愈普遍,因此須要對nginx的性能狀態作一些監控來發現出來出現的問題。html
zabbix監控nginx,首先確認nginx的監控指標,主要有:基本活動指標,錯誤指標,性能指標。 nginx
nginx處理流程圖具體以下:shell
註釋:Accepts(接受)、Handled(已處理)、Requests(請求數)是一直在增長的計數器。Active(活躍)、Waiting(等待)、Reading(讀)、Writing(寫)隨着請求量而增減express
名稱vim |
描述centos |
指標類型bash |
Accepts(接受)服務器 |
NGINX 所接受的客戶端鏈接數app |
資源: 功能curl |
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是否開啓,查看已開啓。
1 2 3 4 5 6 7 |
|
2,nginx_status開啓的步驟:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
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來進行測試:
1 2 3 4 5 |
|
備註:
Active connections –當前活躍的鏈接數量
server accepts handled requests — 總共處理了756072922個鏈接 , 成功建立 756072922次握手, 總共處理了1136799890個請求
reading — 讀取客戶端的鏈接數.
writing — 響應數據到客戶端的數量
waiting — 開啓 keep-alive 的狀況下,這個值等於 active – (reading+writing), 意思就是 Nginx 已經處理完正在等候下一次請求指令的駐留鏈接.
(二)配置zabbix_agentd
1,編寫腳步來獲取nginx的相關信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
2,配置zabbix_agentd.conf。啓用UserParameter,並配置相關的參數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
3,重啓zabbix_agentd客戶端
1 2 3 4 |
|
4,在zabbix服務端(server)進行測試。
1 2 3 |
|
(三)在網頁上配置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 ,那麼它在發送響應後將處於等待狀態
附nginx.xml
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>2.0</version>
<date>2013-03-26T04:17:58Z</date>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<templates>
<template>
<template>Template_Nginx</template>
<name>Template_Nginx</name>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<items>
<item>
<name>Nginx $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[waiting,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[writing,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[active_connections,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[reading,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1/sec</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[handled_requests,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>1</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1/sec</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[accepted_connections,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>1</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Nginx $1/sec</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx[handled_connections,{$NGINX_STATUS_URL}]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>1</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Nginx</name>
</application>
</applications>
<valuemap/>
</item>
<item>
<name>Number of $1 process</name>
<type>0</type>
<snmp_community>public</snmp_community>
<multiplier>0</multiplier>
<snmp_oid>interfaces.ifTable.ifEntry.ifInOctets.1</snmp_oid>
<key>proc.num[nginx]</key>
<delay>300</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authpassphrase/>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications/>
<valuemap/>
</item>
</items>
<discovery_rules/>
<macros>
<macro>
<macro>{$NGINX_STATUS_URL}</macro>
<value>http://127.0.0.1:10061/nginx_status</value>
</macro>
</macros>
<templates/>
<screens/>
</template>
</templates>
<triggers>
<trigger>
<expression>{Template_Nginx:proc.num[nginx].last(0)}=0</expression>
<name>Nginx is not running on {HOSTNAME}</name>
<url/>
<status>0</status>
<priority>4</priority>
<description>Nginx is not running.
It has been stopped / shutdown or has crashed.
Check on the server for more details:
- w / last
- dmesg logs
- /var/log/messages
- nginx error logs</description>
<type>0</type>
<dependencies/>
</trigger>
</triggers>
<graphs>
<graph>
<name>Nginx - Connections and Requests status</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>0</show_work_period>
<show_triggers>0</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>1</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>1</drawtype>
<color>FF9999</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[accepted_connections,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>1</sortorder>
<drawtype>2</drawtype>
<color>990000</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[handled_connections,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>2</sortorder>
<drawtype>0</drawtype>
<color>009900</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[handled_requests,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
</graph_items>
</graph>
<graph>
<name>Nginx - Threads status</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>0</show_work_period>
<show_triggers>0</show_triggers>
<type>1</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>1</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>1</drawtype>
<color>990000</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[writing,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>1</sortorder>
<drawtype>1</drawtype>
<color>999900</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[reading,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
<graph_item>
<sortorder>2</sortorder>
<drawtype>1</drawtype>
<color>009900</color>
<yaxisside>0</yaxisside>
<calc_fnc>4</calc_fnc>
<type>0</type>
<item>
<host>Template_Nginx</host>
<key>nginx[waiting,{$NGINX_STATUS_URL}]</key>
</item>
</graph_item>
</graph_items>
</graph>
</graphs>
</zabbix_export>