Skip to content

Instantly share code, notes, and snippets.

@hapylestat
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save hapylestat/665ef0ea94a103154f2d to your computer and use it in GitHub Desktop.

Select an option

Save hapylestat/665ef0ea94a103154f2d to your computer and use it in GitHub Desktop.
Get a list of hard_disks with their health status (smartctl required)
#!/bin/bash
COLOR_BEGIN="\033["
COLOR_RED="${COLOR_BEGIN}0;31m"
COLOR_ITEM="${COLOR_BEGIN}1;38;05;214m"
COLOR_VALUE="${COLOR_BEGIN}1;38;05;110m"
COLOR_END="\033[0m"
mypath=/sys/block
temp=/tmp/hddlist.tmp
drives=$(ls $mypath|grep s)
print_device(){
echo -en ${COLOR_RED}Device${COLOR_END}: ${COLOR_VALUE}$1${COLOR_END}
}
print_inline_property(){
echo -en " "${COLOR_ITEM}$1:${COLOR_END} ${COLOR_VALUE}$2${COLOR_END}
}
print_property(){
echo -e " "${COLOR_ITEM}$1:${COLOR_END} ${COLOR_VALUE}$2${COLOR_END}
}
for item in $drives; do
model=$(cat $mypath/$item/device/model)
size=$(cat $mypath/$item/device/block/$item/size)
block_size=$(cat $mypath/$item/device/block/$item/queue/hw_sector_size)
h_size=$(( ((($size*$block_size) / 1024) / 1024) /1024 ))
smartctl -x /dev/$item >$temp
serial=$(cat $temp|grep "Serial Number"|cut -d ":" -f 2)
family=$(cat $temp |grep "Model Family"|cut -d ":" -f 2)
temperature=$(cat $temp|grep "Current Temperature"|cut -d ":" -f 2)
reallocation_count=$(cat $temp|grep "Reallocated_Event_Count"|awk {"print \$8 }")
if [ "$reallocation_count" == "" ]; then
reallocation_count=$(cat $temp|grep "Reallocated_Sector_Ct"|awk {"print \$8 }")
fi
poweron=$(cat $temp|grep "Power_On_Hours"|awk {"print \$8"})
port_path=$(readlink -s "$mypath/$item")
port_pos=$(expr match "$port_path" ".*ata")
port=ata$(echo ${port_path:$port_pos}|cut -d "/" -f 1)
port_speed=$(cat /sys/class/ata_link/link${port:3}/sata_spd)
offline_uncorrectable=$(cat $temp|grep "Offline_Uncorrectable"|awk {"print \$8 }")
current_pending=$(cat $temp|grep "Current_Pending"|awk {"print \$8 }")
print_device "$item"; print_property "Model/Family/Serial" "$model /$family /$serial"
print_property "Size" "$(($h_size/1024)).$(($h_size%1024 )) Tb"
print_property "Power On" "$poweron hrs/$(($poweron/24)) days/~$((($poweron/24)/365)).$((($poweron/24)%365)) years"
print_inline_property "Port" "$port"; print_property "Port speed" "$port_speed"
print_inline_property "Reallocations/Pending/Uncorrectable" "$reallocation_count/$current_pending/$offline_uncorrectable";
print_property "Temperature" "$temperature"
echo ""
done
rm -f $temp
@hapylestat

Copy link
Copy Markdown
Author

Will look like:

Device: sda       Model/Family/Serial: WDC WD30EZRS-00J / Western Digital Caviar Green (AF) / WD-WCAWZ0364150
       Size: 2.746 Tb
       Power On: 33350 hrs/1389 days/~3.294 years
       Port: ata1       Port speed: 3.0 Gbps
       Reallocations/Pending/Uncorrectable: 0/1/0       Temperature:  27 Celsius


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment