zabbix

zabbix监控nginx

2017-07-13  本文已影响56人  WFF_FFW

环境介绍

操作系统:Centos 6.* ( 双核4G、硬盘至少20G)
zabbix_agentd版本:3.0.3
zabbix_server版本:3.2.6
nginx版本:1.8.0
zabbix server : 192.168.1.127
zabbix agentd :192.168.1.101

配置zabbix监控nginx

1):zabbix监控nginx所需脚本
(监控哪台服务器就将脚本放在那边)

#!/bin/bash
HOST="127.0.0.1"
PORT="80"
 
# 检测nginx进程是否存在
function ping {
    /sbin/pidof nginx | wc -l 
}
# 检测nginx性能
function active {
    /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
    /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
    /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
    /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
    /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
    /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
    /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# 执行function
$1

2):给脚本添加执行权限

chmod +x ngx_status.sh

3):添加监控nginx配置文件

vim /usr/local/nginx/conf/conf.d/zabbix.conf 
server {

   listen      *:80 default_server;
   server_name _;
   location    /ngx_status
   {
       stub_status on;
       access_log off;
       #allow 127.0.0.1;
       #deny all;
   }
}

4):agentd配置文件添加nginx键值

UserParameter=nginx.status[*],/etc/zabbix/ngx_status.sh $1

5):重启服务

/etc/init.d/nginx restart
/etc/init.d/zabbix_agentd restart

6):agentd端本地测试获取数据

 curl "http://127.0.0.1:80/ngx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
zabbix_nginx2.png

7):server端测试获取数据

/usr/local/zabbix/bin/zabbix_get -s 192.168.1.101 -k 'nginx.status[accepts]'
zabbix_nginx.png

Web端配置:模板使用zabbix自带的nginx模板即可,如何添加模板在此就不赘述了。

注意
1:上述3,在nginx.conf中最后一个" } "前添加一条指向conf.d下的文件即可

include /usr/local/nginx/conf/conf.d/*.conf;
上一篇下一篇

猜你喜欢

热点阅读