Nagios通过匹配监控进程CPU及内存使用

在配置nagios时,发现想监控的一些程序无法直接获得CPU及内存的使用率:


如:我想监控Mysql的CPU及内存的使用率

    check_proc -e mysql:error -w 20:30 -c 30:60

就会通过查找mysql和error的进程。以上的命令就是:

ps aux  | grep mysql | grep error


睡觉



#!/bin/bash
# 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



英文不行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值