Created
April 21, 2017 11:13
-
-
Save furkanmustafa/d905c0e6cca97329db3ede4ce085f934 to your computer and use it in GitHub Desktop.
Linux Battery and Power Usage Monitoring Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
import os | |
def readfile(path): | |
f = open(path, 'r') | |
if not f: | |
return false | |
val = f.read() | |
f.close() | |
return val | |
status = readfile('/sys/class/power_supply/BAT0/status').rstrip('\n'); | |
if os.path.isfile('/sys/class/power_supply/BAT0/power_now'): | |
power = readfile('/sys/class/power_supply/BAT0/power_now') | |
power = float(power) / 1000000 | |
elif os.path.isfile('/sys/class/power_supply/BAT0/current_now') and os.path.isfile('/sys/class/power_supply/BAT0/voltage_now'): | |
current = readfile('/sys/class/power_supply/BAT0/current_now') | |
current = float(current) / 1000000 | |
voltage = readfile('/sys/class/power_supply/BAT0/voltage_now') | |
voltage = float(voltage) / 1000000 | |
power = current * voltage | |
else: | |
print("Not Supported") | |
exit(1) | |
charge_now = float(readfile('/sys/class/power_supply/BAT0/charge_now')) | |
charge_full = float(readfile('/sys/class/power_supply/BAT0/charge_full')) | |
charge_full_design = float(readfile('/sys/class/power_supply/BAT0/charge_full_design')) | |
charge_percent = round((charge_now / charge_full) * 100) | |
charge_percent_design = round((charge_now / charge_full_design) * 100) | |
print status + ': ' + str(round(power, 2)) + " W (" + str(charge_percent) + "%) design(" + str(charge_percent_design) + "%) " + str(round(charge_now / 100000, 1)) + " Wh remaining" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment