This repository was archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsmart.py
More file actions
executable file
·74 lines (56 loc) · 1.53 KB
/
smart.py
File metadata and controls
executable file
·74 lines (56 loc) · 1.53 KB
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
#!/usr/bin/env python3
"""Description: Allow Zabbix to monitor smartvalues.
Author: Olivier van der Toorn <oliviervdtoorn@gmail.com>
"""
import sys
import subprocess
import re
def smartvalues(disk):
"""Retrieve the smartvalues for the given disk.
:param disk: disk to check output for
:type disk: str
:return: smartctl output
"""
command = ['sudo', 'smartctl', '-A', disk]
output = subprocess.getoutput(" ".join(command))
return output
def filter_output(attribute, output):
"""Filter the output, looking for the given attribute.
:param attribute: string to look for
:type attribute: str
:param output: output from smartctl
:type output: str
:return: line containing attribute
"""
for line in output.split('\n'):
if attribute in line:
break
else:
line = "0"
return line
def main(*args):
"""Main function calls all the other functions.
"""
if len(args) == 1:
disk = args[0]
return smartvalues(disk)
elif len(args) == 2:
disk = args[0]
attribute = args[1]
output = filter_output(
attribute, smartvalues(disk))
splitted_data = re.split(r'\s+', output.lstrip())
if len(splitted_data) >= 10:
value = int(splitted_data[9])
else:
value = 0
return value
return 0
def init():
"""Ensures 100% code coverage.
"""
if __name__ == '__main__':
value = main(*sys.argv[1:])
print(value)
return value
init()