ZABBIX 3.4 监控Nginx 状态(七)
Posted cloudos
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZABBIX 3.4 监控Nginx 状态(七)相关的知识,希望对你有一定的参考价值。
一、环境准备
1、在nginx的配置文件中,添加status配置
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 172.17.19.19; //zabbix服务器的IP地址,一般是内网地址
deny all;
}
//在虚拟主机 server {} 中加入上面配置,也可以单独定义一个专门用于监控的虚拟主机。
//deny all , 拒绝除 allow 中的主机之外所有主机访问此 URL ,实现过程中如果遇到 403 ,有可能是你把自己测试的机器拒绝了
2、访问设置好的nginx-status链接,如图所示:
3、nginx Status 详细说明:
Activeconnections:对后端发起的活动连接数;
server accepts 66:nginx 总共处理了66个连接;
handled:成功创建了66次握手;
requests:总共处理了66请求。
Reading:nginx读取客户端的header数;
Writing: nginx 返回给客户端的header数;
Waiting: nginx 请求处理完成,正在等待下一请求指令的连接。
在访问效率很高,请求很快被处理完毕的情况下,Waiting 数比较多是正常的。如果 reading + writing 数较多,则说明并发访问量很大,正在处理过程中
二、监控脚本编写
1、编写脚本,放于 /etc/zabbix/zabbix_agentd.d/目录下
#!/bin/bash #Script to fetch nginx statuses for monitoring systems #Author Tony HOST="127.0.0.1" PORT="80" function ping { /sbin/pidof nginx | wc -l } 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}\' } $1
赋予脚本执行权限:
[root@Node1 zabbix_agentd.d]# chmod a+x nginx_status.sh //赋予执行权限 [root@Node1 zabbix_agentd.d]# ll nginx_status.sh -rwxr-xr-x 1 root root 1157 Jan 18 15:45 nginx_status.sh
2、添加zabbix配置文件,放于 /etc/zabbix/zabbix_agentd.d/目录下(agent的配置文件 /etc/zabbix/zabbix_agentd.conf 中定义了其他key的包含目录)创建配置文件nginx_status.conf
[root@Node1 zabbix_agentd.d]# cat nginx_status.conf UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh $1
3、确保配置Agent配置文件开启自定义参数UnsafeUserParameters=1
[root@Node1 zabbix_agentd.d]# !grep grep -n \'^[a-Z]\' /etc/zabbix/zabbix_agentd.conf 13:PidFile=/var/run/zabbix/zabbix_agentd.pid 32:LogFile=/var/log/zabbix/zabbix_agentd.log 43:LogFileSize=0 57:DebugLevel=3 97:Server=172.17.21.208 138:ServerActive=172.17.21.208 149:Hostname=Node1.contoso.com 267:Include=/etc/zabbix/zabbix_agentd.d/*.conf 286:UnsafeUserParameters=1 //1代表允许,0代表关闭
4、重启zabbix-agent服务
[root@Node1 ~]# systemctl restart zabbix-agent.service
5、在zabbix servere服务器上测试,是否能正常获取数据
[root@Node3 zabbix3.4.5]# zabbix_get -s 172.17.21.206 -p 10050 -k nginx.status[ping] 1 [root@Node3 zabbix3.4.5]# zabbix_get -s 172.17.21.206 -p 10050 -k nginx.status[accepts] 306 [root@Node3 zabbix3.4.5]# zabbix_get -s 172.17.21.206 -p 10050 -k nginx.status[handled] 307 [root@Node3 zabbix3.4.5]# zabbix_get -s 172.17.21.206 -p 10050 -k nginx.status[requests] 311 [root@Node3 zabbix3.4.5]#
三、WEB界面配置
1、创建模板:Configuration-->Templates-->Greate template,创建模板名称:Template App NGINX,单击Add即可
2、给模板依次添加相应Item、Trigger、Graphs,如下图所示
3、关联模板,Configuration-->Hosts-->Node1--Templates,选择模板,点击Updtae即可
4、查看Nginx状态:Monitoring-->Graphs-->Host(Node1)-->Grahp(nginx status connections与nginx status server)
备注:若相关数据比较小的话,可以使用ab进行简单压测,然后再看数值变化
[root@Node1 zabbix_agentd.d]# ab -n 10000 -c 1000 http://Node1.contoso.com/
附模板:Template_Nginx_Status.xml
<?xml version="1.0" encoding="UTF-8"?> <zabbix_export> <version>3.4</version> <date>2018-01-18T11:00:49Z</date> <groups> <group> <name>Templates</name> </group> </groups> <templates> <template> <template>Template App NGINX</template> <name>Template App NGINX</name> <description>nginx statuses Author:371304</description> <groups> <group> <name>Templates</name> </group> </groups> <applications> <application> <name>Nginx</name> </application> </applications> <items> <item> <name>nginx status server accepts</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>nginx.status[accepts]</key> <delay>30s</delay> <history>90d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>accepts</description> <inventory_link>0</inventory_link> <applications> <application> <name>Nginx</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> </item> <item> <name>nginx status connections active</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>nginx.status[active]</key> <delay>30s</delay> <history>90d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>acitve</description> <inventory_link>0</inventory_link> <applications> <application> <name>Nginx</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> </item> <item> <name>nginx status server handled</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>nginx.status[handled]</key> <delay>30s</delay> <history>90d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>handled</description> <inventory_link>0</inventory_link> <applications> <application> <name>Nginx</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> </item> <item> <name>nginx status PING</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>nginx.status[ping]</key> <delay>30s</delay> <history>90d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>ping</description> <inventory_link>0</inventory_link> <applications> <application> <name>Nginx</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> </item> <item> <name>nginx status connections reading</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>nginx.status[reading]</key> <delay>30s</delay> <history>90d</history> <trends>365d</trends> <status>0</status> <value_typezabbix管理七之监控nginx性能