最近一直在做服務器端性能優化的事, 而監控是性能優化的首要條件.下面是用zabbix監控Nginx安裝的過程:
1.linux下Nginx的安裝
2. 配置Nginx_status
在${NGINX_HOME}/conf/nginx.conf的server{}中添加以代碼:
- location /nginx_status {
- stub_status on;
- access_log off;
- #allow 127.0.0.1;
- #deny all;
- }
注: 爲了安全可以添加allow, deny; allow 是允許某個ip或者一個ip段訪問, deny禁止某個ip或者一個ip段訪問;
3. 安裝zabbix_server與zabbix_agent
大家可以去網上找找相關的資料.
4. 配置zabbix_agent: /etc/zabbix/zabbix_agentd.conf文件
- Server=zabbix_server的ip
- ServerActive=zabbix_server的ip
- Hostname=本機ip #不要寫127.0.0.1
5. 添加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
6. 在/etc/zabbix/zabbix_agentd.d/下添加zabbix_nginx的數據收集文件userparameter_nginx.conf
- UserParameter=nginx.accepts,/etc/zabbix/scripts/nginx_status.sh accepts
- UserParameter=nginx.handled,/etc/zabbix/scripts/nginx_status.sh handled
- UserParameter=nginx.requests,/etc/zabbix/scripts/nginx_status.sh requests
- UserParameter=nginx.connections.active,/etc/zabbix/scripts/nginx_status.sh active
- UserParameter=nginx.connections.reading,/etc/zabbix/scripts/nginx_status.sh reading
- UserParameter=nginx.connections.writing,/etc/zabbix/scripts/nginx_status.sh writing
- UserParameter=nginx.connections.waiting,/etc/zabbix/scripts/nginx_status.sh waiting
7. 重啓zabbix-agent服務
- service zabbix-agent restart
8.測試zabbix-server可以獲取數據
- zabbix_get -s nginx服務ip -p 10050 -k "nginx.connections.active"
9. 配置zabbix-server的nginx status模版
- <?xml version="1.0" encoding="UTF-8"?>
- <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></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_username>
- <ipmi_password></ipmi_password>
- <groups>
- <group>Freetrade</group>
- </groups>
- <triggers/>
- <items>
- <item type="7" key="nginx.accepts" value_type="3">
- <description>Nginx Accepts</description>
- <ipmi_sensor></ipmi_sensor>
- <delay>30</delay>
- <history>365</history>
- <trends>365</trends>
- <status>0</status>
- <data_type>0</data_type>
- <units></units>
- <multiplier>0</multiplier>
- <delta>0</delta>
- <formula>0</formula>
- <lastlogsize>0</lastlogsize>
- <logtimefmt></logtimefmt>
- <delay_flex></delay_flex>
- <authtype>0</authtype>
- <username></username>
- <password></password>
- <publickey></publickey>
- <privatekey></privatekey>
- <params></params>
- <trapper_hosts>localhost</trapper_hosts>
- <snmp_community></snmp_community>
- <snmp_oid></snmp_oid>
- <snmp_port>161</snmp_port>
- <snmpv3_securityname></snmpv3_securityname>
- <snmpv3_securitylevel>0</snmpv3_securitylevel>
- <snmpv3_authpassphrase></snmpv3_authpassphrase>
- <snmpv3_privpassphrase></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></ipmi_sensor>
- <delay>30</delay>
- <history>365</history>
- <trends>365</trends>
- <status>0</status>
- <data_type>0</data_type>
- <units></units>
- <multiplier>0</multiplier>
- <delta>0</delta>
- <formula>0</formula>
- <lastlogsize>0</lastlogsize>
- <logtimefmt></logtimefmt>
- <delay_flex></delay_flex>
- <authtype>0</authtype>
- <username></username>
- <password></password>
- <publickey></publickey>
- <privatekey></privatekey>
- <params></params>
- <trapper_hosts>localhost</trapper_hosts>
- <snmp_community></snmp_community>
- <snmp_oid></snmp_oid>
- <snmp_port>161</snmp_port>
- <snmpv3_securityname></snmpv3_securityname>
- <snmpv3_securitylevel>0</snmpv3_securitylevel>
- <snmpv3_authpassphrase></snmpv3_authpassphrase>
- <snmpv3_privpassphrase></snmpv3_privpassphrase>
- <applications>
- <application>Nginx</application>
- </applications>
- </item>
- <item type="7" key="nginx.connections.reading" value_type="3">
- <description>Nginx Connections Reading</description>
-
- <application>Nginx</application>
- </applications>
- </item>
- <item type="7" key="nginx.connections.reading" value_type="3">
- <description>Nginx Connections Reading</description>
- <ipmi_sensor></ipmi_sensor>
- application>Nginx</application>