5fa00804a03f97448.jpg_fo742.jpg

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

#WARNLIMIT=-1      #警告值 超过该值则认为是需要告警
#ERRORLIMIT=-1         #错误值 超过该值认为是错误

FLAGE=
FLAGEID=
#是否输出FLAGPID
FLAGEOPID=1
#是否输出RUNTIME
RUNTIME=1
CONN=0              #是否检查连接数
CONNMIN=
CONNMAX=
MEMORY=0            #是否检查内存
MEMORYMIN=
MEMORYMAX=
CPU=0               #是否检查CPU
CPUMIN=
CPUMAX=
THREADNUM=0         #是否检查线程
THREADNUMMIN=
THREADNUMMAX=
PORT=
IP=localhost
CURNUM=             #当前的性能参数

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

quota=
QUOTASTR=
solr_acquisition_json=""


#返回是否服务是否存活
function isAlive(){
    CORENUM=`cat /proc/cpuinfo |grep "physical id"|wc -l`
    FLAGEID=$(netstat -lnp | grep -w $PORT | awk '{print $NF}' | awk -F '/' '{print $1}' |head -n 1)
    #-ne 1
    if [[ "$FLAGEID" == "" ]];then
        gotErr 3
    fi
    ZOMBIE=$(ps -A -ostat,pid | grep -e '^[Zz]' | grep $FLAGEID)
    if [[ "$ZOMBIE" != "" ]];then
       gotErr 4
    fi
    solr_acquisition_json="$solr_acquisition_json,'FLAGEPID':$FLAGEID"
}


#处理连接数
function getConnNum(){
    CURNUM=$(sudo netstat -apn | grep $FLAGEID | grep ESTABLISHED | wc -l)
    solr_acquisition_json="$solr_acquisition_json,'ConnectionNum':$CURNUM"
}

#ps aux 4是内存 累加4的值
function getMemory(){
    CURNUM=$(ps aux | grep $FLAGEID |awk 'BEGIN{sum=0}{sum+=$4}END{print sum}')
    solr_acquisition_json="$solr_acquisition_json,'Memory':$CURNUM"
}

#ps aux 3是CPU 累加3的值
function getCPU(){
    CURNUM=$(ps aux | grep $FLAGEID |awk 'BEGIN{sum=0}{sum+=$3}END{print sum}')
    if [ "$CORENUM" -ne 0 ];then
       CURNUM=$(printf "%.2f" `echo "scale=2;$CURNUM/$CORENUM" | bc`)
    fi
    solr_acquisition_json="$solr_acquisition_json,'CPU':$CURNUM"
}

#ps elm 是PID所包含的线程数目
function getThreadNum(){
    CURNUM=$(ps -mp $FLAGEID |wc -l | awk 'BEGIN{sum=0}{sum=$1-2}END{print sum}')
    solr_acquisition_json="$solr_acquisition_json,'Thread':$CURNUM"
}


#获取jvm_state
function getjvmStates(){
    jvm_states_content=`curl http://"$IP":"$PORT"/solr/admin/system 2>/dev/null`
    echo "$jvm_states_content" >jvmState.txt
}

#获取性能指标
function getCapabilityQuota(){
    capability_quota=`curl http://"$IP":"$PORT"/solr/admin/cores 2>/dev/null`
    echo "$capability_quota" >capability.txt
}

#ps -eo pid,etime 是查询进程运行时间
function getRuntime(){
    CURNUM=$(ps -eo pid,etime | grep $FLAGEID |awk '{if($1=='$FLAGEID') print $2}')
    solr_acquisition_json="$solr_acquisition_json,'RUNTIME':'$CURNUM'"
}

function getJvm_memory_raw_free(){
    free=`awk -F'[<>]' '{for(i=1;i<=NF;i++){if($i~/^long name="free"$/){print $(i+1)}}}' jvmState.txt`
        free_first=`echo $free|awk 'NR==1{print $1}'`
        CURNUM=`awk 'BEGIN{printf "%.2f\n",('$free_first'/ 1024/1024)}'`
        if [ -z $CURNUM ];then
            CURNUM=0
        fi
        solr_acquisition_json="$solr_acquisition_json,'jvm_memory_raw_free':$CURNUM"
}

function getJvm_memory_raw_max(){
    max=`awk -F'[<>]' '{for(i=1;i<=NF;i++){if($i~/^long name="max"$/){print $(i+1)}}}' jvmState.txt`
        max_first=`echo $max|awk 'NR==1{print $1}'`
        CURNUM=`awk 'BEGIN{printf "%.2f\n",('$max_first'/ 1024/1024)}'`
        if [ -z $CURNUM ];then
            CURNUM=0
        fi
        solr_acquisition_json="$solr_acquisition_json,'jvm_memory_raw_max':$CURNUM"
}

function getJvm_memory_raw_total(){
    total=`awk -F'[<>]' '{for(i=1;i<=NF;i++){if($i~/^long name="total"$/){print $(i+1)}}}' jvmState.txt`
        total_first=`echo $total|awk 'NR==1{print $1}'`
        CURNUM=`awk 'BEGIN{printf "%.2f\n",('$total_first'/ 1024/1024)}'`
        if [ -z $CURNUM ];then
            CURNUM=0
        fi
        solr_acquisition_json="$solr_acquisition_json,'jvm_memory_raw_total':$CURNUM"
}

function getJvm_memory_raw_used(){
    used=`awk -F'[<>]' '{for(i=1;i<=NF;i++){if($i~/^long name="used"$/){print $(i+1)}}}' jvmState.txt`
        used_first=`echo $used|awk 'NR==1{print $1}'`
        CURNUM=`awk 'BEGIN{printf "%.2f\n",('$used_first'/ 1024/1024)}'`
        if [ -z $CURNUM ];then
            CURNUM=0
        fi
        solr_acquisition_json="$solr_acquisition_json,'jvm_memory_raw_used':$CURNUM"
}


function getJvm_memory_raw_usedPer(){
    usedPer=`awk -F'[<>]' '{for(i=1;i<=NF;i++){if($i~/^double name="used%"$/){print $(i+1)}}}' jvmState.txt`
        usedPer_first=`echo $usedPer|awk 'NR==1{print $1}'`
        CURNUM=`awk -v awk_usedPer_first="$usedPer_first" 'BEGIN{printf "%.2f\n",(awk_usedPer_first)}'`
        if [ -z $CURNUM ];then
            CURNUM=0
        fi
        solr_acquisition_json="$solr_acquisition_json,'jvm_memory_raw_usedPer':$CURNUM"
}

function getmaxDoc(){
    maxdoc=`awk -F'[<>]' '{for(i=1;i<=NF;i++){if($i~/^int name="maxDoc"$/){print $(i+1)}}}' capability.txt`
        CURNUM=`echo $maxdoc|awk 'NR==1{print $1}'`
        if [ -z $CURNUM ];then
            CURNUM=0
        fi
        solr_acquisition_json="$solr_acquisition_json,'maxDoc':$CURNUM"
}

function getnumDocs(){
    numdocs=`awk -F'[<>]' '{for(i=1;i<=NF;i++){if($i~/^int name="numDocs"$/){print $(i+1)}}}' capability.txt`
        CURNUM=`echo $numdocs|awk 'NR==1{print $1}'`
        if [ -z $CURNUM ];then
            CURNUM=0
        fi
        solr_acquisition_json="$solr_acquisition_json,'numDocs':$CURNUM"
}

function getsegmentCount(){
    segmentcount=`awk -F'[<>]' '{for(i=1;i<=NF;i++){if($i~/^int name="segmentCount"$/){print $(i+1)}}}' capability.txt`
        CURNUM=`echo $segmentcount|awk 'NR==1{print $1}'`
        if [ -z $CURNUM ];then
            CURNUM=0
        fi
        solr_acquisition_json="$solr_acquisition_json,'segmentCount':$CURNUM"
}

function parseFileds(){
    solr_acquisition_json="$solr_acquisition_json,'$1':'$CURNUM'"
}

#依据传入的参数值来判断我们要监控的服务是什么
#然后通过各个函数获取到具体的值
#值之间的
function analysisStat(){

#获取jvm信息
    getjvmStates
    #获取性能指标
    getCapabilityQuota

        getConnNum

        getMemory

        getCPU

        getThreadNum

        getRuntime

        if [[ "$IP" == "" || "$IP" == "<<ip>>" ]];then
            CURNUM=""
            parseFileds "jvm_memory_raw_free"
            parseFileds "jvm_memory_raw_max"
            parseFileds "jvm_memory_raw_total"
            parseFileds "jvm_memory_raw_used"
            parseFileds "jvm_memory_raw_usedPer"
            parseFileds "maxDoc"
            parseFileds "numDocs"
            parseFileds "segmentCount"
            return
        fi

        getJvm_memory_raw_free
        getJvm_memory_raw_max
        getJvm_memory_raw_total
        getJvm_memory_raw_used
        getJvm_memory_raw_usedPer
        getmaxDoc
        getnumDocs
        getsegmentCount

}

#输出结果
function analysisResult(){
    result="{'cpname':'solr',${solr_acquisition_json#*,}}"
    result=`echo ${result//\'/\"}`
    echo $result
}

#输出错误信息并且退出程序
function showHelp(){
    if [ "$1" != "" ];then
        echo "请输入$1"
    fi
    echo "check_solr.sh 可以监听本地的solr的状态 参数如下"
    echo "check_solr.sh [-w 连接数,CPU,内存] [-c 连接数,CPU,内存] [-P <port>]"
    echo "-w 表示监控警告的值"
    echo "-c 表示异常值"
    echo "-p <port> 特征字符串 表示监控哪个solr"
    echo "-I 表示主机IP"
    exit 1
}

while getopts "I:p:h" arg
do
    case $arg in
        I)
            IP=$OPTARG
            ;;
        p)
            PORT=$OPTARG
            ;;
        h)
            showHelp
            ;;
        ?) 
            showHelp
            ;;
    esac
done

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



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

isAlive
analysisStat
analysisResult

发表评论

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

Captcha Code