#!/bin/bash
# ********************************************** ***************************** #
# File Name: bdm_check_sysdisk.sh
# Description: 检查系统盘及脏数据分区状态
# Author: w00480285
# Created: 2021-03-05
# Function List：1) 脏数据分区检查；
#                2) 系统盘state检查，慢盘检查；
# **************************************************************************** #
PRODUCT_CONF=/OSM/conf/product.ini
SYSDEVINFO_TMPFILE=/tmp/sysdevinfo
# diagnose命令执行
function do_bdm_diagnose_cmd()
{
   echo -e "${1}\nexit\n" | diagnose --auto | grep -v "/diagnose>"
}
# 1）系统盘脏数据分区检查；
# 从配置文件中获取系统盘数量
# 2200 V3 8G/16G分别是1/2个系统盘; 升级配置的系统盘信息
function check_sysdisk_num()
{
    local L_SYSDISK_CONFIG=`cat $PRODUCT_CONF | grep "UPGSysDiskNum" | awk -F = '{print $2}'`
    local mem1=`echo $L_SYSDISK_CONFIG | awk -F ";" '{print $1}'| awk -F ":" '{print $1}'`
    local mem2=`echo $L_SYSDISK_CONFIG | awk -F ";" '{print $2}'| awk -F ":" '{print $1}'`
    if [ "x$L_SYSDISK_CONFIG" == "x" ]; then
        OS_echo "[bdm_check_sysdisk.sh]Failed to get sysdisk from config."
        exit 1
    fi
    if [ 1 == ${#L_SYSDISK_CONFIG} ]; then
        OS_echo "[bdm_check_sysdisk.sh]Sysdisk num($L_SYSDISK_CONFIG) from config."
        return $L_SYSDISK_CONFIG
    fi
    # 优化内存换算为GB后不准确的场景，如换算结果为7.9G，取8G
    local L_CTRL_MEM=`cat /proc/osp/bsp | grep "byte memory" | grep -oE "[0-9]+"`
    let L_CTRL_MEM_IN_GB=$L_CTRL_MEM/1024/1024/1024
    let L_CTRL_MEM_IN_GB=$L_CTRL_MEM_IN_GB-$mem1+$L_CTRL_MEM_IN_GB-$mem2
    if [ $L_CTRL_MEM_IN_GB -le 0 ]; then
        OS_echo "[bdm_check_sysdisk.sh]Controller with 1 sysdisk."
        L_CTRL_MEM_IN_GB=$mem1
    else
        OS_echo "[bdm_check_sysdisk.sh]Controller with 2 sysdisk."
        L_CTRL_MEM_IN_GB=$mem2
    fi

    local L_CFG_DISK_NUM=`echo $L_SYSDISK_CONFIG | awk -F "$L_CTRL_MEM_IN_GB" '{print $2}' | awk -F ";" '{print $1}' | awk -F ":" '{print $2}'`

    OS_echo "[bdm_check_sysdisk.sh]Get sysdisk num($L_CFG_DISK_NUM) from config."
    return $L_CFG_DISK_NUM
}
check_sysdisk_num
SYSDISKNUM=$?
if [ $SYSDISKNUM == 0 ]; then
    OS_echo "[bdm_check_sysdisk.sh]Sysdisk number is zero in config"
    exit 1
fi

# 获取本控normal的脏数据逻辑分区数
CACHEPARTITIONNUM=`(do_bdm_diagnose_cmd "ld showlds") | grep "partitionType(1)" | grep -v "sd(none)" | grep "state(0)" | wc -l`
# 配置文件中系统盘数量大于脏数据分区数量，检查不通过
if [ $SYSDISKNUM -gt $CACHEPARTITIONNUM ]; then
    OS_echo "[bdm_check_sysdisk.sh]SYS_DISK number($SYSDISKNUM) is not equal normal CACHE_PARTITION number($CACHEPARTITIONNUM)."
    exit 1
fi

# 2）系统盘状态检查，使用sddebug showdev查询系统盘类型和块设备名，nvme系统盘不检查，确认阵列系统盘获取块设备名；
if [ -f $SYSDEVINFO_TMPFILE ]; then
    rm $SYSDEVINFO_TMPFILE
fi
# 获取系统盘sdid type blkdev;
(do_bdm_diagnose_cmd "sddebug showdisk blkdev") | awk '{print $2,$5,$8}' | grep -wE '3[2-9]|[4-5][0-9]|6[0-3]' > $SYSDEVINFO_TMPFILE

while read devinfo; do
    read sdid devtype blkdev <<< $(echo $devinfo)
    # 检查系统盘是否慢盘
    isslow=`(do_bdm_diagnose_cmd "ld showabnormallds") | grep "partitionType(1)" | grep sd\($sdid\) | awk '{print $10}'`
    if [ "x$isslow" != "x" ]; then
        if [ "$isslow" != "slow(0)" ]; then
            OS_echo "[bdm_check_sysdisk.sh]sdId($sdid) devtype($devtype) is slow."
            exit 1
        fi
    fi
    # nvme盘不检查offline
    if [ $devtype == "NVMe_SSD" ]; then
        OS_echo "[bdm_check_sysdisk.sh]Skip check nvme ssd status, sdId($sdid), blkdev($blkdev)."
        continue
    fi
    # 检查系统盘状态是否running
    blkstate=`cat /sys/block/$blkdev/device/state`
    if [ $blkstate != "running" ]; then
        OS_echo "[bdm_check_sysdisk.sh]sdId($sdid), devtype($devtype), blkdev($blkdev), state($blkstate)."
        exit 1
    fi
done < $SYSDEVINFO_TMPFILE
OS_echo "[bdm_check_sysdisk.sh]All check passed."

exit 0
