zabbix 针对nginx 状态监控

 

环境:Centos 6.5

zabbix 2.0

Zabbix Server: 192.168.137.132

Zabbix nginx 客户端: 192.168.137.130

 

前提是nignx在编译安装时加上:--with-http_stub_status_module-----这个大家都知道!!

 

同时还要在nginx的配置文件中加上:

 

   location /status

       {

          stub_status on;

          access_log off;

          allow 127.0.0.1;

          allow 192.168.137.132;    #这是我们zabbix监控服务器IP

       }

 

下面说下zabbixnginx的监控原理:

 

Zabbix Server <---(Zabbix Agent)--->nginx客户端key(要写入客户端配置文件中) <--- 脚本 --->nginx状态参数

 

nginx web界面nginx状态显示情况如下图:

 

Zabbix监控nginx_status

 

1.创建获取nginxstatus参数数据传送到server端脚本

 

   #cat >> /usr/local/sbin/nginx_status.sh << EOF

   #!/bin/bash

    #Script to fetch nginx statuses for tribily monitoring systems

    #License: GPLv4

    #Set Variables

   BKUP_DATE=`/bin/date +%Y%m%d`

   LOG="/etc/zabbix/nginx_status.log"

   #HOST=`/sbin/ifconfig em1 | sed -n '/inet /{s/.*addr://;s/ .*//;p}'`

   HOST=`/sbin/ifconfig eth0 |grep "inet addr" |awk -F[:""] '{print $13}'`

   PORT="80"

    

    #Functions to return nginx stats

   function active {

       /usr/bin/curl "http://$HOST:$PORT/status" 2>/dev/null| grep'Active' | awk '{print $NF}'      

       }    

   function reading {

       /usr/bin/curl "http://$HOST:$PORT/status" 2>/dev/null| grep'Reading' | awk '{print $2}'      

       }    

   function writing {

       /usr/bin/curl "http://$HOST:$PORT/status" 2>/dev/null| grep'Writing' | awk '{print $4}'      

       }    

   function waiting {

       /usr/bin/curl "http://$HOST:$PORT/status" 2>/dev/null| grep'Waiting' | awk '{print $6}'      

       }    

   function accepts {

       /usr/bin/curl "http://$HOST:$PORT/status" 2>/dev/null| awkNR==3 | awk '{print $1}'

       }    

   function handled {

       /usr/bin/curl "http://$HOST:$PORT/status" 2>/dev/null| awkNR==3 | awk '{print $2}'

       }      

   function requests {

       /usr/bin/curl "http://$HOST:$PORT/status" 2>/dev/null| awkNR==3 | awk '{print $3}'

       }

    #Run the requested function

   $1

   EOF

 

 

 

 

 

这个脚本当中定义了一些状态项名称的函数,函数的功能是通过curl这个工具抓取到nginxweb页面的状态数据。

 

2.添加agent客户端key值,以便让zabbix server获取状态数据

 

cat>>/etc/zabbix/zabbix_agentd.conf<< EOF

#nginx_status_key

UserParameter=nginx.accepts,/usr/local/sbin/nginx_status.shaccepts

UserParameter=nginx.handled,/usr/local/sbin/nginx_status.shhandled

UserParameter=nginx.requests,/usr/local/sbin/nginx_status.shrequests

UserParameter=nginx.connections.active,/usr/local/sbin/nginx_status.shactive

UserParameter=nginx.connections.reading,/usr/local/sbin/nginx_status.shreading

UserParameter=nginx.connections.writing,/usr/local/sbin/nginx_status.shwriting

UserParameter=nginx.connections.waiting,/usr/local/sbin/nginx_status.shwaitin

EOF

测试

 

sudo /usr/local/zabbix/bin/zabbix_get -s192.168.137.130 -p 10050 -k "nginx.connectio

 

如果能获取到数据说明配置成功!


你可能感兴趣的:(zabbix,服务搭建)