Skip to content


给nagios增加监控当前php进程数的插件,并用pnp出图

脚本说明
脚本默认监控为php-fpm 以TCP方式运行在本机的端口php
php-fpm.conf例

  1. <value name="listen_address">127.0.0.1:9000/value>

其它地址需在脚本中修改相应地址127.0.0.1:9000

最终输出的$total processes为当前正在执行或等待的php数,此数为0或越少越好.

vi check_phpprocs.sh

  1. #!/bin/bash
  2. #########################################################################
  3. #
  4. # File:         check_phpprocs.sh
  5. # Description:  Nagios check plugins to check php process in *nix.
  6. # Language:     GNU Bourne-Again SHell
  7. # Version:      1.0.0
  8. # Date:         2015-1-16
  9. # Author:       C1g
  10. # Bog:          http://blog.C1gStudio.com
  11. #########################################################################
  12.  
  13. path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  14. export PATH
  15. STATE_OK=0
  16. STATE_WARNING=1
  17. STATE_CRITICAL=2
  18. STATE_UNKNOWN=3
  19.  
  20. warn_num=100
  21. critical_num=200
  22.  
  23. usage(){
  24.   echo -e "Usage: $0  -w|--warning warning threshold -c|--critical critical threshold"
  25. }
  26. select_arg(){
  27. if [ $# -eq 0 ];then
  28.   return 1
  29. fi
  30. until [ $# -eq 0 ];do
  31.   case $1 in
  32.     -w|--warning)
  33.       [ $# -lt 2 ] && return 1
  34.       if ! echo $2 |grep -E -q "^[1-9][0-9]*$";then
  35.         return 1
  36.       fi
  37.       warn_num=$2
  38.          shift 2
  39.       ;;
  40.     -c|--critical)
  41.       [ $# -lt 2 ] && return 1
  42.       if ! echo $2 |grep -E -q "^[1-9][0-9]*$";then
  43.         return 1
  44.       fi
  45.       critical_num=$2
  46.         shift 2
  47.       ;;
  48.     *)
  49.         return 1
  50.      ;;
  51.   esac
  52. done
  53. return 0
  54. }
  55.  
  56. select_arg $@
  57. [ $? -ne 0 ] && usage && exit $STATE_UNKNOWN
  58.  
  59. #echo "warn :$warn_num"
  60. #echo "critical :$critical_num"
  61.  
  62. if [ $critical_num -lt $warn_num ];then
  63.     usage
  64.     exit $STATE_UNKNOWN
  65. fi
  66.  
  67. total=`netstat -n | grep 127.0.0.1:9000 | wc -l`
  68. if [ $total -lt $warn_num ];then
  69.     echo "PHP OK - $total processes |PHP=$total;$warn_num;$critical_num;0"
  70.     exit $STATE_OK
  71. elif [ $total -ge $warn_num -a $total -lt $critical_num ];then
  72.     echo "PHP WARNING - $total processes |PHP=$total;$warn_num;$critical_num;0"
  73.     exit $STATE_WARNING
  74. elif [ $total -ge $critical_num ];then
  75.     echo "PHP CRITICAL - $total processes |PHP=$total;$warn_num;$critical_num;0"
  76.     exit $STATE_CRITICAL
  77. else
  78.     echo "UNKNOWN STATE"
  79.     exit $STATE_UNKNOWN
  80. fi

增加执行权限,宫户端nrpe路径为/opt/nagios/
chown nagios:nagios check_phpprocs.sh
chmod 755 check_phpprocs.sh
mv ./check_phpprocs.sh /opt/nagios/libexec/

编辑宫户端nrpe.cfg增加监控命令,这里设置waring阀值为100,critical阀值为200
vi /opt/nagios/etc/nrpe.cfg

  1. command[check_phpprocs]=/opt/nagios/libexec/check_phpprocs.sh -w 100 -c 200

重启宫户端nrpe

  1. kill `cat /var/run/nrpe.pid`
  2. /opt/nagios/bin/nrpe -c /opt/nagios/etc/nrpe.cfg -d

编辑监控端主机文件c1gstudio.cfg,添加监控服务
vi /usr/local/nagios/etc/objects/c1gstudio.cfg

define service{
use local-service,srv-pnp ; Name of service template to use
host_name c1gstudio
service_description PHP Processes
check_command check_nrpe!check_phpprocs
notifications_enabled 1
}
pnp4nagios已配好,srv-pnp为配好的模板 可以直接出图
templates.cfg

  1. define service {
  2.   name       srv-pnp
  3.   register   0
  4.   action_url /pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
  5.   process_perf_data               1

参见:http://blog.c1gstudio.com/archives/552

重启nagios
/etc/init.d/nagios reload

查看状态
nagios_checkphpprocs

下载check_phpprocs.zip check_phpprocs

参考:http://blog.csdn.net/xluren/article/details/17724043

Posted in Nagios.

Tagged with , , , .


No Responses (yet)

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.