1. 启用php-fpm状态功能
php-fpm
和nginx
一样内建了一个状态页,对于想了解php-fpm的状态以及监控php-fpm非常有帮助。为了后续的Prometheus监控,我们需要先了解php-fpm状态页是怎么回事。
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = debug
[www]
listen = /tmp/php-cgi.sock
listen.backlog = 1024
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 300
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 10240
pm.status_path = /phpfpm_status
ping.path = /ping
request_terminate_timeout = 1200
pm.process_idle_timeout = 0
request_slowlog_timeout = 5
slowlog = var/log/slow.log
rlimit_files = 65535
默认情况下为pm.status_path = /status,当然也可以改成其他的等等。
如上,配置后,重启即开启了php-fpm
状态功能
2. nginx配置
在默认主机里面加上location
或者你希望能访问到的主机里面。
server {
listen 80;
server_name 127.0.0.1;
location /nginx_status {
stub_status on;
access_log off;
}
location ~ ^/(phpfpm_status|ping)$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
# fastcgi_pass 127.0.0.1:9000;
allow 127.0.0.1;
deny all;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
error_log /var/log/wwwlogs/127error.log ;
access_log /var/log/wwwlogs/127access.log main;
}
3. 重启nginx/php-fpm
service php-fpm restart
service nginx restart
4.测试
[root@iz conf.d]# curl http://127.0.0.1/phpfpm_status
pool: www
process manager: dynamic
start time: 21/Nov/2019:10:42:33 +0800
start since: 4498
accepted conn: 4896
listen queue: 0
max listen queue: 0
listen queue len: 0
idle processes: 19
active processes: 1
total processes: 20
max active processes: 14
max children reached: 0
slow requests: 153
[root@iz conf.d]# curl http://127.0.0.1/ping
pong
[root@iz conf.d]#
[root@iz conf.d]# curl http://127.0.0.1/nginx_status
Active connections: 5
server accepts handled requests
3481898 3481898 3526133
Reading: 0 Writing: 1 Waiting: 4
5. 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,缓慢请求的数量
6. 被监控服务器安装PHP-FPM-exporter
6.1 下载:`php-fpm-exporter`
地址:*https://github.com/bakins/php-fpm-exporter/releases*
6.2 安装php-fpm-exporter
mkdir -p /usr/local/php-fpm-exporter
mv php-fpm-exporter.linux.amd64 /usr/local/php-fpm-exporter/php-fpm-exporter
chmod +x /usr/local/php-fpm-exporter/php-fpm-exporter
6.3 启动php-fpm-exporter
nohup ./php-fpm-exporter --addr 172.19.14.250:9190 --endpoint http://127.0.0.1/phpfpm_status > /usr/local/php-fpm-exporter/php-fpm-exporter.log 2>&1 &
7.Prometheus端
7.1 配置Prometheus
# vim /usr/local/prometheus/prometheus.yml
scrape_configs:
- job_name: 'PHP-FPM'
static_configs:
- targets:
- 172.19.14.250:9190
7.2 重启
systemctl restart prometheus
8.Grafana端
dashboards.id 推荐3901
模板地址:https://grafana.com/dashboards/3901
参考:http://www.ttlsa.com/php/use-php-fpm-status-page-detail/