Linux下服務(wù)器做了硬件raid之后,磁盤的狀態(tài)比較難定位,windows則可以通過MegaRAID來檢測,此腳本通過MegaCli來達到定位raid下哪塊磁盤是壞塊的功能,在nagios上面可以實現(xiàn)通過定期通過檢測以郵箱或者短信等形式,來達到預(yù)警的功能,腳本在幾臺物理機上面測試過,是沒問題的,分享給各位,也希望大家能相互討論,學(xué)習(xí)。
會寧ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
一、安裝Megacli:
rpm-ivh megacli-8.00.46-2.x86_64.rpm
二、添加腳本到nagios監(jiān)控:
執(zhí)行visudo,然后在文件中root ALL=(ALL) ALL下面加入如下一行:
nagios ALL=(ALL)NOPASSWD:/usr/local/nagios/libexec/check_raid.sh
并注釋以下一行
#Defaults requiretty
把腳本放在/usr/local/nagios/libexec目錄下,chmod +x check_raid.sh,賦予x權(quán)限,并編輯/usr/local/nagios/etc/nrpe.cfg加入
command[check_raid]=/usr/bin/sudo/usr/local/nagios/libexec/check_raid.sh
重啟nrpe(根據(jù)安裝方式的不同,可能有差異)
#pkill nrpe #/usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg -d
三、監(jiān)控腳本說明:
#!/bin/sh #Program: # for monitor raid disk state #history: #------ First release #檢測是否是LSI卡 rcexist=`dmesg| grep RAID | grep LSI` if [ ! -n"$rcexist" ]; then echo "not LSI or no raid" exit 2 fi OUTPUT='' #判斷raid類型 R1=`/usr/sbin/MegaCli-cfgdsply -aALL | grep "RAID Level" |awk -F: '{print $2}' | sed -e"s/^[ ]*//" | grep -c "Primary-1, Secondary-0, RAID LevelQualifier-0"` R0=`/usr/sbin/MegaCli-cfgdsply -aALL | grep "RAID Level" |awk -F: '{print $2}' | sed -e"s/^[ ]*//" | grep -c "Primary-0, Secondary-0, RAID LevelQualifier-0"` R5=`/usr/sbin/MegaCli-cfgdsply -aALL | grep "RAID Level" |awk -F: '{print $2}' | sed -e"s/^[ ]*//" | grep -c "Primary-5, Secondary-0, RAID LevelQualifier-3"` R10=`/usr/sbin/MegaCli-cfgdsply -aALL | grep "RAID Level" |awk -F: '{print $2}' | sed -e"s/^[ ]*//" | grep -c "Primary-1, Secondary-3, RAID LevelQualifier-0"` if [ $R1-ge 2 ];then OUTPUT+="RAID10 " elif [ $R1-eq 1 ];then OUTPUT+="RAID1 " fi if [ $R0-ne 0 ];then OUTPUT+="RAID0 " fi if [ $R5-ne 0 ];then OUTPUT+="RAID5 " fi if [ $R10-ne 0 ];then OUTPUT+="RAID10 " fi #以上的if是根據(jù)資料和實際情況做了微調(diào) #raid下面總的磁盤數(shù) DiskNum=`/usr/sbin/MegaCli-cfgdsply -aALL | grep -c "Non Coerced Size"` OUTPUT+="TotalDisk:$DiskNum" #處于raid中的正常的盤數(shù) OnlineDisk=`/usr/sbin/MegaCli-cfgdsply -aALL | grep "Online" | wc -l` OUTPUT+="online: $OnlineDisk" if [$DiskNum -ne $OnlineDisk ];then echo "CRITICAL:$OUTPUT" exit 2 fi #是否有壞的盤 FailDisk=`/usr/sbin/MegaCli-AdpAllInfo -aALL | grep "Failed Disks" | awk '{print $4}'` if [$FailDisk -eq 0 ];then OUTPUT+=" failed disk:0 " else OUTPUT+=" failed disk:$FailDisk" echo "CRITICAL: $OUTPUT" exit 2 fi #預(yù)警的盤以及位置 CriticalDisk=`/usr/sbin/MegaCli-AdpAllInfo -aALL | grep "Critical Disks" | awk '{print $4}'` if [$CriticalDisk -eq 0 ];then OUTPUT+="critiDisk is 0" else CriDisk=`/usr/sbin/MegaCli -cfgdsply -aALL| grep -E 'Predictive|Slot' | awk \ '{if(NR%3){printf$0":"}else{print $0}}'|awk -F':' '{if($4!=0){print $2+1}}'` OUTPUT+=" critidisk in $CriDiskslot" echo "WARNING: $OUTPUT" exit 1 fi #MediaErrcount檢測壞塊和哪塊盤 MediaErrcount=`/usr/sbin/MegaCli-pdlist -aALL | grep -E "Media Error" |awk -F’:’ -v errcount=0 \ '{errcount+=$2}END{printerrcount}'` OtherErrcount=`/usr/sbin/MegaCli-pdlist -aALL | grep -E "Other Error" |awk -F’:’ -v errcount=0 \ '{errcount+=$2}END{printerrcount}'` #壞盤的位置 if [ $MediaErrcount-ne 0 -o $OtherErrcount -ne 0 ];then mDoD=`/usr/sbin/MegaCli -pdlist -aALL |grep -E "Media Error|Other Error|Slot" | awk \ '{if(NR%3){printf$0":"}else{print $0}}' | awk -F':' '{if($4!=0||$6!=0){print $2+1}}'` OUTPUT+=" bad block in $mDoD" echo "CRITICAL: $OUTPUT" exit 2 else OUTPUT+=" mediaerr:0 othererr:0" fi #raid狀態(tài)是否正常 raidstate=`/usr/sbin/MegaCli-LDInfo -Lall -aAll | grep 'State' |awk -F':' '{print $2}' | \ sort |uniq | sed -e "s/^[ ]*//" | awk '{if($0 != "Optimal"){print"bad"}}'` if ["$raidstate" != "bad" ];then OUTPUT+=" raidstate:ok" else OUTPUT+=" raidstate:bad" echo "CRITICAL: $OUTPUT" exit 2 fi rm -rf./MegaSAS.log echo$OUTPUT
檢測結(jié)果如下:
RAID5 Total Disk: 4 online: 4 failed disk:0 critidisk is 0 mediaerr:0 othererr:0 raidstate:ok