入职以后,领导让我着手zabbix监控的部署和具体的维护。监控php首先就是一个挺重要的事情了。zabbix监控php可以通过php自带的一个status功能来监控,需要在配置文件里面开启,同时在nginx里面配置php status可以访问
1.php-fpm 开启status
grep pm.status_path /usr/local/php/etc/php-fpm.confpm.status_path = /php-fpm-status
默认情况下为/status,当然也可以改成/phpfpm_status等,我这里是改成/php-fpm-status
2.nginx配置
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详解
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 配置
[root@localhost zabbix_agentd.conf.d]# cat check_php_status.conf UserParameter=php-fpm.status[*],/etc/zabbix/alertscripts/php_status.sh $1UserParameter=php_status,ps -ef | grep php-fpm | awk '{ print $9}' | grep master | wc -lUserParameter=process.php.memory,/home/hckjS213/zabbix/etc/script/processstatus.sh phpmemUserParameter=process.php.cpu,/home/hckjS213/zabbix/etc/script/processstatus.sh phpcpuUserParameter=process.php.num,/home/hckjS213/zabbix/etc/script/processstatus.sh phpnum
5.编写php监控脚本
#!/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" == "" ];thenecho 0elseecho $outputfi }$1
php进程资源使用率脚本
#!/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" inphpmem)phpmem;;phpcpu)phpcpu;;phpnum)phpnum;;*)echo "Usage: $0 {phpmem|phpcpu|phpnum}"esac
6.测试
[root@Zabbix_server ~]#/usr/local/zabbix/bin/zabbix_get -s 192.168.50.119 -k php-fpm.status[active] 1[root@Zabbix_server ~]# zabbix_get -s 10.144.164.53 -p 10050 -k process.php.memory 2070688