用Zabbix监控nginx日志里的http状态码

方法很简单,用脚本取nginx上一个小时输出的日志,然后计算几种http状态码出现的次数即可,修改这里用到的那个脚本,再次感谢krish@toonheart.com


#!/bin/bash
# Script to fetch nginx statuses for tribily monitoring systems
 
HOST=m.xyz.com
#HOST=`/sbin/ifconfig eth0 | sed -n '/inet /{s/.*addr://;s/ .*//;p}'`
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/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}'
        }

# Get http code status

logname='/usr/local/nginx/logs/abc.com.log'

function code_404 {
        more $logname |grep `date -d '1 hour ago' "+%d/%b/%Y:%H"`|cut -d '"' -f3|cut -d ' ' -f2|sort|uniq -c|sort -r|grep 404$|awk '{print $1}'
        }

function code_503 {
        more $logname |grep `date -d '1 hour ago' "+%d/%b/%Y:%H"`|cut -d '"' -f3|cut -d ' ' -f2|sort|uniq -c|sort -r|grep 503$|awk '{print $1}'
        }

function code_499 {
        more $logname |grep `date -d '1 hour ago' "+%d/%b/%Y:%H"`|cut -d '"' -f3|cut -d ' ' -f2|sort|uniq -c|sort -r|grep 499$|awk '{print $1}'
        }

function code_500 {
        more $logname |grep `date -d '1 hour ago' "+%d/%b/%Y:%H"`|cut -d '"' -f3|cut -d ' ' -f2|sort|uniq -c|sort -r|grep 500$|awk '{print $1}'
        }

function code_400 {
        more $logname |grep `date -d '1 hour ago' "+%d/%b/%Y:%H"`|cut -d '"' -f3|cut -d ' ' -f2|sort|uniq -c|sort -r|grep 400$|awk '{print $1}'
        }

# Run the requested function
$1

 

再修改/usr/local/zabbix_agent/conf/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

UserParameter=nginx.code_404,/usr/local/zabbix_agent/bin/get_nginx_status.sh code_404
UserParameter=nginx.code_499,/usr/local/zabbix_agent/bin/get_nginx_status.sh code_499
UserParameter=nginx.code_500,/usr/local/zabbix_agent/bin/get_nginx_status.sh code_500
UserParameter=nginx.code_503,/usr/local/zabbix_agent/bin/get_nginx_status.sh code_503
UserParameter=nginx.code_400,/usr/local/zabbix_agent/bin/get_nginx_status.sh code_400

 

重启zabbix客户端程序,然后在服务器端测试:

root@ABC:/usr/local/zabbix/bin# ./zabbix_get  -s 192.168.0.1 -p 10050 -k "nginx.code_404"
11

 

最后,在Zabbix的web前端修改Template Nginx Status,添加几个状态码的Items即可。

 

 

用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模板即可。

安装zabbix2.2.5+php5.5+mysql5.5.9+nginx1.5.1

先安装php,nginx和mysql
php编译参数:

./configure \
–prefix=/usr/local/php5 \
–with-bz2 \
–with-curl \
–enable-ftp \
–enable-sockets \
–enable-bcmath \
–disable-ipv6 \
–with-gd \
–with-jpeg-dir=/usr/local \
–with-png-dir=/usr/local \
–with-freetype-dir=/usr/local \
–enable-gd-native-ttf \
–with-iconv-dir=/usr/local \
–enable-mbstring \
–enable-calendar \
–with-gettext \
–with-libxml-dir=/usr/local \
–with-zlib \
–with-pdo-mysql=mysqlnd \
–with-mysqli=mysqlnd \
–with-mysql=mysqlnd \
–enable-dom \
–enable-xml \
–enable-fpm \
–with-libdir=lib64

 

php装好之后需要修改php.ini为以下值,以满足zabbix安装需要

max_execution_time = 300
memory_limit = 128M
post_max_size = 16M
upload_max_filesize = 2M
max_input_time = 300
date.timezone PRC

nginx编译参数

./configure –prefix=/usr/local/nginx –add-module=/usr/local/src/nginx_upstream_check_module-master –with-http_stub_status_module –with-http_ssl_module –with-http_realip_module –with-http_sub_module –with-http_gzip_static_module –with-http_flv_module –with-http_dav_module –with-http_spdy_module –with-pcre –with-debug

 
mysql安装略过 安装zabbix
注意:zabbix只使用普通用户运行,如果在root环境下运行zabbix,那么zabbix将会主动使用zabbix用户来运行。

groupadd zabbix&&useradd -g zabbix zabbix
cd /usr/local/src/
wget http://jaist.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/2.2.5/zabbix-2.2.5.tar.gz
tar -zxvf zabbix-2.2.5.tar.gz
cd /usr/local/src/zabbix-2.2.5
./configure –prefix=/usr/local/zabbix/ –enable-server –enable-agent –with-mysql –with-net-snmp –with-libcurl –with-libxml2
make&&make install

 

复制前端文件到主目录

cp -r /usr/local/src/zabbix-2.2.5/frontends/php/* /usr/local/nginx/html/zabbix/

 

修改nginx虚拟主机配置

server {
        listen       80;
        server_name  zabbix.abc.com;
        access_log  /usr/local/nginx/logs/zabbix.abc.com.log  main;

        index         index.html index.htm index.php;
        root         /usr/local/nginx/html/zabbix;

        location / {
                try_files $uri $uri/ /index.php?$args;
        }
        location ~ ^(.+.php)(.*)$ {
                fastcgi_split_path_info ^(.+.php)(.*)$;
                include fastcgi.conf;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param  PATH_INFO          $fastcgi_path_info;
        }
}

 
访问http://zabbix.abc.com开始配置

安装zabbix客户端

groupadd zabbix&&useradd -g zabbix zabbix
cd /usr/loca/src/
wget http://www.zabbix.com/downloads/2.2.5/zabbix_agents_2.2.5.linux2_6.amd64.tar.gz
mkdir /usr/local/zabbix_agent
tar -zxvf zabbix_agents_2.2.5.linux2_6.amd64.tar.gz -C /usr/local/zabbix_agent/
cd /usr/local/zabbix_agent/&&mkdir logs
chown -R zabbix:zabbix/usr/local/zabbix_agent

 

修改客户端配置文件

# vi /usr/local/zabbix_agent/conf/zabbix_agentd.conf

LogFile=/usr/local/zabbix_agent/logs/zabbix_agentd.log
Server=192.168.0.1
ServerActive=192.168.0.1
Hostname=Zabbix server

 

注意:Server和ServerActive都是指定zabbixserver的IP地址,Server是用来允许192.168.0.1来客户端取数据。而serverActive的192.168.0.1的意思是,客户端主动提交数据给服务器端 启动客户端

/usr/local/zabbix_agent/sbin/zabbix_agentd -c /usr/local/zabbix_agent/conf/zabbix_agentd.conf

检查是否正常启动

netstat -nlp|grep 10050

 

写一个客户端启动脚本链接到/etc/init.d/目录下

# more /etc/init.d/zabbix_agentd_srv
 
#!/bin/bash
# chkconfig: 345 20 80
# description: zabbix_agentd auto start-stop script.

case "$1" in

start)
echo "Starting zabbix_agentd ... "
/usr/local/zabbix_agent/sbin/zabbix_agentd -c /usr/local/zabbix_agent/conf/zabbix_agentd.conf
;;

stop)
echo "Stopping zabbix_agentd ... " 
/usr/bin/killall zabbix_agentd 
;;

restart)
$0 stop
sleep 2
$0 start
;;

*)
echo "Usage: $0 {start|stop|restart}" 
exit 1
esac