脚本说明
脚本默认监控为php-fpm 以TCP方式运行在本机的端口php
php-fpm.conf例
其它地址需在脚本中修改相应地址127.0.0.1:9000
最终输出的$total processes为当前正在执行或等待的php数,此数为0或越少越好.
vi check_phpprocs.sh
#!/bin/bash
#########################################################################
#
# File: check_phpprocs.sh
# Description: Nagios check plugins to check php process in *nix.
# Language: GNU Bourne-Again SHell
# Version: 1.0.0
# Date: 2015-1-16
# Author: C1g
# Bog: http://blog.C1gStudio.com
#########################################################################
path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
warn_num=100
critical_num=200
usage(){
echo -e “Usage: $0 -w|–warning warning threshold -c|–critical critical threshold”
}
select_arg(){
if [ $# -eq 0 ];then
return 1
fi
until [ $# -eq 0 ];do
case $1 in
-w|–warning)
[ $# -lt 2 ] && return 1
if ! echo $2 |grep -E -q “^[1-9][0-9]*$”;then
return 1
fi
warn_num=$2
shift 2
;;
-c|–critical)
[ $# -lt 2 ] && return 1
if ! echo $2 |grep -E -q “^[1-9][0-9]*$”;then
return 1
fi
critical_num=$2
shift 2
;;
*)
return 1
;;
esac
done
return 0
}
select_arg $@
[ $? -ne 0 ] && usage && exit $STATE_UNKNOWN
#echo “warn :$warn_num”
#echo “critical :$critical_num”
if [ $critical_num -lt $warn_num ];then
usage
exit $STATE_UNKNOWN
fi
total=`netstat -n | grep 127.0.0.1:9000 | wc -l`
if [ $total -lt $warn_num ];then
echo “PHP OK – $total processes |PHP=$total;$warn_num;$critical_num;0”
exit $STATE_OK
elif [ $total -ge $warn_num -a $total -lt $critical_num ];then
echo “PHP WARNING – $total processes |PHP=$total;$warn_num;$critical_num;0”
exit $STATE_WARNING
elif [ $total -ge $critical_num ];then
echo “PHP CRITICAL – $total processes |PHP=$total;$warn_num;$critical_num;0”
exit $STATE_CRITICAL
else
echo “UNKNOWN STATE”
exit $STATE_UNKNOWN
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
command[check_phpprocs]=/opt/nagios/libexec/check_phpprocs.sh -w 100 -c 200
重启宫户端nrpe
kill `cat /var/run/nrpe.pid`
/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
define service {
name srv-pnp
register 0
action_url /pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
process_perf_data 1
参见:http://blog.c1gstudio.com/archives/552
重启nagios
/etc/init.d/nagios reload
下载check_phpprocs.zip check_phpprocs
参考:http://blog.csdn.net/xluren/article/details/17724043
No Responses (yet)
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.