入职以后,领导让我着手zabbix监控的部署和具体的维护。监控php首先就是一个挺重要的事情了。zabbix监控php可以通过php自带的一个status功能来监控,需要在配置文件里面开启,同时在nginx里面配置php status可以访问

1.php-fpm 开启status

greppm.status_path/usr/local/php/etc/php-fpm.confpm.status_path=/php-fpm-status

默认情况下为/status,当然也可以改成/phpfpm_status等,我这里是改成/php-fpm-status

2.nginx配置

server{listen80;server_name127.0.0.1;location/phpfpm_status{fastcgi_pass127.0.0.1:9000;includefastcgi_params;fastcgi_paramSCRIPT_FILENAME$fastcgi_script_name;}}

3.php status详解

pool–fpm池子名称,大多数为wwwprocessmanager–进程管理方式,值:static,dynamicorondemand.dynamicstarttime–启动日期,如果reload了php-fpm,时间会更新startsince–运行时长acceptedconn–当前池子接受的请求数listenqueue–请求等待队列,如果这个值不为0,那么要增加FPM的进程数量maxlistenqueue–请求等待队列最高的数量listenqueuelen–socket等待队列长度idleprocesses–空闲进程数量activeprocesses–活跃进程数量totalprocesses–总进程数量maxactiveprocesses–最大的活跃进程数量(FPM启动开始算)maxchildrenreached-大道进程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量太小了,请改大一点。slowrequests–启用了php-fpmslow-log,缓慢请求的数量

4.添加zabbix_agent 配置

[root@localhostzabbix_agentd.conf.d]#catcheck_php_status.confUserParameter=php-fpm.status[*],/etc/zabbix/alertscripts/php_status.sh$1UserParameter=php_status,ps-ef|grepphp-fpm|awk'{print$9}'|grepmaster|wc-lUserParameter=process.php.memory,/home/hckjS213/zabbix/etc/script/processstatus.shphpmemUserParameter=process.php.cpu,/home/hckjS213/zabbix/etc/script/processstatus.shphpcpuUserParameter=process.php.num,/home/hckjS213/zabbix/etc/script/processstatus.shphpnum


5.编写php监控脚本

#!/bin/bashCURL=`whichcurl`functionidle{$CURL"http://127.0.0.1/php-fpm-status"2>/dev/null|grep-w'^idleprocesses:'|awk'{print$3}'}functiontotal{$CURL"http://127.0.0.1/php-fpm-status"2>/dev/null|grep-w'^totalprocesses:'|awk'{print$3}'}functionactive{$CURL"http://127.0.0.1/php-fpm-status"2>/dev/null|grep-w'^activeprocesses:'|awk'{print$3}'}functionlisten_queue{$CURL"http://127.0.0.1/php-fpm-status"2>/dev/null|grep-w"^listenqueue:"|awk'{print$3}'}functionslow_requests{output=`$CURL"http://127.0.0.1/php-fpm-status"2>/dev/null|grep-w"^slowrequests:"|awk'{print$3}'`if["$output"==""];thenecho0elseecho$outputfi}$1

php进程资源使用率脚本

#!/bin/bash#chckforphpandsphinxusedcpuandmemoryphpmem(){psaux|grep"php"|grep-v"grep"|grep-v"processstatus.sh"|awk'{sum+=$6};END{printsum}'}phpcpu(){psaux|grep"php"|grep-v"grep"|grep-v"processstatus.sh"|awk'{sum+=$3};END{printsum}'}phpnum(){psaux|grep"php"|grep-v"grep"|grep-v"processstatus.sh"|wc-l}case"$1"inphpmem)phpmem;;phpcpu)phpcpu;;phpnum)phpnum;;*)echo"Usage:$0{phpmem|phpcpu|phpnum}"esac

6.测试

[root@Zabbix_server~]#/usr/local/zabbix/bin/zabbix_get-s192.168.50.119-kphp-fpm.status[active]1[root@Zabbix_server~]#zabbix_get-s10.144.164.53-p10050-kprocess.php.memory2070688

附件:http://down.51cto.com/data/2368482