Press "Enter" to skip to content

Zabbix监控php-fpm

主机环境:
[root@test ~]# uname -a
Linux test 3.10.0-693.el7.x86_64 #1 SMP Thu Jul 6 19:56:57 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@test ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.4 (Maipo)
[root@test ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.139.132 netmask 255.255.255.0 broadcast 192.168.139.255
ether 00:0c:29:ca:b7:70 txqueuelen 1000 (Ethernet)
RX packets 356 bytes 41989 (41.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 224 bytes 41057 (40.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Zabbix版本:3.4.9
php-fpm版本:
[root@test ~]# php-fpm -v
PHP 5.4.16 (fpm-fcgi) (built: Apr 12 2018 19:03:25)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
主机防火墙和selinux关闭
Zabbix yum安装参考:https://www.tracymc.cn/archives/614,编译安装可参考https://www.tracymc.cn/archives/218.

zabbix监控php-fpm主要是通过nginx配置php-fpm的状态输出页面,在正则取值.
具体步骤:
1.修改php-fpm配置
在php-fpm的配置文件中(/etc/php-fpm.d/www.conf)找到“pm.status_path = /status”并将前面的分号去掉.
yum安装的话默认路径为/etc,找不到的话可以find或者locate查找.注意这里不是修改的/etc/php-fpm.conf这个配置文件,而是/etc/php-fpm.d/www.conf这个配置文件,直接在/etc/php-fpm.conf添加"pm.status_path = /status"的话会报如下错误,这个要注意!!!!!
[root@test ~]# systemctl restart php-fpm //重启php-fpm

2.修改nginx配置
在nginx配置的server段添加如下配置:
location /status {
allow 127.0.0.1#你允许的ip
deny all;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
截图如下:
[root@test conf.d]# nginx -t //验证配置文件是否正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@test conf.d]# systemctl restart nginx //重启nginx
主机浏览器打开http://127.0.0.1:8080/status验证nginx配置修改是否生效,如下:
具体参数含义如下:
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,缓慢请求的数量

3.添加监控脚本
[root@test conf.d]# cat /etc/zabbix/scripts/php-fpm_status.sh //脚本的端口根据实际情况填写,我这里的nginx是8080端口,如上截图
listenqueue(){
wget --quiet -O - http://127.0.0.1:8080/status?auto |grep "listen queue:"|grep -vE "len|max"|awk '{print$3}'
}

listenqueuelen(){
wget --quiet -O - http://127.0.0.1:8080/status?auto |grep "listen queue len" |awk '{print$4}'
}

idle(){
wget --quiet -O - http://127.0.0.1:8080/status?auto |grep "idle processes" |awk '{print$3}'
}
active(){
wget --quiet -O - http://127.0.0.1:8080/status?auto |grep "active" |awk '{print$3}'|grep -v "process"
}
total(){
wget --quiet -O - http://127.0.0.1:8080/status?auto |grep "total processes" |awk '{print$3}'
}

mactive(){

wget --quiet -O - http://127.0.0.1:8080/status?auto |grep "max active processes:" |awk '{print$4}'
}

since(){
wget --quiet -O - http://127.0.0.1:8080/status?auto |grep "start since: " |awk '{print$3}'
}

conn(){
wget --quiet -O - http://127.0.0.1:8080/status?auto |grep "accepted conn" |awk '{print$3}'
}

reached(){
wget --quiet -O - http://127.0.0.1:8080/status?auto |grep "max children reached" |awk '{print$4}'
}
requests(){
wget --quiet -O - http://127.0.0.1:8080/status?auto |grep "slow requests" |awk '{print$3}'
}
$1

[root@test zabbix]# chmod u+x /etc/zabbix/scripts/php-fpm_status.sh //赋予执行权限
[root@test ~]# cat /etc/zabbix/zabbix_agentd.conf |grep conf|grep -v ‘#’ //yum安装zabbix的话agent默认配置文件为/etc/zabbix/zabbix_agentd.conf
Include=/etc/zabbix/zabbix_agentd.d/*.conf //你懂的,包含/etc/zabbix/zabbix_agentd.d下面的配置
设置键值:
[root@test zabbix]# cat /etc/zabbix/zabbix_agentd.d/userparameter_php_fpm.conf
UserParameter=idle.processe,/etc/zabbix/scripts/php-fpm_status.sh idle
UserParameter=total.processes,/etc/zabbix/scripts/php-fpm_status.sh total
UserParameter=active.processes,/etc/zabbix/scripts/php-fpm_status.sh active
UserParameter=max.active.processes,/etc/zabbix/scripts/php-fpm_status.sh mactive
UserParameter=listen.queue.len,/etc/zabbix/scripts/php-fpm_status.sh listenqueuelen
UserParameter=listen.queue,/etc/zabbix/scripts/php-fpm_status.sh listenqueue
UserParameter=start.since,/etc/zabbix/scripts/php-fpm_status.sh since
UserParameter=accepted.conn,/etc/zabbix/scripts/php-fpm_status.sh conn
UserParameter=max.children.reached,/etc/zabbix/scripts/php-fpm_status.sh reached
UserParameter=slow.requests,/etc/zabbix/scripts/php-fpm_status.sh requests
[root@test zabbix]# systemctl restart zabbix-agent //重启agent

4.导入模板
模板下载地址:php-fpm_status
先将模板存到本地,再导入到zabbix-server中.
导入下载模板,如下:
导入成功后可以在配置-模板查看已导入的模板,如下:

5.关联模板
添加模板:
配置-主机点击相关主机进去配置模板,如下:

6.zabbix web验证是否有数据
关联模板后,稍等一会,查看web页面是否有数据.
部分截图如下:

注:模板没有设置触发器,可自行设定.

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注