为什么要监控php-fpm的状态?
因为php-fpm多进程模型,线上一般都是static静态进程数配置,很容易因为I/O慢导致进程全部占满,造成拒绝服务。
php-fpm有哪些状态需要关注?
活跃进程的比例,可以反馈出进程数是否够用。
如何监控?
php-fpm自带status接口可以拉取到json格式的状态信息,通过nginx向外暴露status接口,并将json数据处理成prometheus格式,供prometheus抓取。
下面是对接步骤。
php-fpm支持status接口
我们需要给php-fpm的进程池开启status接口,找到php-fpm.conf,编辑如下内容:
Shell
; Note: The value must start with a leading slash (/). The value can be
; anything, but it may not be a good idea to use the .php extension or it
; may conflict with a real PHP file.
; Default Value: not set
pm.status_path = /pmstatus
1
2
3
4
5
;Note:Thevaluemuststartwithaleadingslash(/).Thevaluecanbe
;anything,butitmaynotbeagoodideatousethe.phpextensionorit
;mayconflictwitharealPHPfile.
;DefaultValue:notset
pm.status_path=/pmstatus
这表示接口地址是/pmstatus。
但是我们没法直接用fcgi协议访问php-fpm,因此还需要配置nginx转发。
nginx转发status接口
我们配置一下容器内的nginx,让其转发/pmstatus接口给PHP-FPM即可:
server {
listen 800;
server_name myserver;
charset utf-8;
location /pmstatus {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/phpfpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
1
2
3
4
5
6
7
8
9
10
11
server{
listen800;
server_namemyserver;
charsetutf-8;
location/pmstatus{
includefastcgi_params;
fastcgi_passunix:/var/run/php/phpfpm.sock;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
includefastcgi_params;
}
我们请求一下/pmstatus接口,可以看到json格式的监控数据:
Shell
curl localhost:800/pmstatus?json
{
"pool": "www",
"process manager": "static",
"start time": 1586943856,
"start since": 14,
"accepted conn": 2,
"listen queue": 0,
"max listen queue": 0,
"listen queue len": 0,
"idle processes": 99,
"active processes": 1,
"total processes": 100,
"max active processes": 1,
"max children reached": 0,
"slow requests": 0
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
curllocalhost:800/pmstatus?json
{
"pool":"www",
"process manager":"static",
"start time":1586943856,
"start since":14,
"accepted conn":2,
"listen queue":0,
"max listen queue":0,
"listen queue len":0,
"idle processes":99,
"active processes":1,
"total processes":100,
"max active processes":1,
"max children reached":0,
"slow requests":0
}
其中重要的指标就是total processes和active processes,表示进程池的上限进程数和当前活跃的进程数。
引入nginx-lua-prometheus库
prometheus只能采集特定格式的metrics,因此我们需要再实现1个接口将上述json数据做一次格式转换。
该接口需要在nginx里面实现,这样最轻量,因此我们需要使用openresty来编写lua程序,具体openresty如何安装大家自行学习。
nginx-lua-prometheus库提供了基于openresty的prometheus封装,地址是:https://github.com/knyar/nginx-lua-prometheus,官方教程说的很明白,下面我带着大家做一下。