利用zabbixt监控nginx的各种状态,达到对nginx的实时监控;
一,首先需要开启nginx的nginx_status模块
开启nginx_status功能: location /nginx_status{ stub_status on; access_log off; allow 192.168.1.113; 访问IP deny all; }
访问: http://192.168.1.113/nginx_status
需要写一个脚本在被监控机上跑:脚本如下:
cat zabbix_nginx_check.sh
#! /bin/bash # Set Variables # require install elinks eg: yum install elink HOST=`/sbin/ifconfig eth0 | sed -n ‘/inet /{s/.*addr://;s/ .*//;p}‘` PORT="80" # Functions to return nginx_status stats function active { /usr/bin/elinks -source "http://$HOST:$PORT/nginx_status" 2> /dev/null| grep ‘Active‘ | awk ‘{print $NF}‘ } function reading { /usr/bin/elinks -source "http://$HOST:$PORT/nginx_status" 2> /dev/null| grep ‘Reading‘ | awk ‘{print $2}‘ } function writing { /usr/bin/elinks -source "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep ‘Writing‘ | awk ‘{print $4}‘ } function waiting { /usr/bin/elinks -source "http://$HOST:$PORT/nginx_status" 2> /dev/null| grep ‘Waiting‘ | awk ‘{print $6}‘ } function accepts { /usr/bin/elinks -source "http://$HOST:$PORT/nginx_status" 2> /dev/null| awk NR==3 | awk ‘{print $1}‘ } function handled { /usr/bin/elinks -source "http://$HOST:$PORT/nginx_status" 2> /dev/null| awk NR==3 | awk ‘{print $2}‘ } function requests { /usr/bin/elinks -source "http://$HOST:$PORT/nginx_status" 2> /dev/null| awk NR==3 | awk ‘{print $3}‘ } # Run the requested function $1
注:因为脚本中需要用到elinks这命令,所以需要安装,安装命令如下:
yum install -y elinks
把脚本放在/etc/zabbix/scripts 目录下,以后所有自定义的脚本全部放在此目录下,方便统一管理;
3。 配置zabbix_agentd.conf 文件
加入如下的内容:
UserParameter=nginx.accepts,/etc/zabbix/scripts/zabbix_nginx_check.sh accepts UserParameter=nginx.handled,/etc/zabbix/scripts/zabbix_nginx_check.sh handled UserParameter=nginx.requests,/etc/zabbix/scripts/zabbix_nginx_check.sh requests UserParameter=nginx.connections.active,/etc/zabbix/scripts/zabbix_nginx_check.sh active UserParameter=nginx.connections.reading,/etc/zabbix/scripts/zabbix_nginx_check.sh reading UserParameter=nginx.connections.writing,/etc/zabbix/scripts/zabbix_nginx_check.sh writing UserParameter=nginx.connections.waiting,/etc/zabbix/scripts/zabbix_nginx_check.sh waiting
重启 zabbix_agentd服务:
service zabbix_agentd restart
4。 在zabbix-server的web上配置相应的模板
Configuration -->Templates -->Create template-->填写Template name(其它的默认即可)
创建Item:
Configuration -->Templates --> Items -- Create item -->填写Name ,Key(其它的默认即可)
这里主要注意两个项,Name和Key,下面是Name对应的Key值:
Name Key nginx_handled nginx.handled nginx_requests nginx.requests nginx_accepts nginx.accepts connections_active nginx.connections.active connections_writing nginx.connection.writing connections.reading nginx.connections.reading connections.waiting nginx.connections.waiting
时间: 2024-11-29 00:19:25