共计 12099 个字符,预计需要花费 31 分钟才能阅读完成。
手把手教你使用 Zabbix 监控 Nginx。
环境介绍:
服务器系统版本:CentOS release 6.6 (Final)
内核版本:Linux hk_nginx2.6.32-504.3.3.el6.x86_64
ZabbixServer 版本:Zabbix server v2.2.2
ZabbixAgent 版本:Zabbix agent v2.2.2
Nginxweb 版本:nginx version: nginx/1.5.8
在 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。不过,一般情况下都是安装了的。
1. 在 nginx 的配置文件中,添加 status 配置。
location/nginx-status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 192.168.1.10; #(zabbix 服务器的 IP 地址,一般是内网地址)
deny all;
}
2. 访问设置好的 nginx-status 链接,如图所示:
3.nginx Status 详细说明:
Activeconnections: 对后端发起的活动连接数;
server accepts 2349542:nginx 总共处理了 2349542 个连接;
handled:成功创建了 64603417 次握手;
requests:总共处理了 8798670 请求。
Reading:nginx 读取客户端的 header 数;
Writing: nginx 返回给客户端的 header 数;
Waiting: nginx 请求处理完成,正在等待下一请求指令的连接。
4. 在 agentd 上编写监控 nginx 的脚本,并且设置属主和属组为 zabbix,赋予执行权限。
cd /usr/local/zabbix/scripts
cat nginx_status.sh
#!/bin/bash
# Script to fetch nginx statuses for tribily monitoring systems
# Author: krish@toonheart.com
# License: GPLv2
# Set Variables
BKUP_DATE=`/bin/date +%Y%m%d`
LOG="/data/log/zabbix/webstatus.log"
HOST=127.0.0.1
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
ll
total 4
-rwxr-xr-x 1 zabbix zabbix 1273 May 13 17:42 nginx_status.sh
5. 修改 nginx 服务器上 zabbix 客户端的 zabbix_agentd.conf 配置文件,然后重启 zabbix agentd 客户端。
cd /usr/local/zabbix/etc
grep -v "^[#;]" zabbix_agentd.conf | grep -v "^$"
LogFile=/tmp/zabbix_agentd.log
Server=192.168.1.10 #zabbix server 端的 IP 地址
ServerActive=192.168.1.10 #zabbix server 端的 IP 地址
Hostname=192.168.1.5 #本地的 IP 地址
UnsafeUserParameters=1
## 下面的是新添加进去的
UserParameter=nginx.accepts,/usr/local/zabbix/scripts/nginx_status.sh accepts
UserParameter=nginx.handled,/usr/local/zabbix/scripts/nginx_status.sh handled
UserParameter=nginx.requests,/usr/local/zabbix/scripts/nginx_status.sh requests
UserParameter=nginx.connections.active,/usr/local/zabbix/scripts/nginx_status.sh active
UserParameter=nginx.connections.reading,/usr/local/zabbix/scripts/nginx_status.sh reading
UserParameter=nginx.connections.writing,/usr/local/zabbix/scripts/nginx_status.sh writing
UserParameter=nginx.connections.waiting,/usr/local/zabbix/scripts/nginx_status.sh waiting
6. 在 zabbix server 端进行 zabbix_get 测试, 取到数据了,说明没问题。
/usr/local/zabbix/bin/zabbix_get -s 192.168.1.5 -p 10050 -k "nginx.connections.active"
11
/usr/local/zabbix/bin/zabbix_get -s 192.168.1.5 -p 10050 -k "nginx.connections.waiting"
10
/usr/local/zabbix/bin/zabbix_get -s 192.168.1.5 -p 10050 -k "nginx.connections.writing"
1
/usr/local/zabbix/bin/zabbix_get -s 192.168.1.5 -p 10050 -k "nginx.accepts"
2350638
/usr/local/zabbix/bin/zabbix_get -s 192.168.1.5 -p 10050 -k "nginx.requests"
8801086
7. 在网上搜到一个 zabbix 中 nginx status 的模板, 把模板导入 zabbix 服务器。
- <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 服务器首页,点击”组态”-“模板”。
点击”汇入”, 如图所示
点击“汇入文件“,然后把找到的 nginx_status.xml 文件,汇入模板。
创建应用集 Nginx Server。
把 nginx 主机连接到模板上去。
最后,看下是否可以检测到数据,是否可以顺利出图。
到此为止,实验结束。
一些 Zabbix 相关教程集合 :
Ubuntu 14.04 下 Zabbix2.4.5 源码编译安装 http://www.linuxidc.com/Linux/2015-05/117657.htm
安装部署分布式监控系统 Zabbix 2.06 http://www.linuxidc.com/Linux/2013-07/86942.htm
《安装部署分布式监控系统 Zabbix 2.06》http://www.linuxidc.com/Linux/2013-07/86942.htm
CentOS 6.3 下 Zabbix 安装部署 http://www.linuxidc.com/Linux/2013-05/83786.htm
Zabbix 分布式监控系统实践 http://www.linuxidc.com/Linux/2013-06/85758.htm
CentOS 6.3 下 Zabbix 监控 apache server-status http://www.linuxidc.com/Linux/2013-05/84740.htm
CentOS 6.3 下 Zabbix 监控 MySQL 数据库参数 http://www.linuxidc.com/Linux/2013-05/84800.htm
64 位 CentOS 6.2 下安装 Zabbix 2.0.6 http://www.linuxidc.com/Linux/2014-11/109541.htm
ZABBIX 的详细介绍 :请点这里
ZABBIX 的下载地址 :请点这里
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2015-09/123039.htm