Zabbix監控Nginx效能的實現方式
nginx和php-fpm一樣內建了一個狀態頁,對於想了解nginx的狀態以及監控nginx非常有用,為了後續的zabbix監控,我們需要先啟用nginx狀態頁
1. 啟用nginx status配置
在預設主機裡面加上location或者你希望能訪問到的主機裡面。
server {
location /ngx_status
{
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
示例:
upstream lvs_server{
server 10.27.13.215:8555; #hd_lvs_voice01
server 10.26.114.166:8555; #hd_lvs_voice02
server 10.27.62.9:8555; #hd_lvs_voice03
server 10.30.196.175:8555; #hd_lvs_voice04
#server 10.30.196.157:8555; #hd_lvs_voice05
}
server {
listen 8555;
access_log /var/log/nginx/voice_lvs.access.log main;
error_log /var/log/nginx/voice_lvs.error.log;
location / {
proxy_pass http://lvs_server;
}
location /index {
proxy_set_header Host $http_host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Content-Type application/octet-stream;
proxy_pass http://lvs_server;
}
location /ngx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
#deny all;
}
}
2. 過載nginx
# nginx -t
# service nginx reload
3. 開啟status頁面
# curl 127.0.0.1:8555/ngx_status
Active connections: 2
server accepts handled requests
3091 3091 3254
Reading: 0 Writing: 1 Waiting: 1
# curl 127.0.0.1:8555/ngx_status
Active connections: 2
server accepts handled requests
3092 3092 3255
Reading: 0 Writing: 1 Waiting: 1
4. nginx status詳解
active connections – 活躍的連線數量
server accepts handled requests — 總共處理了11989個連線 , 成功建立11989次握手, 總共處理了11991個請求
reading — 讀取客戶端的連線數.
writing — 響應資料到客戶端的數量
waiting — 開啟 keep-alive 的情況下,這個值等於 active – (reading+writing), 意思就是 Nginx 已經處理完正在等候下一次請求指令的駐留連線.
以上為nginx效能計數,我們除了監控以上資料,還需要監控nginx程序狀態,並且配置觸發器
zabbix客戶端配置
編寫客戶端指令碼nginx_status.sh
# 檢測nginx效能
mkdir -p /usr/local/zabbix-agent/scripts
vim /usr/local/zabbix-agent/scripts/nginx_status.sh
#!/bin/bash
HOST="127.0.0.1"
PORT="8555"
# 檢測nginx程序是否存在
function ping {
/sbin/pidof nginx | wc -l
}
# 檢測nginx效能
function active {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
/usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# 執行function
$1
新增指令碼執行許可權
# chmod +x /usr/local/zabbix-agent/scripts/nginx_status.sh
zabbix客戶端配置
將自定義的UserParameter加入配置檔案,然後重啟agentd,如下:
# vim /etc/zabbix/zabbix_agentd.conf
UserParameter=nginx.status[*],/usr/local/zabbix-agent/scripts/nginx_status.sh $1
重啟zabbix-agent
/etc/init.d/zabbix-agent restart
zabbix_get獲取資料
此步驟可以跳過,但是最好是測試一下,因為通過此命令我們可以檢測配置是否正確
# zabbix_get -s ip -p 10050 -k 'nginx.status[accepts]'
3101
# zabbix_get -s ip -p 10050 -k 'nginx.status[ping]'
1
zabbix web端配置
匯入Template App NGINX模板
Link NGINX模板
到了最後一個階段,登陸zabbix管理端,link模板到nginx伺服器:configuration->hosts->點選nginx所在伺服器->點選template->Link new templates輸入"Template App NGINX"->Add->最後點選update
監控效果
監控模板:zbx3_export_templates.xml
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>3.0</version>
<date>2017-07-17T09:37:18Z</date>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<templates>
<template>
<template>Template nginx status</template>
<name>Template nginx status</name>
<description>nginx監控模板</description>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<applications>
<application>
<name>nginx</name>
</application>
</applications>
<items>
<item>
<name>nginx status server accepts</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx.status[accepts]</key>
<delay>60</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>1</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>accepts</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx status connections active</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx.status[active]</key>
<delay>60</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>active</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx status server handled</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx.status[handled]</key>
<delay>60</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>1</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>handled</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx status PING</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx.status[ping]</key>
<delay>60</delay>
<history>30</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>is live</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>nginx</name>
</application>
</applications>
<valuemap>
<name>Service state</name>
</valuemap>
<logtimefmt/>
</item>
<item>
<name>nginx status connections reading</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx.status[reading]</key>
<delay>60</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>reading</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx status server requests</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx.status[requests]</key>
<delay>60</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>1</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>requests</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx status connections waiting</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx.status[waiting]</key>
<delay>60</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>waiting</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
<item>
<name>nginx status connections writing</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>nginx.status[writing]</key>
<delay>60</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description>writing</description>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>nginx</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
</item>
</items>
<discovery_rules/>
<macros/>
<templates/>
<screens/>
</template>
</templates>
<triggers>
<trigger>
<expression>{Template nginx status:nginx.status[ping].last(0)}=0 and {Template nginx status:nginx.status[ping].last(1)}=0</expression>
<name>nginx is down!</name>
<url/>
<status>0</status>
<priority>4</priority>
<description/>
<type>0</type>
<dependencies/>
</trigger>
</triggers>
<graphs>
<graph>
<name>nginx status</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>1</show_work_period>
<show_triggers>1</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>0</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>0</drawtype>
<color>1A7C11</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template nginx status</host>
<key>nginx.status[active]</key>
</item>
</graph_item>
<graph_item>
<sortorder>1</sortorder>
<drawtype>0</drawtype>
<color>F63100</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template nginx status</host>
<key>nginx.status[reading]</key>
</item>
</graph_item>
<graph_item>
<sortorder>2</sortorder>
<drawtype>0</drawtype>
<color>2774A4</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template nginx status</host>
<key>nginx.status[waiting]</key>
</item>
</graph_item>
<graph_item>
<sortorder>3</sortorder>
<drawtype>0</drawtype>
<color>A54F10</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template nginx status</host>
<key>nginx.status[writing]</key>
</item>
</graph_item>
<graph_item>
<sortorder>4</sortorder>
<drawtype>0</drawtype>
<color>FC6EA3</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template nginx status</host>
<key>nginx.status[accepts]</key>
</item>
</graph_item>
<graph_item>
<sortorder>5</sortorder>
<drawtype>0</drawtype>
<color>6C59DC</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template nginx status</host>
<key>nginx.status[handled]</key>
</item>
</graph_item>
<graph_item>
<sortorder>6</sortorder>
<drawtype>0</drawtype>
<color>AC8C14</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template nginx status</host>
<key>nginx.status[requests]</key>
</item>
</graph_item>
</graph_items>
</graph>
</graphs>
<value_maps>
<value_map>
<name>Service state</name>
<mappings>
<mapping>
<value>0</value>
<newvalue>Down</newvalue>
</mapping>
<mapping>
<value>1</value>
<newvalue>Up</newvalue>
</mapping>
</mappings>
</value_map>
</value_maps>
</zabbix_export>