bundlewrap/bundles/telegraf-battery-usage/files/telegraf-plugin-battery-usage

34 lines
1,011 B
Text
Raw Permalink Normal View History

2021-11-15 20:18:42 +00:00
#!/usr/bin/env python3
from os import listdir
from os.path import join
from sys import exit, stderr
try:
for power_supply in listdir('/sys/class/power_supply/'):
if power_supply.startswith('BAT'):
out = {}
for measurement in {
'capacity',
'cycle_count',
'energy_full',
'energy_full_design',
'energy_now',
'power_now',
'voltage_now',
}:
try:
with open(join('/sys/class/power_supply', power_supply, measurement)) as f:
out[measurement] = f.read().splitlines()[0].strip()
except Exception as e:
print(repr(e), file=stderr)
print('battery,name={} {}'.format(
power_supply,
','.join([f'{k}={v}' for k, v in sorted(out.items())]),
))
except Exception as e:
print(repr(e), file=stderr)
exit(1)