admin
不忘初心,方得始终
级别: 管理员
只看楼主 | | | 0楼 发表于:2016-03-24 17:23

Zabbix server监控Nginx Web Server

1.环境介绍:
Zabbix Server:172.17.8.112
Nginx Server(Zabbix Agent): 172.17.8.109
Zabbix Server监控Nginx web server,需要nginx加载了—with-http_stub_status_module模块。因为zabbix监控nginx是根据nginx的Stub Status模块,抓取Status模块所提供的数据。
2.在nginx的配置文件nginx.conf配置文件中添加status配置。
location /nginx-status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;
            allow 172.17.8.112 #(zabbix服务器的IP地址,一般是内网地址)
            deny all;
        }
3.访问设置好的nginx-status链接。
Nginx Status参数说明:
Active Connections:对后端发起的活动连接数。
server accepts:nginx一共处理了XXX个连接。
Handled: 成功创建了XXX次握手。
Requests:一共处理了XXX请求。
Reading:nginx读取客户端的header数。
Writing:nginx返回给客户端的header数。
Waiting:nginx请求处理完成,正在等待下一个请求指令的连接。
4.在zabbix_agentd上编写监控nginx的脚本,并且设置属主和属组为zabbix,赋予执行权限。
mkdir –p /etc/zabbix/scripts
cd /etc/zabbix/scripts
vim nginx_status.sh,内容如下:
#!/bin/bash
# Script to fetch nginx statuses for tribily monitoring systems
# Author: lutaoxu@87long.com
# License: GPLv2
# Set Variables
#BKUP_DATE=`/bin/date +%Y%m%d`
HOST=localhost
PORT="80"
# Functions to return nginx stats
function active {
  /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Active' | awk '{print $NF}'
  }
function reading {
  /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Reading' | awk '{print $2}'
  }
function writing {
  /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Writing' | awk '{print $4}'
  }
function waiting {
  /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
  }
function accepts {
  /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $1}'
  }
function handled {
  /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $2}'
  }
function requests {
  /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $3}'
  }
# Run the requested function
$1
chmod o+x nginx_status.sh
chown zabbix.zabbix nginx_status.sh
5.修改nginx服务器上zabbix客户端zabbix_agentd.conf配置文件,在配置文件最下面添加监控内容后,重启zabbix agentd服务。
UserParameter=nginx.accepts,/etc/zabbix/scripts/nginx_status.sh accepts  
UserParameter=nginx.handled,/etc/zabbix/scripts/nginx_status.sh handled  
UserParameter=nginx.requests,/etc/zabbix/scripts/nginx_status.sh requests  
UserParameter=nginx.connections.active,/etc/zabbix/scripts/nginx_status.sh active  
UserParameter=nginx.connections.reading,/etc/zabbix/scripts/nginx_status.sh reading  
UserParameter=nginx.connections.writing,/etc/zabbix/scripts/nginx_status.sh writing  
UserParameter=nginx.connections.waiting,/etc/zabbix/scripts/nginx_status.sh waiting
6.在zabbix  server端进行Zabbix_get测试,获取到数据,说明没问题。
/usr/bin/zabbix_get -s 172.17.8.109 -p 10050 -k "nginx.connections.active"
/usr/bin/zabbix_get -s172.17.8.109 -p 10050 -k "nginx.connections.writing"
/usr/bin/zabbix_get -s 172.17.8.109 -p 10050 -k "nginx.connections.reading"
/usr/bin/zabbix_get -s 172.17.8.109 -p 10050 -k "nginx.connections.waiting"
/usr/bin/zabbix_get -s 172.17.8.109 -p 10050 -k "nginx.requests"
/usr/bin/zabbix_get –s 172.17.8.109 -p 10050 -k "nginx.handled"
/usr/bin/zabbix_get -s 172.17.8.109 -p 10050 -k "nginx.accepts"
7.在网上搜索zabbix中nginx status的模板,把模板导入zabbix 服务器。
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export version="1.0" date="14.03.11" time="08.48">
- <hosts>
- <host name="Nginx Status">
  <proxy_hostid>0</proxy_hostid>
  <useip>1</useip>
  <dns />
  <ip>127.0.0.1</ip>
  <port>10050</port>
  <status>3</status>
  <useipmi>0</useipmi>
  <ipmi_ip>127.0.0.1</ipmi_ip>
  <ipmi_port>623</ipmi_port>
  <ipmi_authtype>0</ipmi_authtype>
  <ipmi_privilege>2</ipmi_privilege>
  <ipmi_username />
  <ipmi_password />
- <groups>
  <group>Freetrade</group>
  </groups>
  <triggers />
- <items>
- <item type="7" key="nginx.accepts" value_type="3">
  <description>Nginx Accepts</description>
  <ipmi_sensor />
  <delay>30</delay>
  <history>365</history>
  <trends>365</trends>
  <status>0</status>
  <data_type>0</data_type>
  <units />
  <multiplier>0</multiplier>
  <delta>0</delta>
  <formula>0</formula>
  <lastlogsize>0</lastlogsize>
  <logtimefmt />
  <delay_flex />
  <authtype>0</authtype>
  <username />
  <password />
  <publickey />
  <privatekey />
  <params />
  <trapper_hosts>localhost</trapper_hosts>
  <snmp_community />
  <snmp_oid />
  <snmp_port>161</snmp_port>
  <snmpv3_securityname />
  <snmpv3_securitylevel>0</snmpv3_securitylevel>
  <snmpv3_authpassphrase />
  <snmpv3_privpassphrase />
- <applications>
  <application>Nginx</application>
  </applications>
  </item>
- <item type="7" key="nginx.connections.active" value_type="3">
  <description>Nginx Connections Active</description>
  <ipmi_sensor />
  <delay>30</delay>
  <history>365</history>
  <trends>365</trends>
  <status>0</status>
  <data_type>0</data_type>
  <units />
  <multiplier>0</multiplier>
  <delta>0</delta>
  <formula>0</formula>
  <lastlogsize>0</lastlogsize>
  <logtimefmt />
  <delay_flex />
  <authtype>0</authtype>
  <username />
  <password />
  <publickey />
  <privatekey />
  <params />
  <trapper_hosts>localhost</trapper_hosts>
  <snmp_community />
  <snmp_oid />
  <snmp_port>161</snmp_port>
  <snmpv3_securityname />
  <snmpv3_securitylevel>0</snmpv3_securitylevel>
  <snmpv3_authpassphrase />
  <snmpv3_privpassphrase />
- <applications>
  <application>Nginx</application>
  </applications>
  </item>
- <item type="7" key="nginx.connections.reading" value_type="3">
  <description>Nginx Connections Reading</description>
  <ipmi_sensor />
  <delay>30</delay>
  <history>365</history>
  <trends>365</trends>
  <status>0</status>
  <data_type>0</data_type>
  <units />
  <multiplier>0</multiplier>
  <delta>0</delta>
  <formula>0</formula>
  <lastlogsize>0</lastlogsize>
  <logtimefmt />
  <delay_flex />
  <authtype>0</authtype>
  <username />
  <password />
  <publickey />
  <privatekey />
  <params />
  <trapper_hosts>localhost</trapper_hosts>
  <snmp_community />
  <snmp_oid />
  <snmp_port>161</snmp_port>
  <snmpv3_securityname />
  <snmpv3_securitylevel>0</snmpv3_securitylevel>
  <snmpv3_authpassphrase />
  <snmpv3_privpassphrase />
- <applications>
  <application>Nginx</application>
  </applications>
  </item>
- <item type="7" key="nginx.connections.waiting" value_type="3">
  <description>Nginx Connections Waiting</description>
  <ipmi_sensor />
  <delay>30</delay>
  <history>365</history>
  <trends>365</trends>
  <status>0</status>
  <data_type>0</data_type>
  <units />
  <multiplier>0</multiplier>
  <delta>0</delta>
  <formula>0</formula>
  <lastlogsize>0</lastlogsize>
  <logtimefmt />
  <delay_flex />
  <authtype>0</authtype>
  <username />
  <password />
  <publickey />
  <privatekey />
  <params />
  <trapper_hosts>localhost</trapper_hosts>
  <snmp_community />
  <snmp_oid />
  <snmp_port>161</snmp_port>
  <snmpv3_securityname />
  <snmpv3_securitylevel>0</snmpv3_securitylevel>
  <snmpv3_authpassphrase />
  <snmpv3_privpassphrase />
- <applications>
  <application>Nginx</application>
  </applications>
  </item>
- <item type="7" key="nginx.connections.writing" value_type="3">
  <description>Nginx Connections Writing</description>
  <ipmi_sensor />
  <delay>30</delay>
  <history>365</history>
  <trends>365</trends>
  <status>0</status>
  <data_type>0</data_type>
  <units />
  <multiplier>0</multiplier>
  <delta>0</delta>
  <formula>0</formula>
  <lastlogsize>0</lastlogsize>
  <logtimefmt />
  <delay_flex />
  <authtype>0</authtype>
  <username />
  <password />
  <publickey />
  <privatekey />
  <params />
  <trapper_hosts>localhost</trapper_hosts>
  <snmp_community />
  <snmp_oid />
  <snmp_port>161</snmp_port>
  <snmpv3_securityname />
  <snmpv3_securitylevel>0</snmpv3_securitylevel>
  <snmpv3_authpassphrase />
  <snmpv3_privpassphrase />
- <applications>
  <application>Nginx</application>
  </applications>
  </item>
- <item type="7" key="nginx.handled" value_type="3">
  <description>Nginx Handled</description>
  <ipmi_sensor />
  <delay>30</delay>
  <history>365</history>
  <trends>365</trends>
  <status>0</status>
  <data_type>0</data_type>
  <units />
  <multiplier>0</multiplier>
  <delta>0</delta>
  <formula>0</formula>
  <lastlogsize>0</lastlogsize>
  <logtimefmt />
  <delay_flex />
  <authtype>0</authtype>
  <username />
  <password />
  <publickey />
  <privatekey />
  <params />
  <trapper_hosts>localhost</trapper_hosts>
  <snmp_community />
  <snmp_oid />
  <snmp_port>161</snmp_port>
  <snmpv3_securityname />
  <snmpv3_securitylevel>0</snmpv3_securitylevel>
  <snmpv3_authpassphrase />
  <snmpv3_privpassphrase />
- <applications>
  <application>Nginx</application>
  </applications>
  </item>
- <item type="7" key="nginx.requests" value_type="3">
  <description>Nginx Requests</description>
  <ipmi_sensor />
  <delay>30</delay>
  <history>365</history>
  <trends>365</trends>
  <status>0</status>
  <data_type>0</data_type>
  <units />
  <multiplier>0</multiplier>
  <delta>0</delta>
  <formula>0</formula>
  <lastlogsize>0</lastlogsize>
  <logtimefmt />
  <delay_flex />
  <authtype>0</authtype>
  <username />
  <password />
  <publickey />
  <privatekey />
  <params />
  <trapper_hosts>localhost</trapper_hosts>
  <snmp_community />
  <snmp_oid />
  <snmp_port>161</snmp_port>
  <snmpv3_securityname />
  <snmpv3_securitylevel>0</snmpv3_securitylevel>
  <snmpv3_authpassphrase />
  <snmpv3_privpassphrase />
- <applications>
  <application>Nginx</application>
  </applications>
  </item>
  </items>
  <templates />
- <graphs>
- <graph name="Nginx Socket Status" width="900" height="200">
  <ymin_type>0</ymin_type>
  <ymax_type>0</ymax_type>
  <ymin_item_key />
  <ymax_item_key />
  <show_work_period>1</show_work_period>
  <show_triggers>1</show_triggers>
  <graphtype>0</graphtype>
  <yaxismin>0.0000</yaxismin>
  <yaxismax>100.0000</yaxismax>
  <show_legend>0</show_legend>
  <show_3d>0</show_3d>
  <percent_left>0.0000</percent_left>
  <percent_right>0.0000</percent_right>
- <graph_elements>
- <graph_element item="Nginx Status:nginx.accepts">
  <drawtype>0</drawtype>
  <sortorder>0</sortorder>
  <color>00EE00</color>
  <yaxisside>0</yaxisside>
  <calc_fnc>2</calc_fnc>
  <type>0</type>
  <periods_cnt>5</periods_cnt>
  </graph_element>
- <graph_element item="Nginx Status:nginx.handled">
  <drawtype>0</drawtype>
  <sortorder>0</sortorder>
  <color>EE0000</color>
  <yaxisside>0</yaxisside>
  <calc_fnc>2</calc_fnc>
  <type>0</type>
  <periods_cnt>5</periods_cnt>
  </graph_element>
- <graph_element item="Nginx Status:nginx.requests">
  <drawtype>0</drawtype>
  <sortorder>1</sortorder>
  <color>EEEE00</color>
  <yaxisside>0</yaxisside>
  <calc_fnc>2</calc_fnc>
  <type>0</type>
  <periods_cnt>5</periods_cnt>
  </graph_element>
  </graph_elements>
  </graph>
- <graph name="Nginx Clients Status" width="900" height="200">
  <ymin_type>0</ymin_type>
  <ymax_type>0</ymax_type>
  <ymin_item_key />
  <ymax_item_key />
  <show_work_period>1</show_work_period>
  <show_triggers>1</show_triggers>
  <graphtype>0</graphtype>
  <yaxismin>0.0000</yaxismin>
  <yaxismax>100.0000</yaxismax>
  <show_legend>0</show_legend>
  <show_3d>0</show_3d>
  <percent_left>0.0000</percent_left>
  <percent_right>0.0000</percent_right>
- <graph_elements>
- <graph_element item="Nginx Status:nginx.connections.active">
  <drawtype>0</drawtype>
  <sortorder>0</sortorder>
  <color>0000EE</color>
  <yaxisside>0</yaxisside>
  <calc_fnc>2</calc_fnc>
  <type>0</type>
  <periods_cnt>5</periods_cnt>
  </graph_element>
- <graph_element item="Nginx Status:nginx.connections.writing">
  <drawtype>0</drawtype>
  <sortorder>1</sortorder>
  <color>EE0000</color>
  <yaxisside>0</yaxisside>
  <calc_fnc>2</calc_fnc>
  <type>0</type>
  <periods_cnt>5</periods_cnt>
  </graph_element>
- <graph_element item="Nginx Status:nginx.connections.waiting">
  <drawtype>0</drawtype>
  <sortorder>2</sortorder>
  <color>EEEE00</color>
  <yaxisside>0</yaxisside>
  <calc_fnc>2</calc_fnc>
  <type>0</type>
  <periods_cnt>5</periods_cnt>
  </graph_element>
- <graph_element item="Nginx Status:nginx.connections.reading">
  <drawtype>0</drawtype>
  <sortorder>3</sortorder>
  <color>00EE00</color>
  <yaxisside>0</yaxisside>
  <calc_fnc>2</calc_fnc>
  <type>0</type>
  <periods_cnt>5</periods_cnt>
  </graph_element>
  </graph_elements>
  </graph>
  </graphs>
  <macros />
  </host>
  </hosts>
  <dependencies />
  </zabbix_export>
8.导入模板。登录zabbix web界面后,点击“组态”—“模板”—“汇入”—“浏览文件”—“选择文件”—“汇入”。
9.创建被监控的主机。
登录zabbix web界面后,选择“组态”—“主机”—“创建主机”—“模板”—“选择nginx的模板”—“启用主机”。