use proper api endpoint to compute rate trafic on wan interface
This commit is contained in:
parent
564d721d31
commit
65179c8b45
4 changed files with 194 additions and 134 deletions
|
@ -54,7 +54,7 @@ class OPNSenseAPI:
|
|||
|
||||
def get_wan_trafic(self):
|
||||
try:
|
||||
data = self.get("/api/diagnostics/traffic/interface")
|
||||
data = self.get("/api/diagnostics/traffic/top/wan")
|
||||
except RequestException as ex:
|
||||
logger.error(
|
||||
"Get diagnostics traffic on WAN interface for %s host failed with the following error %r",
|
||||
|
@ -62,29 +62,10 @@ class OPNSenseAPI:
|
|||
ex,
|
||||
)
|
||||
return None, None
|
||||
return (
|
||||
int(data["interfaces"]["wan"]["bytes received"]),
|
||||
int(data["interfaces"]["wan"]["bytes transmitted"]),
|
||||
)
|
||||
|
||||
|
||||
# def get_server_system_status(self):
|
||||
# # https://192.168.200.1/api/core/system/status
|
||||
# return {
|
||||
# "CrashReporter":
|
||||
# {
|
||||
# "statusCode":2,
|
||||
# "message":"No problems were detected.",
|
||||
# "logLocation":"/crash_reporter.php",
|
||||
# "timestamp":"0",
|
||||
# "status":"OK"
|
||||
# },
|
||||
# "Firewall":{
|
||||
# "statusCode":2,
|
||||
# "message":"No problems were detected.",
|
||||
# "logLocation":"/ui/diagnostics/log/core/firewall",
|
||||
# "timestamp":"0",
|
||||
# "status":"OK"
|
||||
# },
|
||||
# "System":{"status":"OK"}
|
||||
# }
|
||||
received = 0
|
||||
transmitted = 0
|
||||
for record in data["wan"]["records"]:
|
||||
received += record["rate_bits_in"]
|
||||
transmitted += record["rate_bits_out"]
|
||||
return received, transmitted
|
||||
|
|
271
tests/common.py
271
tests/common.py
|
@ -49,101 +49,180 @@ def generate_get_vip_status_paylaod(state_wan, state_lan, maintenance_mode):
|
|||
def generate_diagnostics_traffic_interface_paylaod():
|
||||
return json.dumps(
|
||||
{
|
||||
"interfaces": {
|
||||
"lan": {
|
||||
"index": "2",
|
||||
"flags": "8963",
|
||||
"promiscuous listeners": "1",
|
||||
"send queue length": "0",
|
||||
"send queue max length": "50",
|
||||
"send queue drops": "0",
|
||||
"type": "Ethernet",
|
||||
"address length": "6",
|
||||
"header length": "18",
|
||||
"link state": "2",
|
||||
"vhid": "0",
|
||||
"datalen": "152",
|
||||
"mtu": "1500",
|
||||
"metric": "0",
|
||||
"line rate": "10000000000 bit/s",
|
||||
"packets received": "3699327747",
|
||||
"input errors": "0",
|
||||
"packets transmitted": "8972963403",
|
||||
"output errors": "0",
|
||||
"collisions": "0",
|
||||
"bytes received": "2474843996609",
|
||||
"bytes transmitted": "11711737078752",
|
||||
"multicasts received": "29274204",
|
||||
"multicasts transmitted": "0",
|
||||
"input queue drops": "0",
|
||||
"packets for unknown protocol": "0",
|
||||
"HW offload capabilities": "0x0",
|
||||
"uptime at attach or stat reset": "1",
|
||||
"name": "LAN",
|
||||
},
|
||||
"opt1": {
|
||||
"index": "3",
|
||||
"flags": "8863",
|
||||
"promiscuous listeners": "0",
|
||||
"send queue length": "0",
|
||||
"send queue max length": "50",
|
||||
"send queue drops": "0",
|
||||
"type": "Ethernet",
|
||||
"address length": "6",
|
||||
"header length": "18",
|
||||
"link state": "2",
|
||||
"vhid": "0",
|
||||
"datalen": "152",
|
||||
"mtu": "1500",
|
||||
"metric": "0",
|
||||
"line rate": "10000000000 bit/s",
|
||||
"packets received": "1120457",
|
||||
"input errors": "0",
|
||||
"packets transmitted": "178007891",
|
||||
"output errors": "0",
|
||||
"collisions": "0",
|
||||
"bytes received": "221674964",
|
||||
"bytes transmitted": "55808795987",
|
||||
"multicasts received": "393767",
|
||||
"multicasts transmitted": "0",
|
||||
"input queue drops": "0",
|
||||
"packets for unknown protocol": "0",
|
||||
"HW offload capabilities": "0x0",
|
||||
"uptime at attach or stat reset": "1",
|
||||
"name": "LANOPNSYNC",
|
||||
},
|
||||
"wan": {
|
||||
"index": "1",
|
||||
"flags": "8963",
|
||||
"promiscuous listeners": "1",
|
||||
"send queue length": "0",
|
||||
"send queue max length": "50",
|
||||
"send queue drops": "0",
|
||||
"type": "Ethernet",
|
||||
"address length": "6",
|
||||
"header length": "18",
|
||||
"link state": "2",
|
||||
"vhid": "0",
|
||||
"datalen": "152",
|
||||
"mtu": "1500",
|
||||
"metric": "0",
|
||||
"line rate": "10000000000 bit/s",
|
||||
"packets received": "9008014155",
|
||||
"input errors": "0",
|
||||
"packets transmitted": "3724630846",
|
||||
"output errors": "0",
|
||||
"collisions": "0",
|
||||
"bytes received": "11725192686820",
|
||||
"bytes transmitted": "2489262014203",
|
||||
"multicasts received": "2288794",
|
||||
"multicasts transmitted": "0",
|
||||
"input queue drops": "0",
|
||||
"packets for unknown protocol": "0",
|
||||
"HW offload capabilities": "0x0",
|
||||
"uptime at attach or stat reset": "1",
|
||||
"name": "WAN",
|
||||
},
|
||||
},
|
||||
"time": 1693404742.796736,
|
||||
"wan": {
|
||||
"records": [
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate_bits_in": 15300,
|
||||
"rate_bits_out": 1720,
|
||||
"rate_bits": 17020,
|
||||
"cumulative_bytes_in": 3830,
|
||||
"cumulative_bytes_out": 441,
|
||||
"cumulative_bytes": 4271,
|
||||
"tags": [],
|
||||
"details": [
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate": "15.3Kb",
|
||||
"rate_bits": 15300,
|
||||
"cumulative": "3.83KB",
|
||||
"cumulative_bytes": 3830,
|
||||
"tags": ["local"],
|
||||
}
|
||||
],
|
||||
"rname": "fake value",
|
||||
"rate_in": "15.3 kb",
|
||||
"rate_out": "1.72 kb",
|
||||
"rate": "17.02 kb",
|
||||
"cumulative_in": "3.83 kb",
|
||||
"cumulative_out": "441.0 b",
|
||||
"cumulative": "4.27 kb",
|
||||
},
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate_bits_in": 4470,
|
||||
"rate_bits_out": 7290,
|
||||
"rate_bits": 11760,
|
||||
"cumulative_bytes_in": 1120,
|
||||
"cumulative_bytes_out": 1820,
|
||||
"cumulative_bytes": 2940,
|
||||
"tags": [],
|
||||
"details": [
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate": "4.47Kb",
|
||||
"rate_bits": 4470,
|
||||
"cumulative": "1.12KB",
|
||||
"cumulative_bytes": 1120,
|
||||
"tags": ["local"],
|
||||
}
|
||||
],
|
||||
"rname": "fake value",
|
||||
"rate_in": "4.47 kb",
|
||||
"rate_out": "7.29 kb",
|
||||
"rate": "11.76 kb",
|
||||
"cumulative_in": "1.12 kb",
|
||||
"cumulative_out": "1.82 kb",
|
||||
"cumulative": "2.94 kb",
|
||||
},
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate_bits_in": 272,
|
||||
"rate_bits_out": 272,
|
||||
"rate_bits": 544,
|
||||
"cumulative_bytes_in": 68,
|
||||
"cumulative_bytes_out": 68,
|
||||
"cumulative_bytes": 136,
|
||||
"tags": [],
|
||||
"details": [
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate": "272b",
|
||||
"rate_bits": 272,
|
||||
"cumulative": "68B",
|
||||
"cumulative_bytes": 68,
|
||||
"tags": ["local"],
|
||||
}
|
||||
],
|
||||
"rname": "fake value",
|
||||
"rate_in": "272.0 b",
|
||||
"rate_out": "272.0 b",
|
||||
"rate": "544.0 b",
|
||||
"cumulative_in": "68.0 b",
|
||||
"cumulative_out": "68.0 b",
|
||||
"cumulative": "136.0 b",
|
||||
},
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate_bits_in": 272,
|
||||
"rate_bits_out": 272,
|
||||
"rate_bits": 544,
|
||||
"cumulative_bytes_in": 68,
|
||||
"cumulative_bytes_out": 68,
|
||||
"cumulative_bytes": 136,
|
||||
"tags": [],
|
||||
"details": [
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate": "272b",
|
||||
"rate_bits": 272,
|
||||
"cumulative": "68B",
|
||||
"cumulative_bytes": 68,
|
||||
"tags": ["local"],
|
||||
}
|
||||
],
|
||||
"rname": "fake value",
|
||||
"rate_in": "272.0 b",
|
||||
"rate_out": "272.0 b",
|
||||
"rate": "544.0 b",
|
||||
"cumulative_in": "68.0 b",
|
||||
"cumulative_out": "68.0 b",
|
||||
"cumulative": "136.0 b",
|
||||
},
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate_bits_in": 0,
|
||||
"rate_bits_out": 480,
|
||||
"rate_bits": 480,
|
||||
"cumulative_bytes_in": 0,
|
||||
"cumulative_bytes_out": 120,
|
||||
"cumulative_bytes": 120,
|
||||
"tags": [],
|
||||
"details": [
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate": "0b",
|
||||
"rate_bits": 0,
|
||||
"cumulative": "0B",
|
||||
"cumulative_bytes": 0,
|
||||
"tags": ["local"],
|
||||
},
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate": "0b",
|
||||
"rate_bits": 0,
|
||||
"cumulative": "0B",
|
||||
"cumulative_bytes": 0,
|
||||
"tags": ["local"],
|
||||
},
|
||||
],
|
||||
"rname": "fake value",
|
||||
"rate_in": "0.0 b",
|
||||
"rate_out": "480.0 b",
|
||||
"rate": "480.0 b",
|
||||
"cumulative_in": "0.0 b",
|
||||
"cumulative_out": "120.0 b",
|
||||
"cumulative": "120.0 b",
|
||||
},
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate_bits_in": 224,
|
||||
"rate_bits_out": 0,
|
||||
"rate_bits": 224,
|
||||
"cumulative_bytes_in": 56,
|
||||
"cumulative_bytes_out": 0,
|
||||
"cumulative_bytes": 56,
|
||||
"tags": [],
|
||||
"details": [
|
||||
{
|
||||
"address": "0.1.2.3",
|
||||
"rate": "224b",
|
||||
"rate_bits": 224,
|
||||
"cumulative": "56B",
|
||||
"cumulative_bytes": 56,
|
||||
"tags": ["local"],
|
||||
}
|
||||
],
|
||||
"rname": "fake value",
|
||||
"rate_in": "224.0 b",
|
||||
"rate_out": "0.0 b",
|
||||
"rate": "224.0 b",
|
||||
"cumulative_in": "56.0 b",
|
||||
"cumulative_out": "0.0 b",
|
||||
"cumulative": "56.0 b",
|
||||
},
|
||||
],
|
||||
"status": "ok",
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
|
@ -83,12 +83,12 @@ def test_get_interface_vip_status_unavailable_rest_api_error():
|
|||
def test_get_wan_traffic():
|
||||
responses.add(
|
||||
responses.GET,
|
||||
f"https://{MAIN_HOST}/api/diagnostics/traffic/interface",
|
||||
f"https://{MAIN_HOST}/api/diagnostics/traffic/top/wan",
|
||||
body=generate_diagnostics_traffic_interface_paylaod(),
|
||||
)
|
||||
assert OPNSenseAPI(MAIN_HOST, LOGIN, PASSWORD).get_wan_trafic() == (
|
||||
11725192686820,
|
||||
2489262014203,
|
||||
20538,
|
||||
10034,
|
||||
)
|
||||
|
||||
|
||||
|
@ -96,7 +96,7 @@ def test_get_wan_traffic():
|
|||
def test_get_wan_traffic_none():
|
||||
responses.add(
|
||||
responses.GET,
|
||||
f"https://{MAIN_HOST}/api/diagnostics/traffic/interface",
|
||||
f"https://{MAIN_HOST}/api/diagnostics/traffic/top/wan",
|
||||
json={"error": "not found"},
|
||||
status=404,
|
||||
)
|
||||
|
|
|
@ -87,7 +87,7 @@ def test_process_requests():
|
|||
)
|
||||
responses.add(
|
||||
responses.GET,
|
||||
f"https://{MAIN_HOST}/api/diagnostics/traffic/interface",
|
||||
f"https://{MAIN_HOST}/api/diagnostics/traffic/top/wan",
|
||||
body=generate_diagnostics_traffic_interface_paylaod(),
|
||||
)
|
||||
|
||||
|
@ -121,14 +121,14 @@ def test_process_requests():
|
|||
assert backup_ha_state_mock.count_state_calls == 1
|
||||
assert backup_ha_state_mock._labels == {"instance": "", "host": BACKUP_HOST}
|
||||
|
||||
assert active_server_bytes_received_mock.value == 11725192686820
|
||||
assert active_server_bytes_received_mock.value == 20538
|
||||
assert active_server_bytes_received_mock.count_set_calls == 1
|
||||
assert active_server_bytes_received_mock._labels == {
|
||||
"instance": "",
|
||||
"host": MAIN_HOST,
|
||||
}
|
||||
|
||||
assert active_server_bytes_transmitted_mock.value == 2489262014203
|
||||
assert active_server_bytes_transmitted_mock.value == 10034
|
||||
assert active_server_bytes_transmitted_mock.count_set_calls == 1
|
||||
assert active_server_bytes_transmitted_mock._labels == {
|
||||
"instance": "",
|
||||
|
@ -150,7 +150,7 @@ def test_process_requests_backup_active():
|
|||
)
|
||||
responses.add(
|
||||
responses.GET,
|
||||
f"https://{BACKUP_HOST}/api/diagnostics/traffic/interface",
|
||||
f"https://{BACKUP_HOST}/api/diagnostics/traffic/top/wan",
|
||||
body=generate_diagnostics_traffic_interface_paylaod(),
|
||||
)
|
||||
|
||||
|
@ -183,14 +183,14 @@ def test_process_requests_backup_active():
|
|||
assert backup_ha_state_mock.count_state_calls == 1
|
||||
assert backup_ha_state_mock._labels == {"instance": "", "host": BACKUP_HOST}
|
||||
|
||||
assert active_server_bytes_received_mock.value == 11725192686820
|
||||
assert active_server_bytes_received_mock.value == 20538
|
||||
assert active_server_bytes_received_mock.count_set_calls == 1
|
||||
assert active_server_bytes_received_mock._labels == {
|
||||
"instance": "",
|
||||
"host": BACKUP_HOST,
|
||||
}
|
||||
|
||||
assert active_server_bytes_transmitted_mock.value == 2489262014203
|
||||
assert active_server_bytes_transmitted_mock.value == 10034
|
||||
assert active_server_bytes_transmitted_mock.count_set_calls == 1
|
||||
assert active_server_bytes_transmitted_mock._labels == {
|
||||
"instance": "",
|
||||
|
@ -213,7 +213,7 @@ def test_process_no_active():
|
|||
)
|
||||
responses.add(
|
||||
responses.GET,
|
||||
f"https://{BACKUP_HOST}/api/diagnostics/traffic/interface",
|
||||
f"https://{BACKUP_HOST}/api/diagnostics/traffic/top/wan",
|
||||
body=generate_diagnostics_traffic_interface_paylaod(),
|
||||
)
|
||||
|
||||
|
@ -265,7 +265,7 @@ def test_process_with_falsy_value():
|
|||
)
|
||||
responses.add(
|
||||
responses.GET,
|
||||
f"https://{BACKUP_HOST}/api/diagnostics/traffic/interface",
|
||||
f"https://{BACKUP_HOST}/api/diagnostics/traffic/top/wan",
|
||||
body=generate_diagnostics_traffic_interface_paylaod(),
|
||||
status=404,
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue