2022-08-07 08:16:07 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from json import loads
|
2023-02-05 16:30:58 +00:00
|
|
|
from subprocess import check_output
|
2022-08-07 08:16:07 +00:00
|
|
|
from sys import stderr
|
|
|
|
|
|
|
|
devices = check_output(['smartctl', '--scan']).decode().splitlines()
|
|
|
|
|
|
|
|
for device in devices:
|
|
|
|
device = device.split(' ')[0]
|
|
|
|
|
|
|
|
try:
|
|
|
|
json = loads(check_output(['smartctl', '-n', 'standby', '-A', '--json=c', device]))
|
|
|
|
|
|
|
|
telegraf_output = set()
|
|
|
|
|
|
|
|
if 'power_on_time' in json:
|
|
|
|
telegraf_output.add('power_on_hours={}'.format(json['power_on_time']['hours']))
|
|
|
|
|
|
|
|
if 'temperature' in json:
|
|
|
|
telegraf_output.add('temperature={}'.format(json['temperature']['current']))
|
|
|
|
|
|
|
|
print('smartd_stats,device={device} {values}'.format(
|
|
|
|
device=device,
|
|
|
|
values=','.join(sorted(telegraf_output)),
|
|
|
|
))
|
|
|
|
|
|
|
|
telegraf_output = set()
|
|
|
|
|
|
|
|
if 'nvme_smart_health_information_log' in json:
|
|
|
|
for k, v in json['nvme_smart_health_information_log'].items():
|
2023-07-13 19:41:52 +00:00
|
|
|
if not str(v).isdigit():
|
|
|
|
continue
|
|
|
|
|
2022-08-07 08:16:07 +00:00
|
|
|
telegraf_output.add(f'{k}={v}')
|
|
|
|
|
|
|
|
if 'ata_smart_attributes' in json:
|
|
|
|
for entry in json['ata_smart_attributes']['table']:
|
2023-07-13 19:41:52 +00:00
|
|
|
if not str(entry['raw']['value']).isdigit():
|
|
|
|
continue
|
|
|
|
|
2022-08-07 08:16:07 +00:00
|
|
|
telegraf_output.add('{}={}'.format(
|
|
|
|
entry['name'],
|
|
|
|
entry['raw']['value'],
|
|
|
|
))
|
|
|
|
|
|
|
|
print('smartd_health,device={device},type={type} {values}'.format(
|
|
|
|
device=device,
|
|
|
|
type=json['device']['type'],
|
|
|
|
values=','.join(sorted(telegraf_output)),
|
|
|
|
))
|
|
|
|
except Exception as e:
|
|
|
|
print(f'{device} {repr(e)}', file=stderr)
|