在伺服器的所有磁碟上執行 smartctl

在伺服器的所有磁碟上執行 smartctl

我的問題非常簡單,我想smartctl -i -A在伺服器擁有的所有磁碟上運行該命令。認為我有太多具有不同數量的磁碟和 RAID 控制器的伺服器,那麼我需要掃描所有驅動程式進行診斷。我正在考慮運行smartctl --scan | awk '{print $1}' >> test.log,所以如果我打開 test.log,我將在其中包含所有驅動器資訊。
之後,我需要執行一些 if 或 do 結構來掃描smartctl所有驅動程式。我不知道這是否是最好的方法,因為我也需要識別 RAID 控制器。正朝著正確的方向前進嗎?

編輯:

我習慣使用這些命令來排除故障:

不含 RAID 控制器

for i in {c..d}; do
    echo "Disk sd$i" $SN $MD
    smartctl -i -A /dev/sd$i |grep -E "^  "5"|^"197"|^"198"|"FAILING_NOW"|"SERIAL""
done

PERC控制器

for i in {0..12}; do
    echo "$i" $SN $MD
    smartctl -i -A -T permissive /dev/sda -d megaraid,$i |grep -E "^  "5"|^"197"|^"198"|"FAILING_NOW"|"SERIAL""
done
/usr/sbin/megastatus –physical
/usr/sbin/megastatus --logical

三件控制器

for i in {0..10}; do
    echo "Disk $i" $SN $MD
    smartctl -i -A /dev/twa0 -d 3ware,$i |grep -E "^  "5"|^"197"|^"198"|"FAILING_NOW"|"SERIAL""
done

SmartArray 和 Megaraid 控制器:

smartctl –a –d cciss,0 /dev/cciss/c0d0
/opt/3ware/9500/tw_cli show
cd /tmp

DD(重寫磁碟區塊(銷毀資料)):

dd if=/dev/zero of=/dev/HD* bs=4M
HD*: sda, sdb…

燒錄(壓力測試(DESTROY DATA)):

/opt/systems/bin/vs-burnin --destructive --time=<hours> /tmp/burninlog.txt

Dmesg&kernerrors:

tail /var/log/kernerrors
dmesg |grep –i –E “”ata”|”fault”|”error”

所以我想做的就是自動化這些指令。
我希望腳本驗證主機擁有的所有磁碟並smartctl針對該情況執行適當的命令。
類似帶有一些選項的選單,讓我選擇是否要執行一個smartctl或一些破壞性命令,如果我選擇運行smartctl
腳本將掃描所有磁碟並根據主機配置運行命令(帶/不帶 RAID 控制器),
如果我選擇運行破壞性命令,腳本會要求我輸入要執行此操作的磁碟號。


編輯2:

我用以下腳本解決了我的問題:

#!/bin/bash
# Troubleshoot.sh
# A more elaborate version of Troubleshoot.sh.

SUCCESS=0
E_DB=99    # Error code for missing entry.

declare -A address
#       -A option declares associative array.



if [ -f Troubleshoot.log ]
then
    rm Troubleshoot.log
fi

if [ -f HDs.log ]
then
    rm HDs.log
fi

smartctl --scan | awk '{print $1}' >> HDs.log
lspci | grep -i raid >> HDs.log

getArray ()
{
    i=0
    while read line # Read a line
    do
        array[i]=$line # Put it into the array
        i=$(($i + 1))
    done < $1
}

getArray "HDs.log"


for e in "${array[@]}"
do
    if [[ $e =~ /dev/sd* || $e =~ /dev/hd* ]]
        then
            echo "smartctl -i -A $e" >> Troubleshoot.log
            smartctl -i -A $e >> Troubleshoot.log # Run smartctl into all disks that the host have
    fi
done
exit $?   # In this case, exit code = 99, since that is function return.

我不知道這個解決方案是正確的還是最好的,但對我有用!
感謝所有幫助!

答案1

所以我想做的就是自動化這些指令。

這已經存在並體現在smartd.

您通常需要在中配置您想要的行為 /etc/smartd.conf

例子:

# DEVICESCAN: tells smartd to scan for all ATA and SCSI devices
# Alternative setting to report more useful raw temperature in syslog.
DEVICESCAN -I 194 -I 231 -I 9

您也可以明確地放置磁碟,例如

/dev/sdc -d 3ware,0 -a -s L/../../7/01

如果smartd發現錯誤,您將收到一封電子郵件:

/dev/hdc -a -I 194 -W 4,45,55 -R 5 -m [email protected]

還有許多其他選項和開關,您需要閱讀 的線上幫助頁smartd.conf

答案2

@xeruf。我在自己的工作中時不時地遇到這個問題,我一直必須重做我的工作,哈哈。為了讓我和其他人可以找到基本的智慧輸出命令,我執行以下命令:

for i in $(ls /dev/sd?); do
    echo "$i"
    sudo smartctl -a $i
done

相關內容