zabbix监控nginx,多亏了容哥(杨容)的帮忙,为了感谢容哥的帮助,写了这篇文章。
环境介绍:
服务器系统版本:CentOSrelease 6.6 (Final)
内核版本:Linux hk_nginx2.6.32-504.3.3.el6.x86_64
ZabbixServer版本:Zabbix server v2.2.2
ZabbixAgent 版本:Zabbix agent v2.2.2
Nginxweb 版本:nginx version: nginx/1.5.8
在zabbix agentd客户端上,查看nginx是否加载了--with-http_stub_status_module。因为zabbix监控nginx是根据nginx的Stub Status模块,抓取Status模块所提供的数据。假如以前没开启,现在想启用StubStatus 模块,在编译nginx 的时候要加上参数 --with-http_stub_status_module,执行./configure && make就可以了,不用make install。不过,一般情况下都是安装了的。
1.在nginx的配置文件中,添加status配置。
location/nginx-status { stub_status on; access_log off; allow 127.0.0.1; allow 192.168.1.10; #(zabbix服务器的IP地址,一般是内网地址) deny all; }
2.访问设置好的nginx-status链接,如图所示:
3.nginx Status 详细说明:
Activeconnections:对后端发起的活动连接数;
server accepts 2349542:nginx 总共处理了2349542个连接;
handled:成功创建了64603417次握手;
requests:总共处理了8798670请求。
Reading:nginx读取客户端的header数;
Writing: nginx 返回给客户端的header数;
Waiting: nginx 请求处理完成,正在等待下一请求指令的连接。
4.在agentd上编写监控nginx的脚本,并且设置属主和属组为zabbix,赋予执行权限。
cd /usr/local/zabbix/scripts cat nginx_status.sh #!/bin/bash # Script to fetch nginx statuses for tribily monitoring systems # Author: [email protected] # License: GPLv2 # Set Variables BKUP_DATE=`/bin/date +%Y%m%d` LOG="/data/log/zabbix/webstatus.log" HOST=127.0.0.1 PORT="80" # Functions to return nginx stats function active { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Active' | awk '{print $NF}' } function reading { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Reading' | awk '{print $2}' } function writing { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Writing' | awk '{print $4}' } function waiting { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Waiting' | awk '{print $6}' } function accepts { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $1}' } function handled { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $2}' } function requests { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $3}' } # Run the requested function $1 chmod o+x nginx_status.sh chown zabbix.zabbix nginx_status.sh ll total 4 -rwxr-xr-x 1 zabbix zabbix 1273 May 13 17:42 nginx_status.sh
5.修改nginx服务器上zabbix客户端的zabbix_agentd.conf配置文件,然后重启zabbix agentd客户端。
cd /usr/local/zabbix/etc grep -v "^[#;]" zabbix_agentd.conf | grep -v "^$" LogFile=/tmp/zabbix_agentd.log Server=192.168.1.10 #zabbix server端的IP地址 ServerActive=192.168.1.10 #zabbix server端的IP地址 Hostname=192.168.1.5 #本地的IP地址 UnsafeUserParameters=1 ##下面的是新添加进去的 UserParameter=nginx.accepts,/usr/local/zabbix/scripts/nginx_status.sh accepts UserParameter=nginx.handled,/usr/local/zabbix/scripts/nginx_status.sh handled UserParameter=nginx.requests,/usr/local/zabbix/scripts/nginx_status.sh requests UserParameter=nginx.connections.active,/usr/local/zabbix/scripts/nginx_status.sh active UserParameter=nginx.connections.reading,/usr/local/zabbix/scripts/nginx_status.sh reading UserParameter=nginx.connections.writing,/usr/local/zabbix/scripts/nginx_status.sh writing UserParameter=nginx.connections.waiting,/usr/local/zabbix/scripts/nginx_status.sh waiting
6.在zabbix server 端进行zabbix_get测试,取到数据了,说明没问题。
/usr/local/zabbix/bin/zabbix_get -s 192.168.1.5 -p 10050 -k "nginx.connections.active" 11 /usr/local/zabbix/bin/zabbix_get -s 192.168.1.5 -p 10050 -k "nginx.connections.waiting" 10 /usr/local/zabbix/bin/zabbix_get -s 192.168.1.5 -p 10050 -k "nginx.connections.writing" 1 /usr/local/zabbix/bin/zabbix_get -s 192.168.1.5 -p 10050 -k "nginx.accepts" 2350638 /usr/local/zabbix/bin/zabbix_get -s 192.168.1.5 -p 10050 -k "nginx.requests" 8801086
7.在网上搜到一个zabbix中nginx status的模板,把模板导入zabbix服务器。
-- - 0 1 127.0.0.1 10050 3 0 127.0.0.1 623 0 2 - Freetrade - - - -
Nginx Accepts 30 365 365 0 0 0 0 0 0 0 localhost 161 0 - Nginx - -
Nginx Connections Active 30 365 365 0 0 0 0 0 0 0 localhost 161 0 - Nginx - -
Nginx Connections Reading 30 365 365 0 0 0 0 0 0 0 localhost 161 0 - Nginx - -
Nginx Connections Waiting 30 365 365 0 0 0 0 0 0 0 localhost 161 0 - Nginx - -
Nginx Connections Writing 30 365 365 0 0 0 0 0 0 0 localhost 161 0 - Nginx - -
Nginx Handled 30 365 365 0 0 0 0 0 0 0 localhost 161 0 - Nginx Nginx Requests 30 365 365 0 0 0 0 0 0 0 localhost 161 0 - Nginx - - - 0 0 1 1 0 0.0000 100.0000 0 0 0.0000 0.0000 -- - 0 0 00EE00 0 2 0 5 - 0 0 EE0000 0 2 0 5 0 1 EEEE00 0 2 0 5 0 0 1 1 0 0.0000 100.0000 0 0 0.0000 0.0000 -- - 0 0 0000EE 0 2 0 5 - 0 1 EE0000 0 2 0 5 - 0 2 EEEE00 0 2 0 5 0 3 00EE00 0 2 0 5
8.登陆到zabbix服务器首页,点击”组态”-“模板”。
点击”汇入”,如图所示
点击“汇入文件“,然后把找到的nginx_status.xml文件,汇入模板。
创建应用集Nginx Server。
把nginx主机连接到模板上去。
到此为止,实验结束。