一开始nagios执行监控脚本的时候,总是不定期地偶尔地出现下面的错误
[: too many arguments
脚本一开始是这么写的:
guard_info=`ps aux | grep guard | grep -v grep | grep guard_replica_id | awk '{print $2}'`
echo guard_info:$guard_info
if [ "$guard_info" = "" ]; then
echo "guard is not found"
exit 0
fi
vsz=`echo $guard_info | awk '{print $3}'`
echo vsz:$vsz
cpu=`echo $guard_info | awk '{print $4}' | awk -F"." '{print $1}'`
echo cpu:$cpu
if [ $vsz -gt 606192 ]; then
echo "vsz as large as $vsz"
exit 2
elif [ $cpu -gt 5 ]; then
echo "cpu percentage is $p_cpu"
exit 2
else
exit 0
fi
最后发现是guard本身会fork子进程,在调用execve之前和fork()之后有那么一个小时间窗口,这个时候会存在两个相同名字相同参数的进程存在。
解决办法就是用elapsed time来对grep出来的guard进程进行排序,取时间最长的那个。最后脚本变成这样:
guard_info=`ps -C guard -o pid=,etime=,vsz=,%cpu= --sort -etime | head -1`
echo guard_info:$guard_info
if [ "$guard_info" = "" ]; then
echo "guard is not found"
exit 0
fi
vsz=`echo $guard_info | awk '{print $3}'`
echo vsz:$vsz
cpu=`echo $guard_info | awk '{print $4}' | awk -F"." '{print $1}'`
echo cpu:$cpu
if [ $vsz -gt 606192 ]; then
echo "vsz as large as $vsz"
exit 2
elif [ $cpu -gt 5 ]; then
echo "cpu percentage is $p_cpu"
exit 2
else
exit 0
fi