diff options
-rwxr-xr-x | text_collector_examples/smartmon.py | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/text_collector_examples/smartmon.py b/text_collector_examples/smartmon.py index 4eb1075..7dbf26e 100755 --- a/text_collector_examples/smartmon.py +++ b/text_collector_examples/smartmon.py | |||
@@ -5,14 +5,16 @@ import csv | |||
5 | import datetime | 5 | import datetime |
6 | import decimal | 6 | import decimal |
7 | import re | 7 | import re |
8 | import subprocess | ||
9 | import shlex | 8 | import shlex |
9 | import subprocess | ||
10 | 10 | ||
11 | device_info_re = re.compile(r'^(?P<k>[^:]+?)(?:(?:\sis|):)\s*(?P<v>.*)$') | 11 | device_info_re = re.compile(r'^(?P<k>[^:]+?)(?:(?:\sis|):)\s*(?P<v>.*)$') |
12 | 12 | ||
13 | ata_error_count_re = re.compile( | 13 | ata_error_count_re = re.compile( |
14 | r'^Error (\d+) \[\d+\] occurred', re.MULTILINE) | 14 | r'^Error (\d+) \[\d+\] occurred', re.MULTILINE) |
15 | 15 | ||
16 | self_test_re = re.compile(r'^SMART.*(PASSED|OK)$', re.MULTILINE) | ||
17 | |||
16 | device_info_map = { | 18 | device_info_map = { |
17 | 'Vendor': 'vendor', | 19 | 'Vendor': 'vendor', |
18 | 'Product': 'product', | 20 | 'Product': 'product', |
@@ -119,10 +121,12 @@ def smart_ctl(*args, check=True): | |||
119 | Returns: | 121 | Returns: |
120 | (str) Data piped to stdout by the smartctl subprocess. | 122 | (str) Data piped to stdout by the smartctl subprocess. |
121 | """ | 123 | """ |
122 | return subprocess.run( | 124 | try: |
123 | ['smartctl', *args], stdout=subprocess.PIPE, check=check, | 125 | return subprocess.run( |
124 | ).stdout.decode('utf-8') | 126 | ['smartctl', *args], stdout=subprocess.PIPE, check=check |
125 | 127 | ).stdout.decode('utf-8') | |
128 | except subprocess.CalledProcessError as e: | ||
129 | return e.output.decode('utf-8') | ||
126 | 130 | ||
127 | def smart_ctl_version(): | 131 | def smart_ctl_version(): |
128 | return smart_ctl('-V').split('\n')[0].split()[1] | 132 | return smart_ctl('-V').split('\n')[0].split()[1] |
@@ -237,12 +241,12 @@ def collect_device_health_self_assessment(device): | |||
237 | Yields: | 241 | Yields: |
238 | (Metric) Device health self assessment. | 242 | (Metric) Device health self assessment. |
239 | """ | 243 | """ |
240 | out = smart_ctl( | 244 | out = smart_ctl('--health', *device.smartctl_select()) |
241 | '--health', *device.smartctl_select() | ||
242 | ).strip().split('\n') | ||
243 | 245 | ||
244 | self_assessment_passed = \ | 246 | if self_test_re.search(out): |
245 | out[4].endswith('PASSED') or out[4].endswith('OK') | 247 | self_assessment_passed = True |
248 | else: | ||
249 | self_assessment_passed = False | ||
246 | 250 | ||
247 | yield Metric( | 251 | yield Metric( |
248 | 'device_smart_healthy', device.base_labels, self_assessment_passed) | 252 | 'device_smart_healthy', device.base_labels, self_assessment_passed) |
@@ -372,4 +376,3 @@ def main(): | |||
372 | 376 | ||
373 | if __name__ == '__main__': | 377 | if __name__ == '__main__': |
374 | main() | 378 | main() |
375 | |||