环境

PHP版本:7.3
在docker里通过supervisor启动PHP

步骤

PHP-FPM端

配置PHP-FPM开启status_path

# vim /etc/php-fpm.d/www.conf
pm.status_path = /status
ping.path = /ping

Nginx端配置监听9010端口

server {
    listen 9010;

    location ~ ^/(status|ping)$ {
        fastcgi_pass php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        allow 127.0.0.1;
        deny all;
    }
}

配置后重启两个服务,尝试获取监听信息

#  curl http://127.0.0.1:9010/status
pool:                 www
process manager:      static
start time:           16/May/2022:17:24:17 +0800
start since:          1839
accepted conn:        832
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       1
active processes:     1
total processes:      2
max active processes: 2
max children reached: 0
slow requests:        0

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,缓慢请求的数量

在nginx的容器里下载php-fpm-exporter

地址:https://github.com/bakins/php-fpm-exporter/releases
把下载的二进制文件,放在/usr/local/prometheus/里,并增加x执行权限。

配置 php-fpm-exporter

在 /etc/supervisord.d/ 目录下新建一个 php-fpm-exporter.ini 文件,文件内容如下:

[program:php-fpm-exporter]
command=/usr/local/prometheus/php-fpm-exporter --addr 0.0.0.0:9190 --endpoint http://127.0.0.1:9010/status
directory=/usr/local/prometheus
startsecs=5
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10
stdout_logfile=/var/log/supervisor/php-fpm-exporter-stdout-supervisor.log
stdout_capture_maxbytes=1MB

配置后,通过supervisor 启动exporter。访问 nginx开放的 http://127.0.0.1:9190/metrics 接口,是否能正常获取数据。正常返回数据如下:

# curl http://127.0.0.1:9190/metrics
# HELP phpfpm_accepted_connections_total Total number of accepted connections
# TYPE phpfpm_accepted_connections_total counter
phpfpm_accepted_connections_total 2
# HELP phpfpm_active_max_processes Maximum active process count
# TYPE phpfpm_active_max_processes counter
phpfpm_active_max_processes 1
# HELP phpfpm_listen_queue_connections Number of connections that have been initiated but not yet accepted
# TYPE phpfpm_listen_queue_connections gauge
phpfpm_listen_queue_connections 0
# HELP phpfpm_listen_queue_length_connections The length of the socket queue, dictating maximum number of pending connections
# TYPE phpfpm_listen_queue_length_connections gauge
phpfpm_listen_queue_length_connections 128
# HELP phpfpm_listen_queue_max_connections Max number of connections the listen queue has reached since FPM start
# TYPE phpfpm_listen_queue_max_connections counter
phpfpm_listen_queue_max_connections 0
# HELP phpfpm_max_children_reached_total Number of times the process limit has been reached
# TYPE phpfpm_max_children_reached_total counter
phpfpm_max_children_reached_total 0
# HELP phpfpm_processes_total process count
# TYPE phpfpm_processes_total gauge
phpfpm_processes_total{state="active"} 1
phpfpm_processes_total{state="idle"} 1
# HELP phpfpm_scrape_failures_total Number of errors while scraping php_fpm
# TYPE phpfpm_scrape_failures_total counter
phpfpm_scrape_failures_total 0
# HELP phpfpm_slow_requests_total Number of requests that exceed request_slowlog_timeout
# TYPE phpfpm_slow_requests_total counter
phpfpm_slow_requests_total 0
# HELP phpfpm_up able to contact php-fpm
# TYPE phpfpm_up gauge
phpfpm_up 1

Prometheus端

配置Prometheus

在 prometheus 的配置文件里增加 php-fpm-exporter 配置

  - job_name: 'PHP-FPM'
    static_configs:
      - targets: ['10.1.0.1:9190']

重启生效

Grafana端

添加dashboards

添加3901的dashboards,获取展示模板。