• 欢迎来到小爱博客,一个分享互联网IT技术的网站,喜欢就收藏吧!

grafana+prometheus+php-fpm-exporter监控php-fpm状态

grafana 小爱 2个月前 (08-14) 48次浏览 已收录 0个评论 扫描二维码

1. 启用php-fpm状态功能

php-fpmnginx一样内建了一个状态页,对于想了解php-fpm的状态以及监控php-fpm非常有帮助。为了后续的Prometheus监控,我们需要先了解php-fpm状态页是怎么回事。

<code class="prism language-bash has-numbering"><span class="token punctuation">[</span>global<span class="token punctuation">]</span>
pid <span class="token operator">=</span> /usr/local/php/var/run/php-fpm.pid
error_log <span class="token operator">=</span> /usr/local/php/var/log/php-fpm.log
log_level <span class="token operator">=</span> debug

<span class="token punctuation">[</span>www<span class="token punctuation">]</span>
listen <span class="token operator">=</span> /tmp/php-cgi.sock
listen.backlog <span class="token operator">=</span> 1024
listen.allowed_clients <span class="token operator">=</span> 127.0.0.1
listen.owner <span class="token operator">=</span> www
listen.group <span class="token operator">=</span> www
listen.mode <span class="token operator">=</span> 0666
user <span class="token operator">=</span> www
group <span class="token operator">=</span> www
pm <span class="token operator">=</span> dynamic
pm.max_children <span class="token operator">=</span> 300
pm.start_servers <span class="token operator">=</span> 20
pm.min_spare_servers <span class="token operator">=</span> 5
pm.max_spare_servers <span class="token operator">=</span> 35
pm.max_requests <span class="token operator">=</span> 10240
pm.status_path <span class="token operator">=</span> /phpfpm_status
ping.path <span class="token operator">=</span> /ping
request_terminate_timeout <span class="token operator">=</span> 1200
pm.process_idle_timeout <span class="token operator">=</span> 0
request_slowlog_timeout <span class="token operator">=</span> 5
slowlog <span class="token operator">=</span> var/log/slow.log
rlimit_files <span class="token operator">=</span> 65535

</code>

默认情况下为pm.status_path = /status,当然也可以改成其他的等等。

如上,配置后,重启即开启了php-fpm状态功能

2. nginx配置

在默认主机里面加上location或者你希望能访问到的主机里面。

<code class="prism language-bash has-numbering">server <span class="token punctuation">{</span>
        listen 80<span class="token punctuation">;</span>
        server_name 127.0.0.1<span class="token punctuation">;</span>
        location /nginx_status <span class="token punctuation">{</span>
                stub_status on<span class="token punctuation">;</span>
                access_log off<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        location ~ ^/<span class="token punctuation">(</span>phpfpm_status<span class="token operator">|</span>ping<span class="token punctuation">)</span>$ <span class="token punctuation">{</span>
				fastcgi_pass unix:/tmp/php-cgi.sock<span class="token punctuation">;</span>
               <span class="token comment"># fastcgi_pass  127.0.0.1:9000;</span>
	        	 allow 127.0.0.1<span class="token punctuation">;</span>
		         deny all<span class="token punctuation">;</span>
                include fastcgi_params<span class="token punctuation">;</span>
                fastcgi_param SCRIPT_FILENAME <span class="token variable">$fastcgi_script_name</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

	error_log  /var/log/wwwlogs/127error.log <span class="token punctuation">;</span>
	access_log  /var/log/wwwlogs/127access.log main<span class="token punctuation">;</span>

<span class="token punctuation">}</span>
</code>

3. 重启nginx/php-fpm

<code class="prism language-bash has-numbering"><span class="token function">service</span> php-fpm restart
<span class="token function">service</span> nginx restart
</code>

4.测试

<code class="prism language-bash has-numbering"><span class="token punctuation">[</span>root@iz conf.d<span class="token punctuation">]</span><span class="token comment"># curl http://127.0.0.1/phpfpm_status</span>
pool:                 www
process manager:      dynamic
start time:           21/Nov/2019:10:42:33 +0800
start since:          4498
accepted conn:        4896
listen queue:         0
max listen queue:     0
listen queue len:     0
idle processes:       19
active processes:     1
total processes:      20
max active processes: 14
max children reached: 0
slow requests:        153
<span class="token punctuation">[</span>root@iz conf.d<span class="token punctuation">]</span><span class="token comment"># curl http://127.0.0.1/ping</span>
pong
<span class="token punctuation">[</span>root@iz conf.d<span class="token punctuation">]</span><span class="token comment"># </span>
<span class="token punctuation">[</span>root@iz conf.d<span class="token punctuation">]</span><span class="token comment"># curl http://127.0.0.1/nginx_status</span>
Active connections: 5 
server accepts handled requests
 3481898 3481898 3526133 
Reading: 0 Writing: 1 Waiting: 4 
</code>

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

6. 被监控服务器安装PHP-FPM-exporter

<code class="has-numbering">	6.1 下载:`php-fpm-exporter`
地址:*https://github.com/bakins/php-fpm-exporter/releases*

	6.2 安装php-fpm-exporter
</code>
<code class="prism language-bash has-numbering"> <span class="token function">mkdir</span> -p /usr/local/php-fpm-exporter
 <span class="token function">mv</span> php-fpm-exporter.linux.amd64 /usr/local/php-fpm-exporter/php-fpm-exporter
 <span class="token function">chmod</span> +x /usr/local/php-fpm-exporter/php-fpm-exporter
</code>
<code class="has-numbering"> 	6.3 启动php-fpm-exporter
</code>
<code class="prism language-bash has-numbering"><span class="token function">nohup</span> ./php-fpm-exporter --addr 172.19.14.250:9190 --endpoint http://127.0.0.1/phpfpm_status  <span class="token operator">></span> /usr/local/php-fpm-exporter/php-fpm-exporter.log 2<span class="token operator">></span><span class="token operator">&</span>1 <span class="token operator">&</span>
</code>

7.Prometheus端

<code class="has-numbering">7.1 配置Prometheus
</code>
<code class="prism language-bash has-numbering"><span class="token comment"># vim /usr/local/prometheus/prometheus.yml</span>
scrape_configs:
  - job_name: <span class="token string">'PHP-FPM'</span>
    static_configs:
      - targets:
        - 172.19.14.250:9190
</code>
<code class="has-numbering">7.2 重启
</code>
<code class="prism language-bash has-numbering"> systemctl restart prometheus
</code>

8.Grafana端

dashboards.id 推荐3901

模板地址:https://grafana.com/dashboards/3901


小爱博客 , 版权所有
转载请注明原文链接:grafana+prometheus+php-fpm-exporter监控php-fpm状态
喜欢 (0)
【你的支持, 我的动力】
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址