| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# This file is part of the battery-stats package. |
| 4 |
# Copyright (C) 2013-2016 Petter Reinholdtsen <[email protected]> |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# |
| 19 |
# Inspired by |
| 20 |
# http://www.ifweassume.com/2013/08/the-de-evolution-of-my-laptop-battery.html |
| 21 |
# See also |
| 22 |
# http://blog.sleeplessbeastie.eu/2013/01/02/debian-how-to-monitor-battery-capacity/ |
| 23 |
|
| 24 |
logfile=/var/log/battery-stats.csv |
| 25 |
|
| 26 |
files="manufacturer \ |
| 27 |
model_name \ |
| 28 |
technology \ |
| 29 |
serial_number \ |
| 30 |
status \ |
| 31 |
cycle_count \ |
| 32 |
energy_now \ |
| 33 |
energy_full \ |
| 34 |
energy_full_design \ |
| 35 |
charge_now \ |
| 36 |
charge_full \ |
| 37 |
charge_full_design \ |
| 38 |
" |
| 39 |
|
| 40 |
TEMP=$(getopt --name collect-csv --options o: --longoptions output: -- "$@") |
| 41 |
eval set -- "$TEMP" |
| 42 |
while true; do |
| 43 |
case "$1" in |
| 44 |
-o|--output) |
| 45 |
logfile="$2"; shift 2; continue |
| 46 |
;; |
| 47 |
--) # no more arguments to parse |
| 48 |
break |
| 49 |
;; |
| 50 |
*) |
| 51 |
printf "Unknown option %s\n" "$1" |
| 52 |
exit 1 |
| 53 |
;; |
| 54 |
esac |
| 55 |
done |
| 56 |
|
| 57 |
if [ ! -e "$logfile" ] ; then |
| 58 |
( |
| 59 |
printf "timestamp,when," |
| 60 |
for f in $files; do |
| 61 |
printf "%s," $f |
| 62 |
done |
| 63 |
echo |
| 64 |
) > "$logfile" |
| 65 |
fi |
| 66 |
|
| 67 |
log_battery() { |
| 68 |
printf "%s,%s," "$(date +%s)" "$(date --iso-8601=seconds)" |
| 69 |
for f in $files; do |
| 70 |
for file in $(echo $f | tr / " "); do |
| 71 |
if [ -e $file ] ; then fexist=$file; else fexist= ; fi |
| 72 |
done |
| 73 |
if [ "$fexist" ] && [ -e "$fexist" ] ; then |
| 74 |
printf "%s," "$(cat $fexist | sed -s 's/\(.* .*\)/"\1"/' )" |
| 75 |
else |
| 76 |
printf "," |
| 77 |
fi |
| 78 |
done |
| 79 |
} |
| 80 |
|
| 81 |
cd /sys/class/power_supply |
| 82 |
|
| 83 |
for bat in $(find -L . -maxdepth 1 -name "BAT*" -type d); do |
| 84 |
# Print complete message in one echo call, to avoid race condition |
| 85 |
# when several log processes run in parallel. |
| 86 |
(cd $bat && echo $(log_battery)) >> "$logfile" |
| 87 |
done |