Zabbix应用监控系列之PHP-FPM状态监控

ZABBIX
时间:2016.09.06
笔者Q:552408925、572891887
架构师群:471443208
bjstack运维社区:524721466
有任何疑问请加群互动,或上社区发帖bjstack运维社区

1.配置所有Agent(标准化目录结构)

  1. [root@linux-node1 ~]# vim /etc/zabbix/zabbix_agentd.conf #编辑配置文件引用key
  2. Include=/etc/zabbix/zabbix_agentd.d/*.conf
  3. [root@linux-node1 ~]# mkdir /etc/zabbix/scripts #存放Shell脚本

2.PHP-FPM工作模式通常与Nginx结合使用,修改php-fpm.conf(找到自己的php-fpm.conf存放路径)

  1. [root@linux-node1 ~]# vim /etc/php-fpm.d/www.conf #我php-fpm存放路径
  2. pm.status_path = /phpfpm_status

3.修改nginx.conf的配置文件,通过Nginx访问PHP-FPM状态。

  1. location ~ ^/(phpfpm_status)$ {
  2. include fastcgi_params;
  3. fastcgi_pass 127.0.0.1:9000;
  4. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  5. }

4.访问测试phpfpm_status

  1. [root@linux-node4 conf.d]# curl http://127.0.0.1/phpfpm_status
  2. pool: www
  3. process manager: dynamic
  4. start time: 05/Jul/2016:15:30:56 +0800
  5. start since: 409
  6. accepted conn: 22
  7. listen queue: 0
  8. max listen queue: 0
  9. listen queue len: 128
  10. idle processes: 4
  11. active processes: 1
  12. total processes: 5
  13. max active processes: 2
  14. max children reached: 0
  15. #PHP-FPM状态解释:
  16. pool #fpm池名称,大多数为www
  17. process manager #进程管理方式dynamic或者static
  18. start time #启动日志,如果reload了fpm,时间会更新
  19. start since #运行时间
  20. accepted conn #当前池接受的请求数
  21. listen queue #请求等待队列,如果这个值不为0,那么需要增加FPM的进程数量
  22. max listen queue #请求等待队列最高的数量
  23. listen queue len #socket等待队列长度
  24. idle processes #空闲进程数量
  25. active processes #活跃进程数量
  26. total processes #总进程数量
  27. max active processes #最大的活跃进程数量(FPM启动开始计算)
  28. max children reached #程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量过小,可以适当调整。

4.编写php-fpm的Shell脚本(如果端口不一致,只需要修改脚本端口即可)

  1. [root@linux-node1 ~]# cd /etc/zabbix/scripts
  2. [root@linux-node1 scripts]# vim phpfpm_status.sh
  3. #!/bin/bash
  4. ############################################################
  5. # $Name: phpfpm_status.sh
  6. # $Version: v1.0
  7. # $Function: Nginx Status
  8. # $Author: xuliangwei
  9. # $organization: www.xuliangwei.com
  10. # $Create Date: 2016-06-23
  11. # $Description: Monitor Nginx Service Status
  12. ############################################################
  13. PHPFPM_COMMAND=$1
  14. PHPFPM_PORT=80 #根据监听不同端口进行调整
  15. start_since(){
  16. /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^start since:/ {print $NF}'
  17. }
  18. accepted_conn(){
  19. /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^accepted conn:/ {print $NF}'
  20. }
  21. listen_queue(){
  22. /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^listen queue:/ {print $NF}'
  23. }
  24. max_listen_queue(){
  25. /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^max listen queue:/ {print $NF}'
  26. }
  27. listen_queue_len(){
  28. /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^listen queue len:/ {print $NF}'
  29. }
  30. idle_processes(){
  31. /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^idle processes:/ {print $NF}'
  32. }
  33. active_processes(){
  34. /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^active processes:/ {print $NF}'
  35. }
  36. total_processes(){
  37. /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^total processes:/ {print $NF}'
  38. }
  39. max_active_processes(){
  40. /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^max active processes:/ {print $NF}'
  41. }
  42. max_children_reached(){
  43. /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^max children reached:/ {print $NF}'
  44. }
  45. slow_requests(){
  46. /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^slow requests:/ {print $NF}'
  47. }
  48. case $PHPFPM_COMMAND in
  49. start_since)
  50. start_since;
  51. ;;
  52. accepted_conn)
  53. accepted_conn;
  54. ;;
  55. listen_queue)
  56. listen_queue;
  57. ;;
  58. max_listen_queue)
  59. max_listen_queue;
  60. ;;
  61. listen_queue_len)
  62. listen_queue_len;
  63. ;;
  64. idle_processes)
  65. idle_processes;
  66. ;;
  67. active_processes)
  68. active_processes;
  69. ;;
  70. total_processes)
  71. total_processes;
  72. ;;
  73. max_active_processes)
  74. max_active_processes;
  75. ;;
  76. max_children_reached)
  77. max_children_reached;
  78. ;;
  79. slow_requests)
  80. slow_requests;
  81. ;;
  82. *)
  83. echo $"USAGE:$0 {start_since|accepted_conn|listen_queue|max_listen_queue|listen_queue_len|idle_processes|active_processes|total_processes|max_active_processes|max_children_reached}"
  84. esac

给脚本添加执行权限

  1. [root@linux-node1 scripts]# chmod +x phpfpm_status.sh

5.key的phpfpm_status.conf的子配置文件如下:

  1. [root@linux-node1 ~]# cat /etc/zabbix/zabbix_agentd.d/phpfpm_status.conf
  2. UserParameter=phpfpm_status[*],/bin/bash /etc/zabbix/scripts/phpfpm_status.sh "$1"

6.重启zabbix-agent

  1. [root@linux-node1 ~]# systemctl restart zabbix-agent

7.测试一定使用Zabbix_get来获取值

  1. [root@linux-node1 zabbix_agentd.d]# zabbix_get -s 192.168.90.11 -k phpfpm_status[accepted_conn]
  2. 45

8.展示所有Key(记得将模板关联主机)如图4-5
4-5

9.查看图形,如图4-4(图形自定义)
4-6