使用Zabbix监控Nginx图文详解


手把手教你使用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链接,如图所示:

使用Zabbix监控Nginx图文详解

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服务器首页,点击”组态”-“模板”。

使用Zabbix监控Nginx图文详解

点击”汇入”,如图所示

使用Zabbix监控Nginx图文详解

点击“汇入文件“,然后把找到的nginx_status.xml文件,汇入模板。

使用Zabbix监控Nginx图文详解

创建应用集Nginx Server。

使用Zabbix监控Nginx图文详解使用Zabbix监控Nginx图文详解

把nginx主机连接到模板上去。

使用Zabbix监控Nginx图文详解

最后,看下是否可以检测到数据,是否可以顺利出图。

使用Zabbix监控Nginx图文详解使用Zabbix监控Nginx图文详解

到此为止,实验结束。

一些Zabbix相关教程集合:

Ubuntu 14.04下Zabbix2.4.5 源码编译安装 

安装部署分布式监控系统Zabbix 2.06

《安装部署分布式监控系统Zabbix 2.06》

CentOS 6.3下Zabbix安装部署

Zabbix分布式监控系统实践

CentOS 6.3下Zabbix监控apache server-status

CentOS 6.3下Zabbix监控MySQL数据库参数

64位CentOS 6.2下安装Zabbix 2.0.6   

ZABBIX 的详细介绍:请点这里
ZABBIX 的下载地址:请点这里

本文永久更新链接地址

相关内容