#!/bin/bash

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems
CurInspectNum="170"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}

isPass=0
LOGFILE="/var/log/inspect.log"

CODE_WHITELIST="
03032YHR 55
03033AUN 67
03033CQQ 72
03033FKM 73
03034AUT 55
03034AUU 55
03033FJB 72
03033FKF 72
03032WFN 55
03033BCW 55
03033FWL 67
03032WFM 55
03033FBL 72
03033GTB 72
03033JPV 55
03033JTC 73
03033QGV 72
03033QGW 73
03033QGY 55
03032VUA 55
03033FWK 67
03032WGK 28
03033AAX 28
03032YRE 55
03033BPF 55
03033GJS 72
03033BDA 55
03033FKJ 73
03033RXK 73
03033GSK 73
03033QRJ 73
03032WET 55
03033AXP 55
03032VUL 55
03032WEJ 55
03032WEP 55
03032YWH 67
03033AUP 67
03033CQU 72
03033CQV 72
03033GDP 72
03032WFG 28
03032VUM 28
03032WEK 28
03032WEQ 28
03032WFB 28
03033CWY 72
03033LBA 37
03032WFA 55
03032WFF 55
03032YHV 55
03033FJH 73
03033FKH 73
03033FJK 73
03033FKL 73
03033FKN 73
03033BGA 55
03033FAM 55
03033BGB 28
03033FAW 28
"

FW_WHITELIST="
1009
1010
1011
1012
1022
1025
1026
1027
1030
1031
1033
1035
1036
1037
1038
2130
2131
2150
2151
2152
2153
2155
2156
2158
2252
3216
3220
3221
3222
3230
3231
3232
3233
3234
3235
3241
3242
3243
3245
3246
3247
3248
3252
3253
3255
"

function LOG()
{
    echo [${time}][$$][$CurInspectFun]$@ >> $LOGFILE
}

##############################################################
## ʣʱ
##############################################################
function calcTimeRemaining()
{
    #  1: diskInfo show_diskһϢ  2: sn
    #  ȫֱ ʣ
    # ֵ 0:ȡʣɹ; 1:smartctlִʧ; 2ȡϵʱʧ; 3:ϵʱΪ; 4: 5:ϵʱСٷֱ
    local diskType=$1
    local sn=$2
    local device=$3

    if [[ "$diskType" == "sas" ]];then
        smartctl_life_temp=$(timeout 30 smartctl -a $device)
        if [ $? -ne 0 ];then
            return 1
        fi
        smartctl_time_temp=$(timeout 30 smartctl -l background $device)
        if [ $? -ne 0 ];then
            return 1
        fi
        powerOnTime=$(awk -F ':' '/power on time/{printf("%s", $2)}' <<< "$smartctl_time_temp" | awk -F" " '{print $2}' | sed -e 's/[ ]//g' | sed -e 's/,//g' | grep -v '[A-Z|a-z]')
        percentageUsed=$(awk -F ':' '/(Percentage|Media) used endurance indicator/{printf("%s", $2)}' <<< "$smartctl_life_temp" | sed -e 's/[ ]//g' | sed -e 's/[%]*$//g' | grep -v '[A-Z|a-z]')
    else
        nvme_smart_temp=$(timeout 30 hioadm info -d ${device} -s)
        if [ $? -ne 0 ];then
            return 1
        fi
        powerOnTime=$(awk -F ':' '/power on hours/{printf("%s", $2)}' <<< "$nvme_smart_temp" | sed -e 's/[ ]//g' | sed -e 's/,//g' | sed -e 's/[a-z|A-Z]//g' | grep -v '[A-Z|a-z]')
        percentageUsed=$(awk -F ':' '/percentage used/{printf("%s", $2)}' <<< "$nvme_smart_temp" | sed -e 's/[ ]//g' | sed -e 's/[%]*$//g' | grep -v '[A-Z|a-z]')
    fi

    local percentagLimt=$(awk -F ' ' '/'03${sn:0:6}'/{printf("%d", $2)}' <<< "$CODE_WHITELIST")
    if [ -z "$powerOnTime" ] || [ -z "$percentageUsed" ]; then  # ߻ȡϵʱʧ
        return 2
    fi

    if [ $powerOnTime -eq 0 ] || [ $percentageUsed -eq 0 ] || [ $percentagLimt -eq 0 ];then  # ϵʱʹٷֱΪ0
        return 3
    fi

    local avgTime=$(($powerOnTime/$percentageUsed))
    if [ $avgTime -eq 0 ];then # Ϊ0ĳ
        return 5
    fi

    local allTime=$(($avgTime*$percentagLimt))
    local timeRemainingHour=$(($allTime-$powerOnTime - 30 * 24))
    timeRemaining=$(($timeRemainingHour/24))
    percentageTure=$(($percentageUsed*100/$percentagLimt))
    if [ $timeRemaining -le 0 ];then  # ϱգӡûʣʱ
        timeRemaining=0
        return 4
    fi
    # ϱգӡʣʱ
    return 0
}

##############################################################
## sasǷ
##############################################################
function checkSASDisk()
{
    if [ -z "$sasDevice" ];then
        return
    fi

    local smartctlIsOkSAS=1
    while read line
    do
        sasDiskInfo=$(timeout 10 disktool -f i $line)
        if [ $? -ne 0 ];then
            LOG "[$LINENO] excute disktool -f i $line err."
            return
        fi
        
        local sn=$(awk -F ':' '/Serial Number/{printf("%s", $2)}' <<< "$sasDiskInfo" | sed -e 's/[ ]//g')
        local fw=$(awk -F ':' '/Firmware Version/{printf("%s", $2)}' <<< "$sasDiskInfo"  | sed -e 's/[ ]//g' | grep '^[0-9]\{4\}')
        local isRiskFw=$(grep "$fw" <<< "$FW_WHITELIST")
        if [ -z "$fw" ] || [ -z "$isRiskFw" ];then
            LOG "[$LINENO]Disk sn:${sn},  fw version is higher than 3255, no need check."
            continue
        fi
        
        local isRisk0303=$(awk -F ' ' '$1=="03'${sn:0:6}'" {printf("%s", $1)}' <<< "$CODE_WHITELIST")
        if [ -z "$isRisk0303" ];then
            LOG "[$LINENO]Disk sn:${sn}, not exist risk , no need check."
            continue
        fi

        isPass=1
        powerOnTime=NA
        percentageUsed=NA
        timeRemaining=NA
        percentageTure=NA
        errCode=NA
        if [ $smartctlIsOkSAS -eq 1 ];then
            calcTimeRemaining "sas" "$sn" "$line"
            errCode=$?

            if [ $errCode -eq 1 ] ||  [ $errCode -eq 2 ];then
                smartctlIsOkSAS=0
            fi
        fi
        echo "[ERR]INFO:Disk sn(${sn}) fw($fw) exist risk, time remaining(${timeRemaining})d powerOnTime(${powerOnTime})h percentageUsed(${percentageUsed}%) percentageTure(${percentageTure}%) code($errCode), suggestions upgrade disk to 3257 or latter." >> ${RESULTFILE} 2>&1
        LOG "[$LINENO]Disk sn:${sn} fw:$fw exist risk, time remaining:${timeRemaining}d powerOnTime:${powerOnTime}h percentageUsed:${percentageUsed}% percentageTure:${percentageTure}% code:$errCode, suggestions upgrade disk to 3257 or latter."
    done <<< "$sasDevice"
    return
}

##############################################################
## nvmeǷ
##############################################################
function checkNVMeDisk()
{
    if [ -z "$nvmeDevice" ];then
        return
    fi

    local nvmeDevices=$(echo $nvmeDevice | sed -e 's/|/\n/g')
    local hioadmInfoOkNVMe=1
    while read line
    do
        nvmeDiskInfo=$(timeout 20 hioadm info -d $line)
        if [ $? -ne 0 ];then
            LOG "[$LINENO] excute hioadm info -d $line err."
            return
        fi
        
        local sn=$(awk -F ':' '/serial number/{printf("%s", $2)}' <<< "$nvmeDiskInfo" | sed -e 's/[ ]//g')
        local fw=$(awk -F ':' '/firmware version/{printf("%s", $2)}' <<< "$nvmeDiskInfo"  | sed -e 's/[ ]//g' | grep '^[0-9]\{4\}')
        local isRiskFw=$(grep "$fw" <<< "$FW_WHITELIST")
        if [ -z "$fw" ] || [ -z "$isRiskFw" ];then
            LOG "[$LINENO]Disk sn:${sn},  fw version is higher than 3255, no need check."
            continue
        fi
        
        local isRisk0303=$(awk -F ' ' '$1=="03'${sn:0:6}'" {printf("%s", $1)}' <<< "$CODE_WHITELIST")
        if [ -z "$isRisk0303" ];then
            LOG "[$LINENO]Disk sn:${sn}, not exist risk , no need check."
            continue
        fi

        isPass=1
        powerOnTime=NA
        percentageUsed=NA
        timeRemaining=NA
        percentageTure=NA
        errCode=NA
        if [ $hioadmInfoOkNVMe -eq 1 ];then
            calcTimeRemaining "nvme" "$sn" "$line"
            errCode=$?

            if [ $errCode -eq 1 ] ||  [ $errCode -eq 2 ];then
                hioadmInfoOkNVMe=0
            fi
        fi
        echo "[ERR]INFO:Disk sn(${sn}) fw($fw) exist risk, time remaining(${timeRemaining})d powerOnTime(${powerOnTime})h percentageUsed(${percentageUsed}%) percentageTure(${percentageTure}%) code($errCode), suggestions upgrade disk to 3257 or latter." >> ${RESULTFILE} 2>&1
        LOG "[$LINENO]Disk sn:${sn} fw:$fw exist risk, time remaining:${timeRemaining}d powerOnTime:${powerOnTime}h percentageUsed:${percentageUsed}% percentageTure:${percentageTure}% code:$errCode, suggestions upgrade disk to 3257 or latter." # ͨԼʣʱ
    done <<< "$nvmeDevices"
    return
}

function checkES3000V5DiskLifeRisk()
{

    LOG "[$LINENO]ProductName=CheckES3000V5DiskLifeRisk"

    nvmeDevice=''
    for devId in {0..7}
    do
        local devMn=$(timeout 30 hioadm info -d nvme$devId | grep "model number" | grep -E "HSSD-D6|HWE5")
        if [ ! -z "$devMn" ]; then
            if [ -z "$nvmeDevice" ]; then
                nvmeDevice="nvme${devId}"
            else
                nvmeDevice=${nvmeDevice}"|nvme${devId}"
            fi
        fi
    done

    sasDevice=$(lsscsi -g | grep -E "HSSD-D6|HWE5" | awk -F' ' '{print $6}' | sed -e 's/[ ]//g')
    if [ -z "$nvmeDevice" ] && [ -z "$sasDevice" ]; then
        echo "INFO:This node does not have a ES3000/HSSD V5 disk. No need check." >> ${RESULTFILE} 2>&1
        LOG "[$LINENO] This node does not have a ES3000/HSSD V5 disk. No need check."
        return
    fi
    checkSASDisk
    checkNVMeDisk

    if [[ $isPass -eq 0 ]];then
        echo "INFO:This node has no risk." >> ${RESULTFILE} 2>&1
        LOG "[$LINENO] This node has no risk."
    fi
    return
}

checkES3000V5DiskLifeRisk
echo "${CurInspectFun}_Pass ${isPass}" >> ${RESULTFILE} 2>&1
[ -f "${G_TMP_INSPECT_PATH}tmpfilesysdiskslot" ] && rm -f "${G_TMP_INSPECT_PATH}tmpfilesysdiskslot"
exit 0