Skip to content

Commit

Permalink
Add parsing of Rx/Tx bytes from ifconfig.
Browse files Browse the repository at this point in the history
  • Loading branch information
patfair committed Jun 12, 2024
1 parent a55db6d commit 11765f0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ $ curl http://10.0.100.2:8081/status
"signalNoiseRatio": 0,
"rxRateMbps": 0,
"rxPackets": 0,
"rxBytes": 0,
"txRateMbps": 0,
"txPackets": 0,
"txBytes": 0,
"bandwidthUsedMbps": 0
},
"blue3": null,
Expand All @@ -82,8 +84,10 @@ $ curl http://10.0.100.2:8081/status
"signalNoiseRatio": 40,
"rxRateMbps": 860.3,
"rxPackets": 4095,
"rxBytes": 5177,
"txRateMbps": 6,
"txPackets": 5246,
"txBytes": 11830,
"bandwidthUsedMbps": 4.102
},
"red2": null,
Expand Down
10 changes: 10 additions & 0 deletions radio/radio_ap.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,15 @@ func (radio *Radio) updateMonitoring() {
} else {
stationStatus.parseAssocList(output)
}

// Update the number of bytes received and transmitted.
output, err = shell.runCommand("ifconfig", stationInterface)
if err != nil {
log.Printf("Error running 'ifconfig %s': %v", stationInterface, err)
stationStatus.RxBytes = monitoringErrorCode
stationStatus.TxBytes = monitoringErrorCode
} else {
stationStatus.parseIfconfig(output)
}
}
}
13 changes: 12 additions & 1 deletion radio/radio_ap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ func TestRadio_updateStationMonitoring(t *testing.T) {
"\tRX: 550.6 MBit/s 4095 Pkts.\n" +
"\tTX: 254.0 MBit/s 0 Pkts.\n" +
"\texpected throughput: unknown"
fakeShell.commandOutput["ifconfig wlan0"] = "wlan0\tLink encap:Ethernet HWaddr 00:00:00:00:00:00\n" +
"\tRX bytes:12345 (12.3 KiB) TX bytes:98765 (98.7 KiB)"
fakeShell.commandOutput["luci-bwc -i wlan0-2"] = "[ 1687496917, 26097, 177, 70454, 846 ],\n" +
"[ 1687496919, 26097, 177, 70454, 846 ],\n" +
"[ 1687496920, 26097, 177, 70518, 847 ],\n" +
Expand All @@ -387,12 +389,16 @@ func TestRadio_updateStationMonitoring(t *testing.T) {
"[ 1687496922, 26097, 177, 70582, 848 ],\n" +
"[ 1687496923, 2609700, 177, 7064600, 849 ]"
fakeShell.commandOutput["iwinfo wlan0-2 assoclist"] = ""
fakeShell.commandOutput["ifconfig wlan0-2"] = ""
fakeShell.commandOutput["luci-bwc -i wlan0-4"] = ""
fakeShell.commandErrors["iwinfo wlan0-4 assoclist"] = errors.New("oops")
fakeShell.commandErrors["ifconfig wlan0-4"] = errors.New("oops")
radio.updateMonitoring()
assert.True(t, radio.StationStatuses["red1"].IsRobotRadioLinked)
assert.Equal(t, 550.6, radio.StationStatuses["red1"].RxRateMbps)
assert.Equal(t, -999.0, radio.StationStatuses["red1"].BandwidthUsedMbps)
assert.Equal(t, 12345, radio.StationStatuses["red1"].RxBytes)
assert.Equal(t, 98765, radio.StationStatuses["red1"].TxBytes)
assert.Equal(
t,
StationStatus{
Expand All @@ -405,17 +411,22 @@ func TestRadio_updateStationMonitoring(t *testing.T) {
StationStatus{
IsRobotRadioLinked: false,
RxRateMbps: -999,
RxBytes: -999,
TxRateMbps: -999,
TxBytes: -999,
SignalNoiseRatio: -999,
BandwidthUsedMbps: 0,
},
*radio.StationStatuses["blue2"],
)
assert.Equal(t, 6, len(fakeShell.commandsRun))
assert.Equal(t, 9, len(fakeShell.commandsRun))
assert.Contains(t, fakeShell.commandsRun, "luci-bwc -i wlan0")
assert.Contains(t, fakeShell.commandsRun, "iwinfo wlan0 assoclist")
assert.Contains(t, fakeShell.commandsRun, "ifconfig wlan0")
assert.Contains(t, fakeShell.commandsRun, "luci-bwc -i wlan0-2")
assert.Contains(t, fakeShell.commandsRun, "iwinfo wlan0-2 assoclist")
assert.Contains(t, fakeShell.commandsRun, "ifconfig wlan0-2")
assert.Contains(t, fakeShell.commandsRun, "luci-bwc -i wlan0-4")
assert.Contains(t, fakeShell.commandsRun, "iwinfo wlan0-4 assoclist")
assert.Contains(t, fakeShell.commandsRun, "ifconfig wlan0-4")
}
23 changes: 22 additions & 1 deletion radio/station_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ type StationStatus struct {
// Number of packets received from the robot radio.
RxPackets int `json:"rxPackets"`

// Number of bytes received from the robot radio.
RxBytes int `json:"rxBytes"`

// Upper-bound link transmit rate (from the access point to the robot radio) in megabits per second.
TxRateMbps float64 `json:"txRateMbps"`

// Number of packets transmitted to the robot radio.
TxPackets int `json:"txPackets"`

// Number of bytes transmitted to the robot radio.
TxBytes int `json:"txBytes"`

// Current five-second average total (rx + tx) bandwidth in megabits per second.
BandwidthUsedMbps float64 `json:"bandwidthUsedMbps"`
}
Expand All @@ -71,7 +77,8 @@ func (status *StationStatus) parseBandwidthUsed(response string) {
}
}

// Parses the given data from the access point's association list and updates the status structure with the result.
// parseAssocList parses the given data from the access point's association list and updates the status structure with
// the result.
func (status *StationStatus) parseAssocList(response string) {
line1Re := regexp.MustCompile(
"((?:[0-9A-F]{2}:){5}(?:[0-9A-F]{2}))\\s+(-\\d+) dBm / (-\\d+) dBm \\(SNR (\\d+)\\)\\s+(\\d+) ms ago",
Expand Down Expand Up @@ -111,3 +118,17 @@ func (status *StationStatus) parseAssocList(response string) {
}
}
}

// parseIfconfig parses the given output from the access point's ifconfig command and updates the status structure with
// the result.
func (status *StationStatus) parseIfconfig(response string) {
bytesRe := regexp.MustCompile("RX bytes:(\\d+) .* TX bytes:(\\d+) ")

status.RxBytes = 0
status.TxBytes = 0
bytesMatch := bytesRe.FindStringSubmatch(response)
if len(bytesMatch) > 0 {
status.RxBytes, _ = strconv.Atoi(bytesMatch[1])
status.TxBytes, _ = strconv.Atoi(bytesMatch[2])
}
}
17 changes: 17 additions & 0 deletions radio/station_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,20 @@ func TestStationStatus_ParseAssocList(t *testing.T) {
status.parseAssocList(response)
assert.Equal(t, StationStatus{}, status)
}

func TestStationStatus_ParseIfconfig(t *testing.T) {
var status StationStatus

status.parseIfconfig("")
assert.Equal(t, StationStatus{}, status)

response := "ath15\tLink encap:Ethernet HWaddr 4A:DA:35:B0:00:2C\n" +
"\tinet6 addr: fe80::48da:35ff:feb0:2c/64 Scope:Link\n" +
"\tUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\n" +
"\tRX packets:690 errors:0 dropped:0 overruns:0 frame:0\n" +
"\tTX packets:727 errors:0 dropped:0 overruns:0 carrier:0\n " +
"\tcollisions:0 txqueuelen:0\n" +
"\tRX bytes:45311 (44.2 KiB) TX bytes:48699 (47.5 KiB)\n"
status.parseIfconfig(response)
assert.Equal(t, StationStatus{RxBytes: 45311, TxBytes: 48699}, status)
}

0 comments on commit 11765f0

Please sign in to comment.