CPU Usage Checking Script

 

cpuscheck.sh

#!/bin/bash

VERSION="0.2"

# log dir
LOGDIR=.

function defaultopt()
{
    if [ x$TIME == x ]; then
        TIME=5
    fi
    if [ x$ALLCPU == x ]; then
        ALLCPU=3
    fi
    if [ x$CORE == x ]; then
        CORE=20
    fi
    if [ x$CONDITION == x ]; then
        CONDITION=OR
    fi
}

# loading conf
if [ -f ./option_cpuscheck.conf ]; then
    . ./option_cpuscheck.conf
    defaultopt
else
    echo "There is no file 'option_cpuscheck.conf'"
    echo "So, It apply default options"
    defaultopt
fi

# Options Function
function usage()
{
    echo -e "\e[34m\e[1m*** The options are :\e[21\033[0m"
    echo "-h or --help: help"
    echo "--allcpu    : Threadhold of utilization CPU(all)"
    echo "--core      : Threadhold of utilization per core."
    echo "--time      : Time for gather the sar data."
    echo "--condition : Conditions for determining utilization of ALLCPU and CORE."
    echo ""
    echo -e "\e[34m\e[1m*** Usage :\e[21\033[0m"
    echo "example 1 : $0"
    echo "            => option_cpuscheck.conf 파일의 옵션값을 적용하여 실행"
    echo ""
    echo "example 2 : $0 --allcpu=5 --core=30 --time=10 --condition=AND"
    echo "            => sar 1초 간격 '10초' 동안 평균 total cpu 사용률 5% 'AND' core 사용률 중 30%를 넘기는 core가 1개 이상 있으면 Abnormal 출력함"
    echo "            => 명령행에 별도의 옵션을 지정하고 실행하면 해당 옵션은 option_cpuscheck.conf 값보다 우선되어 적용."
    echo ""
    echo "example 3 : $0 --allcpu=4 --core=25"
    echo "            => sar 1초 간격 '옵션초' 동안 평균 total cpu 사용률 4% '옵션조건' core 사용률 중 25%를 넘기는 core가 1개 이상 있으면 Abnormal 출력함"
    echo "            => 명령행에 옵션을 넣지 않은 값은 자동으로 option_cpuscheck.conf 파일의 옵션값을 적용함."
    echo ""
    echo "example 4 : $0 --allcpu=99 --core=25 --condition=or"
    echo "            => sar 1초 간격 '옵션초' 동안 평균 total cpu 사용률 99% 'OR' core 사용률 중 25% 넘기는 core가 있으면 Abnormal 출력함"
    echo "            =>  --allcpu=99 와 --condition=OR 를 동시에 적용하면 core별 사용률만 따짐."
    echo -e "\033[0m"
}
 
# Options Input
while [ "$1" != "" ]; do
    PARAM=`echo $1 | awk -F= '{print $1}'`
    VALUE=`echo $1 | awk -F= '{print $2}'`
    case $PARAM in
        -h | --help)
            usage
            exit
            ;;
        -v | --version)
            echo "$0 Version: $VERSION"
            exit
            ;;
        --allcpu)
            ALLCPU=$VALUE
            if [ x$ALLCPU == x ] || [ $ALLCPU -ge 100 ]; then
                ALLCPU=3
            fi
            ;;
        --core)
            CORE=$VALUE
            if [ x$CORE == x ] || [ $CORE -ge 100 ]; then
                CORE=20
            fi
            ;;
        --time)
            TIME=$VALUE
            if [ x$TIME == x ]; then
                TIME=5
            fi
            ;;
        --condition)
            CONDITION=$VALUE
            if [ x$CONDITION == x ]; then
                CONDITION=OR
            fi
            CONDITION=$(echo "$CONDITION" | tr '[:upper:]' '[:lower:]')
            if ! [ "$CONDITION" = "and" ] || [ "$CONDITION" = "or" ]; then
                CONDITION=OR
            fi
            ;;
        *)
            echo -e "\033[1;31m\e[5mError\e[25m: unknown option parameter \"$PARAM\"\033[0m"
            echo ""
            usage
            exit 1
            ;;
    esac
    shift
done

# Run SAR
RUN="sar -P ALL 1 $TIME | grep Average"

# logging
LOGTIME=`date +%Y-%m-%d"_"%T"_"%Z" UnixTime="%s`
LOGFILE=$(echo $LOGDIR/`basename $0 | awk -F '.' '{print "log_"$1}'`_$HOSTNAME.log)
TMPFILE=$(echo $LOGFILE'_tmp')
echo $LOGTIME >> $LOGFILE
echo "Command: '$RUN', Threshold(All):'$ALLCPU', Threshold(Core):'$CORE', Condi='$CONDITION'" >> $LOGFILE
printf -vch "%94s" "" >> $LOGFILE
printf "%s\n" "${ch// /-}" >> $LOGFILE
echo "Collecting... CPU Usage for $TIME seconds."
eval `echo $RUN` >> $LOGFILE
printf -vch "%94s" "" >> $LOGFILE
printf "%s\n" "${ch// /-}" >> $LOGFILE
echo "Status : Normal" >> $LOGFILE
printf -vch "%94s" "" >> $LOGFILE
printf "%s\n" "${ch// /=}" >> $LOGFILE

UTIME=$(echo $LOGTIME | awk -F "UnixTime=" '{print $2}')
awk "/$UTIME/,/^=/" $LOGFILE >> $TMPFILE
sed -i '/'$UTIME'$/,$d' $LOGFILE

CPUCOUNT=$(grep $UTIME -A 100 $TMPFILE | grep ^Average: | grep -v -e CPU -e all | wc -l)
ALLCPUUSE=$(grep $UTIME -A 100 $TMPFILE | grep ^Average: | grep all | awk '{ total = $3 + $5 } END { print total }')
ALLCPUUSE2=$(printf "%5.2f\n" $ALLCPUUSE)
YESNO=$(echo "$ALLCPUUSE $ALLCPU YES NO*" | awk '{ if ( $1 <= $2 ) print $3; if ( $1 >= $2 ) print $4 }')

sed -i '/CPU/ s/$/  | %us+sy  OK?/' $TMPFILE
sed -i "/all/ s/$/  |  $ALLCPUUSE2  $YESNO/" $TMPFILE

FLAG1=0
if [ "$YESNO" == "NO*" ]; then
    FLAG1=1
fi

COUNT=0
FLAG2=0
while [ $CPUCOUNT -gt $COUNT ]
do
    COREUSE=$(grep $UTIME -A 100 $TMPFILE | grep ^Average: | awk -v count="$COUNT" '{ if ( $2 == count ) print $0 }' | awk '{total = $3 + $5}END{print total}')
    COREUSE2=$(printf "%5.2f\n" $COREUSE)
    YESNO2=$(echo "$COREUSE $CORE YES NO*" | awk '{ if ( $1 <= $2 ) print $3; if ( $1 >= $2 ) print $4 }')
    if [ $COUNT -gt 9 ]; then
        sed -i "/Average:         $COUNT/ s/$/  |  $COREUSE2  $YESNO2/" $TMPFILE
    else
        sed -i "/Average:          $COUNT/ s/$/  |  $COREUSE2  $YESNO2/" $TMPFILE
    fi
    if [ $YESNO2 == "NO*" ]; then
        let "FLAG2 += 1"
    fi
    let "COUNT += 1"
done

function abnormalmessage()
{
    echo -e "\033[1;31m- 먼저 아래 명령을 쉘에 복사/붙여넣기 하여 로그의 (NO*) 부분을 확인하십시오.\e[21\033[0m"
    echo -e "\033[1;31mgrep $UTIME -A 100 $LOGFILE\e[21\033[0m"
    echo -e "\033[1;31m- 그리고 $LOGDIR/readme.txt 파일의 'Abnormal 가이드'를 참고 하세요.\e[21\033[0m"
    echo ""
    echo -e "\033[1;31mStatus: Abnormal\e[21\033[0m"
    sed -i 's/Normal/Abnormal/g' $TMPFILE
}

function status1()
{
if [ $FLAG1 -eq 1 ] || [ $FLAG2 -ge 1 ]; then
    abnormalmessage
else
    echo -e "\033[1;32mStatus: Normal\e[21\033[0m"
fi
}

function status2()
{
if [ $FLAG1 -eq 1 ] && [ $FLAG2 -ge 1 ]; then
    abnormalmessage
else
    echo -e "\033[1;32mStatus: Normal\e[21\033[0m"
fi
}

case $CONDITION in
    "OR"|"or") status1 ;;
    "AND"|"and") status2 ;;
    *) echo "Check option of condition. Current CONDITION Value is: $CONDITION" , So apply OR.; CONDITION=OR ; status1 ;;
esac

cat "$TMPFILE" >> $LOGFILE
rm -f $TMPFILE

exit 0

 

option_cpuscheck.conf

## Value of Options for cpuscheck.sh

# *Unit: Second. *Mean: Time for gather the sar data. *Available value: 1 ~ 180. *Recommended value: 5 ~ 10
TIME=5

# *Unit: Percentage. *Mean: Threadhold of utilization CPU(all). *Available value: 1 ~ 99. *Recommended value: 3 ~ 5
ALLCPU=4

# *Unit: Percentage. *Mean: Threadhold of utilization per core. *Available value: 1 ~ 99. *Recommended value: 20 ~ 30
CORE=25

# *Unit: Words. *Mean: Conditions for determining utilization of ALLCPU and CORE. *Available value : AND or OR, *Recommended value: OR
CONDITION=OR

 

CPU Usage Checking Script

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다