-
Notifications
You must be signed in to change notification settings - Fork 0
/
pydrivehealth.py
executable file
·90 lines (68 loc) · 2.75 KB
/
pydrivehealth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
from __future__ import print_function
import pySMART
import tabulate
import os
import sys
import pwd
import re
import argparse
def get_smart_attribute(drive, attribute):
for driveattribute in drive.attributes:
if not driveattribute:
continue
if driveattribute.name == attribute:
return driveattribute.raw
def get_drive_age(drive):
poweronhours = get_smart_attribute(drive, 'Power_On_Hours')
hours = re.split('h', str(poweronhours), 1)[0]
if hours == "None":
return
return str('%8.2f' % (int(hours) / 24 / 365)) + ' years'
def get_drive_temperature(drive):
temp = get_smart_attribute(drive, 'Temperature_Celsius')
if temp:
temp = str(temp) + 'C'
return temp
def get_drive_reallocated_sectors(drive):
return get_smart_attribute(drive, 'Reallocated_Sector_Ct')
def display_drive_information(devlist, verbose):
header = ['Name', 'Health', 'Model', 'Capacity', 'Temperature', 'Age (on)', 'Reallocated Sectors']
if verbose:
header = ['Name', 'Health', 'Model', 'Serial', 'Capacity', 'Temperature', 'Age (on)', 'Reallocated Sectors']
data = [ header ]
for device in devlist.devices:
data.append(get_drive_information(device, verbose))
print('{0} drives online'.format(len(devlist.devices)))
print(tabulate.tabulate(data, 'firstrow' , 'rst'))
def get_drive_information(drive, verbose):
'''
Arguments are:
* drive, a pySMART devicelist object, and
* verbose, a boolean flag that increases the number of disk attributes this function returns
Returns an array of
'''
temp = get_drive_temperature(drive)
age = get_drive_age(drive)
reallocated_sectors = get_drive_reallocated_sectors(drive)
if verbose:
data = ( drive.name, drive.assessment, drive.model, drive.serial, drive.capacity, temp, age, reallocated_sectors )
# data = [ drive.name, 'PASS', 'XXXXXXXXX', 'XXXXXXXXXXXXXX', drive.capacity, temp, age, reallocated_sectors ]
else:
data = ( drive.name, drive.assessment, drive.model, drive.capacity, temp, age, reallocated_sectors )
# data = [ drive.name, 'PASS', '4.00 TB' , temp, age, reallocated_sectors ]
return data
def main():
'''
Ensure this script is run as root, so that it can query disk information.
Parse arguments and then display information on the disk drives in the system
'''
if os.getuid() != 0:
raise RuntimeError('Run this script as root, as it needs to query disks.\nsudo {0}'.format(sys.argv[0]))
parser = argparse.ArgumentParser(description='A script to display disk attributes')
parser.add_argument('-v', '--verbose', action='store_true', help='Show verbose disk attributes view')
args = parser.parse_args()
devlist = pySMART.DeviceList()
display_drive_information(devlist, args.verbose)
if __name__ == '__main__':
main()