工做中有用到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;若是有,恭喜你,監控設置就說明成功了!
若是想作些告警之類的,能夠在規則中設定觸發器便可了,由於你們需求各不相同,故在此就再也不多說了。