Telegraf Laptop Battery Plugin
Wanted to expand a little on my previous blog post Custom Telegraf Plugin , and decided to do a simple battery monitor. The end result looks something like this:
I decided to read from the file /sys/class/power_supply/BAT0/capacity on my Ubuntu 14.04 machine, this file merely shows the current battery percent:
# cat /sys/class/power_supply/BAT0/capacity
62
All that is needed is a little Python script for converting this output to JSON, my script outputs like this:
# /battery.py
{"status": "Charging", "capacity": 63}
The code is very basic Python: /battery.py
#!/usr/bin/env python
import json
with open('/sys/class/power_supply/BAT0/capacity') as f:
capacity = f.read().split('\n')[0]
capacity = int(capacity)
with open('/sys/class/power_supply/BAT0/status') as f:
status = f.read().split('\n')[0]
results = dict(capacity=capacity,
status=status)
print json.dumps(results)
All that is left is to add this script to our Telegraf configuration directory:
# cat /etc/telegraf/telegraf.d/battery.conf
[[inputs.exec]]
command = "/battery.py"
data_format = "json"
name_suffix = "_battery"
interval = "5s"
And there you have it, a simple battery tracker.