Skip to content

Instantly share code, notes, and snippets.

@tommybutler
Last active December 29, 2023 11:52
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save tommybutler/7592005 to your computer and use it in GitHub Desktop.
Save tommybutler/7592005 to your computer and use it in GitHub Desktop.
Script to quickly scan the S.M.A.R.T. health status of all your hard drive devices in Linux (at least all the ones from /dev/sda to /dev/sdzz). You need smartctl installed on your system for this script to work, and your hard drives need to have S.M.A.R.T. capabilities (they probably do).
#!/bin/bash
# install the smartctl package first! (apt-get install smartctl)
if sudo true
then
true
else
echo 'Root privileges required'
exit 1
fi
for drive in /dev/sd[a-z] /dev/sd[a-z][a-z]
do
if [[ ! -e $drive ]]; then continue ; fi
echo -n "$drive "
smart=$(
sudo smartctl -H $drive 2>/dev/null |
grep '^SMART overall' |
awk '{ print $6 }'
)
[[ "$smart" == "" ]] && smart='unavailable'
echo "$smart"
done
@teezyyoxo
Copy link

this is awesome, thank you!

@tommybutler
Copy link
Author

tommybutler commented May 31, 2021 via email

@BloodBlight
Copy link

Switching to this will better support SAS disks:

      sudo smartctl -H $drive 2>/dev/null |
      grep '^SMART overall\|^SMART Health Status' |
      rev | cut -d ' ' -f1 | rev

@rickygm
Copy link

rickygm commented Mar 10, 2022

work gr8

@tommybutler
Copy link
Author

tommybutler commented Mar 10, 2022 via email

@dgyioulos
Copy link

How can this code be modified to exclude drives marked as "unavailable?

@tommybutler
Copy link
Author

tommybutler commented Dec 5, 2022 via email

@Jolly-Pirate
Copy link

Modify line 14 to include nvme:
for drive in /dev/sd[a-z] /dev/sd[a-z][a-z] /dev/nvme[0-9]n[0-9]

@tommybutler
Copy link
Author

tommybutler commented Jan 26, 2023 via email

@BloodBlight
Copy link

If anyone is interested, I ended up taking this a bit further. It lists all disks, supports outputting to JSON (I have a crontab job that sends the results to node-red and does alerting), but also supports showing the remaining life of a lot of SSDs (mostly enterprise):
https://github.com/BloodBlight/CephNotes/blob/main/SmartHealth

There is also a ListDisks script that shows a lot of details in one quick script:
https://github.com/BloodBlight/CephNotes/blob/main/ListDisks

@rickygm
Copy link

rickygm commented Jan 30, 2023

thank BloodBlight

@eightseventhreethree
Copy link

Why not just use lsblk

for disk in $(lsblk --json | jq -r '.blockdevices[].name'); do smartctl --all /dev/${disk}; don

@tommybutler
Copy link
Author

tommybutler commented Nov 4, 2023 via email

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