diff --git a/aircon.sh b/aircon.sh index 3a27ad1..95b9bf4 100755 --- a/aircon.sh +++ b/aircon.sh @@ -98,7 +98,7 @@ function get_midrange() { # outputs midpoint of temperature range for given zon } # get_perfect_tolerance zone_idx -function get_perfect_tolerance() { # outputs the difference from midpoint inside which temperature is considered "perfect" +function get_perfect_tolerance() { # outputs the difference from midpoint after which temperature is considered "perfect" local min max min=${zwantmin[$idx]} max=${zwantmax[$idx]} @@ -171,9 +171,9 @@ fi midtol=$(get_perfect_tolerance $idx) #echo "zone ${zname[$idx]} temp ${ztemp[$idx]} mid is $mid midtol is $midtol perfect is >= $(echo "$mid - $midtol" | bc)" if [[ $airconmode == "heat" ]]; then - thisperfect=$(echo "${ztemp[$idx]} >= (${mid} - ${midtol})" | bc) + thisperfect=$(echo "${ztemp[$idx]} >= (${mid} + ${midtol})" | bc) elif [[ $airconmode == "cool" ]]; then - thisperfect=$(echo "${ztemp[$idx]} <= (${mid} + ${midtol})" | bc) + thisperfect=$(echo "${ztemp[$idx]} <= (${mid} - ${midtol})" | bc) fi fi fi @@ -376,6 +376,7 @@ function generate_actions() { # populates global: nairconcommands & airconcmd local globaction mzmustmove poss nomyzone local db db=0 + [[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -en "${GREEN}${BOLD}>> ${PLAIN}${GREEN}Determining actions... ${PLAIN}" nairconcommands=0 powerchange="" @@ -595,6 +596,7 @@ function generate_actions() { # populates global: nairconcommands & airconcmd if [[ -n $globaction ]]; then gen_aircon_command -1 ${globaction} fi + echo -e "${GREEN}${BOLD}ok${PLAIN}" } # gen_aircon_command zone_idx "command1 command2 etc" @@ -936,6 +938,7 @@ function load_config() { local cond allconds x local db parse_config || return 1 + [[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -en "${GREEN}${BOLD}>> ${PLAIN}${GREEN}Loading config file... ${PLAIN}" db=0 rv=0 while read line; do @@ -979,6 +982,13 @@ function load_config() { fi fi done < "$CONFIGFILE" + if [[ $cronmode -eq 0 && $logmode -eq 0 ]]; then + if [[ $rv -eq 0 ]]; then + echo -e "${GREEN}${BOLD}ok${PLAIN}" + else + echo -e "${RED}${BOLD}fail${PLAIN}" + fi + fi return $rv } @@ -1165,6 +1175,7 @@ function parse_config() { local config_human line_human errstr local ign="" thisignored=0 oneof + [[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -en "${GREEN}${BOLD}>> ${PLAIN}${GREEN}Parsing config file... ${PLAIN}" if [[ -n $1 ]]; then show=$1 fi @@ -1194,7 +1205,6 @@ function parse_config() { fi fi - IFS=' ' read -ra tok <<< "$line" if [[ ${tok[0]} == "temp" ]]; then local min max @@ -1266,6 +1276,7 @@ function parse_config() { done < "$CONFIGFILE" if [[ $fileok -eq 1 ]]; then + [[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -e "${GREEN}${BOLD}ok${PLAIN}" if [[ $show -eq 2 ]]; then echo -e "${config_human}" elif [[ $show -eq 1 ]]; then @@ -1273,6 +1284,7 @@ function parse_config() { fi rv=0 else + [[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -e "${RED}${BOLD}fail${PLAIN}" error "Configuration file is invalid:" echo -e "${RED}$errstr${plainc}" | sed -e 's/^/ /' rv=1 @@ -1280,32 +1292,47 @@ function parse_config() { return $rv } -# if we can ping any of the args, return ok +# if we can ping ANY of the args, return ok function canping() { - local ip db host thisrv mac + local n hname ip db host success=0 mac local os os=$(uname -s) db=0 + # get ip and clear arp + n=0 for host in $*; do + hname[$n]="$host" if [[ $os == "Darwin" ]]; then - ip=$(dscacheutil -q host -a name $host | grep ^ip_address: | awk '{ print $NF}') + ip[$n]=$(dscacheutil -q host -a name $host | grep ^ip_address: | awk '{ print $NF}') else - ip=$(getent hosts $host | grep -v :) + ip[$n]=$(getent hosts $host | grep -v : | awk '{ print $1 }') fi - ip=${ip%% *} - if [[ -z $ip ]]; then - thisrv=1 - else - arp -d $ip >/dev/null 2>&1 - #ping -c1 -w1 -n -q $ip >/dev/null 2>&1 & - arping -c10 -W0.1 -w3 -C1 -q ${ip} 2>/dev/null - sleep 0.3 - mac=$(arp -n $ip) - mac=$(echo "$mac" | egrep -v "Host|xpired" | awk '{print $2}') - [[ $mac == *:* ]] && thisrv=0 || thisrv=1 + if [[ -n ${ip[$n]} ]]; then + arp -d ${ip[$n]} >/dev/null 2>&1 fi - [[ $db -eq 1 ]] && info "canping() $host ($ip) is $thisrv - mac=$mac" - if [[ $thisrv -eq 0 ]]; then + done + + # send new arp req + for n in ${!ip[@]}; do + if [[ -n ${ip[$n]} ]]; then + arping -c10 -W0.1 -w4 -C1 -q ${ip[$n]} 2>/dev/null & + fi + done + + # wait for arp replies or timeout + wait + sleep 0.4 + + # now check entries + for n in ${!ip[@]}; do + if [[ -n ${ip[$n]} ]]; then + mac=$(arp -n ${ip[$n]}) + #mac=$(arp -n $ip | egrep -v "Host|xpired" | awk '{print $2}') + #[[ $mac == *:* ]] && thisrv=0 + [[ $? -eq 0 ]] && success=1 + fi + [[ $db -eq 1 ]] && info "canping() ${hname[$n]} (${ip[$n]}) is $success - mac=$mac" + if [[ $success -eq 1 ]]; then return 0 fi done @@ -1878,10 +1905,10 @@ else fi # ping all hosts in background to populate arp table -for x in ${devices}; do - ping -c1 -w1 -n -q $ip >/dev/null 2>&1 & -done -wait +#for x in ${devices}; do +# ping -c1 -w1 -n -q $ip >/dev/null 2>&1 & +#done +#wait if [[ $robtest -eq 1 ]]; then for x in ${!zname[@]}; do @@ -1915,6 +1942,7 @@ if [[ $showwho -eq 1 ]]; then echo exit 0 elif [[ $showwho -eq 2 ]]; then + [[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -en "${GREEN}${BOLD}>> ${PLAIN}${GREEN}Querying zone owners... ${PLAIN}" # get max device hostname length maxlen=1 for x in ${ownerhost[@]}; do @@ -1925,23 +1953,31 @@ elif [[ $showwho -eq 2 ]]; then HFORMAT="%-${maxlen}s${GREEN}%-16s${PLAIN}%s\n" AFORMAT="%-${maxlen}s${RED}%-16s${PLAIN}%s\n" alldevs=$(echo "${ownerhost[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ') + + for x in $alldevs; do + canping ${x} >/dev/null 2>&1 + pingres[$x]=$? + done + + [[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -e "${GREEN}${BOLD}ok${PLAIN}" echo + printf "$TFORMAT" "Devices" "Availability" "Zones owned" for x in $alldevs; do - if canping ${x}; then + if [[ ${pingres[$x]} -eq 0 ]]; then thisform="$HFORMAT" str="Available" else thisform="$AFORMAT" str="Unavailable" fi - unset mydevs || declare -a mydevs + unset mydevs || declare -a mydevs for i in ${!ownerzone[@]}; do if [[ ${ownerhost[$i]} == *${x}* ]]; then mydevs+=("${ownerzone[$i]}") - fi - done - allmydevs=$(echo "${mydevs[@]}" | tr '\n' ' ') + fi + done + allmydevs=$(echo "${mydevs[@]}" | tr '\n' ' ') printf "$thisform" "${x}" "$str" "${allmydevs}" done echo