Prometheus监控PHP-FPM

环境

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,获取展示模板。

prometheus-webhook-dingtalk 是一个用于将 Prometheus 监控告警消息发送到钉钉的 Webhook 工具。它可以帮助你将 Prometheus 监控告警通过钉钉机器人发送到指定的群组或用户。 你可以通过以下步骤来配置和使用 prometheus-webhook-dingtalk: 1. 安装 prometheus-webhook-dingtalk:你可以使用 Go 工具链来安装 prometheus-webhook-dingtalk,运行以下命令: ``` go get github.com/timonwong/prometheus-webhook-dingtalk/cmd/dingtalk ``` 2. 创建钉钉机器人:在钉钉中创建一个自定义机器人,并获取到它的 Webhook 地址,用于将告警消息发送到指定的群组或用户。 3. 创建配置文件:在 prometheus-webhook-dingtalk 的配置文件中,你需要指定钉钉机器人的 Webhook 地址以及其他相关参数。你可以创建一个名为 config.yml 的配置文件,并将以下示例内容填入: ```yaml listen: 0.0.0.0:8060 dingtalk: webhook: https://oapi.dingtalk.com/robot/send?access_token=your_webhook_token ``` 4. 启动 prometheus-webhook-dingtalk:运行以下命令来启动 prometheus-webhook-dingtalk: ``` dingtalk -config.file=config.yml ``` 5. 配置 Prometheus:在 Prometheus 的配置文件中,添加以下内容来指定告警消息的接收端: ```yaml receivers: - name: 'dingtalk' webhook_configs: - url: 'http://prometheus-webhook-dingtalk:8060/dingtalk/webhook' ``` 6. 重新启动 Prometheus:确保 Prometheus 已经重新加载了配置文件,并重启 Prometheus 服务。 现在,当 Prometheus 监控触发告警时,prometheus-webhook-dingtalk 将会将告警消息发送到钉钉机器人的 Webhook 地址,从而通知到指定的群组或用户。 请注意,以上步骤仅为一般示例,实际操作可能会因环境和需求而有所不
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值