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

FLAGE=
VALUE=0
SPACEMINVALUE=
SPACEMAXVALUE=
INODEMINVALUE=
INODEMAXVALUE=
CURNUM=             #当前的性能参数
TOTALSPACE=
NUM=
WranResultStr=      #最后形成的告警字符串
ErrorResultStr=     #最后形成的错误字符串
DataResultStr=      #性能呢个参数字符串
QUOTASTR=
SPACEMIN=
SPACEMAX=
INODEMIN=
INODEMAX=


StateDesStr=
MsgContent=
Remark=
ExitCode=0
NUM=
quota=


function analysisSpaceStat(){
    NUM=$(df -hP  | grep $FLAGE | wc -l)
    if [ $NUM -eq 1 ];then
        CURNUM=$(df -hP  | grep $FLAGE | awk '{print $5}'| sed "s/[^0-9\.]//g")
    else
        CURNUM=$(df -hP  | grep $FLAGE | awk '{if($6==FLAGE)print $5}' FLAGE="$FLAGE"  | sed "s/[^0-9\.]//g" | head -n 1)
    fi
    parseFileds "disk"

    if [ $NUM -eq 1 ];then
        CURNUM=$(df -hP  | grep $FLAGE | awk '{print $2}')
    else
        CURNUM=$(df -hP  | grep $FLAGE | awk '{if($6==FLAGE)print $2}' FLAGE="$FLAGE" | head -n 1)
    fi
    if [ -z $CURNUM ];then
        CURNUM=0
    fi
    size $CURNUM
    parseFileds "total_disk"

    if [ $NUM -eq 1 ];then
        CURNUM=$(df -hP  | grep $FLAGE | awk '{print $4}')
    else
        CURNUM=$(df -hP  | grep $FLAGE | awk '{if($6==FLAGE)print $4}' FLAGE="$FLAGE" | head -n 1)
    fi
    if [ -z $CURNUM ];then
        CURNUM=0
    fi
    size $CURNUM
    parseFileds "avail_disk"

    if [ $NUM -eq 1 ];then
        CURNUM=$(df -iP  | grep $FLAGE | awk '{print $5}'| sed "s/[^0-9\.]//g")
    else
        CURNUM=$(df -iP  | grep $FLAGE | awk '{if($6==FLAGE)print $5}' FLAGE="$FLAGE"  | sed "s/[^0-9\.]//g" | head -n 1)
    fi
    if [[ $CURNUM == "" ]];then
        CURNUM=0
    fi
    parseFileds "inode"

    if [ $NUM -eq 1 ];then
        TOTALSPACE=$(df -hP  | grep $FLAGE | awk '{print $2}')
    else
        TOTALSPACE=$(df -hP  | grep $FLAGE | awk '{if($6==FLAGE)print $2}' FLAGE="$FLAGE")
    fi
    size $TOTALSPACE
    parseFileds "totalspace"

}

function size(){
    disksize=`echo $1 | tr -cd "[0-9,.]"`
    unit=`echo $1 | sed 's/[0-9,.]//g'`
    if [[ $unit == "T" ]];then
          #disksize=`echo $disksize*1024*1024 | bc`
          disksize=$(echo $disksize 1024 |awk '{ printf "%0.2f\n" ,$1*$2*$2}')
     fi
    if [[ $unit == "G" ]];then
         #disksize=`echo $disksize*1024 | bc`
         disksize=$(echo $disksize 1024 |awk '{ printf "%0.2f\n" ,$1*$2}')
     fi
    if [[ $unit == "M" ]];then
        #disksize=`echo $disksize | bc`
        disksize=$(echo $disksize 1024 |awk '{ printf "%0.2f\n" ,$1}')
    fi
    CURNUM=`echo $disksize | awk '{printf ("%.2f\n",$1)}'`
}


#判断以上三个值并拼接结果字符串
function parseFileds(){
    if [[ "$DataResultStr" != "" ]];then
        DataResultStr=$DataResultStr","
    fi
    DataResultStr="$DataResultStr'$1':'$CURNUM'"

}

#输出错误信息并且退出程序
function showHelp(){
    if [ "$1" != "" ];then
        echo "请输入$1"
    fi
    echo "check_disk.sh 可以监听本地的磁盘的状态 参数如下"
    echo "check_disk.sh [-p <str>]"
    echo "-p <str> 特征字符串 表示监控哪个硬盘挂载点 如/data"
    exit 1
}

function resultOutput(){
    result="{'cpname':'disk',$DataResultStr}"
    result=`echo ${result//\'/\"}`
    echo $result
}

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

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


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

analysisSpaceStat

resultOutput
exit $ExitCode

例子

[root@sre ~]# bash check_disk.sh -p /data
{"cpname":"disk","disk":"25","total_disk":"60416.00","avail_disk":"44032.00","inode":"2","totalspace":"60416.00"}

发表评论

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

Captcha Code