Split-out ip addr parser
authorSiraaj Khandkar <siraaj@khandkar.net>
Wed, 1 Aug 2018 19:47:02 +0000 (15:47 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Wed, 1 Aug 2018 19:47:02 +0000 (15:47 -0400)
bin/khatus
bin/khatus_parse_ip_addr [new file with mode: 0755]
bin/khatus_sensor_net_addr_io

index ead9dbd..da0c78c 100755 (executable)
@@ -256,6 +256,7 @@ main() {
     cmd_sens_fan="khatus_sensor_fan $fan_path"
     cmd_sens_bluetooth="khatus_sensor_bluetooth_power $dir_bin"
     cmd_sens_mpd_state="khatus_sensor_mpd_state $dir_bin"
+    cmd_sens_net_addr_io="khatus_sensor_net_addr_io $dir_bin"
 
     # TODO: Redirect each worker's stderr to a dedicated log file
     pipe="$file_pipe"
@@ -271,7 +272,7 @@ main() {
     fork_poller $interval_inp_volume     "$pipe" "$dir_bin" "$log" khatus_sensor_volume            'in:VOLUME'
     fork_poller $interval_inp_bluetooth  "$pipe" "$dir_bin" "$log" "$cmd_sens_bluetooth"           'in:BLUETOOTH_POWER'
     fork_poller $interval_inp_net_wifi   "$pipe" "$dir_bin" "$log" khatus_sensor_net_wifi_status   'in:NET_WIFI_STATUS'
-    fork_poller $interval_inp_net_io     "$pipe" "$dir_bin" "$log" khatus_sensor_net_addr_io       'in:NET_ADDR_IO'
+    fork_poller $interval_inp_net_io     "$pipe" "$dir_bin" "$log" "$cmd_sens_net_addr_io"         'in:NET_ADDR_IO'
     fork_poller $interval_inp_disk_space "$pipe" "$dir_bin" "$log" "$cmd_sens_disk_space"          'in:DISK_SPACE'
     fork_poller $interval_inp_disk_io    "$pipe" "$dir_bin" "$log" "$cmd_sens_disk_io"             'in:DISK_IO'
     fork_poller $interval_inp_loadavg    "$pipe" "$dir_bin" "$log" khatus_sensor_loadavg           'in:LOAD_AVG'
diff --git a/bin/khatus_parse_ip_addr b/bin/khatus_parse_ip_addr
new file mode 100755 (executable)
index 0000000..bc833b2
--- /dev/null
@@ -0,0 +1,37 @@
+#! /usr/bin/awk -f
+
+/^[0-9]+:/ {
+    sub(":$", "", $1)
+    sub(":$", "", $2)
+    sequence = $1
+    interface = $2
+    interfaces[sequence] = interface
+}
+
+/^ +inet [0-9]/ {
+    sub("/[0-9]+", "", $2)
+    addr = $2
+    addrs[interface] = addr
+}
+
+/^ +RX: / {transfer_direction = "r"}
+/^ +TX: / {transfer_direction = "w"}
+
+/^ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ *$/ {
+    io[interface, transfer_direction] = $1;
+}
+
+END {
+    for (seq=1; seq<=sequence; seq++) {
+        interface = interfaces[seq]
+        label = substr(interface, 1, 1)
+        addr = addrs[interface]
+        if (addr) {
+            curr_read  = io[interface, "r"]
+            curr_write = io[interface, "w"]
+            print(interface, addr, curr_write, curr_read)
+        } else {
+            print(interface)
+        }
+    }
+}
index 48b9b07..dd9086c 100755 (executable)
@@ -2,39 +2,6 @@
 
 set -e
 
-ip -s addr \
-| awk '
-    /^[0-9]+:/ {
-        sub(":$", "", $1)
-        sub(":$", "", $2)
-        sequence = $1
-        interface = $2
-        interfaces[sequence] = interface
-    }
+dir_bin="$1"
 
-    /^ +inet [0-9]/ {
-        sub("/[0-9]+", "", $2)
-        addr = $2
-        addrs[interface] = addr
-    }
-
-    /^ +RX: / {transfer_direction = "r"}
-    /^ +TX: / {transfer_direction = "w"}
-
-    /^ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ *$/ {
-        io[interface, transfer_direction] = $1;
-    }
-
-    END {
-        for (seq=1; seq<=sequence; seq++) {
-            interface = interfaces[seq]
-            label = substr(interface, 1, 1)
-            if (addrs[interface]) {
-                curr_read  = io[interface, "r"]
-                curr_write = io[interface, "w"]
-                print(interface, addrs[interface], curr_write, curr_read)
-            } else {
-                print(interface)
-            }
-        }
-    }'
+ip -s addr | "$dir_bin"/khatus_parse_ip_addr
This page took 0.026042 seconds and 4 git commands to generate.