Debian - How to monitor battery capacity

Recently battery in my notebook started to behave erratically. It will charge only to 70-80% of its maximum capacity. I suspect that it is probably slowly dying so I will have to replace it soon but at first I will look at a several of utilities to monitor battery capacity.

sysfs

You can easily get battery status using sysfs filesystem without any additional utilities.

$ ls /sys/class/power_supply/BAT0/
alarm               current_now   model_name     status      uevent
charge_full         cycle_count   power          subsystem   voltage_min_design
charge_full_design  device        present        technology  voltage_now
charge_now          manufacturer  serial_number  type
$ cat /sys/class/power_supply/BAT0/charge_full_design 
5856000
$ cat /sys/class/power_supply/BAT0/charge_full
4785000
$ cat /sys/class/power_supply/BAT0/charge_now 
2206000
$ cat /sys/class/power_supply/BAT0/status 
Charging

ACPI utilities

ACPI utilities will provide information in more user friendly form:

$ acpi -bi
Battery 0: Charging, 50%, 01:44:12 until charged
Battery 0: design capacity 5856 mAh, last full capacity 4785 mAh = 81%
$ acpitool -B
  Battery #1     : present
    Remaining capacity : unknown, 50.14%, 01:44:20
    Design capacity    : 5856 mA
    Last full capacity : 4785 mA, 81.71% of design capacity
    Capacity loss      : 18.29%
    Present rate       : 1372 mA
    Charging state     : Charging
    Battery type       : Li-ion 
    Model number       : Dell
    Serial number      : 2372

IBAM

Intelligent Battery Monitor uses statistical and adaptive linear methods to provide accurate estimations of minutes of battery left or of the time needed until full recharge.

To install ibam execute command:

$ sudo apt-get install ibam

To view graphs you need to install gnuplot-qt (KDE) or gnuplot-x11 package:

$ sudo apt-get install gnuplot-qt

Edit /etc/rc.local file as root and add commands:

#!/bin/sh -e

# IBAM for milosz desktop user
watch -n 60 su milosz -c ibam > /dev/null &

exit 0

After system reboot statistics will be collected every minute as milosz user so replace milosz with your username.

To monitor battery capacity execute command:

$ ibam -ra
Bios percentage:            58 %
Battery percentage:         66 %
Soft low percentage limit:  5 %
Charge percentage:          58 %
Bios time left:              1:28:32
Battery time left:           1:55:47
Adapted battery time left:   1:32:53
Charge time left:            1:24:51
Adapted charge time left:    1:45:46
Total battery time:          2:55:36
Adapted total battery time:  2:20:53
Total charge time:           3:23:28
Adapted total charge time:   4:13:37
Profile logging enabled.
Current file: /home/milosz/.ibam/profile-001-charge

To view battery graph execute command:

$ ibam -r --plot
Charge time left:            2:25:06
Adapted charge time left:    2:23:04

ibam

battery-stats

It didn't worked for me because of init_acpi_acadapt() returned NOT_SUPPORTED bug.

RRDtool

You can graph battery capacity (or even all available data) using rrdtool although it requires a little bit more research.

To install rrdtool execute command:

$ sudo apt-get install rrdtool

I will use ~milosz/.battery directory to store rrd file and shell scripts.

$ mkdir ~/.battery

Read and take note of the maximum capacity value as it will be used the next step.

$ cat /sys/class/power_supply/BAT0/charge_full_design 
5856000

Create rrd file to store battery statistics. Maximum battery charge value is 5856 mAh so I will use <0,6000> mAh as an acceptable range of values. Stored values: one per minute for the first day, one per three minutes for the first week, one per five minutes for the first month.

$ rdtool create ~/.battery/battery_capacity.rrd -s 60 DS:capacity:GAUGE:120:0:6000 \
   RRA:MAX:0.5:1:1440 \
   RRA:MAX:0.5:3:5040 \
   RRA:MAX:0.5:5:8928

Create ~/.battery/update_battery_charge.sh script as it will be used to read and store battery charge value. Modify rrd_file for the same reason as earlier.

#!/bin/sh
# Update battery charge level

rrd_file="/home/milosz/.battery/battery_capacity.rrd"

charge_now=`cat /sys/class/power_supply/BAT0/charge_now` 
charge_now=`expr ${charge_now} / 1000`

rrdtool update ${rrd_file} N:${charge_now}

Set executable bit:

$ chmod +x ~/.battery/battery_update_charge.sh

Edit /etc/rc.local file as root and replace milosz with your username. Statistics will be collected every minute as milosz user. Execute command directly or reboot system.

#!/bin/sh -e

# Update battery charge level rrd file for milosz desktop user
watch -n 60 su milosz /home/milosz/.battery/update_battery_charge.sh

exit 0

To create create nice looking graphs create ~/.battery/graph_battery_charge.sh script and modify rrd_file and destination variables.

#!/bin/sh                                                                                                                                                                                       
# Create "battery charge level" graphs for 1 day, 1 week and 1 month                                                                                                                            
                                                                                                                                                                                                
rrd_file="/home/milosz/.battery/battery_capacity.rrd"                                                                                                                                           
destination="/home/milosz/.battery/"                                                                                                                                                            
                                                                                                                                                                                                
for period in "1d" "1w" "1m"                                                                                                                                                                    
do                                                                                                                                                                                              
  /usr/bin/rrdtool graph - \                                                                                                                                                                    
    --imgformat=PNG -N  \                                                                                                                                                                       
    --start=-${period} \                                                                                                                                                                        
    --end=-60 \
    --title="Battery capacity - ${period}" \
    --rigid \
    --base=1000 \
    --full-size-mode \
    --height=210 \
    --width=590 \
    --upper-limit=6000 \
    --lower-limit=0 \
    --vertical-label="mAh" \
    --slope-mode \
    --border 0 \
    --color BACK#FFFFFF \
    --color GRID#FFFFFF \
    --color MGRID#FFFFFF \
    DEF:a=${rrd_file}:capacity:MAX \
    HRULE:5856#FF0000:"Maximum battery capacity" \
    CDEF:b=a,UN,PREV,a,IF \
    LINE:b#dddddd \
    LINE:a#000FF0FF:"Battery capacity" > ${destination}battery_charge_level_${period}.png
done

Set executable bit:

$ chmod +x ~/.battery/graph_battery_charge.sh

Execute it periodically to regenerate battery graphs (use cron to automate task) and view at the graphs located in ~/.battery directory.

battery_charge_level_1d

battery_charge_level_1w

battery_charge_level_1m