Nginx自帶監控模塊ngx_http_stub_status_module提供Nginx的基本信息php
在編譯安裝Nginx時加參數 --with-http_stub_status_module html
安裝好之後能夠經過nginx -V|grep http_stub_status_module 查看狀態模塊是否已安裝nginx
PHP-FPM也自帶監控,經過在php-fpm.conf中設置shell
pm.status_path = /php-fpm_statusjson
就能夠獲取URL的方式獲取PHP-FPM的狀態bash
參考文章併發
https://rtcamp.com/tutorials/php/fpm-status-page/ less
添加nginx_status.conf
curl
server { listen 88 ; location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; # allow 10.4.1.125; deny all; } location /php-fpm_status { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
$ curl 127.0.0.1:88/nginx_status Active connections: 1 server accepts handled requests 788163 788163 788163 Reading: 0 Writing: 1 Waiting: 0
Active connections socket
The current number of active client connections including Waiting
connections.
活躍客戶端鏈接數,包括處於等待狀態的鏈接數
accepts
The total number of accepted client connections.
接收到的客戶端鏈接總數
handled
The total number of handled connections. Generally, the parameter value is the same as accepts
unless some resource limits have been reached
處理請求的總數。一般狀況下,這個值和accepts的值相同。除非達到了一些資源限制。例如設置worker_connections 1024; 設置一個worker進程可以打開的最大併發鏈接數。
requests
The total number of client requests.
客戶端請求總數
Reading
The current number of connections where nginx is reading the request header.
當前Nginx正在讀取請求頭的鏈接數量
Writing
The current number of connections where nginx is writing the response back to the client.
當前Nginx正在將響應寫回到客戶端的鏈接數量
Waiting
The current number of idle client connections waiting for a request.
當前正在等待請求的閒置客戶端鏈接數量
$ curl 127.0.0.1:88/php-fpm_status pool: www process manager: dynamic start time: 16/Nov/2014:13:29:11 +0800 start since: 77844 accepted conn: 202788 listen queue: 0 max listen queue: 1 listen queue len: 128 idle processes: 6 active processes: 1 total processes: 7 max active processes: 4 max children reached: 0 slow requests: 0
pool pool名稱
process manager static or dynamic
start time 啓動時間
start since 啓動了多長時間,以秒爲單位
accepted conn pool接收到的請求數量
listen queue the number of request in the queue of pending connections.這個值若是不爲0,最好增長PHP-FPM的進程數量
max listen queue the maximum number of requests in the queue of pending connections since FPM has started
listen queue len the size of the socket queue of pending connections
idle processes the number of idle processes
active processes the number of active processes
total processes the number of idle + active processes
max active processes the maximum number of active processes since FPM has started
max children reached number of times, the process limit has been reached, when pm tries to start more children. If that value is not zero, then you may need to increase max process limit for your PHP-FPM pool. Like this, you can find other useful information to tweak your pool better way.若是這個值不爲0,最好增大最大進程的限制。
slow requests 若是這個值不爲0,表示有處理慢的程序
$ curl 127.0.0.1:88/php-fpm_status?full pool: www process manager: dynamic start time: 17/Nov/2014:16:09:17 +0800 start since: 1139 accepted conn: 3354 listen queue: 0 max listen queue: 0 listen queue len: 128 idle processes: 5 active processes: 1 total processes: 6 max active processes: 2 max children reached: 0 slow requests: 0 ************************ pid: 19921 state: Idle start time: 17/Nov/2014:16:09:17 +0800 start since: 1139 requests: 563 request duration: 223 request method: GET request URI: /php-fpm_status content length: 0 user: - script: - last request cpu: 0.00 last request memory: 262144
curl 127.0.0.1:88/php-fpm_status?json
curl 127.0.0.1:88/php-fpm_status?html
curl 127.0.0.1:88/php-fpm_status?xml
2.編寫Nginx和PHP-FPM狀態信息獲取腳本
nginx_status.sh
#!/bin/bash #check nginx status #ip=$(ifconfig eth0|grep "inet addr"|sed 's/^.*addr://'|awk '{print $1}') #echo $ip ip=127.0.0.1 port=88 #echo $ip:$port function active() { /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Active"|awk '{print $NF}' } function reading() { /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Reading"|awk '{print $2}' } function writing() { /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Writing"|awk '{print $4}' } function waiting() { /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Waiting"|awk '{print $6}' } function accepts() { /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $1}' } function handled() { /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $2}' } function requests(){ /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $3}' } case $1 in active) active ;; reading) reading ;; writing) writing ;; waiting) waiting ;; accepts) accepts ;; handled) handled ;; requests) requests ;; *) exit 1 ;; esac
php-fpm_status.sh
#!/bin/bash #check php-fpm status ip=127.0.0.1 port=88 function idle() { /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "idle processes"|awk '{print $3}' } function active() { /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "active processes"|awk '{print $3}'|grep -v "processes" } function total() { /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "total processes"|awk '{print $3}'|grep -v "processes" } function mactive() { /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "max active processes"|awk '{print $4}' } function conn() { /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "accepted conn"|awk '{print $3}' } function since() { /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "start since"|awk '{print $3}' } function slow() { /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "slow requests"|awk '{print $3}' } function listenqueue() { /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "listen queue:"|grep -v "max"|awk '{print $3}' } function maxlistenqueue() { /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "max listen queue:"|awk '{print $4}' } function listenqueuelen() { /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "listen queue len:"|awk '{print $4}' } function maxchildren() { /usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "max children reached:"|awk '{print $4}' } $1
3.添加zabbix的子配置文件
nginx_status_zabbix.conf
### Option: UserParameter # User-defined parameter to monitor. There can be several user-defined parameters. # Format: UserParameter=<key>,<shell command> # See 'zabbix_agentd' directory for examples. # # Mandatory: no # Default: # UserParameter= # /usr/local/zabbix/bin/nginx_status.sh UserParameter=nginx.accepts,/usr/local/zabbix/bin/nginx_status.sh accepts UserParameter=nginx.handled,/usr/local/zabbix/bin/nginx_status.sh handled UserParameter=nginx.requests,/usr/local/zabbix/bin/nginx_status.sh requests UserParameter=nginx.connections.active,/usr/local/zabbix/bin/nginx_status.sh active UserParameter=nginx.connections.reading,/usr/local/zabbix/bin/nginx_status.sh reading UserParameter=nginx.connections.writing,/usr/local/zabbix/bin/nginx_status.sh writing UserParameter=nginx.connections.waiting,/usr/local/zabbix/bin/nginx_status.sh waiting
php-fpm_status.conf
UserParameter=php-fpm.idle.processes,/usr/local/zabbix/bin/php-fpm_status.sh idle UserParameter=php-fpm.total.processes,/usr/local/zabbix/bin/php-fpm_status.sh total UserParameter=php-fpm.active.processes,/usr/local/zabbix/bin/php-fpm_status.sh active UserParameter=php-fpm.max.active.processes,/usr/local/zabbix/bin/php-fpm_status.sh mactive UserParameter=php-fpm.listen.queue.len,/usr/local/zabbix/bin/php-fpm_status.sh listenqueuelen UserParameter=php-fpm.listen.queue,/usr/local/zabbix/bin/php-fpm_status.sh listenqueue UserParameter=php-fpm.start.since,/usr/local/zabbix/bin/php-fpm_status.sh since UserParameter=php-fpm.accepted.conn,/usr/local/zabbix/bin/php-fpm_status.sh conn UserParameter=php-fpm.slow.requests,/usr/local/zabbix/bin/php-fpm_status.sh slow UserParameter=php-fpm.max.listen.queue,/usr/local/zabbix/bin/php-fpm_status.sh maxlistenqueue UserParameter=php-fpm.max.children,/usr/local/zabbix/bin/php-fpm_status.sh maxchildren
4.添加zabbix監控模板
模板參見附件