工做中有用到Nginx作负载均衡,因此想尝试一下,把Nginx服务用zabbix监控起来html
记录下操做步骤,以防下次忘记nginx
涉及的实验环境:web
服务器操做系统:CentOS 7.2vim
zabbix版本:zabbix-2.2.5后端
Nginx版本:nginx-1.10.1安全
要作监控步骤前,先查看下当前Nginx中是否加载了--with-http_stub_status_module的模块。bash
# /data0/nginx/sbin/nginx -V nginx version: nginx/1.10.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/data0/nginx --pid-path=/data0/nginx/logs/nginx.pid --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --add-module=/root/nginx_upstream_check_module-master/ --add-module=/data0/soft/ngx_cache_purge-master/
由于这边zabbix是根据nginx的Stub Status模块,来抓取Status模块所提供的数据。服务器
假如从前还没有开启此模块,如今想启用StubStatus模块,你能够从新编译nginx源码安装包,而后在编译时记得加上参数 --with-http_stub_status_module,执行./configure && make就能够了,不用make install,若是make install记得先备份下,配置文件nginx.conf,以防止配置文件的内容都遗失了。app
由于Nginx配置时,我都有加载此模块,因此就很少加介绍了。负载均衡
从新加载nginx模块能够参照我以前写的博客
http://10803382.blog.51cto.com/10793382/1924871
在你的nginx.conf确认下,有没有下列内容,若是没有,那你能够在nginx.conf当中添加查看nginxstatus配置后,重启nginx服务便可。
# vim /data0/nginx/conf/nginx.conf ##找到server模块,server{}里加入下面的内容 #20170627 Nginx监控设置 location /nginxstatus{ stub_status on; access_log /data0/nginx/logs/status.log; allow 10.60.0.71; ##容许zabbix监控主机内网IP访问 allow 10.60.0.163; ##容许nginx本机内网IP访问 deny all; auth_basic "nginxstatus"; } #20170627 END
由于限定可访问IP,有助于保护网址安全
# /data0/nginx/sbin/nginx -t nginx: the configuration file /data0/nginx/conf/nginx.conf syntax is ok nginx: configuration file /data0/nginx/conf/nginx.conf test is successful # /data0/nginx/sbin/nginx -s reload
查看你设置好的nginxstatus链接情况
http://网址/nginxstatus
# curl http://127.0.0.1/nginxstatus <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.10.1</center> </body> </html>
你会发现出现了错误提示,显示了301 网站有设定了永久重定向,
若是你和我是同样的状况,网址作了https,那么请在nginx主机、zabbix监控主机上用网址来访问便可
# curl https://www.sss.com/nginxstatus Active connections: 9 server accepts handled requests 1453770 1453770 2718730 Reading: 0 Writing: 4 Waiting: 5
注:
Activeconnections:对后端发起的活动链接数;
server accepts :nginx 总共处理了1453770个链接;
handled:成功建立了1453770次握手;
requests:总共处理了2718730个请求。
Reading:nginx读取客户端的header数;
Writing: nginx 返回给客户端的header数;
Waiting: nginx 请求处理完成,正在等待下一请求指令的链接。
在上面的准备工做完了之后,接下来,咱们就要开始在nginx的那台机器上编辑一个可让zabbix服务器获取到数据的脚本
注:由于个人网站作了https,因此不是像别人同样直接访问IP和端口就能够了,我就直接访问网址便可
# vim /data0/zabbix/scripts/nginx_status.sh #!/bin/bash # Script is userd to fetch nginx statuses for zabbix monitoring systems # Set Variables BKUP_DATE=`/bin/date +%Y%m%d` LOG="/data0/zabbix/log/webstatus.log" HOST=www.sss.com export HOST # Function used to check the process of nginx function ping { ps -ef |grep nginx | wc -l } # Functions to return nginx stats function active { /usr/bin/curl "https://$HOST/nginxstatus" 2>/dev/null| grep 'Active' | awk '{print $NF}' } function reading { /usr/bin/curl "https://$HOST/nginxstatus" 2>/dev/null| grep 'Reading' | awk '{print $2}' } function writing { /usr/bin/curl "https://$HOST/nginxstatus" 2>/dev/null| grep 'Writing' | awk '{print $4}' } function waiting { /usr/bin/curl "https://$HOST/nginxstatus" 2>/dev/null| grep 'Waiting' | awk '{print $6}' } function accepts { /usr/bin/curl "https://$HOST/nginxstatus" 2>/dev/null| awk NR==3 | awk '{print $1}' } function handled { /usr/bin/curl "https://$HOST/nginxstatus" 2>/dev/null| awk NR==3 | awk '{print $2}' } function requests { /usr/bin/curl "https://$HOST/nginxstatus" 2>/dev/null| awk NR==3 | awk '{print $3}' } # Run the requested function $1
#上面是根据个人状况设定的,固然还有其余更简洁的方式,但由于能够实现想要的功能,因此就先不进行更改了。
赋予脚本可执行权限
# chmod +x nginx_status.sh
将脚本更改至zabbix用户和群组管理
# chown zabbix:zabbix nginx_status.sh
# ll nginx_status.sh -rwxr-xr-x 1 zabbix zabbix 1168 Jun 27 13:58 nginx_status.sh
在本地查看下你的设定是否能够出结果,否则出错,却没有发现,后面可能就没有办法收取到数据,也能够及时排错。
# /usr/bin/curl "https://www.sss.com/nginxstatus" 2>/dev/null| grep 'Active' | awk '{print $NF}' 12
上面数据一个个试过,若是能够像上面同样能出数据,说明没有问题。那你就能够到zabbix客户端去添加Nginx服务的模块获取设定
# grep -v "^[#;]" /data0/zabbix/etc/zabbix_agentd.conf | grep -v "^$" LogFile=/tmp/zabbix_agentd.log Server=10.60.0.71 ##zabbix server监控主机的IP地址 ServerActive=10.60.0.71 ##zabbix server监控主机的IP地址 Hostname=10.60.0.163 ##zabbix client 即本机nginx的IP地址 UnsafeUserParameters=1 ##默认状况下可能没有开启,那么你就把前面的注释去掉便可
那么在zabbix_agentd.conf里随便找个位置添加下列设定便可,我是放在了UnsafeUserParameters下面。
# vim /data0/zabbix/etc/zabbix_agentd.conf UserParameter=nginx.accepts,/data0/zabbix/scripts/nginx_status.sh accepts UserParameter=nginx.handled,/data0/zabbix/scripts/nginx_status.sh handled UserParameter=nginx.requests,/data0/zabbix/scripts/nginx_status.sh requests UserParameter=nginx.connections.active,/data0/zabbix/scripts/nginx_status.sh active UserParameter=nginx.connections.reading,/data0/zabbix/scripts/nginx_status.sh reading UserParameter=nginx.connections.writing,/data0/zabbix/scripts/nginx_status.sh writing UserParameter=nginx.connections.waiting,/data0/zabbix/scripts/nginx_status.sh waiting
重启下zabbix agent服务,让咱们刚刚的配置设定生效
# pkill -9 zabbix_agentd # /data0/zabbix/sbin/zabbix_agentd
如今关于zabbix client端的设定都已经完毕。转站zabbix server监控主机
首先测试zabbix server是否能够经过zabbix_get来获取到zabbix client端Nginx服务的数据
# /usr/local/zabbix/bin/zabbix_get -s 10.60.0.163 -p 10050 -k "nginx.accepts" 1422577
# /usr/local/zabbix/bin/zabbix_get -s 10.60.0.163 -p 10050 -k "nginx.handled" 1422800
# /usr/local/zabbix/bin/zabbix_get -s 10.60.0.163 -p 10050 -k "nginx.requests" 2654085
# /usr/local/zabbix/bin/zabbix_get -s 10.60.0.163 -p 10050 -k "nginx.connections.active" 17
# /usr/local/zabbix/bin/zabbix_get -s 10.60.0.163 -p 10050 -k "nginx.connections.reading" 0
# /usr/local/zabbix/bin/zabbix_get -s 10.60.0.163 -p 10050 -k "nginx.connections.writing" 3
# /usr/local/zabbix/bin/zabbix_get -s 10.60.0.163 -p 10050 -k "nginx.connections.waiting" 9
从上面上看,Nginx服务被监控项都是没有问题的。
在网上搜索到适合你用Nginx status的zabbix模板,而后把模板导入到zabbix web内,下面是我在本身的zabbix web上汇出的Nginx status的模板。若是以为合适,也能够在文章最下方下载个人模板。
<?xml version="1.0" encoding="UTF-8" ?> - <zabbix_export> <version>2.0</version> <date>2017-06-27T13:30:39Z</date> - <groups> - <group> <name>Freetrade</name> </group> </groups> - <templates> - <template> <template>Nginx Status</template> <name>Nginx Status</name> - <groups> - <group> <name>Freetrade</name> </group> </groups> - <applications> - <application> <name>Nginx</name> </application> </applications> - <items> - <item> <name>Nginx Accepts</name> <type>7</type> <snmp_community /> <multiplier>0</multiplier> <snmp_oid /> <key>nginx.accepts</key> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts>localhost</allowed_hosts> <units /> <delta>0</delta> <snmpv3_contextname /> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase /> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase /> <formula>0</formula> <delay_flex /> <params /> <ipmi_sensor /> <data_type>0</data_type> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <port /> <description /> <inventory_link>0</inventory_link> - <applications> - <application> <name>Nginx</name> </application> </applications> <valuemap /> </item> - <item> <name>Nginx Connections Active</name> <type>7</type> <snmp_community /> <multiplier>0</multiplier> <snmp_oid /> <key>nginx.connections.active</key> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts>localhost</allowed_hosts> <units /> <delta>0</delta> <snmpv3_contextname /> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase /> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase /> <formula>0</formula> <delay_flex /> <params /> <ipmi_sensor /> <data_type>0</data_type> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <port /> <description /> <inventory_link>0</inventory_link> - <applications> - <application> <name>Nginx</name> </application> </applications> <valuemap /> </item> - <item> <name>Nginx Connections Reading</name> <type>7</type> <snmp_community /> <multiplier>0</multiplier> <snmp_oid /> <key>nginx.connections.reading</key> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts>localhost</allowed_hosts> <units /> <delta>0</delta> <snmpv3_contextname /> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase /> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase /> <formula>0</formula> <delay_flex /> <params /> <ipmi_sensor /> <data_type>0</data_type> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <port /> <description /> <inventory_link>0</inventory_link> - <applications> - <application> <name>Nginx</name> </application> </applications> <valuemap /> </item> - <item> <name>Nginx Connections Waiting</name> <type>7</type> <snmp_community /> <multiplier>0</multiplier> <snmp_oid /> <key>nginx.connections.waiting</key> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts>localhost</allowed_hosts> <units /> <delta>0</delta> <snmpv3_contextname /> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase /> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase /> <formula>0</formula> <delay_flex /> <params /> <ipmi_sensor /> <data_type>0</data_type> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <port /> <description /> <inventory_link>0</inventory_link> - <applications> - <application> <name>Nginx</name> </application> </applications> <valuemap /> </item> - <item> <name>Nginx Connections Writing</name> <type>7</type> <snmp_community /> <multiplier>0</multiplier> <snmp_oid /> <key>nginx.connections.writing</key> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts>localhost</allowed_hosts> <units /> <delta>0</delta> <snmpv3_contextname /> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase /> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase /> <formula>0</formula> <delay_flex /> <params /> <ipmi_sensor /> <data_type>0</data_type> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <port /> <description /> <inventory_link>0</inventory_link> - <applications> - <application> <name>Nginx</name> </application> </applications> <valuemap /> </item> - <item> <name>Nginx Handled</name> <type>7</type> <snmp_community /> <multiplier>0</multiplier> <snmp_oid /> <key>nginx.handled</key> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts>localhost</allowed_hosts> <units /> <delta>0</delta> <snmpv3_contextname /> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase /> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase /> <formula>0</formula> <delay_flex /> <params /> <ipmi_sensor /> <data_type>0</data_type> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <port /> <description /> <inventory_link>0</inventory_link> - <applications> - <application> <name>Nginx</name> </application> </applications> <valuemap /> </item> - <item> <name>Nginx Requests</name> <type>7</type> <snmp_community /> <multiplier>0</multiplier> <snmp_oid /> <key>nginx.requests</key> <delay>30</delay> <history>365</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts>localhost</allowed_hosts> <units /> <delta>0</delta> <snmpv3_contextname /> <snmpv3_securityname /> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase /> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase /> <formula>0</formula> <delay_flex /> <params /> <ipmi_sensor /> <data_type>0</data_type> <authtype>0</authtype> <username /> <password /> <publickey /> <privatekey /> <port /> <description /> <inventory_link>0</inventory_link> - <applications> - <application> <name>Nginx</name> </application> </applications> <valuemap /> </item> </items> <discovery_rules /> <macros /> <templates /> <screens /> </template> </templates> - <graphs> - <graph> <name>Nginx Clients Status</name> <width>900</width> <height>200</height> <yaxismin>0.0000</yaxismin> <yaxismax>100.0000</yaxismax> <show_work_period>1</show_work_period> <show_triggers>1</show_triggers> <type>0</type> <show_legend>1</show_legend> <show_3d>0</show_3d> <percent_left>0.0000</percent_left> <percent_right>0.0000</percent_right> <ymin_type_1>0</ymin_type_1> <ymax_type_1>0</ymax_type_1> <ymin_item_1>0</ymin_item_1> <ymax_item_1>0</ymax_item_1> - <graph_items> - <graph_item> <sortorder>0</sortorder> <drawtype>0</drawtype> <color>0000EE</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> - <item> <host>Nginx Status</host> <key>nginx.connections.active</key> </item> </graph_item> - <graph_item> <sortorder>1</sortorder> <drawtype>0</drawtype> <color>EE0000</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> - <item> <host>Nginx Status</host> <key>nginx.connections.writing</key> </item> </graph_item> - <graph_item> <sortorder>2</sortorder> <drawtype>0</drawtype> <color>EEEE00</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> - <item> <host>Nginx Status</host> <key>nginx.connections.waiting</key> </item> </graph_item> - <graph_item> <sortorder>3</sortorder> <drawtype>0</drawtype> <color>00EE00</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> - <item> <host>Nginx Status</host> <key>nginx.connections.reading</key> </item> </graph_item> </graph_items> </graph> - <graph> <name>Nginx Socket Status</name> <width>900</width> <height>200</height> <yaxismin>0.0000</yaxismin> <yaxismax>100.0000</yaxismax> <show_work_period>1</show_work_period> <show_triggers>1</show_triggers> <type>0</type> <show_legend>1</show_legend> <show_3d>0</show_3d> <percent_left>0.0000</percent_left> <percent_right>0.0000</percent_right> <ymin_type_1>0</ymin_type_1> <ymax_type_1>0</ymax_type_1> <ymin_item_1>0</ymin_item_1> <ymax_item_1>0</ymax_item_1> - <graph_items> - <graph_item> <sortorder>0</sortorder> <drawtype>0</drawtype> <color>00EE00</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> - <item> <host>Nginx Status</host> <key>nginx.accepts</key> </item> </graph_item> - <graph_item> <sortorder>0</sortorder> <drawtype>0</drawtype> <color>EE0000</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> - <item> <host>Nginx Status</host> <key>nginx.handled</key> </item> </graph_item> - <graph_item> <sortorder>1</sortorder> <drawtype>0</drawtype> <color>EEEE00</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> - <item> <host>Nginx Status</host> <key>nginx.requests</key> </item> </graph_item> </graph_items> </graph> </graphs> </zabbix_export>
首先你要先在zabbix web端建立一个空的templates即模板以下图:
"组态--模板"-->到右上角"建立模板",填入你想要的模板名称,以及所属于该模板的主机,最后保存
把你下载好并修改为适合你的模板汇入到原建立好的空templates内,固然只要导入成功的话,你能够后期进templates去修改为适合你的。
选择右上角的"汇入"--"选择文件"--"汇入"
要出现汇入成功的提示,才说明你的模板是合适的,可是合适不必定有用哦
而后把你要的监控的Nginx主机归入到模板上去,再而后到被监控的Nginx主机里去查看,该模块是否被连结了
你应该能够看到你的Nginx主机上多了一些图形及项目,且在模板上已经连接到了你设定的新模板了
若是点进去看,nginx主机的监控项目中,Nginx服务相关的监控项目为Zabbix端点代理程式(主动式)哦
固然若是你没有导入模板成功,那你就老老实实一个个建立吧,不过也很简单啦!一步一步来就行了,多作加深印象,还便于理解
先创建新模板
在新建模板里增长一个应用集
添加监控项进入新建模板,添加的内容一项一项地加,大体基本相同,惟一不一样处是键值和名称
增长图形监控,到这里,只要你把监控项目建立好,在图形监控只要选择你要呈现图形的监控项目便可
到监控主机里进模板连结,直接选择现有的模板,而后添加保存
从上面看被监控主机已经载入新模板且项目集也存在了
最后查看经过"监测中"--"图形"选择被监控主机的图形名称,来看看图是否有加载Nginx Client Status,Nginx Socket Status;若是有,恭喜你,监控设置就说明成功了!
若是想作些告警之类的,能够在规则中设定触发器便可了,由于你们需求各不相同,故在此就再也不多说了。