DHUxFf.jpg

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

PORT=
LOG_PATH=
FLAGEID=
CURNUM=             #当前的性能参数
CORENUM=1

kafka_result_json=""

function gotErr(){
    if [ "1" -eq 3 ];then        echo "无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置"
    elif [ "1" -eq 4 ];then
        echo "该组件进程为僵尸进程,请确认并请检查该组件状态"
    fi

    exit 1
}

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


#处理连接数
function getConnNum(){
    CURNUM=(sudo netstat -apn | grep FLAGEID | grep ESTABLISHED | wc -l)
}

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

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

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

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

#ps -eo pid,lstart是查询进程的开始时间
function getStarttime(){
    CURNUM=(ps -eo pid,lstart | grep FLAGEID |awk '{if(1=='FLAGEID') print2 ,3 ,4 ,5 ,6}')
}

function getBacklogData(){
    if [ "LOG_PATH" == "" ];then
        CURNUM=
        return
    fi
    CURNUM=`du -b{LOG_PATH}|sort -rn|head -n 1|awk '{printf "%.2f\n", (1/1024/1024/1024)}'`
}

#采集指标
function analysisStat(){
    getConnNum
    parseFileds "ConnectionNum"

    getMemory
    parseFileds "Memory"

    getCPU
    parseFileds "CPU"

    getThreadNum
    parseFileds "Thread"

    getRuntime
    parseFileds "RUNTIME"

    getStarttime
    parseFileds "STARTTIME"

    if [[ "LOG_PATH" == "" || "LOG_PATH" == "<<log_path>>" ]];then
        CURNUM=""
        parseFileds "backlogData"
        return
    fi

    getBacklogData
    parseFileds "backlogData"
}

#组装指标
function parseFileds(){
kafka_result_json="kafka_result_json,'1':'CURNUM'"
}

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

#输出错误信息并且退出程序
function showHelp(){
    if [ "1" != "" ];then
        echo "请输入1"
    fi
    echo "check_kafaka.sh 可以监听本地的kafka的状态 参数如下"
    echo "check_kafaka.sh  [-p <port>] [-l <log_path>]"
    echo "-p <port> 端口号"
    echo "-l <log_path> kafka logs 路径"
    exit 1
}

while getopts "p:l:h" arg
do
    casearg in
        p)
            PORT=OPTARG
            ;;
        l)
            LOG_PATH=OPTARG
            ;;
        h)
            showHelp
            ;;
        ?) 
            showHelp
            ;;
    esac
done

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


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

isAlive
analysisStat
analysisResult

发表评论

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

Captcha Code