Monitoring Hard Disks with SMART

I have quite a few hard disks running at all times. I came across this article this weekend and whipped up a little something to keep an eye on them.
import subprocess
import os

disk_uuids = [entry.split()[0].split('=')[1] for entry in open('/etc/fstab')
if entry.startswith('UUID=')]

disk_devices = []
for uuid in disk_uuids:
uuid_path = '/dev/disk/by-uuid/' + uuid
try:
dev = os.readlink(uuid_path)
dev = os.path.join(os.path.dirname(uuid_path), dev)
dev = os.path.abspath(dev)
except OSError:
print '%s does not exist.' % uuid_path
continue
disk_devices.append(dev)

disk_health = {}
for disk in disk_devices:
p = subprocess.Popen(['smartctl', '-Hc', disk], stdout=subprocess.PIPE)
for line in p.stdout.readlines():
if line.startswith('SMART overall-health self-assessment test result:'):
health = line.split()[-1]
disk_health[disk] = health
break

for disk, health in disk_health.iteritems():
print '%-12.12s%s' % (disk, health)

No comments:

Post a Comment