monit-battery is a script to check the status of a battery. Although it's designed to work with a monitoring system such as m/monit, and as such, has proper exit codes, it can be used by itself too. This is handy for using in conjunction with scripts.
monit-battery [OPTIONS]| argument | description |
|---|---|
| -h, --help | Prints this help text |
| -b, --battery | Battery name (can be found in /sys/class/power_supply). Defaults to first BAT found |
| -c, --capacity | Capacity level % to error on. Defaults to 15 |
| -d, --discharge | Error if discharging. Disabled by default |
| -i, --ignorebadcharge | By default, the script will error if a power source is plugged in, and the battery is discharging (bad charge). This setting disables the check. |
| -w, --dischargewait | Wait until battery is below this percent capacity before determining the battery is discharging. Default is 85. Allows for differences in potential capacity (age of battery) and short term power spikes. |
# check first battery found
monit-battery
# error if BAT2 capacity is 50%
monit-battery --battery BAT2 --capacity 50
# error if battery is discharging (for example a laptop that should usually be plugged in)
monit-battery --discharge check program battery with path "/bin/monit-battery --badchargewait 80"
if status != 0 then alert
Since a battery can have more than one error condition, they are added together, similar to chmod octal permissions:
- 0: no errors
- 1: low capacity
- 2: discharging
- 4: plugged in, but discharging (bad charge)
So, an exit code of 1 is 'low capacity', but an exit code of 3 is 'low capacity' & 'discharging'
All info is pulled from /sys/class/power_supply, so there are no application requirements beyond bash.
Creates a temporary file, /tmp/monit-battery to store status