Thought I’d share this little script. Basically what I needed to do was build a way to track what hard drives were connected to my linux raid arrays along with their serial numbers and temperatures. This script requires hdparm and hddtemp which are both readily available on most distros.

#!/bin/bash

# set LANG for encoding celcius symbol, UTF-8 screws it up
LANG=en_US
IFS=$’\n’
for file in /dev/md[0-9]*; do
VAL=`mdadm -D $file`;
for line in $VAL; do
if [[ $line =~ \/dev\/(h|s)d[a-z][0-9] ]] ; then
DISK=`echo $line | awk ‘{ print $7 }’`
TEMP=`hddtemp $DISK | awk -F’: ‘ ‘{ print $3 }’`
SERIAL=`hdparm -I $DISK | grep “Serial Number” | awk ‘{ print $3 }’`
SPECLINE=”${DISK} ${SERIAL} ${TEMP}”
echo $line | sed “s;$DISK;$SPECLINE;”
else
echo $line
fi
done

for disk in `echo $VAL | grep ‘^[ ]*[0-9]’ | awk ‘{ print $7 }’`; do
echo $disk;
done
done

— sample output —

/dev/md1:
Version : 1.2
Creation Time : Tue Nov 30 15:12:06 2010
Raid Level : raid5
Array Size : 5860552704 (5589.06 GiB 6001.21 GB)
Used Dev Size : 976758784 (931.51 GiB 1000.20 GB)
Raid Devices : 7
Total Devices : 7
Persistence : Superblock is persistent
Update Time : Fri Dec 17 01:58:35 2010
State : clean
Active Devices : 7
Working Devices : 7
Failed Devices : 0
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 128K
Name : skyn:bigstore  (local to host skyn)
UUID : 77df4227:2c4c30e8:93438ed9:49939563
Events : 60568
Number   Major   Minor   RaidDevice State
0       8       97        0      active sync   /dev/sdg1 WD-WMATV8578493 36°C
1       8      145        1      active sync   /dev/sdj1 WD-WMATV7948948 34°C
2       8      209        2      active sync   /dev/sdn1 WD-WMATV7901255 36°C
3       8       81        3      active sync   /dev/sdf1 WD-WMATV7875948 37°C
4       8      161        4      active sync   /dev/sdk1 WD-WMATV8049056 37°C
5       8       33        5      active sync   /dev/sdc1 WD-WMATV8516087 34°C
7       8        1        6      active sync   /dev/sda1 WD-WMATV7450752 34°C