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

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

FLAGE=
FLAGEID=
#是否输出FLAGPID
FLAGEOPID=1
#是否输出RUNTIME
RUNTIME=1
#是否输出STARTTIME
STARTTIME=0
CONN=0              #是否检查连接数
CONNMIN=
CONNMAX=
MEMORY=0            #是否检查内存
MEMORYMIN=
MEMORYMAX=
CPU=0               #是否检查CPU
CPUMIN=
CPUMAX=
THREADNUM=0         #是否检查线程
THREADNUMMIN=
THREADNUMMAX=

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

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

quota=
QUOTASTR=
phpfpm_acquisition_json=""

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

    elif [ "$1" -eq 3 ];then 
        #
    processIsAlive
    portIsExist
    result="{'cpname':'php-fpm',${phpfpm_acquisition_json#*,}}"
    result=`echo ${result//\'/\"}`
    echo $result
        exit 3
    fi

    exit $1
}

#返回是否服务是否存活
function isAlive(){
    CORENUM=`cat /proc/cpuinfo |grep "physical id"|wc -l`
    FLAGEID=$(netstat -lnp | grep -w $PORT |grep php-fpm|awk '{print $NF}' | awk -F '/' '{print $1}'|head -n 1)
    if [[ "$FLAGEID" == "" ]];then
        gotErr 3
    fi

    phpfpm_acquisition_json="$phpfpm_acquisition_json,'FLAGEPID':$FLAGEID"
}


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

#判断进程是否存活
function processIsAlive(){
    result=$(ps -ef |grep 'php-fpm: master process' |grep -v 'grep'  |wc -l)
    if [[ "$result" -eq 0 ]];then
       CURNUM=2
    else
       CURNUM=0
    fi

    phpfpm_acquisition_json="$phpfpm_acquisition_json,'process_alive':$CURNUM"
}

#判断端口是否存在
function portIsExist(){
   result=$(netstat -anpl|egrep 'php-fpm|php-cgi'|egrep 'LISTEN'| wc -l)
    if [[ "$result" -eq 0 ]];then
       CURNUM=2
    else
       CURNUM=0
    fi
    phpfpm_acquisition_json="$phpfpm_acquisition_json,'port_exist':$CURNUM"
}


#ps aux 4是内存 累加4的值
function getMemory(){
    CURNUM=$(ps aux | grep $FLAGEID |awk 'BEGIN{sum=0}{sum+=$4}END{print sum}')
    phpfpm_acquisition_json="$phpfpm_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
    phpfpm_acquisition_json="$phpfpm_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}')
    phpfpm_acquisition_json="$phpfpm_acquisition_json,'thread':$CURNUM"
}

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

#ps -eo pid,lstart是查询进程的开始时间
function getStarttime(){
    CURNUM=$(ps -eo pid,lstart | grep $FLAGEID |awk '{if($1=='$FLAGEID') print $2 ,$3 ,$4 ,$5 ,$6}')
    phpfpm_acquisition_json="$phpfpm_acquisition_json,'STARTTIME':'$CURNUM'"
}
#依据传入的参数值来判断我们要监控的服务是什么
#然后通过各个函数获取到具体的值
#值之间的
function analysisStat(){


    getConnNum

    getMemory

    getCPU

    getThreadNum

    getRuntime

    getStarttime

    processIsAlive

    portIsExist


    result="{'cpname':'php-fpm',${phpfpm_acquisition_json#*,}}"
    result=`echo ${result//\'/\"}`
    echo $result

}

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


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

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




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

isAlive
analysisStat
#analysisResult

发表评论

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

Captcha Code