文章目录
要监控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 = /statusping.path = /ping
目前可用于监控PHP的Exporter有三个:
- https://github.com/bakins/php-fpm-exporter
- https://github.com/hipages/php-fpm_exporter
- https://github.com/Lusitaniae/phpfpm_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: wwwprocess manager: dynamicstart time: 24/Sep/2022:22:50:44 +0800start since: 2219accepted conn: 42listen queue: 0max listen queue: 0listen queue len: 0idle processes: 12active processes: 1total processes: 13max active processes: 5max children reached: 0slow 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-exporterchmod 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_exporterWants=network-online.targetAfter=network-online.target[Service]User=rootGroup=rootType=simpleExecStart=/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_exporterchmod 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_exporter2Wants=network-online.targetAfter=network-online.target[Service]User=wwwGroup=wwwType=simpleExecStart=/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'