use proper api endpoint to compute rate trafic on wan interface

This commit is contained in:
Pierre Verkest 2023-09-02 02:16:27 +02:00
parent 564d721d31
commit 65179c8b45
4 changed files with 194 additions and 134 deletions

View file

@ -54,7 +54,7 @@ class OPNSenseAPI:
def get_wan_trafic(self): def get_wan_trafic(self):
try: try:
data = self.get("/api/diagnostics/traffic/interface") data = self.get("/api/diagnostics/traffic/top/wan")
except RequestException as ex: except RequestException as ex:
logger.error( logger.error(
"Get diagnostics traffic on WAN interface for %s host failed with the following error %r", "Get diagnostics traffic on WAN interface for %s host failed with the following error %r",
@ -62,29 +62,10 @@ class OPNSenseAPI:
ex, ex,
) )
return None, None return None, None
return (
int(data["interfaces"]["wan"]["bytes received"]),
int(data["interfaces"]["wan"]["bytes transmitted"]),
)
received = 0
# def get_server_system_status(self): transmitted = 0
# # https://192.168.200.1/api/core/system/status for record in data["wan"]["records"]:
# return { received += record["rate_bits_in"]
# "CrashReporter": transmitted += record["rate_bits_out"]
# { return received, transmitted
# "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"}
# }

View file

@ -49,101 +49,180 @@ def generate_get_vip_status_paylaod(state_wan, state_lan, maintenance_mode):
def generate_diagnostics_traffic_interface_paylaod(): def generate_diagnostics_traffic_interface_paylaod():
return json.dumps( return json.dumps(
{ {
"interfaces": { "wan": {
"lan": { "records": [
"index": "2", {
"flags": "8963", "address": "0.1.2.3",
"promiscuous listeners": "1", "rate_bits_in": 15300,
"send queue length": "0", "rate_bits_out": 1720,
"send queue max length": "50", "rate_bits": 17020,
"send queue drops": "0", "cumulative_bytes_in": 3830,
"type": "Ethernet", "cumulative_bytes_out": 441,
"address length": "6", "cumulative_bytes": 4271,
"header length": "18", "tags": [],
"link state": "2", "details": [
"vhid": "0", {
"datalen": "152", "address": "0.1.2.3",
"mtu": "1500", "rate": "15.3Kb",
"metric": "0", "rate_bits": 15300,
"line rate": "10000000000 bit/s", "cumulative": "3.83KB",
"packets received": "3699327747", "cumulative_bytes": 3830,
"input errors": "0", "tags": ["local"],
"packets transmitted": "8972963403", }
"output errors": "0", ],
"collisions": "0", "rname": "fake value",
"bytes received": "2474843996609", "rate_in": "15.3 kb",
"bytes transmitted": "11711737078752", "rate_out": "1.72 kb",
"multicasts received": "29274204", "rate": "17.02 kb",
"multicasts transmitted": "0", "cumulative_in": "3.83 kb",
"input queue drops": "0", "cumulative_out": "441.0 b",
"packets for unknown protocol": "0", "cumulative": "4.27 kb",
"HW offload capabilities": "0x0", },
"uptime at attach or stat reset": "1", {
"name": "LAN", "address": "0.1.2.3",
}, "rate_bits_in": 4470,
"opt1": { "rate_bits_out": 7290,
"index": "3", "rate_bits": 11760,
"flags": "8863", "cumulative_bytes_in": 1120,
"promiscuous listeners": "0", "cumulative_bytes_out": 1820,
"send queue length": "0", "cumulative_bytes": 2940,
"send queue max length": "50", "tags": [],
"send queue drops": "0", "details": [
"type": "Ethernet", {
"address length": "6", "address": "0.1.2.3",
"header length": "18", "rate": "4.47Kb",
"link state": "2", "rate_bits": 4470,
"vhid": "0", "cumulative": "1.12KB",
"datalen": "152", "cumulative_bytes": 1120,
"mtu": "1500", "tags": ["local"],
"metric": "0", }
"line rate": "10000000000 bit/s", ],
"packets received": "1120457", "rname": "fake value",
"input errors": "0", "rate_in": "4.47 kb",
"packets transmitted": "178007891", "rate_out": "7.29 kb",
"output errors": "0", "rate": "11.76 kb",
"collisions": "0", "cumulative_in": "1.12 kb",
"bytes received": "221674964", "cumulative_out": "1.82 kb",
"bytes transmitted": "55808795987", "cumulative": "2.94 kb",
"multicasts received": "393767", },
"multicasts transmitted": "0", {
"input queue drops": "0", "address": "0.1.2.3",
"packets for unknown protocol": "0", "rate_bits_in": 272,
"HW offload capabilities": "0x0", "rate_bits_out": 272,
"uptime at attach or stat reset": "1", "rate_bits": 544,
"name": "LANOPNSYNC", "cumulative_bytes_in": 68,
}, "cumulative_bytes_out": 68,
"wan": { "cumulative_bytes": 136,
"index": "1", "tags": [],
"flags": "8963", "details": [
"promiscuous listeners": "1", {
"send queue length": "0", "address": "0.1.2.3",
"send queue max length": "50", "rate": "272b",
"send queue drops": "0", "rate_bits": 272,
"type": "Ethernet", "cumulative": "68B",
"address length": "6", "cumulative_bytes": 68,
"header length": "18", "tags": ["local"],
"link state": "2", }
"vhid": "0", ],
"datalen": "152", "rname": "fake value",
"mtu": "1500", "rate_in": "272.0 b",
"metric": "0", "rate_out": "272.0 b",
"line rate": "10000000000 bit/s", "rate": "544.0 b",
"packets received": "9008014155", "cumulative_in": "68.0 b",
"input errors": "0", "cumulative_out": "68.0 b",
"packets transmitted": "3724630846", "cumulative": "136.0 b",
"output errors": "0", },
"collisions": "0", {
"bytes received": "11725192686820", "address": "0.1.2.3",
"bytes transmitted": "2489262014203", "rate_bits_in": 272,
"multicasts received": "2288794", "rate_bits_out": 272,
"multicasts transmitted": "0", "rate_bits": 544,
"input queue drops": "0", "cumulative_bytes_in": 68,
"packets for unknown protocol": "0", "cumulative_bytes_out": 68,
"HW offload capabilities": "0x0", "cumulative_bytes": 136,
"uptime at attach or stat reset": "1", "tags": [],
"name": "WAN", "details": [
}, {
}, "address": "0.1.2.3",
"time": 1693404742.796736, "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",
}
} }
) )

View file

@ -83,12 +83,12 @@ def test_get_interface_vip_status_unavailable_rest_api_error():
def test_get_wan_traffic(): def test_get_wan_traffic():
responses.add( responses.add(
responses.GET, 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(), body=generate_diagnostics_traffic_interface_paylaod(),
) )
assert OPNSenseAPI(MAIN_HOST, LOGIN, PASSWORD).get_wan_trafic() == ( assert OPNSenseAPI(MAIN_HOST, LOGIN, PASSWORD).get_wan_trafic() == (
11725192686820, 20538,
2489262014203, 10034,
) )
@ -96,7 +96,7 @@ def test_get_wan_traffic():
def test_get_wan_traffic_none(): def test_get_wan_traffic_none():
responses.add( responses.add(
responses.GET, responses.GET,
f"https://{MAIN_HOST}/api/diagnostics/traffic/interface", f"https://{MAIN_HOST}/api/diagnostics/traffic/top/wan",
json={"error": "not found"}, json={"error": "not found"},
status=404, status=404,
) )

View file

@ -87,7 +87,7 @@ def test_process_requests():
) )
responses.add( responses.add(
responses.GET, 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(), 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.count_state_calls == 1
assert backup_ha_state_mock._labels == {"instance": "", "host": BACKUP_HOST} 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.count_set_calls == 1
assert active_server_bytes_received_mock._labels == { assert active_server_bytes_received_mock._labels == {
"instance": "", "instance": "",
"host": MAIN_HOST, "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.count_set_calls == 1
assert active_server_bytes_transmitted_mock._labels == { assert active_server_bytes_transmitted_mock._labels == {
"instance": "", "instance": "",
@ -150,7 +150,7 @@ def test_process_requests_backup_active():
) )
responses.add( responses.add(
responses.GET, 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(), 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.count_state_calls == 1
assert backup_ha_state_mock._labels == {"instance": "", "host": BACKUP_HOST} 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.count_set_calls == 1
assert active_server_bytes_received_mock._labels == { assert active_server_bytes_received_mock._labels == {
"instance": "", "instance": "",
"host": BACKUP_HOST, "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.count_set_calls == 1
assert active_server_bytes_transmitted_mock._labels == { assert active_server_bytes_transmitted_mock._labels == {
"instance": "", "instance": "",
@ -213,7 +213,7 @@ def test_process_no_active():
) )
responses.add( responses.add(
responses.GET, 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(), body=generate_diagnostics_traffic_interface_paylaod(),
) )
@ -265,7 +265,7 @@ def test_process_with_falsy_value():
) )
responses.add( responses.add(
responses.GET, 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(), body=generate_diagnostics_traffic_interface_paylaod(),
status=404, status=404,
) )