From b4612a8afc54fdba4622309e085f6cbff80d44ee Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Tue, 7 Aug 2018 19:04:56 -0400 Subject: [PATCH] Grab wifi status from iwconfig instead of nmcli which reduces our resource footprint --- README.md | 3 --- bin/khatus | 10 ++++++++- bin/khatus_controller | 10 +++++++-- bin/khatus_parse_iwconfig | 37 +++++++++++++++++++++++++++++++ bin/khatus_sensor_net_wifi_status | 16 +++++-------- 5 files changed, 59 insertions(+), 17 deletions(-) create mode 100755 bin/khatus_parse_iwconfig diff --git a/README.md b/README.md index 12d9f70..2bf2f0d 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,3 @@ TODO - priority - snooze time (if already alerted, when to re-alert?) - text: subject/body -- Reduce wireless sensor resource footprint: - - use `nmcli monitor`, instead of polling, for state changes - - use `iwconfig`, instead of `nmcli`, for SSID and signal strength diff --git a/bin/khatus b/bin/khatus index bf4db40..6ae08b0 100755 --- a/bin/khatus +++ b/bin/khatus @@ -54,6 +54,7 @@ main() { ["--weather_station_id"]='KJFK' ["--screen_brightness_device_name"]='acpi_video0' ["--prefixes_of_net_interfaces_to_show"]='w' # comma-separated + ["--wifi_interface"]='' ["--disk_space_device"]='/' ["--disk_io_device"]='sda' ["--thermal_zone"]=0 @@ -105,6 +106,12 @@ main() { esac done + if [ "${opts['--wifi_interface']}" = '' ] + then + echo 'Please provide the required parameter: --wifi_interface' >&2 + exit 1 + fi + ( echo '==============================================' echo "Khatus starting with the following parameters:" @@ -140,6 +147,7 @@ main() { cmd_sens_mpd_state="khatus_sensor_mpd_state $bin" cmd_sens_net_addr_io="khatus_sensor_net_addr_io $bin" cmd_sens_volume="khatus_sensor_volume $bin" + cmd_sens_wifi="khatus_sensor_net_wifi_status $bin ${opts['--wifi_interface']}" fork_watcher "$pipe" "$bin" "khatus_sensor_energy $bin" 'in:ENERGY' fork_poller "${opts['--interval_inp_datetime']}" "$pipe" "$bin" khatus_sensor_datetime 'in:DATE_TIME' @@ -149,7 +157,7 @@ main() { fork_poller "${opts['--interval_inp_mpd_song']}" "$pipe" "$bin" khatus_sensor_mpd_song 'in:MPD_SONG' fork_poller "${opts['--interval_inp_volume']}" "$pipe" "$bin" "$cmd_sens_volume" 'in:VOLUME' fork_poller "${opts['--interval_inp_bluetooth']}" "$pipe" "$bin" "$cmd_sens_bluetooth" 'in:BLUETOOTH_POWER' - fork_poller "${opts['--interval_inp_net_wifi']}" "$pipe" "$bin" khatus_sensor_net_wifi_status 'in:NET_WIFI_STATUS' + fork_poller "${opts['--interval_inp_net_wifi']}" "$pipe" "$bin" "$cmd_sens_wifi" 'in:NET_WIFI_STATUS' fork_poller "${opts['--interval_inp_net_io']}" "$pipe" "$bin" "$cmd_sens_net_addr_io" 'in:NET_ADDR_IO' fork_poller "${opts['--interval_inp_disk_space']}" "$pipe" "$bin" "$cmd_sens_disk_space" 'in:DISK_SPACE' fork_poller "${opts['--interval_inp_disk_io']}" "$pipe" "$bin" "$cmd_sens_disk_io" 'in:DISK_IO' diff --git a/bin/khatus_controller b/bin/khatus_controller index de971b2..c67e640 100755 --- a/bin/khatus_controller +++ b/bin/khatus_controller @@ -97,7 +97,7 @@ { shift() shift() - db["net_wifi_status"] = $0 + set_net_wifi_status() } /^OK in:BLUETOOTH_POWER/\ @@ -338,6 +338,12 @@ function set_disk_io( curr_w, curr_r, prev_w, prev_r) { db["disk_io_diff_r"] = curr_r - prev_r } +function set_net_wifi_status( interface) { + interface = $1 + shift() + db["net_wifi_status", interface] = $0 +} + function set_net_addr_io( \ interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\ ) { @@ -475,7 +481,7 @@ function make_status_net( \ io_stat = "--" } if (interface ~ "^w") { - label = label ":" db["net_wifi_status"] + label = label ":" db["net_wifi_status", interface] } if (++count_printed > 1) { sep = " " diff --git a/bin/khatus_parse_iwconfig b/bin/khatus_parse_iwconfig new file mode 100755 index 0000000..9a18cfa --- /dev/null +++ b/bin/khatus_parse_iwconfig @@ -0,0 +1,37 @@ +#! /usr/bin/awk -f +# +# Example iwconfig output: +# ----------------------- +# $ iwconfig wlp3s0 +# wlp3s0 IEEE 802.11 ESSID:"BPLUNWIRED" +# Mode:Managed Frequency:5.785 GHz Access Point: E2:55:2D:C0:64:B8 +# Bit Rate=135 Mb/s Tx-Power=15 dBm +# Retry short limit:7 RTS thr:off Fragment thr:off +# Power Management:on +# Link Quality=59/70 Signal level=-51 dBm +# Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 +# Tx excessive retries:0 Invalid misc:0 Missed beacon:0 +# +# +# USAGE: khatus_parse_iwconfig -v requested_interface="$wifi_interface" + +/^[a-z0-9]+ +IEEE 802\.11 +ESSID:/ { + interface = $1 + split($4, essid_parts, ":") + essid[interface] = essid_parts[2] + gsub("\"", "", essid[interface]) +} + +/^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ { + split($2, lq_parts_eq, "=") + split(lq_parts_eq[2], lq_parts_slash, "/") + cur = lq_parts_slash[1] + max = lq_parts_slash[2] + link[interface] = cur / max * 100 +} + +END { + i = requested_interface + status = link[i] ? sprintf("%s %s:%d%%", i, essid[i], link[i]) : "--" + print(status) +} diff --git a/bin/khatus_sensor_net_wifi_status b/bin/khatus_sensor_net_wifi_status index de7fd58..033572a 100755 --- a/bin/khatus_sensor_net_wifi_status +++ b/bin/khatus_sensor_net_wifi_status @@ -2,14 +2,8 @@ set -e -nmcli \ - -f ACTIVE,SSID,SIGNAL \ - -t \ - d wifi \ -| awk \ - -F ':' \ - ' - BEGIN {wifi_status = "--"} - $1 == "yes" {wifi_status = $2 ":" $3 "%"} - END {print wifi_status} - ' +dir_bin="$1" +wifi_interface="$2" + +iwconfig "$wifi_interface" \ +| "$dir_bin"/khatus_parse_iwconfig -v requested_interface="$wifi_interface" -- 2.20.1