zabbix监控nginx

1.在nginx中开启状态模板

[[email protected] ~]# vim /usr/local/nginx-1.8.0/conf/nginx.conf

    location /stat {
        stub_status on;
        access_log off;
        allow 本机的IP地址;          #只允许本机访问
        deny all;
    }
zabbix监控nginx

2.写出监控nginx的脚本

[[email protected] ~]# vim /home/shell/nc_nginx_check.sh 
#!/bin/bash
# Script to fetch nginx statuses for tribily monitoring systems
# Author: Dick
# Data: 2016-05-12
HOST=网站的域名


# Functions to return nginx stats
function active {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| grep 'Active' | awk '{print $NF}'
  }
function reading {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| grep 'Reading' | awk '{print $2}'
  }
function writing {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| grep 'Writing' | awk '{print $4}'
  }
function waiting {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
  }
function accepts {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| awk NR==3 | awk '{print $1}'
  }
function handled {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| awk NR==3 | awk '{print $2}'
  }
function requests {
  /usr/bin/curl "http://$HOST/stat" 2>/dev/null| awk NR==3 | awk '{print $3}'
  }
# Run the requested function
$1               #接收第一个参数

[[email protected] ~]# vim /etc/zabbix/zabbix_agentd.conf      #添加如下参数

UnsafeUserParameters=1
UserParameter=nginx.accepts,/home/shell/nc_nginx_check.sh accepts
UserParameter=nginx.handled,/home/shell/nc_nginx_check.sh handled
UserParameter=nginx.requests,/home/shell/nc_nginx_check.sh requests
UserParameter=nginx.connections.active,/home/shell/nc_nginx_check.sh active
UserParameter=nginx.connections.reading,/home/shell/nc_nginx_check.sh reading
UserParameter=nginx.connections.writing,/home/shell/nc_nginx_check.sh writing
UserParameter=nginx.connections.waiting,/home/shell/nc_nginx_check.sh waiting

[[email protected] ~]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent:         [  OK  ]
Starting Zabbix agent:                 [  OK  ]
[[email protected] ~]# 

3.在zabbix服务端测试是否有数据

[[email protected] ~]# yum install zabbix-get -y

[[email protected] ~]# zabbix_get -s 被监控端的IP地址  -k "nginx.accepts"
16187540
[[email protected] ~]# zabbix_get -s 被监控端的IP地址 -k "nginx.handled"
16187585
[[email protected] ~]# zabbix_get -s 被监控端的IP地址 -k "nginx.requests"
55652248
[[email protected] ~]# zabbix_get -s 被监控端的IP地址 -k "nginx.connections.active"
349
[[email protected] ~]# 


4.在zabbix窗口添加模板,分类,项目

(1).创建模板

zabbix监控nginx

(2).创建分类

zabbix监控nginx

(3).创建应用集

zabbix监控nginx

(4).创建项目

zabbix监控nginx

(5).创建监控项

zabbix监控nginx

zabbix监控nginx


5.查看监控的状态图

zabbix监控nginx


错误总结:

[[email protected] ~]# zabbix_get -s 被监控端的IP地址 -k "nginx.connections.writing"
ZBX_NOTSUPPORTED: Timeout while executing a shell script.

[[email protected] ~]#

解决方法:

    在zabbix_agent端修改Timeout参数

[[email protected] ~]# vim /etc/zabbix/zabbix_agentd.conf 
Timeout=20                    #默认是关闭,去掉注释,将值修改为20


[[email protected] ~]# zabbix_get -s 被监控端的IP地址 -k "nginx.connections.writing"

ZBX_NOTSUPPORTED
[[email protected] ~]#

解决方法:

    在zabbix_agent端修改Timeout参数

[[email protected] ~]# vim /etc/zabbix/zabbix_agentd.conf

AllowRoot=1

UnsafeUserParameters=1

EnableRemoteCommands=1

[[email protected] ~]#










本文转自 z597011036 51CTO博客,原文链接:http://blog.51cto.com/tongcheng/1772583,如需转载请自行联系原作者