#!/bin/bash
# ********************************************** ***************************** #
# File Name: fdsa_check_sysdisk.sh
# Description: fdsa系统盘检测
# Input: none        
# Output: none
# Created: 2019-10-23
# Function List：
# History
# **************************************************************************** #

source /OSM/script/os_load_base.sh

#目录检测列表：
CHECK_DIRECTORIES[0]="/startup_disk/conf/"
CHECK_DIRECTORIES[1]="/OSM/coffer_data/"
CHECK_DIRECTORIES[2]="/OSM/coffer_log/"
CHECK_DIRECTORIES[3]="/OSM/coffer_runningdata/"
#目录中新建文件名字：
NEW_FILE_NAME="fdsa_sysdisk_check"
#文件检测列表：
CHECK_FILES[0]="/startup_disk/conf/conf_local/group"
CHECK_FILES[1]="/startup_disk/conf/conf_local/passwd"
CHECK_FILES[2]="/startup_disk/conf/conf_local/sha256sum_ps"
CHECK_FILES[3]="/startup_disk/conf/conf_local/shadow"
CHECK_FILES[4]="/startup_disk/conf/conf_local/extend.ini"

FAULT_COUNT=0

SYSDISK_TIMESTAMP=/OSM/coffer_data/fdsa/sysdiskcheck_timestamp_sph60
FIFITEEN_TIMESTAMP=1296000
TWODAY_TIMESTAMP=172800
FDSAENV_FILE=/OSM/fdsa/conf/env.ini

#1.目录中是否能新建文件
check_create_files()
{
    i=0
    while [ $i -lt ${#CHECK_DIRECTORIES[*]} ]
    do
        if [ -d ${CHECK_DIRECTORIES[$i]} ];then
            if touch ${CHECK_DIRECTORIES[$i]}$NEW_FILE_NAME 2>&1 | grep "Input/output error" 2>/dev/null 1>/dev/null; then
                OS_echo "[fdsa_check_sysdisk.sh]SYS_RAID:${CHECK_DIRECTORIES[$i]} can not create new file,error is 'Input/output error'."
                let FAULT_COUNT++
            elif touch ${CHECK_DIRECTORIES[$i]}$NEW_FILE_NAME 2>&1 | grep "Read-only file system" 2>/dev/null 1>/dev/null; then
                OS_echo "[fdsa_check_sysdisk.sh]SYS_RAID:${CHECK_DIRECTORIES[$i]} can not create new file,error is 'Read-only file system'."
                let FAULT_COUNT++
            fi
            rm -rf ${CHECK_DIRECTORIES[$i]}$NEW_FILE_NAME 2>/dev/null 1>/dev/null
        fi
        let i++
    done
    
    return 0
}
#2.关键文件是否可读
check_files_read()
{
    i=0
    while [ $i -lt ${#CHECK_FILES[*]} ]
    do
        if cat ${CHECK_FILES[$i]} 2>&1 | grep "Input/output error" 2>/dev/null 1>/dev/null;then
            OS_echo "[fdsa_check_sysdisk.sh]SYS_RAID:${CHECK_FILES[$i]} is not read."
            let FAULT_COUNT++
        fi
        let i++
    done

    return 0
}
#3.检查文件系统状态
check_file_system_state()
{
	i=0
	while [ $i -lt ${#CHECK_DIRECTORIES[*]} ]
	do
        if [ -d ${CHECK_DIRECTORIES[$i]} ];then
            mount_dir=$(echo ${CHECK_DIRECTORIES[$i]} | sed 's/\/*$//')

            device_list=$(cat /proc/mounts | grep ${mount_dir} | awk '{print $1}')
		    if [ ! -n "$device_list" ]; then
			    let i++
			    continue
		    fi

		    for dev_name in $device_list; do
			    fs_state=`sudo tune2fs -l ${dev_name} | grep 'Filesystem state' | awk -F: '{print $2}' | sed 's/^[\t ]*//g'`
			    if [ "${fs_state}" != "" ] && [ "${fs_state}" != "clean" ]; then
				    OS_echo "[fdsa_check_sysdisk.sh]SYS_RAID:${mount_dir} filesystem state error."
				    let FAULT_COUNT++
			    fi
		    done
        fi
		let i++
	done

	return 0
}
#延迟检测
delay_check()
{
    if [ -f "${SYSDISK_TIMESTAMP}" ];then
        line=$(cat ${SYSDISK_TIMESTAMP} | awk '{print $0}')
        if [ "x${line}" = "x" ];then
            timestamp=$(date +%s)
            echo $timestamp > ${SYSDISK_TIMESTAMP}
            result=$?
            if [ $result -eq 0 ];then
                OS_echo " fdsa_check_sysdisk_DTS2024010214668.sh  create ${SYSDISK_TIMESTAMP} and write time $timestamp."
                exit 0
            else
                return
            fi
        fi

        timestamp=$(date +%s)
        if [ $timestamp -lt $line ];then
            OS_echo " fdsa_check_sysdisk_DTS2024010214668 nowtime $timestamp lt $line."
            return
        fi
        diff=$((timestamp - line))
		nodeid=0
        if [ -f "${FDSAENV_FILE}" ];then
            nodeid=`cat ${FDSAENV_FILE}  | grep "^nid=" | grep -o '[0-9]\+'`
        fi
        delaytime=$((${FIFITEEN_TIMESTAMP} + ($nodeid * $TWODAY_TIMESTAMP)))
        if [ $diff -lt ${delaytime} ];then
            exit 0
        fi
    fi
    return
}

delay_check

#系统盘检测主函数：

check_create_files
check_files_read
check_file_system_state

if [ 0 -ne $FAULT_COUNT ]; then
    OS_echo "[fdsa_check_sysdisk.sh]Sysdisk check is error."
    exit 1
fi

exit 0
