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

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

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

#连接数
CONN=-1
CONNMIN=-1
CONNMAX=-1
#CPU
CPU=-1
CPUMIN=-1
CPUMAX=-1
#内存
MEMORY=-1
MEMORYMIN=-1
MEMORYMAX=-1
#上传
UPLOAD=-1
UPLOADMIN=-1
UPLOADMAX=-1
#下载
DOWNLOAD=-1
DOWNLOADMIN=-1
DOWNLOADMAX=-1
#总容量大小
TOTALSPACE=-1
TOTALSPACEMIN=-1
TOTALSPACEMAX=-1
#已使用容量大小
USEDSPACE=-1
USEDSPACEMIN=-1
USEDSPACEMAX=-1
#未使用容量大小
UNUSEDSPACE=-1
UNUSEDSPACEMIN=-1
UNUSEDSPACEMAX=-1
PORT=
FLAGEID=
#是否输出FLAGPID
FLAGEOPID=1
#是否输出RUNTIME
RUNTIME=1
#是否输出STARTTIME
STARTTIME=0
#是否输出FILENUM
CLUSERFILENUM=1
QUOTASTR=
CORENUM=1
quota=
#安装目录
INSTALL_PATH=
#运行目录
CONFIG_PATH=/etc/fdfs/
StatStr=
IP=
VERSION=5
function gotErr(){
    if [ "1" -eq 2 ];then        result="{'cpname':'fastdfs',DataResultStr}"
        result=`echo {result//\'/\"}`
        echoresult
    elif [ "1" -eq 3 ];then
        echo "无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置"
    elif [ "1" -eq 4 ];then
        echo "该组件进程为僵尸进程,请确认并请检查该组件状态"
      exit 3
    fi

    exit 1
}

function isAlive(){
  CORENUM=`cat /proc/cpuinfo |grep "physical id"|wc -l`
  FLAGEID=(netstat -lnp | grep fdfs_stor |grep -w PORT | grep tcp | grep -v "127.0.0.1"|awk '{printNF}'| 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
  installs=`ls -l /proc/FLAGEID/exe | awk '{for(i=1;i<=NF;i++){if(i ~ /fdfs_storaged/) printi}}'`
  INSTALL_PATH=(locate fdfs_monitor |grep -w 'fdfs_monitor'|head -n 1)
  IP=(ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk '{print2}' | awk -F ':' '{print 2}')
    StatStr=`{INSTALL_PATH} {CONFIG_PATH}storage.conf 2>&1 &`
    #echo "{StatStr}"
    VERSION=`echo "{StatStr}" | grep "version" |head -n 1 |awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print (i+1)}}'|awk -F ':' '{print1}'`
    if echo VERSION|grep -qe '^3';then
        VERSION=3
    elif echoVERSION|grep -qe '^5';then
        VERSION=5
    fi
    if [[ VERSION == '' ]];then
        VERSION=5
    fi
    #echo "VERSION=VERSION"
}


#处理连接数
function getConnNum(){
    if [ VERSION -eq 3 ];then
        CURNUM=`netstat -antp | grep "IP:PORT" | grep ESTABLISHED -c`
    else
        CURNUM=`echo "{StatStr}" | grep -v "source storage id = IP"| grep -A60 -w "id =IP"| grep "connection.current_count" |awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print(i+1)}}'|awk -F ':' '{print 1}'`
  fi
}

#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
}

#已使用容量大小
function getUsedSpace(){
    if [VERSION -eq 3 ];then
        total=`echo "{StatStr}" | grep -v "source ip_addr"| grep -A60 -w "ip_addr =IP"| grep "total storage" |awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print(i+1)}}'|awk -F ':' '{print 1}'`
        total={total%%GB}
        free=`echo "{StatStr}" | grep -v "source ip_addr"| grep -A60 -w "ip_addr =IP"| grep "free storage"|awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print(i+1)}}'|awk -F ':' '{print 1}'`
        free={free%%GB}
        if [[ "total" == "" || "free" == "" ]];then
            CURNUM=""
            return
        fi
        CURNUM=[`echo "total-free"|bc`*1024]
    else
        total=`echo "{StatStr}" | grep -v "source storage id = IP"| grep -A60 -w "id =IP"| grep "total storage" |awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print(i+1)}}'|awk -F ':' '{print 1}'`
        free=`echo "{StatStr}" | grep -v "source storage id = IP"| grep -A60 -w "id =IP"| grep "free storage"|awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print(i+1)}}'|awk -F ':' '{print 1}'`
        if [[ "total" == "" || "free" == "" ]];then
            CURNUM=""
            return
        fi
        CURNUM=`echo "total-free"|bc`
  fi
}

#未使用容量大小
function getUnUsedSpace(){
    if [VERSION -eq 3 ];then
        CURNUM=`echo "{StatStr}" | grep -v "source ip_addr"| grep -A60 -w "ip_addr =IP"| grep "free storage" |awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print(i+1)}}'|awk -F ':' '{print 1}'`
        CURNUM=[{CURNUM%%GB}*1024]
    else
        CURNUM=`echo "{StatStr}" | grep -v "source storage id = IP"| grep -A60 -w "id =IP"| grep "free storage" |awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print(i+1)}}'|awk -F ':' '{print 1}'`
  fi
}

#获取storage总容量大小
function getTotalSpace(){
    if [VERSION -eq 3 ];then
        CURNUM=`echo "{StatStr}" | grep -v "source ip_addr"| grep -A60 -w "ip_addr =IP"| grep "total storage" |awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print(i+1)}}'|awk -F ':' '{print 1}'`
        CURNUM=[{CURNUM%%GB}*1024]
    else
        CURNUM=`echo "{StatStr}" | grep -v "source storage id = IP"| grep -A60 -w "id =IP"| grep "total storage" |awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print(i+1)}}'|awk -F ':' '{print 1}'`
  fi
}

#上传
function getUpload(){
    if [VERSION -eq 3 ];then
        CURNUM=`echo "{StatStr}" | grep -v "source ip_addr"| grep -A60 -w "ip_addr =IP"| grep "total_upload_bytes" |awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print(i+1)}}'|awk -F ':' '{print 1}'`
        CURNUM=(echo "CURNUM" | awk '{printf("%.2f\n",1/1024)}')
    else
        CURNUM=`echo "{StatStr}" | grep -v "source storage id =IP"| grep -A60 -w "id = IP"| grep "total_upload_bytes"|awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print (i+1)}}'|awk -F ':' '{print1}'`
        CURNUM=(echo "CURNUM" | awk '{printf("%.2f\n",1/1024)}')
  fi
}

#下载
function getDownload(){
    if [VERSION -eq 3 ];then
        CURNUM=`echo "{StatStr}" | grep -v "source ip_addr"| grep -A60 -w "ip_addr =IP"| grep "stotal_download_bytes" |awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print(i+1)}}'|awk -F ':' '{print 1}'`
        CURNUM=(echo "CURNUM" | awk '{printf("%.2f\n",1/1024)}')
    else
        CURNUM=`echo "{StatStr}" | grep -v "source storage id =IP"| grep -A60 -w "id = IP"| grep "stotal_download_bytes"|awk -F ' ' '{for(i=1;i<NF;++i){if(index(i,"=")==1)print (i+1)}}'|awk -F ':' '{print1}'`
        CURNUM=(echo "CURNUM" | awk '{printf("%.2f\n",1/1024)}')
  fi
}

#文件数
function getTotalFileNums(){
    CURNUM=`echo "{StatStr}" | grep -v "source storage id = IP"| grep -A60 -w "Storage 1" | grep "success_upload_count" | awk -F '=' '{printNF}'| awk '{sum+=1}END{print sum}'`
}

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

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




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


        CURNUM=FLAGEID
        parseTimeFileds "FLAGEPID"

        getRuntime
        parseTimeFileds "RUNTIME"

        getStarttime
        parseTimeFileds "STARTTIME"

        getConnNum
        parseFileds "CONN"

        getCPU
        parseFileds "CPU"

        getMemory
        parseFileds "MEMORY"

        getUpload
        parseFileds "UPLOAD"

        getDownload
        parseFileds "DOWNLOAD"

        getTotalSpace
        parseFileds "TOTALSPACE"

        getUsedSpace
        parseFileds "USEDSPACE"

        getUnUsedSpace
        parseFileds "UNUSEDSPACE"

        getTotalFileNums
        parseTimeFileds "CLUSERFILENUM"

}
function parseTimeFileds(){
    if [[ "DataResultStr" != "" ]];then
        DataResultStr=DataResultStr","
    fi
    DataResultStr="DataResultStr'1':'CURNUM'"
}
#CURNUM            当前值
#WARNLIMIT         告警的上限
#ERRORLIMIT    异常的上限
#判断以上三个值并拼接结果字符串
function parseFileds(){
    if [[ "DataResultStr" != "" ]];then
        DataResultStr=DataResultStr","
    fi
    DataResultStr="DataResultStr'1':'CURNUM'"
}

#分析结果 给出给出状态
function analysisResult(){
        gotErr 2
}

#输出错误信息并且退出程序
function showHelp(){
    if [ "1" != "" ];then
        echo "请输入1"
    fi
    echo "check_fastdfs.sh 可以监听本地的FastDFS的状态 参数如下"
    echo "check_fastdfs.sh [ -p <port>] [ -b <path>] [ -u <http_url>]"
    echo "-p 用于标示进程的端口号"
    echo "-b 配置文件的路径,默认为/etc/fdfs/"
    echo "-u 访问fastdfs的地址"
    exit 1
}

while getopts "u:p:b:f:hP:" arg
do
    case arg in
        h)
            showHelp
            ;;
        u)
            ;;                 p)
            PORT=OPTARG
            ;;  
        b)
            CONFIG_PATH=OPTARG
            ;;                 ?)            showHelp
            ;;
    esac
done

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

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

isAlive
analysisStat
analysisResult

发表评论

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

Captcha Code