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

BASEDIR="/usr/bin"

Survivor0_percent=

Survivor1_percent=

Eden_percent=

Old_percent=

Perm_percent=

EYGCT=

EFGCT=
myPath=
FLAGEPID=              #传入的参数,Java进程的pid
CURNUM=             #当前的性能参数
PORT=

jvm_json=

function gotErr(){
    if [ "$1" -eq 0 ];then
        jvm_json="{$jvm_json}"
        jvm_json=`echo ${jvm_json//\'/\"}`
        echo $jvm_json
    elif [ "$1" -eq 3 ];then 
        echo "该端口已不存在"
    fi

    exit $1
}

function isJstat(){
    myPath=${BASEDIR}/jstat
    #判断${BASEDIR}/jstat是否存在,存在就return,不存在goError 0
    if [[ -x ${myPath} ]];then
        return
    fi
    gotErr 0
}
#返回是否服务是否存活
function isAlive(){
    FLAGEPID=$(netstat -lnp | grep -w $PORT | awk '{print $NF}' | awk -F '/' '{print $1}' |head -n 1)
    if [[ "$FLAGEPID" == "" ]];then
        gotErr 3
    fi
    jvm_json="'cpname':'jvm','FLAGEPID':'$FLAGEPID'"
}
#依据传入的参数值来判断我们要监控的服务是什么
#然后通过各个函数获取到具体的值
#值之间的
function analysisStat(){
    result=`${myPath} -gcutil $FLAGEPID|awk 'NR==2'`
    CURNUM=$(echo ${result} |cut -d " " -f 1)

    parseFileds "Survivor0_percent"

    CURNUM=$(echo ${result} |cut -d " " -f 2)
    parseFileds "Survivor1_percent"

    CURNUM=$(echo ${result}|cut -d " " -f 3)
    parseFileds "Eden_percent"

    CURNUM=$(echo ${result} |cut -d " " -f 4)
    parseFileds "Old_percent"

    CURNUM=$(echo ${result} |cut -d " " -f 5)
    parseFileds "Perm_percent"

    YGC=`echo ${result} |cut -d " " -f 6`
    YGCT=`echo ${result} |cut -d " " -f 7`
    if [ "$YGC" == 0 ];then
        CURNUM=0
        parseFileds "EYGCT"
    else
        CURNUM=`awk 'BEGIN{printf "%.2f\n",('$YGCT'/'$YGC')*1000}'`
        parseFileds "EYGCT"
    fi

    FGC=`echo ${result} |cut -d " " -f 8`
    FGCT=`echo ${result} |cut -d " " -f 9`
    if [ "$FGC" == 0 ];then
        CURNUM=0
        parseFileds "EFGCT"
    else
        CURNUM=`awk 'BEGIN{printf "%.2f\n",('$FGCT'/'$FGC')*1000}'`
        parseFileds "EFGCT"
    fi

}

#CURNUM            当前值
#WARNLIMIT         告警的上限
#ERRORLIMIT    异常的上限
#判断以上三个值并拼接结果字符串
function parseFileds(){
     jvm_json="$jvm_json,'$1':'$CURNUM'"
}

#分析结果 给出给出状态
function analysisResult(){
    jvm_json="{$jvm_json}"
    jvm_json=`echo ${jvm_json//\'/\"}`
    echo $jvm_json
}

#输出错误信息并且退出程序
function showHelp(){
    if [ "$1" != "" ];then
        echo "请输入$1"
    fi
    echo "check_linux_jvm.sh 可以监听本地主机的存活状态 参数如下"
    echo "check_linux_jvm.sh [-p <port>] [-b <jstat_path>]"
    echo "-b jstat地址,可以不填,默认 /usr/bin"
    echo "-P 表示端口号,必填"
    exit 1
}

while getopts "P:f:b:h" arg
do
    case $arg in
        P)
            PORT=$OPTARG
            ;;
        b)
            BASEDIR=$OPTARG
            ;;
        h)
            showHelp
            ;;
        ?) 
            showHelp
            ;;
    esac
done

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


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

analysisStat
analysisResult

发表回复

您的电子邮箱地址不会被公开。

Captcha Code