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

 

 

发表评论

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