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.
解决方法:
在zabbix_agent端修改Timeout参数
[root@web03 ~]# vim /etc/zabbix/zabbix_agentd.conf
Timeout=20 #默认是关闭,去掉注释,将值修改为20
本文出自 “一起走过的日子” 博客,请务必保留此出处http://tongcheng.blog.51cto.com/6214144/1772583
原文地址:http://tongcheng.blog.51cto.com/6214144/1772583