|
zabbix监控nginx需要开启nginx status,要确认nginx编译http_sub_module模块!
1、检查当前nginx是否安装配置http_sub_module模块
# nginx -V
Tengine version: Tengine/1.5.2 (nginx/1.2.9)
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/data/PRG/nginx --with-pcre=../pcre-8.32/ --with-http_stub_status_module --without-select_module --without-poll_module --with-http_concat_module --with-http_sysguard_module --add-module=../ngx_cache_purge-2.0 --with-http_perl_module
如果你的环境中可以看到有 --with-http_sub_module 这个模块,就代表可以启用status 。如果没有,可以在编译时加上。
2、配置nginx status
server {
listen 8080;
access_log off;
server_name localhost;
root /var/www/html;
location /NginxStatus {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
配置完成后重新加载 nginx –s reload
3、访问http://127.0.0.1:8080/NginxStatus/ 即可查看到状态信息
4、编写nginx_status.sh脚本 (/data/PRG/zabbix/scripts/nginx_status.sh )
#!/bin/bash
# DateTime: 2017-01-22
# AUTHOR:kbsonlong
# Description:zabbix监控nginx性能以及进程状态
# Note:此脚本需要配置在被监控端
# zabbix_agentd.conf: 添加自定义key,UserParameter=nginx.status
,/data/PRG/zabbix/scripts/nginx_status.sh -F $1 -P $2
HOST="127.0.0.1"
PORT="80"
# 检测nginx进程是否存在
function ping {
/sbin/pidof nginx | wc -l
}
# 检测nginx性能
function active {
/usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
/usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
/usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
/usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
/usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
/usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
/usr/bin/curl "http://$HOST:$PORT/NginxStatus/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# 执行function
while getopts "H:P:F:" OPT; do
case $OPT in
"P")
PORT=$OPTARG
;;
"F")
fun=$OPTARG
;;
"H")
HOST=$OPTARG
;;
esac
done
$fun
5、zabbix agent配置自定义key
vim /data/PRG/zabbix/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=nginx.status
,/data/PRG/zabbix/scripts/nginx_status.sh -F $1 -P $2
配置完成后需要重启zabbix agent,自定义的key才生效
6、zabbix web端配置监控模板,将如下内容保存xml文件,然后导入模板,即可发现nginx.status模板(需要注意,本模板监控端口是8080,如果需要修改监控端口,可以直接将xml文件中8080端口替换,或者导入之后在web界面修改)
3.2
2017-01-22T08:57:20Z
Templates
nginx.status
nginx.status
Templates
nginx.status
nginx Accepts
0
0
nginx.status[accepts,8080]
30
90
365
0
3
0
0
0
0
1
0
0
0
nginx.status
nginx Active
0
0
nginx.status[active,8080]
30
90
365
0
3
0
0
0
0
1
0
0
0
nginx.status
nginx handled
0
0
nginx.status[handled,8080]
30
90
365
0
3
0
0
0
0
1
0
0
0
nginx.status
nginx Reading
0
0
nginx.status[reading,8080]
30
90
365
0
3
0
0
0
0
1
0
0
0
nginx.status
nginx requests
0
0
nginx.status[requests,8080]
30
90
365
0
3
0
0
0
0
1
0
0
0
nginx.status
nginx waiting
0
0
nginx.status[waiting,8080]
30
90
365
0
3
0
0
0
0
1
0
0
0
nginx.status
nginx writing
0
0
nginx.status[writing,8080]
30
90
365
0
3
0
0
0
0
1
0
0
0
nginx.status
nginx Clients Status
900
200
0.0000
100.0000
1
1
0
1
0
0.0000
0.0000
0
0
0
0
0
0
1A7C11
0
2
0
nginx.status
nginx.status[active,8080]
1
0
F63100
0
2
0
nginx.status
nginx.status[reading,8080]
2
0
2774A4
0
2
0
nginx.status
nginx.status[waiting,8080]
3
0
A54F10
0
2
0
nginx.status
nginx.status[writing,8080]
nginx Socket Status
900
200
0.0000
100.0000
1
1
0
1
0
0.0000
0.0000
0
0
0
0
0
0
1A7C11
0
2
0
nginx.status
nginx.status[accepts,8080]
1
0
F63100
0
2
0
nginx.status
nginx.status[handled,8080]
2
0
2774A4
0
2
0
nginx.status
nginx.status[requests,8080]
7、导入模板
配置 –> 模板 –> 导入 –> 选择文件 –>刚刚保存的xml文件 –> 导入
8、链接到需要监控的主机
9、查看最新数据及图形
附上脚本和模板网盘地址:http://pan.baidu.com/s/1c1JieHU
|
|
|