在配置nagios时,发现想监控的一些程序无法直接获得CPU及内存的使用率:
如:我想监控Mysql的CPU及内存的使用率
check_proc -e mysql:error -w 20:30 -c 30:60
就会通过查找mysql和error的进程。以上的命令就是:
ps aux | grep mysql | grep error
# This Script is used to check process.
# And to Check CPU Load and Memory used.
#set -xv
USAGE() {
cat<<eof
USAGE: check_app_proc.sh -e REG1:REG2:REG3 -w cpuwarning:memorywarning -c cpucritical:memorycritial -CM
This scripts only test in bash.
PURPOSE: Some time we want to monitor some application runing in our system, but when this application runin
g,we could not find this process by application. so this scripts will use -e option to find it.
OPTIONS:
-e : the application identifie,separte by ":"
-w : warning cpuwarning:memorywarning
-c : -c cpucritical:memorycritial
-C : Show CPU information
-M : Show Memory infomation
-H : Show help menu
eof
}
OUTPUT(){
declare -a out_msg=( "unrecognized option" "invalid arguments" "Warning values must littler than Critial value" "Critial values must be digidt" "Warning values must be digidt" "Some values not get use ps" "then number of WARNING must EQ CRITIAL" "The PS execute error" )
echo "$0 : ${out_msg[$1]}"
USAGE
exit ${SER_STS[$2]}
}
MON_STAT=
EXT_STAT=
TMP_REG=
declare -a SER_STS=( 0 1 2 3 )
if [ x$1 == x ]; then
OUTPUT "1" ${SER_STS[3]}
fi
if [[ $1 =~ ^[a-zA-Z]+ ]] ;then
OUTPUT "1" ${SER_STS[3]}
fi
while getopts "e:w:c:CMH" arg
do
case $arg in
e)
if [[ $OPTARG =~ ^: ]]
then
OUTPUT "2" ${SER_STS[3]}
fi
declare -a REG_OPS=(${OPTARG//\:/ })
;;
w)
if [[ $OPTARG =~ ^: ]]
then
OUTPUT "2" ${SER_STS[3]}
elif [[ ${OPTARG//\:/} =~ ^[0-9]+$ ]]
then
declare -a WAN_OPS=(${OPTARG//\:/ })
else
OUTPUT "5" ${SER_STS[3]}
fi
;;
c)
if [[ $OPTARG =~ ^: ]]
then
OUTPUT "2" ${SER_STS[3]}
elif [[ ${OPTARG//\:/} =~ ^[0-9]+$ ]]
then
declare -a CRI_OPS=(${OPTARG//\:/ })
else
OUTPUT "4" ${SER_STS[3]}
fi
;;
C)
: ${CPU_OPS:=1}
;;
M)
: ${MEM_OPS:=1}
;;
H)
USAGE
;;
?)
OUTPUT "1" ${SER_STS[3]}
;;
esac
done
if test ${#WAN_OPS[@]} -eq ${#CRI_OPS[@]};then
while [ ${tm:=0} -lt ${#WAN_OPS[@]} ]
do
test ${WAN_OPS[$tm]} -gt ${CRI_OPS[$tm]} && OUTPUT "3" ${SER_STS[3]}
let tm=$tm+1
done
else
OUTPUT "7" ${SER_STS[3]}
fi
for tn in ${REG_OPS[@]}
do
TMP_REG=''${TMP_REG}'|grep '${tn}''
done
TMP_PS="ps aux ${TMP_REG} | grep -v $0"
: ${GLASS_PS:=`eval $TMP_PS`}
if test -z "$GLASS_PS" ; then
OUTPUT "8" ${SER_STS[3]}
else
declare -a GLASS_ARR=(`echo ${GLASS_PS}|awk '{OFS=" ";print $2,$3,$4;}'`)
for ct in ${GLASS_ARR[@]}
do
if test -z ${ct};then
OUTPUT "6"
exit ${SER_STS[3]}
fi
done
DATA_PRT="Process ${REG_OPS[0]} CPU Usage : ${GLASS_ARR[1]} MEMORY USAGE: ${GLASS_ARR[2]} | use_cpu=${GLASS_ARR[1]};mem_usage=${GLASS_ARR[2]}; cpu_critical=${CRI_OPS[0]};mem_critical=${CRI_OPTS[1]};cpu_warning=${WAN_OPS[0]};mem_warning=${WAN_OPS[1]}"
if test $(echo "${GLASS_ARR[1]} < ${CRI_OPS[0]}" | bc -l) -eq 0 -o $(echo "${GLASS_ARR[2]} < ${CRI_OPS[1]}" | bc -l) -eq 0 ; then
echo -n CRITICAL - $DATA_PRT
exit ${SER_STS[2]}
elif test $(echo "${GLASS_ARR[1]} < ${WAN_OPS[0]}" | bc -l) -eq 0 -o $(echo "${GLASS_ARR[2]} < ${WAN_OPS[1]}" | bc -l) -eq 0 ; then
echo -n WARNING - $DATA_PRT
exit ${SER_STS[1]}
else
echo -n OK - $DATA_PRT
exit ${SER_STS[0]}
fi
fi
英文不行。