zabbix监控nginx

 nginx status详解

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

--------------------------------------------

首先在agent端的nginx.conf中添加下面内容

# vim /usr/local/nginx/conf/nginx.conf

server {
           listen 80;
           server_name localhost;

           location /nginxstatus{
                                          stub_status on;
                                          access_log off;
                                          allow 127.0.0.1;
                                          allow 192.168.1.0/24;                ----->zabbix server端的IP地址
                                          deny all;
                                          }

}

 

然后写监控nginx状态的脚本

# vim /usr/local/zabbix/scripts/nginx.status.sh

#!/bin/bash
function active {
                       curl "http://127.0.0.1/nginxstatus" 2>/dev/null|awk '/Active/ {print $3}'
}
function reading {
                         curl "http://127.0.0.1/nginxstatus" 2>/dev/null|awk '/Reading/ {print $2}'
}
function writing {
                        curl "http://127.0.0.1/nginxstatus" 2>/dev/null|awk '/Writing/ {print $4}'
}
function waiting {
                       curl "http://127.0.0.1/nginxstatus" 2>/dev/null|awk '/Waiting/ {print $6}'
}
function accepts {
                         curl "http://127.0.0.1/nginxstatus" 2>/dev/null|awk NR==3|awk '{print $1}'
}
function handled {
                         curl "http://127.0.0.1/nginxstatus" 2>/dev/null|awk NR==3|awk '{print $2}'

}

function requests {
                         curl "http://127.0.0.1/nginxstatus" 2>/dev/null|awk NR==3|awk '{print $3}'

}

case "$1" in
active)
       active
       ;;
reading)
       reading
       ;;
writing)
       writing
       ;;
waiting)
       waiting
       ;;
accepts)
      accepts
      ;;
handled)
      handled
      ;;
requests)
     requests
     ;;
*)
echo "Usage: $0 {nginx_site_dicovery}"
      echo "Usage: $0 {active [host]|reading [host]|writing [host]|waiting [host]|accepts [host]|handled [host]|requests [host]}"
      esac

修改权限

# chmod 755 /usr/local/zabbix/scripts/mysql.status.sh

测试脚本

# /usr/local/zabbix/scripts/nginx.status.sh active
1

 

修改zabbix agent的配置文件

# vim /usr/local/zabbix/etc/zabbix_agentd.conf

Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/      --->配置文件key的路径

UnsafeUserParameters=1                                            ---》允许特殊字符

 

增加nginx key的配置文件

# vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/nginx.status.conf

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.active,/usr/local/zabbix/scripts/nginx.status.sh active
UserParameter=nginx.reading,/usr/local/zabbix/scripts/nginx.status.sh reading
UserParameter=nginx.writing,/usr/local/zabbix/scripts/nginx.status.sh writing
UserParameter=nginx.waiting,/usr/local/zabbix/scripts/nginx.status.sh waiting

然后agent端测试

# /usr/local/zabbix/sbin/zabbix_agentd -t nginx.active
nginx.active [t|1]

重启agent服务

service zabbix_agentd restart

在zabbix添加nginx模板key查看即可

 

posted @ 2015-01-19 09:31  鸾舞春秋  阅读(572)  评论(0编辑  收藏  举报