前言
一開始unraid轉(zhuǎn)移到openmediavault的時候硬霍,就很糾結(jié)于omv是否需要給硬盤設(shè)置高級電源管理庇麦。這項操作在unraid里是很方便統(tǒng)一對硬盤進行設(shè)置的吻谋,但是在omv里需要逐一對于硬盤進行操作(尤其是我的硬盤柜總共有40+硬盤)逝变,而且也不是很明白高級電源管理里的各種選項巡社,就一直很勸退……
但是電費單子下來之后膛堤,我覺得還是很有必要實操一下了。
我的硬盤柜主要是做半冷存儲晌该,主要是大量寫入后肥荔,幾乎不做改動,過了一定時間的存儲期限后將其刪除朝群。
因此在我的使用場景下燕耿,沒有很長期的頻繁讀寫操作,所以我的電源管理設(shè)置是127姜胖,停轉(zhuǎn)時間是30min誉帅。(也許后期會提高到60,需要運行一段時間看LoadCycleCount增長是不是很夸張再做決定)
正題
查詢硬盤休眠狀態(tài)
設(shè)置好硬盤的電源管理后右莱,為了驗證是否設(shè)置成功并有效的可以Standby已達到節(jié)能的目的蚜锨,便搜索了一下相關(guān)內(nèi)容,然后找到@龍?zhí)靑van 的文檔
《OMV查詢硬盤休眠狀態(tài)(Linux通用)》 http://www.reibang.com/p/74f65ac27a2a
@龍?zhí)靑van 的文檔使用下來體驗良好隧出,可以快速在不喚醒硬盤的情況下查閱硬盤IDLE狀態(tài)踏志,即Active或者Standby。
但是同時也到一定的問題胀瞪,比如一開始無法正確獲取硬盤sdaa /sdab 之類的盤符
dsk=`ls /dev/|grep 'sd[a-z]$'` #此代碼只能識別三位字符盤符针余,不能識別如 sdaasdab 等驅(qū)動器名稱
dsk=`ls /dev/sd* | grep -Po 'sd(a{2}|[a-z]+)$'` #修改為此即可獲取sdaa/sdab等盤位
原文的作者也很好的寫明白了一步一步的過程饲鄙,所以很容易看明白是怎么寫出這個腳本的。
基于此圆雁,跟著他的思路和方法忍级,我跟著測試改寫了一些代碼,使其實現(xiàn)了更多更適合我的使用場景的功能伪朽。
以下提供兩種輸出界面的腳本
加料版結(jié)果A
1.首先解決不能正確識別到sdaa等同類盤符的磁盤轴咱;
2.增加分割線方便閱讀echo結(jié)果;
3.使用lsblk和grep將硬盤對應(yīng)的路徑和盤符大小標識出來烈涮,方便跟文件系統(tǒng)一一對應(yīng)朴肺,使得結(jié)果更直觀易讀;
4.使用if語句判斷硬盤狀態(tài)坚洽,并使用顏色標記硬盤的不同狀態(tài)戈稿,紅色ACTIVE,綠色STANDBY讶舰,白色UNKOWN鞍盗;
5.增加 if 嵌套語句判斷除開 ACTIVE 和 STANDBY 之外狀態(tài)的硬盤;
6.使用for語句將UNKNOWN狀態(tài)的硬盤列舉出來跳昼,并且在段落最后單獨echo出數(shù)量以及具體盤符般甲;
...
A腳本完整版如下:
-簡單閱讀一遍可以直接抄作業(yè)敷存,按照上文提到的@龍?zhí)靑van 的文檔存儲腳本,對腳本賦予權(quán)限即可挪略。不在此贅述历帚。
-需要注意的是,顏色標識在omv的計劃任務(wù)里并不支持杠娱。
#!/bin/bash
#function-1:顯示硬盤盤符+Lable+容量以及硬盤上電狀態(tài)
#function-2:通過echo -e轉(zhuǎn)義與if...elif...else語句使其active呈現(xiàn)紅色,standby呈現(xiàn)綠色,方便快速檢視硬盤上電情況
#function-3:原for循環(huán)語句中加入判斷和循環(huán)計數(shù)方式,統(tǒng)計active和standby的數(shù)量
#function-4:增加 if 嵌套語句判斷除開 active 和 standby 之外狀態(tài)的硬盤挽牢,并將其標記為 unknown
#function-5:將unknown狀態(tài)的硬盤列舉出來(對for語句上癮了……
dsk=`ls /dev/sd* | grep -Po 'sd(a{2}|[a-z]+)$'`
echo `date +%c`
standby=0
active=0
unknown=0
c=0
for i in $dsk;
do
echo -e "\n";
echo -e "-----------------------";
echo -n "/dev/$i : " ;
stats=`smartctl -i -n standby /dev/$i|grep "mode"|awk '{print $4}' `;
#echo $stats
if [[ $stats == STANDBY ]]||[[ $stats == ACTIVE ]]||[[ $stats == IDLE_A ]]
then
for s in $stats;
do
if [ $s == STANDBY ]
then
echo -e "\033[30;42m STANDBY \033[0m"
let standby=$standby+1
else
echo -e "\033[37;41m ACTIVE \033[0m"
let active=$active+1
fi
done
else
echo -e "\033[30;47m UNKNOWN \033[0m"
let unknown=$unknown+1
for un in $i
do
list[c]=$un
((c++))
done
fi
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $7}' `;
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $4}' `;
done
echo -e "\n";
echo -e "\033[37;41mActive Disk in Total=$active \033[0m";
echo -e "\033[30;42mStandby Disk in Total=$standby \033[0m";
echo -e "\033[30;47mUnknown Disk in Total=$unknown \033[0m";
#echo $c
echo -e "Unknown Disk list: ";
for((b=0;b<=$c;b++));
do
if [[ $b -lt $c ]]
then
echo ${list[b]}
fi
done
echo -e "\n";
exit
加料版B界面
B版腳本
#!/bin/bash
#function-1:顯示硬盤盤符+Lable+容量以及硬盤上電狀態(tài)
#function-2:通過echo -e轉(zhuǎn)義與if...elif...else語句使其active呈現(xiàn)紅色,standby呈現(xiàn)綠色,方便快速檢視硬盤上電情況
#function-3:原for循環(huán)語句中加入判斷和循環(huán)計數(shù)方式,統(tǒng)計active和standby的數(shù)量
#function-4:增加 if 嵌套語句判斷除開 active 和 standby 之外狀態(tài)的硬盤,并將其標記為 unknown
dsk=`ls /dev/sd* | grep -Po 'sd(a{2}|[a-z]+)$'`
echo `date +%c`
printf "%-11s %-10s %-40s %-10s\n" Disk Stats DiskLable Capacity
standby=0
active=0
unknown=0
c=0
for i in $dsk;
do
#echo -e "\n";
#echo -e "-----------------------";
printf "%-11s" /dev/$i:;
#echo -n -e "/dev/$i :\t" ;
stats=`smartctl -i -n standby /dev/$i|grep "mode"|awk '{print $4}'`;
#echo $stats
if [[ $stats == STANDBY ]]||[[ $stats == ACTIVE ]]||[[ $stats == IDLE_A ]]
then
for s in $stats;
do
if [ $s == STANDBY ]
then
# printf("[%-8s]" "\033[1;30;42m STANDBY \033[0m");
echo -e -n "\033[30;42mSTANDBY\033[0m"
printf "%-5s" ;
let standby=$standby+1
else
echo -e -n "\033[37;41mACTIVE \033[0m"
printf "%-5s" ;
let active=$active+1
fi
done
else
echo -e -n "\033[30;47mUNKNOWN\033[0m"
printf "%-5s" ;
let unknown=$unknown+1
for un in $i
do
list[c]=$un
((c++))
done
fi
#輸出mountpoint之前判斷是否mount
mountpoint=`lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $7}'`;
if [[ $mountpoint == */srv/dev* ]]
then
printf "%-40s" "`lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $7}'`";
else
echo -n Not Mounted! ;
fi
#printf "%-40s" "`lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $7}'`";
printf "%-10s\n" `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $4}'`;
#echo -n -e "`lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $7}'`\t";#lable
#echo -n -e "\t" #釋義:-n 為echo輸出后不換行 -e和\t組合表示插入tab制表符 方便輸出統(tǒng)一格式
#echo -e `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $4}'`; #capacity
done
#各狀態(tài)硬盤數(shù)量統(tǒng)計顯示
echo -e "\n";
echo -e "\033[37;41mActive Disk in Total=$active \033[0m";
echo -e "\033[30;42mStandby Disk in Total=$standby \033[0m";
echo -e "\033[30;47mUnknown Disk in Total=$unknown \033[0m";
#echo $c
echo -e "Unknown Disk list: ";
for((b=0;b<=$c;b++));
do
if [[ $b -lt $c ]]
then
echo "/dev/${list[b]}"
fi
done
echo -e "\n";
exit
本文到此結(jié)束
使用愉快摊求,以下內(nèi)容只是作為我改寫過程的記錄禽拔,方便還原各個功能點引入時候的思路,感興趣的可以看一下幫忙指正室叉。
v1
#!/bin/bash
#function-1:顯示硬盤盤符+Lable+容量以及硬盤上電狀態(tài)
dsk=`ls /dev/|grep 'sd[a-z]$'` #此代碼只能識別三位字符盤符睹栖,不能識別如 sdaasdab 等驅(qū)動器名稱
dsk=`ls /dev/sd* | grep -Po 'sd(a{2}|[a-z]+)$'` #修改為此即可
echo `date +%c`
for i in $dsk;
do
echo -n "/dev/$i : " ; #顯示盤符
echo `smartctl -i -n standby /dev/$i|grep "mode"|awk '{print $4}' `; #顯示硬盤通電狀態(tài)
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $7} `; #顯示硬盤 lable
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $4}' `; #顯示硬盤大小
echo -e "\n"; #換行
done
exit
v2
增加顏色顯示加以區(qū)分,建立分割線茧痕,方便查看各硬盤
#!/bin/bash
dsk=`ls /dev/sd* | grep -Po 'sd(a{2}|[a-z]+)$'`
echo `date +%c`
for i in $dsk;
do
echo -e "\033[37;41m !Disk Stats! \033[0m";
echo -n "/dev/$i : " ;
echo `smartctl -i -n standby /dev/$i|grep "mode"|awk '{print $4}' `;
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $7}' `;
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $4}' `;
echo -e "\n";
done
exit
v3
增加條件判斷
active 紅色
stanby 綠色
check 黃色
#!/bin/bash
#function-1:顯示硬盤盤符+Lable+容量以及硬盤上電狀態(tài)
#function-2:通過echo -e轉(zhuǎn)義與if...elif...else語句使其active呈現(xiàn)紅色,standby呈現(xiàn)綠色,方便快速檢視硬盤上電情況
dsk=`ls /dev/sd* | grep -Po 'sd(a{2}|[a-z]+)$'`
echo `date +%c`
for i in $dsk;
do
echo -e "-----------------------";
echo -n "/dev/$i : " ;
stats=`smartctl -i -n standby /dev/$i|grep "mode"|awk '{print $4}' `;
for s in $stats;
do
echo "$s"
if [ $s == STANDBY ]
then
echo -e "\033[30;42m STANDBY \033[0m";
elif [ $s == ACTIVE ]
then
echo -e "\033[37;41m ACTIVE \033[0m";
else
echo -e "\033[30;43m PLEASE CKECK \033[0m";
fi
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $7}' `;
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $4}' `;
echo -e "\n";
done
done
exit
v4
增加硬盤狀態(tài)的數(shù)量統(tǒng)計
代碼中對 unknown 的判斷是無效的野来,下一個功能版本中修復(fù)。(原因為 stats 有可能返回為空 string
#!/bin/bash
#function-1:顯示硬盤盤符+Lable+容量以及硬盤上電狀態(tài)
#function-2:通過echo -e轉(zhuǎn)義與if...elif...else語句使其active呈現(xiàn)紅色,standby呈現(xiàn)綠色,方便快速檢視硬盤上電情況
#function-3:原for循環(huán)語句中加入判斷和循環(huán)計數(shù)方式,統(tǒng)計active和standby的數(shù)量
dsk=`ls /dev/sd* | grep -Po 'sd(a{2}|[a-z]+)$'`
echo `date +%c`
standby=0
active=0
unknown=0
for i in $dsk;
do
echo -e "\n";
echo -e "-----------------------";
echo -n "/dev/$i : " ;
stats=`smartctl -i -n standby /dev/$i|grep "mode"|awk '{print $4}' `;
for s in $stats;
do
if [ $s == STANDBY ]
then
echo -e "\033[30;42m STANDBY \033[0m";
let standby=$standby+1
elif [ $s == ACTIVE ]
then
echo -e "\033[37;41m ACTIVE \033[0m";
let active=$active+1
else
echo -e "\033[30;43m UNKNOWN \033[0m";
let unknown=$unknown+1
fi
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $7}' `;
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $4}' `;
done
done
echo -e "\n";
echo -e "\033[37;41mActive Disk in Total=$active \033[0m";
echo -e "\033[30;42mStandby Disk in Total=$standby \033[0m";
echo -e "\033[30;47mUnknown Disk in Total=$unknown \033[0m";
echo -e "\n";
exit
v5
增加功能 4
#!/bin/bash
#function-1:顯示硬盤盤符+Lable+容量以及硬盤上電狀態(tài)
#function-2:通過echo -e轉(zhuǎn)義與if...elif...else語句使其active呈現(xiàn)紅色,standby呈現(xiàn)綠色,方便快速檢視硬盤上電情況
#function-3:原for循環(huán)語句中加入判斷和循環(huán)計數(shù)方式,統(tǒng)計active和standby的數(shù)量
#function-4:增加 if 嵌套語句判斷除開 active 和 standby 之外狀態(tài)的硬盤踪旷,并將其標記為 unknown
dsk=`ls /dev/sd* | grep -Po 'sd(a{2}|[a-z]+)$'`
echo `date +%c`
standby=0
active=0
unknown=0
for i in $dsk;
do
echo -e "\n";
echo -e "-----------------------";
echo -n "/dev/$i : " ;
stats=`smartctl -i -n standby /dev/$i|grep "mode"|awk '{print $4}' `;
#echo $stats
if [[ $stats == STANDBY ]]||[[ $stats == ACTIVE ]]||[[ $stats == IDLE_A ]]
then
for s in $stats;
do
if [ $s == STANDBY ]
then
echo -e "\033[30;42m STANDBY \033[0m"
let standby=$standby+1
else
echo -e "\033[37;41m ACTIVE \033[0m"
let active=$active+1
fi
done
else
echo -e "\033[30;47m UNKNOWN \033[0m"
let unknown=$unknown+1
fi
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $7}' `;
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $4}' `;
done
echo -e "\n";
echo -e "\033[37;41mActive Disk in Total=$active \033[0m";
echo -e "\033[30;42mStandby Disk in Total=$standby \033[0m";
echo -e "\033[30;47mUnknown Disk in Total=$unknown \033[0m";
echo -e "\n";
exit
v6
#!/bin/bash
#function-1:顯示硬盤盤符+Lable+容量以及硬盤上電狀態(tài)
#function-2:通過echo -e轉(zhuǎn)義與if...elif...else語句使其active呈現(xiàn)紅色,standby呈現(xiàn)綠色,方便快速檢視硬盤上電情況
#function-3:原for循環(huán)語句中加入判斷和循環(huán)計數(shù)方式,統(tǒng)計active和standby的數(shù)量
#function-4:增加 if 嵌套語句判斷除開 active 和 standby 之外狀態(tài)的硬盤曼氛,并將其標記為 unknown
#function-5:將unknown狀態(tài)的硬盤列舉出來(對for語句上癮了……
dsk=`ls /dev/sd* | grep -Po 'sd(a{2}|[a-z]+)$'`
echo `date +%c`
standby=0
active=0
unknown=0
a=0
b=0
c=0
for i in $dsk;
do
echo -e "\n";
echo -e "-----------------------";
echo -n "/dev/$i : " ;
stats=`smartctl -i -n standby /dev/$i|grep "mode"|awk '{print $4}' `;
#echo $stats
if [[ $stats == STANDBY ]]||[[ $stats == ACTIVE ]]||[[ $stats == IDLE_A ]]
then
for s in $stats;
do
if [ $s == STANDBY ]
then
echo -e "\033[30;42m STANDBY \033[0m"
let standby=$standby+1
else
echo -e "\033[37;41m ACTIVE \033[0m"
let active=$active+1
fi
done
else
echo -e "\033[30;47m UNKNOWN \033[0m"
let unknown=$unknown+1
for un in $i
do
list[c]=$un
((c++))
done
fi
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $7}' `;
echo `lsblk /dev/$i|grep "/srv/dev-disk-by-label-"|awk '{print $4}' `;
done
echo -e "\n";
echo -e "\033[37;41mActive Disk in Total=$active \033[0m";
echo -e "\033[30;42mStandby Disk in Total=$standby \033[0m";
echo -e "\033[30;47mUnknown Disk in Total=$unknown \033[0m";
#echo $c
echo -e "Unknown Disk list: ";
for((b=0;b<=$c;b++));
do
if [[ $b -lt $c ]]
then
echo ${list[b]}
fi
done
echo -e "\n";
exit