Obtaining key attributes and Health of HDD and SSD using smartmontools

Obtaining key attributes and Health of HDD and SSD using smartmontools

S.M.A.R.T. (Self-Monitoring, Analysis and Reporting Technology; written as SMART) SMART is a system for monitoring and early detection of errors of storage media such as hard disks or SSDs.
 All current hard drives and SSDs have SMART functionality. However, the data provided by SMART, (SMART attributes), are different from manufacturer to manufacturer.

NOTE:Run IDE as administrator and SMARTMONTOOLS should be installed in the system

This python script displays the HDD and SSD health status and also its key attributes
import os #device record class DeviceRecord: def __init__(self): self.deviceHealth = None self.deviceAttributes = None class SMART: #to run a smartctl command def RunSmartCtl(self, strArgs): #get the command with strArgs as the argument cmdString = "smartctl " + strArgs #get command output and store each line in a list output = os.popen(cmdString).read() lines = str.splitlines(output) return lines #get device health def GetDeviceHealth(self, deviceId): deviceInfoLines = self.RunSmartCtl("-H " + deviceId) return deviceInfoLines[-2] #get device attributes def GetDeviceAttributes(self, deviceId): deviceInfoLines = self.RunSmartCtl("-A " + deviceId) for i in range(len(deviceInfoLines)): if deviceInfoLines[i].startswith('ID#'): index = i+1 break infoDict = dict() for i in range(index, len(deviceInfoLines)-1): x = deviceInfoLines[i].strip().split() infoDict[x[0]] = x[0:len(x)] return infoDict if __name__ == '__main__': smart_features = SMART() deviceid = input("Enter device id: ") #fetchig disk health devHealth = smart_features.GetDeviceHealth(deviceid) #printing device health information print("Disk Health Information") print(devHealth) #fetching attributes details devAttr=smart_features.GetDeviceAttributes(deviceid) #printing attributes information print("Disk attribute Informaion") print("ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH") for attributes in devAttr: print('\t'.join(devAttr[attributes][:6]))