用Zabbix监控Nginx

首先在需要监控的nginx虚拟主机中打开stub_status配置项,并允许zabbxi服务器访问

location /nginx-status {
    stub_status on;
    access_log off;
    allow 1.2.3.4;
    deny all;
}

 

访问nginx-status页面得到的结果如下

Active connections: 1121 
server accepts handled requests
 1189 1189 1191 
Reading: 0 Writing: 7 Waiting: 42

 

结果说明:

active connections – 活跃的连接数量
server accepts handled requests — 总共处理了1189个连接 , 成功创建1189次握手, 总共处理了1191个请求
reading — 读取客户端的连接数.
writing — 响应数据到客户端的数量
waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.

 

添加get_nginx_status.sh脚本(感谢krish@toonheart.com,直接拿来用了),客户端服务器上需要有curl环境

#!/bin/bash#
# Script to fetch nginx statuses for tribily monitoring systems
# Author: [email]krish@toonheart.com[/email]
# License: GPLv2
 
HOST=www.abc.com
PORT="80"
#webstatus=nginx-status

# 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/in/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

 

在zabbix客户端配置文件中Include目录/usr/local/zabbix_agent/conf/zabbix_agentd,并设置UnsafeUserParameters=1
在zabbix_agentd文件夹中添加文件userparameter_nginx.conf,内容如下:

UserParameter=nginx.accepts,/usr/local/zabbix_agent/bin/get_nginx_status.sh accepts
UserParameter=nginx.handled,/usr/local/zabbix_agent/bin/get_nginx_status.sh handled
UserParameter=nginx.requests,/usr/local/zabbix_agent/bin/get_nginx_status.sh requests
UserParameter=nginx.connections.active,/usr/local/zabbix_agent/bin/get_nginx_status.sh active
UserParameter=nginx.connections.reading,/usr/local/zabbix_agent/bin/get_nginx_status.sh reading
UserParameter=nginx.connections.writing,/usr/local/zabbix_agent/bin/get_nginx_status.sh writing
UserParameter=nginx.connections.waiting,/usr/local/zabbix_agent/bin/get_nginx_status.sh waiting

 

在zabbix前端导入nginx模板,内容如下

<zabbix_export version="1.0" date="14.03.11" time="08.48">
  
    
      <proxy_hostid>0
      1
      
      127.0.0.1
      10050
      3
      0
      <ipmi_ip>127.0.0.1
      <ipmi_port>623
      <ipmi_authtype>0
      <ipmi_privilege>2
      <ipmi_username>
      <ipmi_password>
      
        cy2009
      
      
      
        
          Nginx Accepts
          <ipmi_sensor>
          30
          365
          365
          0
          <data_type>0
          
          0
          0
          0
          0
          
          <delay_flex>
          0
          
          
          
          
          
          <trapper_hosts>localhost
          <snmp_community>
          <snmp_oid>
          <snmp_port>161
          <snmpv3_securityname>
          <snmpv3_securitylevel>0
          <snmpv3_authpassphrase>
          <snmpv3_privpassphrase>
          
            Nginx
          
        
        
          Nginx Connections Active
          <ipmi_sensor>
          30
          365
          365
          0
          <data_type>0
          
          0
          0
          0
          0
          
          <delay_flex>
          0
          
          
          
          
          
          <trapper_hosts>localhost
          <snmp_community>
          <snmp_oid>
          <snmp_port>161
          <snmpv3_securityname>
          <snmpv3_securitylevel>0
          <snmpv3_authpassphrase>
          <snmpv3_privpassphrase>
          
            Nginx
          
        
        
          Nginx Connections Reading
          <ipmi_sensor>
          30
          365
          365
          0
          <data_type>0
          
          0
          0
          0
          0
          
          <delay_flex>
          0
          
          
          
          
          
          <trapper_hosts>localhost
          <snmp_community>
          <snmp_oid>
          <snmp_port>161
          <snmpv3_securityname>
          <snmpv3_securitylevel>0
          <snmpv3_authpassphrase>
          <snmpv3_privpassphrase>
          
            Nginx
          
        
        
          Nginx Connections Waiting
          <ipmi_sensor>
          30
          365
          365
          0
          <data_type>0
          
          0
          0
          0
          0
          
          <delay_flex>
          0
          
          
          
          
          
          <trapper_hosts>localhost
          <snmp_community>
          <snmp_oid>
          <snmp_port>161
          <snmpv3_securityname>
          <snmpv3_securitylevel>0
          <snmpv3_authpassphrase>
          <snmpv3_privpassphrase>
          
            Nginx
          
        
        
          Nginx Connections Writing
          <ipmi_sensor>
          30
          365
          365
          0
          <data_type>0
          
          0
          0
          0
          0
          
          <delay_flex>
          0
          
          
          
          
          
          <trapper_hosts>localhost
          <snmp_community>
          <snmp_oid>
          <snmp_port>161
          <snmpv3_securityname>
          <snmpv3_securitylevel>0
          <snmpv3_authpassphrase>
          <snmpv3_privpassphrase>
          
            Nginx
          
        
        
          Nginx Handled
          <ipmi_sensor>
          30
          365
          365
          0
          <data_type>0
          
          0
          0
          0
          0
          
          <delay_flex>
          0
          
          
          
          
          
          <trapper_hosts>localhost
          <snmp_community>
          <snmp_oid>
          <snmp_port>161
          <snmpv3_securityname>
          <snmpv3_securitylevel>0
          <snmpv3_authpassphrase>
          <snmpv3_privpassphrase>
          
            Nginx
          
        
        
          Nginx Requests
          <ipmi_sensor>
          30
          365
          365
          0
          <data_type>0
          
          0
          0
          0
          0
          
          <delay_flex>
          0
          
          
          
          
          
          <trapper_hosts>localhost
          <snmp_community>
          <snmp_oid>
          <snmp_port>161
          <snmpv3_securityname>
          <snmpv3_securitylevel>0
          <snmpv3_authpassphrase>
          <snmpv3_privpassphrase>
          
            Nginx
          
        
      
      
      
        
          <ymin_type>0
          <ymax_type>0
          <ymin_item_key>
          <ymax_item_key>
          <show_work_period>1
          <show_triggers>1
          0
          0.0000
          100.0000
          <show_legend>0
          <show_3d>0
          <percent_left>0.0000
          <percent_right>0.0000
          <graph_elements>
            <graph_element item="Nginx Status:nginx.accepts">
              0
              0
              00EE00
              0
              <calc_fnc>2
              0
              <periods_cnt>5
            
            <graph_element item="Nginx Status:nginx.handled">
              0
              0
              EE0000
              0
              <calc_fnc>2
              0
              <periods_cnt>5
            
            <graph_element item="Nginx Status:nginx.requests">
              0
              1
              EEEE00
              0
              <calc_fnc>2
              0
              <periods_cnt>5
            
          
        
        
          <ymin_type>0
          <ymax_type>0
          <ymin_item_key>
          <ymax_item_key>
          <show_work_period>1
          <show_triggers>1
          0
          0.0000
          100.0000
          <show_legend>0
          <show_3d>0
          <percent_left>0.0000
          <percent_right>0.0000
          <graph_elements>
            <graph_element item="Nginx Status:nginx.connections.active">
              0
              0
              0000EE
              0
              <calc_fnc>2
              0
              <periods_cnt>5
            
            <graph_element item="Nginx Status:nginx.connections.writing">
              0
              1
              EE0000
              0
              <calc_fnc>2
              0
              <periods_cnt>5
            
            <graph_element item="Nginx Status:nginx.connections.waiting">
              0
              2
              EEEE00
              0
              <calc_fnc>2
              0
              <periods_cnt>5
            
            <graph_element item="Nginx Status:nginx.connections.reading">
              0
              3
              00EE00
              0
              <calc_fnc>2
              0
              <periods_cnt>5

 

然后在zabbix前端为主机链接nginx status模板即可。

发表评论

电子邮件地址不会被公开。 必填项已用*标注