Skip to content

Instantly share code, notes, and snippets.

@nicolasnoble
Created October 26, 2022 16:41
Show Gist options
  • Save nicolasnoble/14b4482ee26238e55447bdfadd47e0f0 to your computer and use it in GitHub Desktop.
Save nicolasnoble/14b4482ee26238e55447bdfadd47e0f0 to your computer and use it in GitHub Desktop.
Script to easily display Linux raid S.M.A.R.T. status
#!/bin/bash
if [ -z "$1" ] ; then
dev=/dev/md0
else
dev=$1
fi
mdadm --detail $dev |
awk ' h == 1 { print $0; } /Number.*Major.*Minor.*RaidDevice.*State/ { h = 1; } ' |
tr \ \\n |
grep dev |
sed s/1// |
sort -u |
while read drv ; do
smartctl -a $drv |
sed 's/Serial.Number: *\(WD-\)\?\(.*\)/0 Serial_Number b c d e f g h \2/' |
sed 's/User.Capacity.*\[\(.*\)\..*/0 User_Capacity b c d e f g h \1/' |
awk ' /Serial.Number/ { print $0; } /User.Capacity/ { print $0; } /^$/ { h = 0; } h == 1 { print $0; } /ATTRIBUTE_NAME.*RAW_VALUE/ { h = 1; } ' |
while read a name b c d e f g h val ; do
echo $drv,$name,$val
if [ "$name" == "Power_On_Hours" ] ; then
echo $drv,Power_On_Days,$((val/24))
echo $drv,Power_On_Years,`echo "$val/8760" | bc -l`
fi
done
done |
sed s/.dev.// |
awk -F, '
!($1 in drives) { drives[$1]; }
!($2 in headers) { headers[$2]; }
{ vals[$1][$2] = $3; }
END {
printf("drive,");
for (drv in drives) {
printf("%s,", drv);
}
printf("\n");
for (hdr in headers) {
printf("%s,", hdr);
for (drv in drives) {
fmt = "%i,";
if (hdr == "Serial_Number") { fmt = "%s,"; }
if (hdr == "Power_On_Years") { fmt = "%0.2f,"; }
printf(fmt, vals[drv][hdr]);
}
printf("\n");
}
} ' |
sed 's/,$//' | column -t -s,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment