Skip to content

Instantly share code, notes, and snippets.

@shreyaspotnis
Last active January 3, 2016 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shreyaspotnis/8495061 to your computer and use it in GitHub Desktop.
Save shreyaspotnis/8495061 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Simple script to monitor battery status, CPU temperature and screen brightness.
# Opens file specified in filename and appends one line of data when the script
# is run.
#
# Usage -
# 1) change the variable filename to whatever file you want to append your log to.
# 2) make this script executable with chmod +x battery_monitor.py
# 3) append it to your crontab using crontab -e. My crontab entry looks like:
# */1 * * * * /path/to/battery_monitor.py
# This runs the script once every minute.
# Note: This script runs fine on my x230 laptop running archlinux. You most
# definitely will have to tinker with it to get it running on yours.
import subprocess as sub
import re
from datetime import datetime
filename = '/path/to/log/file'
def parseSpaces(commandList):
p = sub.Popen(commandList, stdout=sub.PIPE, stderr=sub.PIPE)
output, errors = p.communicate()
return re.split(' ', str(output))
def getBrightness():
actual = '/sys/class/backlight/intel_backlight/actual_brightness'
maxBrightness = '/sys/class/backlight/intel_backlight/max_brightness'
ac = int(open(actual, 'r').read())
maxb = int(open(maxBrightness, 'r').read())
return int((ac*100)/maxb)
parsed = parseSpaces(['acpi', '-bi'])
if parsed[2][:-1] == 'Discharging':
log = [str(datetime.now()), # datetime
parsed[2][:-1], # chargingStatus
parsed[3][:-1], # charge remaiting
parsed[4], # time remaining
parsed[14], # list full charge
parsed[17][:-3]] # max capacity
else:
log = [str(datetime.now()), # datetime
parsed[2][:-1], # chargingStatus
parsed[3][:-1], # charge remaiting
parsed[4], # time remaining
'-', # list full charge
'-'] # max capacity
parsed = parseSpaces(['acpi', '-t'])
log.append(parsed[3]) # CPU temperature
log.append(str(getBrightness())) # backlight brightness
with open(filename, 'a') as fp:
fp.write('\t'.join(log))
fp.write('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment