配置 Zabbix 监控 Nginx(Apache、php-fpm)
2019獨角獸企業重金招聘Python工程師標準>>>
Zabbix 監控 Nginx
? ? 使用 zabbix 監控 nginx,實際上是通過?nginx 自帶 status 模塊來獲取數據的,所以需要配置 ngx_status。
? ? 啟用 nginx status 模塊,需要編譯時帶上參數?--with-http_sub_module(實際上在編譯時帶上?--with-http_stub_status_module 這個參數也是可以顯示 nginx status的)
? ? 配置文件中添加 nginx_status location:
location /nginx_status {stub_status on;access_log off;allow 192.168.0.1;deny all;}? ? 重啟 nginx,訪問 http://localhost/nginx_status:
Nginx status 監控狀態含義
Active connections: 2???#nginx?正處理的活動連接數2個。server accepts handled requests591 591 4936???????? # nginx啟動到現在共處理了 591 個連接?,?成功創建 591 次握手 一般跟第一個一樣,差值為請求丟失數,?總共處理了 4936 次請求。Reading: 0??????#nginx?讀取到客戶端的?Header?信息數。Writing: 1??????#nginx?返回給客戶端的?Header?信息數。Waiting: 1??????#開啟?keep-alive?的情況下,這個值等于?active - (reading + writing),意思就是?Nginx?已經處理完正在等候下一次請求指令的駐留連接。這個狀態信息,從nginx啟動算起,包括重載配置文件,也會清零。?
Zabbix 客戶端配置
? ? 由于監控 nginx 狀態的 key 在 zabbix agent 中并沒有預先定義的key,這時候我們可以通過編寫 zabbix 的用戶參數的方法來監控我們要求的項目 item。形象一點說 zabbix 代理端配置文件中的 User parameters就相當于通過腳本獲取要監控的值,然后把相關的腳本或者命令寫入到配置文件中的 User parameter 中然后 zabbix server 讀取配置文件中的返回值通過處理前端的方式返回給用戶。
? ? 配置/etc/zabbix/zabbix_agent.conf?語法:
UserParameter=<key>,<command># 其中 UserParameter 為關鍵字,key 為用戶自定義 key 名字可以隨便起,<command> 為我們要運行的命令或者腳本。# 傳參數 UserParameter=key[*],command#例 UserParameter=ping[*],echo $1 ping[0] # return '0' ping[111] # return '111'? ? 添加自定義 Userparameter 到配置文件:
# 參數寫死 UserParameter=nginx.Accepted-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a accepted UserParameter=nginx.Active-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a active UserParameter=nginx.Handled-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a handled UserParameter=nginx.Reading-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a reading UserParameter=nginx.Total-Requests,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a requests UserParameter=nginx.Waiting-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a waiting UserParameter=nginx.Writting-Connections,/usr/local/zabbix-3.0.0/scripts/getNginxInfo.py -h 127.0.0.1 -p 80 -a writing# 變量形式 UserParameter=nginx.status[*],/usr/local/zabbix-3.0.0/scripts/ngx_status.sh $1getNginxInfo.py:
?
#!/bin/env python # # Options: # # -a active # -a accepted # -a handled # -a requests # -a reading # -a writing # -a waiting #import urllib2, base64, sys, getopt import re##def Usage ():print "Usage: getNginxInfo.py -h 127.0.0.1 -p 80 -a [active|accepted|handled|request|reading|writing|waiting]"sys.exit(2)##def main ():# Default valueshost = "localhost"port = "80"getInfo = "None"if len(sys.argv) < 2:Usage()try:opts, args = getopt.getopt(sys.argv[1:], "h:p:a:")except getopt.GetoptError:Usage()# Assign parameters as variablesfor opt, arg in opts :if opt == "-h" :host = argif opt == "-p" :port = argif opt == "-a" :getInfo = argurl="http://" + host + ":" + port + "/nginx_status/"request = urllib2.Request(url)result = urllib2.urlopen(request)buffer = re.findall(r'\d{1,8}', result.read())## Format: ## Active connections: 196 ## server accepts handled requests ## 272900 272900 328835 ## Reading: 0 Writing: 6 Waiting: 190if ( getInfo == "active"):print buffer[0]elif ( getInfo == "accepted"):print buffer[1]elif ( getInfo == "handled"):print buffer[2]elif ( getInfo == "requests"):print buffer[3]elif ( getInfo == "reading"):print buffer[4]elif ( getInfo == "writing"):print buffer[5]elif ( getInfo == "waiting"):print buffer[6]else:print "unknown"sys.exit(1)if __name__ == "__main__":main()ngx_status.sh
?
#!/bin/bashHOST="127.0.0.1" PORT="8000"#檢查 nginx 進程是否存在 function ping {/sbin/pidof nginx|wc -l }#檢查 nginx 性能 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 '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}' }# 執行 funct $1zabbix_get 測試獲取數據
?
[root@localhost ~]# /usr/local/zabbix-3.0.0/bin/zabbix_get -s 127.0.0.1 -k 'Nginx.Accepted-Connections' 17311zabbix Web 端配置
? ? 導入 Template APP Nginx,并配置:
?
Zabbix 監控 Apache?
? ? Apache 監控原理跟 Nginx 相同,通過 Apache 的 status 模塊獲取數據,修改配置文件:
# 去掉注釋 LoadModule status_module modules/mod_status.so# 末尾添加 ExtendedStatus On <location /server-status>SetHandler server-statusOrder Deny,AllowDeny from allAllow from 127.0.0.1 </location>? ? curl http://localhost/server-status 即可獲取 Apache 狀態信息;
?
Zabbix 監控 php-fpm
? ? php-fpm?和 nginx?一樣內建了一個狀態頁,用于了解 php-fpm 的狀態以及監控 php-fpm;
# cat /usr/local/php-5.5.10/etc/php-fpm.conf | grep status_pathpm.status_path = /status? ? 假設使用 Nginx 加載 PHP:
server {listen *:80 default_server;server_name _;location ~ ^/(status|ping)${include fastcgi_params;fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;}}? ? 由于 php-fpm 運行在 127.0.0.1:9000 端口上的,所以 curl http://localhost/status
pool: www process manager: dynamic start time: 14/May/2016:22:40:15 +0800 start since: 58508 accepted conn: 33 listen queue: 0 max listen queue: 8 listen queue len: 0 idle processes: 2 active processes: 1 total processes: 3 max active processes: 5 max children reached: 0 slow requests: 2091????php-fpm status詳解
pool # fpm池子名稱,大多數為www process manager # 進程管理方式,值:static, dynamic or ondemand. dynamic start time # 啟動日期,如果reload了php-fpm,時間會更新 start since # 運行時長 accepted conn # 當前池子接受的請求數 listen queue # 請求等待隊列,如果這個值不為0,那么要增加FPM的進程數量 max listen queue # 請求等待隊列最高的數量 listen queue len # socket等待隊列長度 idle processes # 空閑進程數量 active processes # 活躍進程數量 total processes # 總進程數量 max active processes # 最大的活躍進程數量(FPM啟動開始算) max children reached # 大道進程最大數量限制的次數,如果這個數量不為0,那說明你的最大進程數量太小了,請改大一點。 slow requests # 啟用了php-fpm slow-log,緩慢請求的數量????php-fpm 狀態頁比較個性化的一個地方是它可以帶參數,可以帶參數json、xml、html并且前面三個參數可以分別和full做一個組合。
curl http://127.0.0.1/status?json curl http://127.0.0.1/status?xml curl http://127.0.0.1/status?html curl http://127.0.0.1/status?full? ? full 會多出幾個狀態:
pid # 進程PID,可以單獨kill這個進程. You can use this PID to kill a long running process. state # 當前進程的狀態 (Idle, Running, …) start time # 進程啟動的日期 start since # 當前進程運行時長 requests # 當前進程處理了多少個請求 request duration # 請求時長(微妙) request method # 請求方法 (GET, POST, …) request URI # 請求URI content length # 請求內容長度 (僅用于 POST) user # 用戶 (PHP_AUTH_USER) (or ‘-’ 如果沒設置) script # PHP腳本 (or ‘-’ if not set) last request cpu # 最后一個請求CPU使用率。 last request memorythe # 上一個請求使用的內存?
轉載于:https://my.oschina.net/u/2470065/blog/845503
總結
以上是生活随笔為你收集整理的配置 Zabbix 监控 Nginx(Apache、php-fpm)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gdb与信号
- 下一篇: Thinkphp3.23 关联模型rel