#!/bin/bash

function set_ifs
{
    IFS_OLD=$IFS
    IFS=$'\n'
}

function restore_ifs
{
    IFS=$IFS_OLD
}

function IsRHNode()
{
    local name=''
    name=$(dmidecode -t 1 | grep "Product Name:" | awk '{print $3}')
    if [ "$name" = "RH2288" -o "$name" = "5288" -o "$name" = "RH2288H" ]; then
        return 0
    else
        return 1
    fi
}


function check_ipmi_info()
{
    bOK=0
    bC72=0
    ipmi_info=""
    #handle ipmitool failed
    for ((i=0;i<7;++i)); do
        ipmi_info=`ipmitool sdr|grep CPU`
        iRet=$?
        if [ $iRet -eq 0 ]; then
            break
        else
            echo "ipmitool sdr failed, return $iRet" 1>&2
        fi
    done

    cpu_info=`echo "$ipmi_info"|grep "Status"|sed 's/^ *//g'|sed "/^$/d"`
    count=`echo "$ipmi_info"|grep "Status"|wc -l`
    
    #ֻܿһCPU
    dmidecode -t 4 | grep -q "Family: Atom"
    if [ $? -eq 0 ];then
        bC72=1
    else
        bC72=0
    fi
    
    if [ $bC72 -eq 1 ];then
        if [ "$count" != "1" ];then
            echo "[ERR]C72 node cpu number($count) is not 1!ERRCODE(7)"
            bOK=1
        fi
    else
        if [ "$count" != "2" ];then
            echo "[ERR]Cpu number($count) is not 2!ERRCODE(7)"
            bOK=1
        fi
    fi

    set_ifs
    for line in $cpu_info; do
        if [ "$line" = "" ];then
            continue;
        fi

        Id=`echo $line | awk '{print $1}' | sed 's/_Status//'`
        Status=`echo $line | awk '{print $3}'`
        Temperature=`echo "$ipmi_info" |grep "_Core_temp" |grep $Id | awk '{print $3}'`

        if [ $bC72 -eq 1 ]; then
            Temperature=`expr $(echo $Temperature|awk -F'.' '{print $1}')`
        else
            Temperature=`expr $Temperature \+ 85`
        fi

        IdNumber=`echo $Id |sed 's/CPU//'`
        CacheSize=`cat /proc/cpuinfo |grep -A3 -B4 "physical id.*: .*$IdNumber"| grep "cache size" |sed -n 'n;$p' | awk '{print $4 $5}'`
        CoresNum=`cat /proc/cpuinfo | grep -A3 -B4 "physical id.*: .*$IdNumber"| grep "cpu cores" |sed -n 'n;$p' | awk '{print $4}'`
        Speed=`cat /proc/cpuinfo | grep -A3 -B4 "physical id.*:.*$IdNumber"| grep "cpu MHz" |sed -n 'n;$p' | awk '{print $4}'`
        Type=`cat /proc/cpuinfo |grep -A3 -B5 "physical id.*:.*$IdNumber"| grep "model name" |sed -n 'n;$p' | awk -F: '{print $2}'`
        if [ $Status == "0x80" ];then
            resultStatus="OK"
        else
            resultStatus="Fault"
        fi

        if [ $bC72 -eq 1 ]; then
            if [ $Temperature -ge 97 ] && [ $Status != "0x80" ]; then
                echo "[ERR]Error-$Id,Temperature($Temperature) >= 97 and Status is not OK,Status ($Status) is not consistent with the standard status (0x80)!ERRCODE(4)"
                bOK=1
            elif [ $Temperature -ge 97 ];then
                 echo "[ERR]Error-$Id,Temperature($Temperature) >= 97.ERRCODE(5)"
                 bOK=1
            elif [ $Status != "0x80" ]; then
                 echo "[ERR]Error:$Id,Status is not OK!Status ($Status) is not consistent with the standard status (0x80). ERRCODE(6)"
                 bOK=1
            fi
        else
            if [ $Temperature -ge 80 ] && [ $Status != "0x80" ]; then
                echo "[ERR]Error-$Id,Temperature($Temperature) >= 80 and Status is not OK,Status ($Status) is not consistent with the standard status (0x80)!ERRCODE(4)"
                bOK=1
            elif [ $Temperature -ge 80 ];then
                echo "[ERR]Error-$Id,Temperature($Temperature) >= 80.ERRCODE(5)"
                bOK=1
            elif [ $Status != "0x80" ]; then
                echo "[ERR]Error:$Id,Status is not OK!Status ($Status) is not consistent with the standard status (0x80). ERRCODE(6)"
                bOK=1
            fi
        fi

        echo "ID:$Id||Temperature(degrees):$Temperature||Status:$resultStatus||CacheSize:$CacheSize||CoresNumber:$CoresNum||Speed(MHz):$Speed||Type:$Type"
    done

    restore_ifs

    return $bOK
}
 
function check_RH_ipmi_info()
{
    local bOK=0
    local singularCPU=0
    for ((i=0;i<7;++i)); do
        ipmi_info=`ipmitool sensor|grep CPU`
        iRet=$?
        if [ $iRet -eq 0 ]; then
            break
        else
            echo "ipmitool sensor failed, return $iRet" 1>&2
        fi
    done
    cat /opt/huawei/deploy/etc/deploy.ini | grep "devicename=" | egrep -w "C36E|P12E" >/dev/null
    if [ $? -eq 0 ];then
        singularCPU=1
    fi
    cpu_info=`echo "$ipmi_info"|grep "Status"|sed 's/^ *//g'|sed "/^$/d"`
   
    set_ifs
    for line in $cpu_info; do
        if [ "$line" = "" ];then
            continue;
        fi

        Id=`echo $line | awk '{print $1}' |tr -d ' '`
        Status=`echo $line | awk '{print $8}'|tr -d "|"`
        #жC36E cpuǷλ isExist=0(λ)isExist=1(λ)
        isExist=$((`echo ${Status:0:4}` & 0x80))
        if [ $singularCPU -eq 1 ] && [ $isExist -eq 0 ];then
            continue
        fi
        Temperature=`echo "$ipmi_info" |grep "DTS" |grep $Id | awk '{print $4}'|awk -F'.' '{print $1}'`
        Temperature=`expr $Temperature \+ 85`
        
        IdNumber=`echo $Id |sed 's/CPU//'`
        IdNumber=$((IdNumber-1))
        CacheSize=`cat /proc/cpuinfo |grep -A3 -B4 "physical id.*: .*$IdNumber"| grep "cache size" |sed -n 'n;$p' | awk '{print $4 $5}'`
        CoresNum=`cat /proc/cpuinfo | grep -A3 -B4 "physical id.*: .*$IdNumber"| grep "cpu cores" |sed -n 'n;$p' | awk '{print $4}'`
        Speed=`cat /proc/cpuinfo | grep -A3 -B4 "physical id.*:.*$IdNumber"| grep "cpu MHz" |sed -n 'n;$p' | awk '{print $4}'`
        Type=`cat /proc/cpuinfo |grep -A3 -B5 "physical id.*:.*$IdNumber"| grep "model name" |sed -n 'n;$p' | awk -F: '{print $2}'`
        if [ $Status == "0x8080" ];then
            resultStatus="OK"
        else
            resultStatus="Fault"
        fi

        if [ $Temperature -ge 84 ] && [ $Status != "0x8080" ]; then
            echo "[ERR]Error-$Id,Temperature($Temperature) >= 84 and Status is not OK,Status ($Status) is not consistent with the standard status (0x8080)!ERRCODE(4)"
            bOK=1
        elif [ $Temperature -ge 84 ];then
            echo "[ERR]Error-$Id,Temperature($Temperature) >= 84.ERRCODE(5)"
            bOK=1
        elif [ $Status != "0x8080" ];then
            echo "[ERR]Error-$Id,Status is not OK!Status ($Status) is not consistent with the standard status (0x8080). ERRCODE(6)"
            bOK=1
        fi
        echo "ID:$Id||Temperature(degrees):$Temperature||Status:$resultStatus||CacheSize:$CacheSize||CoresNumber:$CoresNum||Speed(MHz):$Speed||Type:$Type"
    done

    restore_ifs

    return $bOK     
}


function check_CPU_info()
{   
    local iRet=0
    IsRHNode
    if [ $? -eq 0 ]; then       
        check_RH_ipmi_info
    else
        check_ipmi_info
    fi
    iRet=$?
    return $iRet
}


check_CPU_info
exit 0


