• Prometheus监控PHP应用


    要监控PHP状态信息,必须先配置显示PHP状态页,这样Prometheus才能通过Exporter进行监控。

    1、配置PHP-FPM,暴露php-fpm状态信息

    官方参考:https://easyengine.io/tutorials/php/fpm-status-page/
    修改/usr/local/php/etc/php-fpm.conf,增加以下内容

    pm.status_path = /status
    ping.path = /ping
    

    目前可用于监控PHP的Exporter有三个:

    本文只介绍前边两个

    2、bakins/php-fpm-exporter监控PHP应用

    bakins/php-fpm-exporter只能通过http读取PHP状态页信息,需要借助nginx来提供PHP状态页数据

    2.1、配置php状态页的http访问

    nginx.conf

    server {
        listen 9010; 
        allow 127.0.0.1;   #限制ip
        allow 192.168.28.131;   #限制ip
        deny all;
    
        location ~ ^/(status|ping)$ {
            fastcgi_pass  unix:/tmp/php-cgi.sock; # unix socket
            #fastcgi_pass 127.0.0.1:9000;         # tcp
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    查看状态页数据

    [root@ ~]# curl http://127.0.0.1:9010/status
    
    # php-fpm状态页的内容如下
    pool:                 www
    process manager:      dynamic
    start time:           24/Sep/2022:22:50:44 +0800
    start since:          2219
    accepted conn:        42
    listen queue:         0
    max listen queue:     0
    listen queue len:     0
    idle processes:       12
    active processes:     1
    total processes:      13
    max active processes: 5
    max children reached: 0
    slow requests:        0
    

    2.2、下载bakins/php-fpm-exporter

    下载地址:https://github.com/bakins/php-fpm-exporter
    下载文件:php-fpm-exporter.linux.amd64

    mv php-fpm-exporter.linux.amd64 /usr/local/php-fpm-exporter
    chmod u+x /usr/local/php-fpm-exporter
    
    # php-fpm-exporter命令参数
    usage: php-fpm-exporter []
    
    Flags:
      -h, --help                   Show context-sensitive help (also try --help-long and --help-man).
          --addr="127.0.0.1:8080"  listen address for metrics handler
          --endpoint="http://127.0.0.1:9000/status"  url for php-fpm status
          --fastcgi=FASTCGI        fastcgi url. If this is set, fastcgi will be used instead of HTTP
          --web.telemetry-path="/metrics"
                                   Path under which to expose metrics. Cannot be /
    
    # 运行
    /usr/local/php-fpm-exporter --addr 0.0.0.0:9011 --endpoint="http://127.0.0.1:9010/status" --web.telemetry-path="/metrics"
    

    查看转成Prometheus监控指标的状态页数据

    [root@s2 ~]# curl http://127.0.0.1:9011/metrics
    

    2.3、配置为系统服务

    vi /usr/lib/systemd/system/php_exporter.service

    [Unit]
    Description=php_exporter
    Wants=network-online.target
    After=network-online.target
    
    [Service]
    User=root
    Group=root
    Type=simple
    ExecStart=/usr/local/php-fpm-exporter \
        --addr=0.0.0.0:9011 \
        --endpoint=http://127.0.0.1:9010/status \
        --web.telemetry-path=/metrics
    
    [Install]
    WantedBy=multi-user.target
    

    php_exporter服务命令

    systemctl daemon-reload       # 通知systemd重新加载配置文件
    systemctl enable php_exporter   # 设置开机启动
    systemctl disable php_exporter  # 取消开机启动
    systemctl start php_exporter    # 启动服务
    systemctl restart php_exporter  # 重启服务
    systemctl stop php_exporter     # 关闭服务
    systemctl status php_exporter   # 查看状态
    

    2.4、配置防火墙

    如果端口未开启,需要开启相关端口

    #启动防火墙
    systemctl start firewalld.service
    #开通端口
    firewall-cmd --zone=public --add-port=9011/tcp --permanent
    #重启防火墙
    firewall-cmd --reload
    

    2.5、与Prometheus集成

    prometheus.yml

    scrape_configs:
      - job_name: 'PHP-FPM'
        static_configs:
          - targets: ['192.168.28.132:9011','192.168.28.136:9011']
    

    重新加载配置

    curl -X POST  http://127.0.0.1:9090/-/reload   # prmetheus不需要登录 
    curl -X POST  http://admin@123456:127.0.0.1:9090/-/reload  # prmetheus需要登录
    

    在这里插入图片描述

    在这里插入图片描述

    2.6、与Grafana集成

    可视化模板:https://grafana.com/grafana/dashboards/3901-php-fpm/
    在这里插入图片描述

    3、hipages/php-fpm_exporter监控php应用

    3.1、下载

    下载:https://github.com/hipages/php-fpm_exporter
    下载文件:php-fpm_exporter_2.2.0_linux_amd64

    mv php-fpm_exporter_2.2.0_linux_amd64 /usr/local/php-fpm_exporter
    chmod u+x /usr/local/php-fpm_exporter
    
    # 运行命令有两个
    # get方式是在命令行下显示结果 
    sudo -u www /usr/local/php-fpm_exporter get --phpfpm.scrape-uri "unix:///tmp/php-cgi.sock;/status"
    
    # server方式是以web方式示结果
    sudo -u www /usr/local/php-fpm_exporter server --web.listen-address ":9253" --phpfpm.scrape-uri "unix:///tmp/php-cgi.sock;/status"
    # web方式示结果查看监控数据
    curl http://127.0.0.1:9253/metrics
    

    3.2、配置为系统服务

    vi /usr/lib/systemd/system/php_exporter2.service

    [Unit]
    Description=php_exporter2
    Wants=network-online.target
    After=network-online.target
    
    [Service]
    User=www
    Group=www
    Type=simple
    ExecStart=/usr/local/php-fpm_exporter server \
        --web.listen-address ":9253" \
        --web.telemetry-path "/metrics" \
        --phpfpm.scrape-uri "unix:///tmp/php-cgi.sock;/status"
    #    --phpfpm.scrape-uri "tcp://127.0.0.1:9000/status"
    [Install]
    WantedBy=multi-user.target
    

    php_exporter2服务命令

    systemctl daemon-reload       # 通知systemd重新加载配置文件
    systemctl enable php_exporter2   # 设置开机启动
    systemctl disable php_exporter2  # 取消开机启动
    systemctl start php_exporter2    # 启动服务
    systemctl restart php_exporter2  # 重启服务
    systemctl stop php_exporter2     # 关闭服务
    systemctl status php_exporter2   # 查看状态
    

    vi /etc/passwd

    www:x:1002:1002::/home/www:/sbin/nologin
    # 修改为
    www:x:1002:1002::/home/www:/bin/bash
    

    3.3、与Prometheus集成

    scrape_configs:
      - job_name: 'PHP-FPM2'
        static_configs:
          - targets: ['192.168.28.136:9253']
            labels:
              namespace: '192.168.28.136:9253'
          - targets: ['192.168.28.132:9253']
            labels:
              namespace: '192.168.28.132:9253'
    

    在这里插入图片描述

    在这里插入图片描述

    3.4、与Grafana集成

    可视化模板:https://grafana.com/grafana/dashboards/15796-php-fpm/
    在这里插入图片描述

  • 相关阅读:
    Python 04 之变量【列表,元组,集合,字典,字符串】
    网络舆情危及企业经营如何应对?舆情优化十大解决方案!
    【PMTU】TCP的MTU探测
    UNIAPP实战项目笔记44 订单页面顶部选项卡 有数据页面样式布局和无数据页面样式布局
    java基础10题
    spring注解之@SpringBootApplication注解
    Git 常用命令及其作用
    C++ 纠错题总结2
    最好的期货开户的标准是什么?
    Windows提示“无法删除文件:无法读源文件或磁盘”怎么办?
  • 原文地址:https://blog.csdn.net/penngo/article/details/127045590