Zabbix监控nginx。感谢荣格(杨蓉)的帮助,写这篇文章是为了感谢荣格的帮助。
环境介绍:
服务器版本:centos版本6.6(最终版)
内核:LinuxHK_nginx2.6.32-504.3.3.el6.x86_64
ZabbixServer版本:Zabbixserverv2.2.2
Zabbix代理版本:Zabbix代理v2.2.2
Nginxweb版本:nginx版本:nginx/1.5.8
在zabbixagentd客户端上,检查nginx是否加载了-with-http_stub_status_module。Zabbix根据nginx的存根状态模块监控nginx,并捕获状态模块提供的数据。如果之前没有开启,现在想启用StubStatus模块,那么在编译nginx的时候就要添加参数-with-http_stub_status_module,并执行。/配置&&Make就可以了,不需要make安装。但是,它通常是安装的。
1.在nginx的配置文件中,添加状态配置。
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.3.nginx状态的详细描述:
Activeconnections:启动到后端的活动连接数;
服务器accepts2349542:nginx共处理了2349542个连接;
已处理:64,603,417次握手成功创建;
请求:共处理了8798670项请求。
读取:nginx读取客户端的头号;
writing:nginx返回给客户端的头的个数;
等待: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.sh5.在nginx服务器上修改zabbix客户端的zabbix_agentd.conf配置文件,然后重启zabbixagentd客户端。
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 waiting6.在zabbix服务器上测试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" 88010867.在网上搜索一个zabbix中nginx状态的模板,将模板导入到zabbix服务器中。
- <zabbix_export version="1.0" date="14.03.11" time="08.48"> - <hosts> - <host name="Nginx Status"> <proxy_hostid>0</proxy_hostid> <useip>1</useip> <dns /> <ip>127.0.0.1</ip> <port>10050</port> <status>3</status> <useipmi>0</useipmi> <ipmi_ip>127.0.0.1</ipmi_ip> <ipmi_port>623</ipmi_port> <ipmi_authtype>0</ipmi_authtype> <ipmi_privilege>2</ipmi_privilege> <ipmi_username /> <ipmi_password /> - <groups> <group>Freetrade</group> </groups> <triggers /> - <items> - <item type="7" key="nginx.accepts" value_type="3"> <description>Nginx Accepts</description> <ipmi_sensor /> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <data_type>0</data_type> <units /> <multiplier>0</multiplier> <delta>0</delta> <formula>0</formula> <lastlogsize>0</lastlogsize> <logtimefmt /> <delay_flex /> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <params /> <trapper_hosts>localhost</trapper_hosts> <snmp_community /> <snmp_oid /> <snmp_port>161</snmp_port> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authpassphrase /> <snmpv3_privpassphrase /> - <applications> <application>Nginx</application> </applications> </item> - <item type="7" key="nginx.connections.active" value_type="3"> <description>Nginx Connections Active</description> <ipmi_sensor /> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <data_type>0</data_type> <units /> <multiplier>0</multiplier> <delta>0</delta> <formula>0</formula> <lastlogsize>0</lastlogsize> <logtimefmt /> <delay_flex /> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <params /> <trapper_hosts>localhost</trapper_hosts> <snmp_community /> <snmp_oid /> <snmp_port>161</snmp_port> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authpassphrase /> <snmpv3_privpassphrase /> - <applications> <application>Nginx</application> </applications> </item> - <item type="7" key="nginx.connections.reading" value_type="3"> <description>Nginx Connections Reading</description> <ipmi_sensor /> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <data_type>0</data_type> <units /> <multiplier>0</multiplier> <delta>0</delta> <formula>0</formula> <lastlogsize>0</lastlogsize> <logtimefmt /> <delay_flex /> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <params /> <trapper_hosts>localhost</trapper_hosts> <snmp_community /> <snmp_oid /> <snmp_port>161</snmp_port> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authpassphrase /> <snmpv3_privpassphrase /> - <applications> <application>Nginx</application> </applications> </item> - <item type="7" key="nginx.connections.waiting" value_type="3"> <description>Nginx Connections Waiting</description> <ipmi_sensor /> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <data_type>0</data_type> <units /> <multiplier>0</multiplier> <delta>0</delta> <formula>0</formula> <lastlogsize>0</lastlogsize> <logtimefmt /> <delay_flex /> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <params /> <trapper_hosts>localhost</trapper_hosts> <snmp_community /> <snmp_oid /> <snmp_port>161</snmp_port> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authpassphrase /> <snmpv3_privpassphrase /> - <applications> <application>Nginx</application> </applications> </item> - <item type="7" key="nginx.connections.writing" value_type="3"> <description>Nginx Connections Writing</description> <ipmi_sensor /> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <data_type>0</data_type> <units /> <multiplier>0</multiplier> <delta>0</delta> <formula>0</formula> <lastlogsize>0</lastlogsize> <logtimefmt /> <delay_flex /> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <params /> <trapper_hosts>localhost</trapper_hosts> <snmp_community /> <snmp_oid /> <snmp_port>161</snmp_port> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authpassphrase /> <snmpv3_privpassphrase /> - <applications> <application>Nginx</application> </applications> </item> - <item type="7" key="nginx.handled" value_type="3"> <description>Nginx Handled</description> <ipmi_sensor /> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <data_type>0</data_type> <units /> <multiplier>0</multiplier> <delta>0</delta> <formula>0</formula> <lastlogsize>0</lastlogsize> <logtimefmt /> <delay_flex /> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <params /> <trapper_hosts>localhost</trapper_hosts> <snmp_community /> <snmp_oid /> <snmp_port>161</snmp_port> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authpassphrase /> <snmpv3_privpassphrase /> - <applications> <application>Nginx</application> </applications> </item> - <item type="7" key="nginx.requests" value_type="3"> <description>Nginx Requests</description> <ipmi_sensor /> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <data_type>0</data_type> <units /> <multiplier>0</multiplier> <delta>0</delta> <formula>0</formula> <lastlogsize>0</lastlogsize> <logtimefmt /> <delay_flex /> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <params /> <trapper_hosts>localhost</trapper_hosts> <snmp_community /> <snmp_oid /> <snmp_port>161</snmp_port> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authpassphrase /> <snmpv3_privpassphrase /> - <applications> <application>Nginx</application> </applications> </item> </items> <templates /> - <graphs> - <graph name="Nginx Socket Status" width="900" height="200"> <ymin_type>0</ymin_type> <ymax_type>0</ymax_type> <ymin_item_key /> <ymax_item_key /> <show_work_period>1</show_work_period> <show_triggers>1</show_triggers> <graphtype>0</graphtype> <yaxismin>0.0000</yaxismin> <yaxismax>100.0000</yaxismax> <show_legend>0</show_legend> <show_3d>0</show_3d> <percent_left>0.0000</percent_left> <percent_right>0.0000</percent_right> - <graph_elements> - <graph_element item="Nginx Status:nginx.accepts"> <drawtype>0</drawtype> <sortorder>0</sortorder> <color>00EE00</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <periods_cnt>5</periods_cnt> </graph_element> - <graph_element item="Nginx Status:nginx.handled"> <drawtype>0</drawtype> <sortorder>0</sortorder> <color>EE0000</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <periods_cnt>5</periods_cnt> </graph_element> - <graph_element item="Nginx Status:nginx.requests"> <drawtype>0</drawtype> <sortorder>1</sortorder> <color>EEEE00</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <periods_cnt>5</periods_cnt> </graph_element> </graph_elements> </graph> - <graph name="Nginx Clients Status" width="900" height="200"> <ymin_type>0</ymin_type> <ymax_type>0</ymax_type> <ymin_item_key /> <ymax_item_key /> <show_work_period>1</show_work_period> <show_triggers>1</show_triggers> <graphtype>0</graphtype> <yaxismin>0.0000</yaxismin> <yaxismax>100.0000</yaxismax> <show_legend>0</show_legend> <show_3d>0</show_3d> <percent_left>0.0000</percent_left> <percent_right>0.0000</percent_right> - <graph_elements> - <graph_element item="Nginx Status:nginx.connections.active"> <drawtype>0</drawtype> <sortorder>0</sortorder> <color>0000EE</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <periods_cnt>5</periods_cnt> </graph_element> - <graph_element item="Nginx Status:nginx.connections.writing"> <drawtype>0</drawtype> <sortorder>1</sortorder> <color>EE0000</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <periods_cnt>5</periods_cnt> </graph_element> - <graph_element item="Nginx Status:nginx.connections.waiting"> <drawtype>0</drawtype> <sortorder>2</sortorder> <color>EEEE00</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <periods_cnt>5</periods_cnt> </graph_element> - <graph_element item="Nginx Status:nginx.connections.reading"> <drawtype>0</drawtype> <sortorder>3</sortorder> <color>00EE00</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <periods_cnt>5</periods_cnt> </graph_element> </graph_elements> </graph> </graphs> <macros /> </host> </hosts> <dependencies /> </zabbix_export>8.登录zabbix服务器首页,点击“配置”-“模板”。
点击导入,如图。
点击“导入文件”,然后将找到的nginx_status.xml文件导入模板。
创建应用程序集Nginx服务器。
将nginx主机连接到模板。
最后看能否检测到数据,能否顺利出图。
至此,实验结束。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)