Add battery-logging systemd timer
This commit is contained in:
parent
73203135b3
commit
da4580b38a
9
systemd/system/log_battery.service
Normal file
9
systemd/system/log_battery.service
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[Unit]
|
||||||
|
Description=Log the current battery charge
|
||||||
|
Wants=log_battery.timer
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
User=root
|
||||||
|
Group=root
|
||||||
|
Type=oneshot
|
||||||
|
ExecStart=/usr/local/bin/log_battery.sh /org/freedesktop/UPower/devices/battery_BAT0 /home/emiliko/safe/loggers/battery/battery.csv
|
11
systemd/system/log_battery.timer
Normal file
11
systemd/system/log_battery.timer
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
[Unit]
|
||||||
|
Description=Log the current battery charge
|
||||||
|
Requires=log_battery.service
|
||||||
|
|
||||||
|
[Timer]
|
||||||
|
Unit=log_battery.service
|
||||||
|
OnCalendar=*-*-* *:00/1:00
|
||||||
|
RandomizedDelaySec=10s
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=timers.target
|
50
systemd/system/timer-scripts/log_battery.sh
Executable file
50
systemd/system/timer-scripts/log_battery.sh
Executable file
|
@ -0,0 +1,50 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
# Look through `upower --dump` to set this variable
|
||||||
|
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")"
|
||||||
|
declare current_power="$(cat "$tmp_file" | awk '
|
||||||
|
BEGIN { b = "'"$BATTERY_PATH"'"; t=0 }
|
||||||
|
|
||||||
|
match($0, b) { t=1 }
|
||||||
|
|
||||||
|
t && match($0, /energy:/) {
|
||||||
|
split($0,a," ")
|
||||||
|
print a[2]
|
||||||
|
exit
|
||||||
|
}')"
|
||||||
|
declare current_percentage="$(cat "$tmp_file" | awk '
|
||||||
|
BEGIN { b = "'"$BATTERY_PATH"'"; t=0 }
|
||||||
|
|
||||||
|
match($0, b) { t=1 }
|
||||||
|
|
||||||
|
t && match($0, /percentage:/) {
|
||||||
|
split($0,a," ")
|
||||||
|
print a[2]
|
||||||
|
exit
|
||||||
|
}')"
|
||||||
|
declare current_state="$(cat "$tmp_file" | awk '
|
||||||
|
BEGIN { b = "'"$BATTERY_PATH"'"; t=0 }
|
||||||
|
|
||||||
|
match($0, b) { t=1 }
|
||||||
|
|
||||||
|
t && match($0, /state:/) {
|
||||||
|
split($0,a," ")
|
||||||
|
print a[2]
|
||||||
|
exit
|
||||||
|
}')"
|
||||||
|
|
||||||
|
echo "$system_name,$current_time,$current_power,$current_percentage,$current_state" >> "$WRITE_PATH"
|
Loading…
Reference in a new issue