Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SMART info parser
#!/usr/bin/python
# encoding: utf-8
import subprocess
import os
import re
print "Storage status:"
for node in os.listdir("/dev"):
if not re.match("[sv]d[a-z]$", node): continue
cmd = "/usr/sbin/smartctl", "-a", "/dev/%s" % node
head_parks = None
for line in subprocess.check_output(cmd).split("\n"):
if line.startswith("194 "):
_, temp = line.rsplit(" ", 1)
temp = int(temp)
if line.startswith("190 "):
_, temp = line.rsplit(" ", 1)
temp = int(temp)
if line.startswith(" 5 "):
reallocated_sectors = int(line.rsplit(" ", 1)[1])
if line.startswith(" 9 "):
_, j = line.rsplit(" ", 1)
lifetime_days = int(j) / 24.0
lifetime_years = 0
while lifetime_days > 365:
lifetime_years += 1
lifetime_days -= 365
if lifetime_years:
lifetime = "%d years, %d days," % (lifetime_years, lifetime_days)
else:
lifetime = "%d days," % lifetime_days
if line.startswith("193 "):
head_parks = int(line.rsplit(" ", 1)[1])
if head_parks > 1000:
head_parks = "%dk" % (head_parks / 1000.0)
else:
head_parks = str(head_parks)
if line.startswith(" 12 "):
power_cycles = int(line.rsplit(" ", 1)[1])
if line.startswith("Device Model:"):
device_model = line.split(":", 1)[1].strip()
if line.startswith("Serial Number:"):
serial_number = line.split(":", 1)[1].strip()
if line.startswith("241 "):
tbs_written = int(line.rsplit(" ", 1)[1]) * 512 / 2**40
device_name = "%s (%s)" % (device_model, serial_number)
if head_parks:
print "* %-60s %18s %d°C, %3d power cycles, %s head parks" % (
device_name, lifetime, temp, power_cycles, head_parks),
else:
print "* %-60s %18s %d°C, %3d power cycles, %dTB written" % (
device_name, lifetime, temp, power_cycles, tbs_written),
if reallocated_sectors:
print reallocated_sectors, "sectors reallocated"
else:
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.