From 7c7e8069e2952967f8f80f87e5577cb3de0dbe1b Mon Sep 17 00:00:00 2001 From: ge Date: Thu, 10 Nov 2022 22:09:14 +0300 Subject: [PATCH] init --- COPYING | 24 ++++++ Makefile | 18 +++++ PKGBUILD | 15 ++++ README | 113 +++++++++++++++++++++++++++ btw | 228 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ btw.1 | 146 +++++++++++++++++++++++++++++++++++ 6 files changed, 544 insertions(+) create mode 100644 COPYING create mode 100644 Makefile create mode 100644 PKGBUILD create mode 100644 README create mode 100755 btw create mode 100644 btw.1 diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..68a49da --- /dev/null +++ b/COPYING @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0b9d1f2 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +PREFIX ?= /usr/local + +install: + install -Dm755 btw $(PREFIX)/bin/btw + install -Dm644 btw.1 $(PREFIX)/share/man/man1/btw.1 + +uninstall: + rm -v $(PREFIX)/bin/btw $(PREFIX)/share/man/man1/btw.1 + +pkgbuild: + mkdir -p src + cp btw btw.1 src/ + makepkg -c + +readme: + @groff -man -Tascii btw.1 | col -bx + +.PHONY: readme diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 0000000..b69792b --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,15 @@ +pkgname=btw +pkgver=1.0 +pkgrel=1 +pkgdesc='' +arch=(any) +url=https://git.nxhs.cloud/ge/btw +license=(Unlicense) +depends=(coreutils acpi libnotify) +provides=(btw) +conflicts=() +srcdir="$PWD" +package() { + install -Dm755 $srcdir/btw $pkgdir/usr/bin/btw + install -Dm644 $srcdir/btw.1 $pkgdir/usr/share/man/man1/btw.1 +} diff --git a/README b/README new file mode 100644 index 0000000..1a05e16 --- /dev/null +++ b/README @@ -0,0 +1,113 @@ +BTW(1) User Commands BTW(1) + + + +NAME + btw - battery watchdog + +SYNOPSIS + btw [-c|--crit value] [-p|--preriod seconds] [-l|--log file] + [-j|--syslog] [-s|--summary text] [-b|--body text] [-w|--watch] + btw --help + btw --version + +DESCRIPTION + Send notification when critical battery level reached. + +OPTIONS + -v, --version + Print version and exit. + + -h, --help + Print this help message and exit. + + -c, --crit value + Battery critical value in percents [default: 10] + + -p, --preriod seconds + Battery check out period in seconds [default: 30] + + -l, --log file + Log file [default: ~/.cache/btw.log] -j, --syslog Use logger in- + stead of log file. + + -s, --summary text + Custom notification title text. + + -b, --body text + Custom notification body text. + + -w, --watch + Follow to the log file (tail -f). + +INVOKING + Run watchdog in background. See application autostart options for your + desktop environment or init system. For example add in your ~/.xinitrc: + + btw & + + or create ~/.config/autostart/btw.desktop if you have GNOME Desktop: + + [Desktop Entry] + Name=Laptop battery watchdog (btw) + Exec=btw + Type=Application + +LOGGING + btw writes battery status log in log file. Set file by --log option. + + Log format: + + [date time] [state] [level]% + + There is: + + [date time] + `date +'%Y-%m-%d %H:%M:%S'` command output. + + [state] + Battery status, can be 'Charging' or 'Discharging'. + + [level] + Battery current level in percents. + + If --syslog option is passed use following commands to watch log. + + btw -jw + # or + journalctl -t btw -f + +EXAMPLES + btw will send notification at every battery checkout while current bat- + tery level is lower than critical value. For example, with following + command: + + btw -c 15 -p 30 + + you will recieve notification every 30 seconds until battery is under + 10%. You can set up custom notification text via --summary and --body + options. + + Variables can be used in notification: + + level Current battery level in percents. + + state Battery status, can be full, charging or discharging. + + For example: + + btw -s 'Battery is very low!' -b 'Status: $state, $level%' + +FILES + ~/.cache/btw.log + Log file. + + /tmp/btw.lock + Lock file. Prevents unwanted notifications. + +SEE ALSO + acpi(1), notify-send(1) + + + +btw 1.0 2022-01-10 BTW(1) diff --git a/btw b/btw new file mode 100755 index 0000000..40909bc --- /dev/null +++ b/btw @@ -0,0 +1,228 @@ +#!/bin/sh +# +# * Battery Watchdog (see help text below). +# +# This is free and unencumbered software released into the public domain. +# +# Anyone is free to copy, modify, publish, use, compile, sell, or +# distribute this software, either in source code form or as a compiled +# binary, for any purpose, commercial or non-commercial, and by any +# means. +# +# In jurisdictions that recognize copyright laws, the author or authors +# of this software dedicate any and all copyright interest in the +# software to the public domain. We make this dedication for the benefit +# of the public at large and to the detriment of our heirs and +# successors. We intend this dedication to be an overt act of +# relinquishment in perpetuity of all present and future rights to this +# software under copyright law. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +# OTHER DEALINGS IN THE SOFTWARE. +# +# For more information, please refer to +# --- +# Dependencies: +# acpi +# libnotify +# +# Installation: +# # on Arch Linux: +# pacman -S acpi # (commynity repo) +# pacman -S libnotyfy # (extra repo) +# # on Debian/Ubuntu: +# apt-get install acpi +# apt-get install libnotify + +print_help() { + printf \ +"btw - battery watchdog. Send notification when critical battery level reached. + +Usage: + btw [-c|--crit \033[4mvalue\033[0m] [-p|--preriod \033[4mseconds\033[0m] \ +[-l|--log \033[4mfile\033[0m] + [-j|--syslog] [-s|--summary t\033[4mext\033[0m] \ +[-b|--body t\033[4mext\033[0m] [-w|--watch] + btw --help + btw --version + +Options: + -v, --version + Print version and exit. + + -h, --help + Print this help message and exit. + + -c, --crit \033[4mvalue\033[0m + Battery critical value in percents [default: 10] + + -p, --preriod \033[4mseconds\033[0m + Battery check out period in seconds [default: 30] + + -l, --log \033[4mfile\033[0m + Log file [default: ~/.cache/btw.log] + + -j, --syslog + Use logger instead of log file. + + -s, --summary \033[4mtext\033[0m + Custom notification title text. + + -b, --body \033[4mtext\033[0m + Custom notification body text. + + -w, --watch + Follow to the log file (tail -f). + +See \033[1mbtw\033[0m(1) for full help. +" +} + +send_notify() { + notify-send --urgency=critical --icon=battery-empty --category=System \ + "$(eval echo "$1")" "$(eval echo "$2")" +} + +is_int() { + if [ -n "$1" ] && [ "$1" -eq "$1" ] 2>/dev/null; then + return 0 + else + echo 'Value is not integer' >&2; exit 1 + fi +} + +# Transform long options to short ones +for ARG in "$@"; do + shift + case "$ARG" in + --crit) + set -- "$@" "-c" + ;; + --period) + set -- "$@" "-p" + ;; + --log) + set -- "$@" "-l" + ;; + --syslog) + set -- "$@" "-j" + ;; + --summary) + set -- "$@" "-s" + ;; + --body) + set -- "$@" "-b" + ;; + --watch) + set -- "$@" "-w" + ;; + --help) + set -- "$@" "-h" + ;; + --version) + set -- "$@" "-v" + ;; + *) + set -- "$@" "$ARG" + esac +done + +# Parse short opts +while getopts c:p:l:js:b:whv OPT; do + case "$OPT" in + c) + is_int "$OPTARG" + critical="$OPTARG" + ;; + p) + is_int "$OPTARG" + period="$OPTARG" + ;; + l) + log="$OPTARG" + ;; + j) + syslog=1 + ;; + s) + summary="$OPTARG" + ;; + b) + body="$OPTARG" + ;; + w) + watch=1 + ;; + h) + print_help + exit 0 + ;; + v) + echo 1.0 + exit 0 + ;; + *) + echo Unknown option: "$OPT" >&2 + exit 1 + esac +done + +# Default values +period="${period:-30}" # check battery every `period` seconds +critical="${critical:-10}" # battery limit (in %) +clevel="${clevel:-100}" # current battery value (in %, temporary variable) +summary="${summary:-Extremely low battery: \$level%}" +body="${body:-Check the charger, battery currently is \$state}" +lock=/tmp/btw.lock # lock file +log="${log:-"$HOME"/.cache/btw.log}" + +mkdir -p "$HOME"/.cache + +if [ -n "$watch" ]; then + if [ -n "$syslog" ]; then + journalctl -t btw -f + else + tail -f "$log" + fi + exit "$?" +fi + +printf 'crit: %s%%; period: %ss; log: %s\n' "$critical" "$period" "$log" >&2 + +while true; do + battery="$(acpi -b | awk '{print $3 " " $4}' | + sed -E 's/,|%//g;' | tr '[:upper:]' '[:lower:]')" + level="${battery##* }" # e.g. "49" (in percents) + state="${battery%% *}" # e.g. "charging" + + if [ "$level" -le "$critical" ]; then + if [ ! -f "$lock" ]; then + send_notify "$summary" "$body" + touch "$lock" + fi + # Warn in every percent less + if [ "$level" -le "$clevel" ]; then + send_notify "$summary" "$body" + touch "$lock" + clevel="$level" + fi + else + [ -f "$lock" ] && rm "$lock" + fi + + log_message="$(printf '%s %s %s%%' \ + "$(date +'%Y-%m-%d %H:%M:%S')" "$state" "$level")" + if [ -n "$syslog" ]; then + echo "$log_message" + logger -i "$$" -t btw -- "$log_message" + else + echo "$log_message" | tee -a "$log" + fi + + sleep "$period" +done diff --git a/btw.1 b/btw.1 new file mode 100644 index 0000000..be75d71 --- /dev/null +++ b/btw.1 @@ -0,0 +1,146 @@ +.\" btw (c) ge +.\" License: The Unlicense +.\" vim: set filetype=groff: +.TH BTW 1 "2022-01-10" "btw 1.0" "User Commands" +.SH NAME +btw \- battery watchdog +.SH SYNOPSIS +.SY btw +.OP \-c|\-\-crit value +.OP \-p|\-\-preriod seconds +.OP \-l|\-\-log file +.OP \-j|\-\-syslog +.OP \-s|\-\-summary text +.OP \-b|\-\-body text +.OP \-w|\-\-watch +.SY btw +.B \-\-help +.SY btw +.B \-\-version +.YS +.SH DESCRIPTION +Send notification when critical battery level reached. +.SH OPTIONS +.TP +\fB\-v\fR, \fB\-\-version\fR +Print version and exit. +.TP +\fB\-h\fR, \fB\-\-help\fR +Print this help message and exit. +.TP +\fB\-c\fR, \fB\-\-crit\fR \fIvalue\fR +Battery critical value in percents [default: 10] +.TP +\fB\-p\fR, \fB\-\-preriod\fR \fIseconds\fR +Battery check out period in seconds [default: 30] +.TP +\fB\-l\fR, \fB\-\-log\fR \fIfile\fR +Log file [default: ~/.cache/btw.log] +\fB\-j\fR, \fB\-\-syslog\fR +Use logger instead of log file. +.TP +\fB\-s\fR, \fB\-\-summary\fR \fItext\fR +Custom notification title text. +.TP +\fB\-b\fR, \fB\-\-body\fR \fItext\fR +Custom notification body text. +.TP +\fB\-w\fR, \fB\-\-watch\fR +Follow to the log file (tail -f). +.SH INVOKING +Run watchdog in background. See application autostart options for your +desktop environment or init system. For example add in your ~/.xinitrc: +.PP +.in +4m +.EX +btw & +.EE +.in +.PP +or create ~/.config/autostart/btw.desktop if you have GNOME Desktop: +.PP +.in +4m +.EX +[Desktop Entry] +Name=Laptop battery watchdog (btw) +Exec=btw +Type=Application +.EE +.in +.PP +.SH LOGGING +btw writes battery status log in log file. Set file by --log option. +.PP +Log format: +.PP +.in +4m +.EX +[date time] [state] [level]% +.EE +.in +.PP +There is: +.TP +[date time] +`date +'%Y-%m-%d %H:%M:%S'` command output. +.TP +[state] +Battery status, can be 'Charging' or 'Discharging'. +.TP +[level] +Battery current level in percents. +.PP +If --syslog option is passed use following commands to watch log. +.PP +.in +4m +.EX +btw -jw +# or +journalctl -t btw -f +.EE +.in +.PP +.SH EXAMPLES +.B btw +will send notification at every battery checkout while current battery +level is lower than critical value. For example, with following command: +.PP +.in +4m +.EX +btw -c 15 -p 30 +.EE +.in +.PP +you will recieve notification every 30 seconds until battery is under 10%. +You can set up custom notification text via --summary and --body options. +.PP +Variables can be used in notification: +.TP +.B level +Current battery level in percents. +.TP +.B state +Battery status, can be +.IR full , +.IR charging +or +.IR discharging . +.PP +For example: +.PP +.in +4m +.EX +btw -s 'Battery is very low!' -b 'Status: $state, $level%' +.EE +.in +.PP +.SH FILES +.TP +~/.cache/btw.log +Log file. +.TP +/tmp/btw.lock +Lock file. Prevents unwanted notifications. +.SH SEE ALSO +.BR acpi (1), +.BR notify-send (1)