一、环境准备
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.4version> <date>2018-01-18T11:00:49Zdate> <groups> <group> <name>Templatesname> group> groups> <templates> <template> <template>Template App NGINXtemplate> <name>Template App NGINXname> <description>nginx statuses Author:371304description> <groups> <group> <name>Templatesname> group> groups> <applications> <application> <name>Nginxname> application> applications> <items> <item> <name>nginx status server acceptsname> <type>7type> <snmp_community/> <snmp_oid/> <key>nginx.status[accepts]key> <delay>30sdelay> <history>90dhistory> <trends>365dtrends> <status>0status> <value_type>3value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0snmpv3_securitylevel> <snmpv3_authprotocol>0snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>acceptsdescription> <inventory_link>0inventory_link> <applications> <application> <name>Nginxname> application> applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> item> <item> <name>nginx status connections activename> <type>7type> <snmp_community/> <snmp_oid/> <key>nginx.status[active]key> <delay>30sdelay> <history>90dhistory> <trends>365dtrends> <status>0status> <value_type>3value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0snmpv3_securitylevel> <snmpv3_authprotocol>0snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>acitvedescription> <inventory_link>0inventory_link> <applications> <application> <name>Nginxname> application> applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> item> <item> <name>nginx status server handledname> <type>7type> <snmp_community/> <snmp_oid/> <key>nginx.status[handled]key> <delay>30sdelay> <history>90dhistory> <trends>365dtrends> <status>0status> <value_type>3value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0snmpv3_securitylevel> <snmpv3_authprotocol>0snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>handleddescription> <inventory_link>0inventory_link> <applications> <application> <name>Nginxname> application> applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> item> <item> <name>nginx status PINGname> <type>7type> <snmp_community/> <snmp_oid/> <key>nginx.status[ping]key> <delay>30sdelay> <history>90dhistory> <trends>365dtrends> <status>0status> <value_type>3value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0snmpv3_securitylevel> <snmpv3_authprotocol>0snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>pingdescription> <inventory_link>0inventory_link> <applications> <application> <name>Nginxname> application> applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> item> <item> <name>nginx status connections readingname> <type>7type> <snmp_community/> <snmp_oid/> <key>nginx.status[reading]key> <delay>30sdelay> <history>90dhistory> <trends>365dtrends> <status>0status> <value_type>3value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0snmpv3_securitylevel> <snmpv3_authprotocol>0snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>readingdescription> <inventory_link>0inventory_link> <applications> <application> <name>Nginxname> application> applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> item> <item> <name>nginx status server requestsname> <type>7type> <snmp_community/> <snmp_oid/> <key>nginx.status[requests]key> <delay>30sdelay> <history>90dhistory> <trends>365dtrends> <status>0status> <value_type>3value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0snmpv3_securitylevel> <snmpv3_authprotocol>0snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>requestsdescription> <inventory_link>0inventory_link> <applications> <application> <name>Nginxname> application> applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> item> <item> <name>nginx status connections waitingname> <type>7type> <snmp_community/> <snmp_oid/> <key>nginx.status[waiting]key> <delay>30sdelay> <history>90dhistory> <trends>365dtrends> <status>0status> <value_type>3value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0snmpv3_securitylevel> <snmpv3_authprotocol>0snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>waitingdescription> <inventory_link>0inventory_link> <applications> <application> <name>Nginxname> application> applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> item> <item> <name>nginx status connections writingname> <type>7type> <snmp_community/> <snmp_oid/> <key>nginx.status[writing]key> <delay>30sdelay> <history>90dhistory> <trends>365dtrends> <status>0status> <value_type>3value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0snmpv3_securitylevel> <snmpv3_authprotocol>0snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>writingdescription> <inventory_link>0inventory_link> <applications> <application> <name>Nginxname> application> applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> item> items> <discovery_rules/> <httptests/> <macros/> <templates/> <screens/> template> templates> <triggers> <trigger> <expression>{Template App NGINX:nginx.status[ping].last()}=0expression> <recovery_mode>0recovery_mode> <recovery_expression/> <name>nginx was down!name> <correlation_mode>0correlation_mode> <correlation_tag/> <url/> <status>0status> <priority>4priority> <description>nginx was down!description> <type>0type> <manual_close>0manual_close> <dependencies/> <tags/> trigger> triggers> <graphs> <graph> <name>nginx status connectionsname> <width>900width> <height>200height> <yaxismin>0.0000yaxismin> <yaxismax>100.0000yaxismax> <show_work_period>1show_work_period> <show_triggers>1show_triggers> <type>0type> <show_legend>1show_legend> <show_3d>0show_3d> <percent_left>0.0000percent_left> <percent_right>0.0000percent_right> <ymin_type_1>0ymin_type_1> <ymax_type_1>0ymax_type_1> <ymin_item_1>0ymin_item_1> <ymax_item_1>0ymax_item_1> <graph_items> <graph_item> <sortorder>0sortorder> <drawtype>0drawtype> <color>1A7C11color> <yaxisside>0yaxisside> <calc_fnc>2calc_fnc> <type>0type> <item> <host>Template App NGINXhost> <key>nginx.status[active]key> item> graph_item> <graph_item> <sortorder>1sortorder> <drawtype>0drawtype> <color>F63100color> <yaxisside>0yaxisside> <calc_fnc>2calc_fnc> <type>0type> <item> <host>Template App NGINXhost> <key>nginx.status[reading]key> item> graph_item> <graph_item> <sortorder>2sortorder> <drawtype>0drawtype> <color>2774A4color> <yaxisside>0yaxisside> <calc_fnc>2calc_fnc> <type>0type> <item> <host>Template App NGINXhost> <key>nginx.status[waiting]key> item> graph_item> <graph_item> <sortorder>3sortorder> <drawtype>0drawtype> <color>A54F10color> <yaxisside>0yaxisside> <calc_fnc>2calc_fnc> <type>0type> <item> <host>Template App NGINXhost> <key>nginx.status[writing]key> item> graph_item> graph_items> graph> <graph> <name>nginx status servername> <width>900width> <height>200height> <yaxismin>0.0000yaxismin> <yaxismax>100.0000yaxismax> <show_work_period>1show_work_period> <show_triggers>1show_triggers> <type>0type> <show_legend>1show_legend> <show_3d>0show_3d> <percent_left>0.0000percent_left> <percent_right>0.0000percent_right> <ymin_type_1>0ymin_type_1> <ymax_type_1>0ymax_type_1> <ymin_item_1>0ymin_item_1> <ymax_item_1>0ymax_item_1> <graph_items> <graph_item> <sortorder>0sortorder> <drawtype>0drawtype> <color>1A7C11color> <yaxisside>0yaxisside> <calc_fnc>2calc_fnc> <type>0type> <item> <host>Template App NGINXhost> <key>nginx.status[accepts]key> item> graph_item> <graph_item> <sortorder>1sortorder> <drawtype>0drawtype> <color>F63100color> <yaxisside>0yaxisside> <calc_fnc>2calc_fnc> <type>0type> <item> <host>Template App NGINXhost> <key>nginx.status[handled]key> item> graph_item> <graph_item> <sortorder>2sortorder> <drawtype>0drawtype> <color>2774A4color> <yaxisside>0yaxisside> <calc_fnc>2calc_fnc> <type>0type> <item> <host>Template App NGINXhost> <key>nginx.status[requests]key> item> graph_item> graph_items> graph> graphs> zabbix_export>