#! /bin/bash
export LANG=en_US.UTF-8

FLAGE=

IN=0                         #网卡接收速率
INMIN=
INMAX=

OUT=0                    #网卡发送速率
OUTMIN=
OUTMAX=

SEND=0                 #网卡发送字节数
SENDMIN=
SENDMAX=

RECEIVE=0              #网卡接收字节数
RECEIVEMIN=
RECEIVEMAX=

TOTAL=0                 #网卡总字节数
TOTALMIN=
TOTALMAX=

IN_PER=0                 #网卡入口流量使用率
IN_PERMIN=
IN_PERMAX=

OUT_PER=0               #网卡出口流量使用率
OUT_PERMIN=
OUT_PERMAX=

CURNUM=             #当前的性能参数

WranResultStr=      #最后形成的告警字符串
ErrorResultStr=         #最后形成的错误字符串
DataResultStr=          #性能呢个参数字符串

quota=
QUOTASTR=
network_acquisition_json=""

function gotErr(){
    if [ "$1" -eq 0 ];then
        echo "mailstatedes=各项指标恢复正常 statedes=各项指标恢复正常|$DataResultStr Total=$CURNUM"
    elif [ "$1" -eq 1 ];then 
        echo "quota=${quota} mailstatedes=$WranResultStr statedes=$WranResultStr ${QUOTASTR} |$DataResultStr Total=$CURNUM"
    elif [ "$1" -eq 2 ];then 
        if [[ "${WranResultStr}" != "" ]]; then
            echo "quota=${quota} mailstatedes=${WranResultStr},${ErrorResultStr} statedes=${WranResultStr},${ErrorResultStr} ${QUOTASTR}|$DataResultStr Total=$CURNUM"
            else
            echo "quota=${quota} mailstatedes=${ErrorResultStr} statedes=${ErrorResultStr} ${QUOTASTR}|$DataResultStr Total=$CURNUM"
        fi

    elif [ "$1" -eq 3 ];then 
        echo "mailstatedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置 statedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置"
    fi

    exit $1
}

#依据传入的参数值来判断我们要监控的服务是什么
#然后通过各个函数获取到具体的值
#值之间的
function analysisStat(){
    #send_o=`ifconfig $FLAGE | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
    #recv_o=`ifconfig $FLAGE | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`
    laststat=`cat /proc/net/dev | grep -w ${FLAGE} |head -n 1 | awk -F ':' '{print $2}'`
    send_l=` echo ${laststat} | awk  '{print $9}'`
    recv_l=`echo ${laststat} | awk  '{print $1}' `
    sleep 1
    nextstat=`cat /proc/net/dev | grep -w ${FLAGE} |head -n 1 | awk -F ':' '{print $2}'`
    send_n=`echo ${nextstat} | awk  '{print $9}'`
    recv_n=`echo ${nextstat} | awk  '{print $1}'`

    send_r=`expr ${send_n} - ${send_l}`
    recv_r=`expr ${recv_n} - ${recv_l} `
    total_r=`expr ${send_r} + ${recv_r}`
    new_total=`echo $total_r | awk '{print($1/1024)}'`

    #send=` echo ${send_n} | awk '{print($1/1024/1024)}'`
    #send=`echo "scale=2;${send_n}/1024/1024" |bc`
    send=$(echo $send_n |awk '{ printf "%0.2f\n" ,$1/1024/1024}')
    #recevied=`echo ${recv_n} | awk '{print($1/1024/1024)}'`
    #recevied=`echo "scale=2;${recv_n}/1024/1024"|bc`
    recevied=$(echo $recv_n |awk '{ printf "%0.2f\n" ,$1/1024/1024}')
    #total=`expr ${send_n} + ${recv_n} `
    #total=`echo ${total} | awk '{print($1/1024/1024)}'`
    total=$(echo $send $ recevied |awk '{ printf "%0.2f\n" ,$1+$2}')
    #total=`echo "scale=2;${total}/1024/1024"|bc`
    #send_speed=`echo ${send_r} | awk '{print($1/1024)}'`
    send_speed=$(echo $send_r |awk '{ printf "%0.2f\n" ,$1/1024}')
    #recv_speed=`echo ${recv_r} | awk '{print($1/1024)}'`
    recv_speed=$(echo $recv_r |awk '{ printf "%0.2f\n" ,$1/1024}')
    #total_speed=`echo ${total_r} | awk '{print($1/1024)}'`
    total_speed=$(echo $total_r |awk '{ printf "%0.2f\n" ,$1/1024}')
    in_per=10
    out_per=10

    #if [ "$IN" -eq 1 ];then
        CURNUM=${recv_speed}
        network_acquisition_json="$network_acquisition_json,'入口流量':$CURNUM"
        #parseFileds "入口流量" $INMIN $INMAX
    #fi
    #if [ "$OUT" -eq 1 ];then
        CURNUM=${send_speed}
        network_acquisition_json="$network_acquisition_json,'出口流量':$CURNUM"
        #parseFileds "出口流量" $OUTMIN $OUTMAX
    #fi
    #if [ "$RECEIVE" -eq 1 ];then
        CURNUM=${recevied}
        network_acquisition_json="$network_acquisition_json,'网卡进口流量':$CURNUM"
        #parseFileds "网卡进口流量" $RECEIVEMIN $RECEIVEMAX
    #fi
    #if [ "$SEND" -eq 1 ];then
        CURNUM=${send}
        network_acquisition_json="$network_acquisition_json,'网卡出口流量':$CURNUM"
        #parseFileds "网卡出口流量" $SENDMIN $SENDMAX
    #fi
    #if [ "$TOTAL" -eq 1 ];then
        CURNUM=${total}
        network_acquisition_json="$network_acquisition_json,'总流量':$CURNUM"
        #parseFileds "总流量" $TOTALMIN $TOTALMAX
    #fi
    #if [ "$IN_PER" -eq 1 ];then
        CURNUM=${in_per}
        network_acquisition_json="$network_acquisition_json,'网卡入口流量使用率':$CURNUM"
        #parseFileds "网卡入口流量使用率" $IN_PERMIN $IN_PERMAX
    #fi
    #if [ "$OUT_PER" -eq 1 ];then
        CURNUM=${out_per}
        network_acquisition_json="$network_acquisition_json,'网卡出口流量使用率':$CURNUM"
        #parseFileds "网卡出口流量使用率" $OUT_PERMIN $OUT_PERMAX
    #fi
    CURNUM=${total_speed}
    network_acquisition_json="$network_acquisition_json,'总速度':$CURNUM"
    #echo "{${network_acquisition_json#*,}}"

    CURNUM=`awk 'BEGIN{printf "%.4f\n",('$new_total'/ 128000) *100}'`
    network_acquisition_json="$network_acquisition_json,'net_used_rate':$CURNUM"

    CURNUM=$new_total

    CURNUM=$(netstat -na|grep ESTABLISHED|wc -l )
    network_acquisition_json="$network_acquisition_json,'estab':$CURNUM"


    CURNUM=$(netstat -na|grep TIME_WAIT|wc -l )
    network_acquisition_json="$network_acquisition_json,'time_wait':$CURNUM"

    result="{'cpname':'interface',${network_acquisition_json#*,}}"
    result=`echo ${result//\'/\"}`
    echo $result
}

function parseTimeFileds(){
    val1=$(echo "$CURNUM $2")
    val2=$(echo "$CURNUM $3")
    DataResultStr="$DataResultStr $1=$CURNUM;$2;$3;0;0"
}

#CURNUM            当前值
#WARNLIMIT         告警的上限
#ERRORLIMIT    异常的上限
#判断以上三个值并拼接结果字符串
function parseFileds(){
    val1=$(echo "$CURNUM $2"| awk '{if($1<=$2){print 1}else{print 0}}')
    val2=$(echo "$CURNUM $3"| awk '{if($1<=$2){print 1}else{print 0}}')

    DataResultStr="$DataResultStr $1=$CURNUM;$2;$3;0;0"
    if [ $val1 -eq 1 ];then
        return 0
    elif [[ $val1 -eq 0 && $val2 -eq 1 ]];then
        if [[ ${WranResultStr} != "" ]]; then
            WranResultStr="${WranResultStr},"
        fi

        WranResultStr="${WranResultStr}$1当前值为${CURNUM}超过告警值${2}"

        if [[ ${quota} != "" ]]; then
            quota="${quota},"
        fi

        quota="${quota}$1"

        QUOTASTR="${QUOTASTR}$1=$1当前值为${CURNUM}超过告警值${2} "

        return 1
    else
        if [[ ${ErrorResultStr} != "" ]]; then
            ErrorResultStr="${ErrorResultStr},"
        fi
        ErrorResultStr="${ErrorResultStr}$1当前值为${CURNUM}超过紧急值${3}"

        if [[ ${quota} != "" ]]; then
            quota="${quota},"
        fi

        quota="${quota}$1"

        QUOTASTR="${QUOTASTR}$1=$1当前值为${CURNUM}超过紧急值${3} "

        return 2
    fi
}

#分析结果 给出给出状态
function analysisResult(){
    if [[ "$WranResultStr" == "" && "$ErrorResultStr" == "" ]];then
        gotErr 0
    elif [[ "$ErrorResultStr" == "" ]];then
        gotErr 1
    else
        gotErr 2
    fi
}

#输出错误信息并且退出程序
function showHelp(){
    if [ "$1" != "" ];then
        echo "请输入$1"
    fi
    echo "check_network.sh 可以监听本地的network的状态 参数如下"
    echo "check_network.sh [-w 入口流量(kb/s),出口流量(kb/s),网卡进口流量(MB),网卡出口流量(MB),总流量(MB),网卡入口流量使用率,网卡出口流量使用率] [-c 入口流量(kb/s),出口流量(kb/s),网卡进口流量(MB),网卡出口流量(MB),总流量(MB),网卡入口流量使用率,网卡出口流量使用率] [-f <str>]"
    echo "-w 表示监控警告的值"
    echo "-c 表示异常值"
    echo "-f <str> 特征字符串 表示监控哪个网卡 如eth0"
    exit 1
}

#检查参数是不是数字
function isNumber(){
    isNum=$(echo "$1" | awk '{print($0~/^([0-9])+[\.]?([0-9])?$/)?1:0}')
    return $isNum
}

function isLarge(){

    isNumber $1
    if [[ $? -eq 0 ]]; then
        return 1
    fi

    isNumber $2
    if [[ $? -eq 0 ]]; then
        return 1
    fi

    isLarge=$(echo "$1 $2"| awk '{if($1>=$2){print 1}else{print 0}}')
    if [ $isLarge -eq 1 ];then
        return 0
    else
        return 1
    fi
}

function checkARG(){
    if [[ "$2" != "" && "$3" != "" ]];then
        isLarge "$3" "$2"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            $1=1
        fi
    fi
    return 0
}

#检查参数是否合法
#即参数是否是小数和整数
#对于守护进程的选择 这个值可以不验证
function checkALL(){

    if [[ "$INMIN" != "" || "$INMAX" != "" ]];then
        if [[ "$INMIN" == "" || "$INMAX" == "" ]]; then
            return 1
        fi
        isLarge "$INMAX" "$INMIN"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            IN=1
        fi
    fi

    if [[ "$OUTMIN" != "" || "$OUTMAX" != "" ]];then
        if [[ "$OUTMAX" == "" || "$OUTMIN" == "" ]]; then
            return 1
        fi
        isLarge "$OUTMAX" "$OUTMIN"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            OUT=1
        fi
    fi

    if [[ "$SENDMIN" != "" || "$SENDMAX" != "" ]];then
        if [[ "$SENDMAX" == "" || "$SENDMIN" == "" ]]; then
            return 1
        fi
        isLarge "$SENDMAX" "$SENDMIN"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            SEND=1
        fi
    fi

    if [[ "$RECEIVEMIN" != "" || "$RECEIVEMAX" != "" ]];then
        if [[ "$RECEIVEMAX" == "" || "$RECEIVEMIN" == "" ]]; then
            return 1
        fi
        isLarge "$RECEIVEMAX" "$RECEIVEMIN"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            RECEIVE=1
        fi
    fi

    if [[ "$TOTALMIN" != "" || "$TOTALMAX" != "" ]];then
        if [[ "$TOTALMAX" == "" || "$TOTALMIN" == "" ]]; then
            return 1
        fi
        isLarge "$TOTALMAX" "$TOTALMIN"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            TOTAL=1
        fi
    fi

    if [[ "$IN_PERMIN" != "" || "$IN_PERMAX" != "" ]];then
        if [[ "$IN_PERMAX" == "" || "$IN_PERMIN" == "" ]]; then
            return 1
        fi
        isLarge "$IN_PERMAX" "$IN_PERMIN"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            IN_PER=1
        fi
    fi

    if [[ "$OUT_PERMIN" != "" || "$OUT_PERMAX" != "" ]];then
        if [[ "$OUT_PERMAX" == "" || "$OUT_PERMIN" == "" ]]; then
            return 1
        fi
        isLarge "$OUT_PERMAX" "$OUT_PERMIN"
        if [[ $? -eq 1 ]]; then
            return 1
        else
            OUT_PER=1
        fi
    fi

    return 0
}


function parseARG(){
    #it mean warning
    if [ $1 -eq 1 ];then
        INMIN=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[^0-9]//g")
        OUTMIN=$(echo $2 | awk -F ',' '{print $2}' | sed "s/[^0-9]//g")
        RECEIVEMIN=$(echo $2 | awk -F ',' '{print $3}' | sed "s/[^0-9]//g")
        SENDMIN=$(echo $2 | awk -F ',' '{print $4}' | sed "s/[^0-9]//g")
        TOTALMIN=$(echo $2 | awk -F ',' '{print $5}' | sed "s/[^0-9]//g")
        IN_PERMIN=$(echo $2 | awk -F ',' '{print $6}' | sed "s/[^0-9]//g")
        OUT_PERMIN=$(echo $2 | awk -F ',' '{print $7}' | sed "s/[^0-9]//g")
    else
        INMAX=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[^0-9]//g")
        OUTMAX=$(echo $2 | awk -F ',' '{print $2}' | sed "s/[^0-9]//g")
        RECEIVEMAX=$(echo $2 | awk -F ',' '{print $3}' | sed "s/[^0-9]//g")
        SENDMAX=$(echo $2 | awk -F ',' '{print $4}' | sed "s/[^0-9]//g")
        TOTALMAX=$(echo $2 | awk -F ',' '{print $5}' | sed "s/[^0-9]//g")
        IN_PERMAX=$(echo $2 | awk -F ',' '{print $6}' | sed "s/[^0-9]//g")
        OUT_PERMAX=$(echo $2 | awk -F ',' '{print $7}' | sed "s/[^0-9]//g")
    fi

}

while getopts "f:h" arg
do
    case $arg in
        f)
            FLAGE=$OPTARG
            ;;
        h)
            showHelp
            ;;
        ?) 
            showHelp
            ;;
    esac
done

if [[ "$FLAGE" == "" ]];then
    showHelp "特征项"
fi

#checkALL
if [[ $? == 1 ]];then
    showHelp "正确参数"
fi


#插件正题部分 
#确定存活的
#启动
#分析

analysisStat
#analysisResult

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code