服務器監控之zabbix監控Nginx篇

服務器監控之zabbix監控Nginx篇

原創  2016年05月31日 09:40:39
  • 2684

最近一直在做服務器端性能優化的事, 而監控是性能優化的首要條件.下面是用zabbix監控Nginx安裝的過程:

1.linux下Nginx的安裝

大家可以參考我以前寫的 Nginx安裝配置篇(Session共享)

2. 配置Nginx_status

在${NGINX_HOME}/conf/nginx.conf的server{}中添加以代碼:
[html]  view plain  copy
  1. location /nginx_status {  
  2.         stub_status on;  
  3.         access_log  off;  
  4.         #allow 127.0.0.1;  
  5.         #deny all;  
  6. }  
注: 爲了安全可以添加allow, deny; allow 是允許某個ip或者一個ip段訪問, deny禁止某個ip或者一個ip段訪問;

3. 安裝zabbix_server與zabbix_agent

大家可以去網上找找相關的資料.

4. 配置zabbix_agent: /etc/zabbix/zabbix_agentd.conf文件

[html]  view plain  copy
  1. Server=zabbix_server的ip  
  2. ServerActive=zabbix_server的ip  
  3. Hostname=本機ip #不要寫127.0.0.1  

5. 添加nginx_status.sh腳本文件

[html]  view plain  copy
  1. #!/bin/bash  
  2.   
  3. #script to fetch nginx statuses for tribily monitoring systems  
  4. # Author: [email protected]  
  5. # License: GPLv2  
  6. # Set Variables  
  7. BKUP_DATE=`/bin/date +%Y%m%d`  
  8. LOG="/data/log/zabbix/webstatus.log"  
  9. HOST=127.0.0.1  
  10. PORT="80"  
  11. # Functions to return nginx stats  
  12. function active {  
  13.   /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Active' | awk '{print $NF}'  
  14. }  
  15. function reading {  
  16.   /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Reading' | awk '{print $2}'  
  17. }  
  18. function writing {  
  19.   /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Writing' | awk '{print $4}'  
  20. }  
  21. function waiting {  
  22.   /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Waiting' | awk '{print $6}'  
  23. }  
  24. function accepts {  
  25.   /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| awk NR==3 | awk '{print $1}'  
  26. }  
  27. function handled {  
  28.   /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| awk NR==3 | awk '{print $2}'  
  29. }  
  30. function requests {  
  31.   /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| awk NR==3 | awk '{print $3}'  
  32. }  
  33. # Run the requested function  
  34. $1  

6. 在/etc/zabbix/zabbix_agentd.d/下添加zabbix_nginx的數據收集文件userparameter_nginx.conf

[html]  view plain  copy
  1. UserParameter=nginx.accepts,/etc/zabbix/scripts/nginx_status.sh accepts  
  2. UserParameter=nginx.handled,/etc/zabbix/scripts/nginx_status.sh handled    
  3. UserParameter=nginx.requests,/etc/zabbix/scripts/nginx_status.sh requests    
  4. UserParameter=nginx.connections.active,/etc/zabbix/scripts/nginx_status.sh active    
  5. UserParameter=nginx.connections.reading,/etc/zabbix/scripts/nginx_status.sh reading    
  6. UserParameter=nginx.connections.writing,/etc/zabbix/scripts/nginx_status.sh writing    
  7. UserParameter=nginx.connections.waiting,/etc/zabbix/scripts/nginx_status.sh waiting  

7. 重啓zabbix-agent服務

[html]  view plain  copy
  1. service zabbix-agent restart  

8.測試zabbix-server可以獲取數據

[html]  view plain  copy
  1. zabbix_get -s nginx服務ip -p 10050 -k "nginx.connections.active"  

9. 配置zabbix-server的nginx status模版

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <zabbix_export version="1.0" date="14.03.11" time="08.48">  
  3.   <hosts>  
  4.     <host name="Nginx Status">  
  5.       <proxy_hostid>0</proxy_hostid>  
  6.       <useip>1</useip>  
  7.       <dns></dns>  
  8.       <ip>127.0.0.1</ip>  
  9.       <port>10050</port>  
  10.       <status>3</status>  
  11.       <useipmi>0</useipmi>  
  12.       <ipmi_ip>127.0.0.1</ipmi_ip>  
  13.       <ipmi_port>623</ipmi_port>  
  14.       <ipmi_authtype>0</ipmi_authtype>  
  15.       <ipmi_privilege>2</ipmi_privilege>  
  16.       <ipmi_username></ipmi_username>  
  17.       <ipmi_password></ipmi_password>  
  18.       <groups>  
  19.         <group>Freetrade</group>  
  20.       </groups>  
  21.       <triggers/>  
  22.       <items>  
  23.         <item type="7" key="nginx.accepts" value_type="3">  
  24.           <description>Nginx Accepts</description>  
  25.           <ipmi_sensor></ipmi_sensor>  
  26.           <delay>30</delay>  
  27.           <history>365</history>  
  28.           <trends>365</trends>  
  29.           <status>0</status>  
  30.           <data_type>0</data_type>  
  31.           <units></units>  
  32.           <multiplier>0</multiplier>  
  33.           <delta>0</delta>  
  34.           <formula>0</formula>  
  35.           <lastlogsize>0</lastlogsize>  
  36.           <logtimefmt></logtimefmt>  
  37.           <delay_flex></delay_flex>  
  38.           <authtype>0</authtype>  
  39.           <username></username>  
  40.           <password></password>  
  41.           <publickey></publickey>  
  42.           <privatekey></privatekey>  
  43.           <params></params>  
  44.           <trapper_hosts>localhost</trapper_hosts>  
  45.           <snmp_community></snmp_community>  
  46.           <snmp_oid></snmp_oid>  
  47.           <snmp_port>161</snmp_port>  
  48.           <snmpv3_securityname></snmpv3_securityname>  
  49.           <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  50.           <snmpv3_authpassphrase></snmpv3_authpassphrase>  
  51.           <snmpv3_privpassphrase></snmpv3_privpassphrase>  
  52.           <applications>  
  53.             <application>Nginx</application>  
  54.           </applications>  
  55.         </item>  
  56.         <item type="7" key="nginx.connections.active" value_type="3">  
  57.           <description>Nginx Connections Active</description>  
  58.           <ipmi_sensor></ipmi_sensor>  
  59.           <delay>30</delay>  
  60.           <history>365</history>  
  61.           <trends>365</trends>  
  62.           <status>0</status>  
  63.           <data_type>0</data_type>  
  64.           <units></units>  
  65.           <multiplier>0</multiplier>  
  66.           <delta>0</delta>  
  67.           <formula>0</formula>  
  68.           <lastlogsize>0</lastlogsize>  
  69.           <logtimefmt></logtimefmt>  
  70.           <delay_flex></delay_flex>  
  71.           <authtype>0</authtype>  
  72.           <username></username>  
  73.           <password></password>  
  74.           <publickey></publickey>  
  75.           <privatekey></privatekey>  
  76.           <params></params>  
  77.           <trapper_hosts>localhost</trapper_hosts>  
  78.           <snmp_community></snmp_community>  
  79.           <snmp_oid></snmp_oid>  
  80.           <snmp_port>161</snmp_port>  
  81.           <snmpv3_securityname></snmpv3_securityname>  
  82.           <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  83.           <snmpv3_authpassphrase></snmpv3_authpassphrase>  
  84.           <snmpv3_privpassphrase></snmpv3_privpassphrase>  
  85.           <applications>  
  86.             <application>Nginx</application>  
  87.           </applications>  
  88.         </item>  
  89.         <item type="7" key="nginx.connections.reading" value_type="3">  
  90.           <description>Nginx Connections Reading</description>  
  91.             
  92.             <application>Nginx</application>  
  93.           </applications>  
  94.         </item>  
  95.         <item type="7" key="nginx.connections.reading" value_type="3">  
  96.           <description>Nginx Connections Reading</description>  
  97.           <ipmi_sensor></ipmi_sensor>  
  98.           application>Nginx</application>
相關文章
相關標籤/搜索