入职以后,领导让我着手zabbix监控的部署和具体的维护。监控php首先就是一个挺重要的事情了。zabbix监控php可以通过php自带的一个status功能来监控,需要在配置文件里面开启,同时在nginx里面配置php status可以访问
1.php-fpm 开启status
|
1
2
|
grep pm.status_path /usr/local/php/etc/php-fpm.conf
pm.status_path = /php-fpm-status
|
默认情况下为/status,当然也可以改成/phpfpm_status等,我这里是改成/php-fpm-status
2.nginx配置
|
1
2
3
4
5
6
7
8
9
|
server { listen 80;
server_name 127.0.0.1;
location /phpfpm_status {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
} |
3.php status详解
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
pool – fpm池子名称,大多数为wwwprocess manager – 进程管理方式,值:static, dynamic or ondemand. dynamicstart 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,缓慢请求的数量 |
4.添加zabbix_agent 配置
|
1
2
3
4
5
6
|
[[email protected] zabbix_agentd.conf.d]# cat check_php_status.conf
UserParameter=php-fpm.status[*],/etc/zabbix/alertscripts/php_status.sh $1
UserParameter=php_status,ps -ef | grep php-fpm | awk '{ print $9}' | grep master | wc -l
UserParameter=process.php.memory,/home/hckjS213/zabbix/etc/script/processstatus.sh phpmem
UserParameter=process.php.cpu,/home/hckjS213/zabbix/etc/script/processstatus.sh phpcpu
UserParameter=process.php.num,/home/hckjS213/zabbix/etc/script/processstatus.sh phpnum
|
5.编写php监控脚本
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#!/bin/bashCURL=`which curl`
function idle {
$CURL "http://127.0.0.1/php-fpm-status" 2>/dev/null| grep -w '^idle processes:' | awk '{print $3}'
}function total {
$CURL "http://127.0.0.1/php-fpm-status" 2>/dev/null| grep -w '^total processes:' | awk '{print $3}'
}function active {
$CURL "http://127.0.0.1/php-fpm-status" 2>/dev/null| grep -w '^active processes:' | awk '{print $3}'
}function listen_queue {
$CURL "http://127.0.0.1/php-fpm-status" 2>/dev/null| grep -w "^listen queue:" | awk '{print $3}'
}function slow_requests {
output=` $CURL "http://127.0.0.1/php-fpm-status" 2>/dev/null| grep -w "^slow requests:"| awk '{print $3}'`
if [ "$output" == "" ];then
echo 0
elseecho $output
fi }
$1 |
php进程资源使用率脚本
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#!/bin/bash#chck for php and sphinx used cpu and memoryphpmem(){ ps aux|grep "php"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}phpcpu(){ ps aux|grep "php"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}phpnum(){ ps aux|grep "php"|grep -v "grep"|grep -v "processstatus.sh"| wc -l
}case "$1" in
phpmem)phpmem;;phpcpu)phpcpu;;phpnum)phpnum;;*)echo "Usage: $0 {phpmem|phpcpu|phpnum}"
esac |
6.测试
|
1
2
3
4
|
[[email protected]_server ~]#/usr/local/zabbix/bin/zabbix_get -s 192.168.50.119 -k php-fpm.status[active]
1
[[email protected]_server ~]# zabbix_get -s 10.144.164.53 -p 10050 -k process.php.memory
2070688 |
本文转自 shouhou2581314 51CTO博客,原文链接:http://blog.51cto.com/thedream/1882958,如需转载请自行联系原作者