您的位置:首页 > 运维架构 > Nginx

zabbix监控nginx

2016-05-12 11:32 816 查看
1.在nginx中开启状态模板
[root@web03 ~]# vim /usr/local/nginx-1.8.0/conf/nginx.conf

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



2.写出监控nginx的脚本
[root@web03 ~]# 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 #接收第一个参数
[root@web03 ~]# 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
[root@web03 ~]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent: [ OK ]
Starting Zabbix agent: [ OK ]
[root@web03 ~]#

3.在zabbix服务端测试是否有数据
[root@db02 ~]# yum install zabbix-get -y

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

4.在zabbix窗口添加模板,分类,项目
(1).创建模板



(2).创建分类



(3).创建应用集



(4).创建项目



(5).创建监控项







5.查看监控的状态图




错误总结:
[root@db02 ~]# zabbix_get -s 被监控端的IP地址 -k "nginx.connections.writing"
ZBX_NOTSUPPORTED: Timeout while executing a shell script.

[root@db02 ~]#
解决方法:
在zabbix_agent端修改Timeout参数
[root@web03 ~]# vim /etc/zabbix/zabbix_agentd.conf
Timeout=20 #默认是关闭,去掉注释,将值修改为20

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

ZBX_NOTSUPPORTED
[root@db02 ~]#
解决方法:

在zabbix_agent端修改Timeout参数
[root@web03 ~]# vim /etc/zabbix/zabbix_agentd.conf
AllowRoot=1
UnsafeUserParameters=1
EnableRemoteCommands=1
[root@web03 ~]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  监控 nginx zabbix