From f7e7c4946479fb3a4f7521794a4993dbc0a7b1ea Mon Sep 17 00:00:00 2001 From: Rob Pearce Date: Mon, 11 Dec 2023 23:02:15 +1100 Subject: [PATCH] added -e and -E options fix bug where empty killfile would be ignored --- aircon.sh | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/aircon.sh b/aircon.sh index 595df8a..d642ee6 100755 --- a/aircon.sh +++ b/aircon.sh @@ -57,11 +57,13 @@ function usage() { echo echo " Modifies aircon based on configured parameters in $DEFAULT_CONFIGFILE." echo - echo " -a file Specify location of arping binary (default: $ARPING_DEF)." - echo " -b Rule debug mode. Show processing of time rules." - echo " -A db Log actions to given influxdb database." - echo " -c Cron mode. Only show output if actions were taken." - echo " -D dbhost Specify influxdb hostname for -I and -A options (default: localhost)" + echo " -a file Specify location of arping binary (default: $ARPING_DEF)." + echo " -b Rule debug mode. Show processing of time rules." + echo " -A db Log actions to given influxdb database." + echo " -c Cron mode. Only show output if actions were taken." + echo " -D dbhost Specify influxdb hostname for -I and -A options (default: localhost)" + echo " -e file After removing expired killfile, write exptext (see -E) to this file." + echo " -E exptext Define 'exptext' for -e option." echo " -g file Send Tele(g)ram notifications via api.telegran.org. File format is:" echo " bottoken" echo " chatid" @@ -1738,17 +1740,20 @@ function show_analysis() { # returns 1 if all good and nothing to show return 0; } -function killfile_active() { # returns true if killfile swill stop us +function killfile_active() { # returns true if killfile will stop us local val now if [[ ! -f ${KILLFILE} ]]; then return 1 fi val=$(cat "${KILLFILE}") - [[ -z $val ]] && return 1 + [[ -z $val ]] && return 0 now=$(date +%s) if [[ $val -le $now ]]; then # killfile expired rm -f "${KILLFILE}" + if [[ -n $expirenotifyfile ]]; then + echo "$expirenotifytext" > "$expirenotifyfile" + fi return 1 fi return 0 @@ -2038,7 +2043,10 @@ fi ALLARGS="$ALLARGS $*" -optstring="aA:bcD:f:g:hHi:I:k:l:Lo:pmRsSt:T:wWy" +expirenotifyfile="" +expirenotifytext="" + +optstring="aA:bcD:e:E:f:g:hHi:I:k:l:Lo:pmRsSt:T:wWy" while getopts "$optstring" i $ALLARGS; do case "$i" in a) @@ -2056,6 +2064,12 @@ while getopts "$optstring" i $ALLARGS; do D) influxhost="${OPTARG}" ;; + e) + expirenotifyfile="$OPTARG" + ;; + E) + expirenotifytext="$OPTARG" + ;; f) CONFIGFILE=${OPTARG} ;; @@ -2130,6 +2144,11 @@ shift $((OPTIND - 1)) AIRCON_URL="http://${AIRCON_IP}:2025" +if [[ -n $expirenotifyfile && -z $expirenotifytext ]]; then + error "Cannot use -e option without -E." + exit 1 +fi + if [[ ! -e $ARPING ]]; then warn "arping binary '$ARPING' not found, will use ping instead" fi @@ -2349,7 +2368,11 @@ fi # Actually run the commands if [[ $DOIT -eq 1 ]]; then - if ! killfile_active; then + if killfile_active; then + if [[ $cronmode -eq 1 ]]; then + echo -e "${RED}Not running commands because killfile exists ($KILLFILE)${PLAIN}" + fi + else run_commands [[ $cronmode -ne 1 ]] && echo