2023-08-28 20:05:11 -06:00
|
|
|
#!/usr/bin/env bash
|
2023-09-02 16:09:43 -06:00
|
|
|
# Look through `upower --dump` to set the battery path variable
|
2023-08-28 20:05:11 -06:00
|
|
|
declare -r BATTERY_PATH="$1"
|
|
|
|
declare -r WRITE_PATH="$2"
|
|
|
|
|
|
|
|
if [[ $# -ne 2 ]]; then
|
|
|
|
echo "USAGE: $(basename "$0") <battery-path> <csv-path>"
|
|
|
|
exit 1
|
|
|
|
elif [[ ! -e "$WRITE_PATH" && -d "$(dirname "$WRITE_PATH")" ]]; then
|
|
|
|
echo "hostname,time,charge,percent,state" > "$WRITE_PATH"
|
|
|
|
fi
|
|
|
|
|
|
|
|
declare -r tmp_file="$(mktemp)"
|
|
|
|
|
|
|
|
upower --dump > "$tmp_file"
|
|
|
|
|
|
|
|
declare system_name="$(hostname)"
|
|
|
|
declare current_time="$(date -u +"%Y-%m-%d %H:%M:%S UTC")"
|
2023-09-02 16:09:43 -06:00
|
|
|
|
|
|
|
upower --dump | awk '
|
2023-08-28 20:05:11 -06:00
|
|
|
BEGIN { b = "'"$BATTERY_PATH"'"; t=0 }
|
|
|
|
|
2023-09-02 16:09:43 -06:00
|
|
|
t && (match($0, /Device:/) || match($0, /Daemon:/)) {
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2023-08-28 20:05:11 -06:00
|
|
|
match($0, b) { t=1 }
|
|
|
|
|
|
|
|
t && match($0, /energy:/) {
|
|
|
|
split($0,a," ")
|
2023-09-02 16:09:43 -06:00
|
|
|
energy = a[2]
|
|
|
|
}
|
2023-08-28 20:05:11 -06:00
|
|
|
|
|
|
|
t && match($0, /percentage:/) {
|
|
|
|
split($0,a," ")
|
2023-09-02 16:09:43 -06:00
|
|
|
percent = a[2]
|
|
|
|
}
|
2023-08-28 20:05:11 -06:00
|
|
|
|
|
|
|
t && match($0, /state:/) {
|
|
|
|
split($0,a," ")
|
2023-09-02 16:09:43 -06:00
|
|
|
state = a[2]
|
|
|
|
}
|
2023-08-28 20:05:11 -06:00
|
|
|
|
2023-09-02 16:09:43 -06:00
|
|
|
END {
|
|
|
|
print "'"${system_name},${current_time},"'"energy","percent","state
|
|
|
|
}' >> "$WRITE_PATH"
|