Allow empty string interface to not call the OPNsense traffic diagnostic REST API endpoint
This commit is contained in:
parent
4fbd6c94c7
commit
d56f8dba6d
4 changed files with 27 additions and 4 deletions
10
README.md
10
README.md
|
@ -75,8 +75,11 @@ optional arguments:
|
||||||
OPNsense user. Expect to be the same on MAIN and
|
OPNsense user. Expect to be the same on MAIN and
|
||||||
BACKUP servers
|
BACKUP servers
|
||||||
--opnsense-interfaces INTERFACES, -i INTERFACES
|
--opnsense-interfaces INTERFACES, -i INTERFACES
|
||||||
OPNsense interfaces (coma separated) list to export
|
OPNsense interfaces (coma separated) list to
|
||||||
trafic rates (bytes/s) (default: wan,lan)
|
export trafic rates (bytes/s). An empty string ''
|
||||||
|
means not calling the traffic diagnostic REST API
|
||||||
|
so no `opnsense_active_server_traffic_rate`
|
||||||
|
metric. (default: wan,lan)
|
||||||
--opnsense-password PASSWORD, -p PASSWORD
|
--opnsense-password PASSWORD, -p PASSWORD
|
||||||
OPNsense password. Expect to be the same on MAIN
|
OPNsense password. Expect to be the same on MAIN
|
||||||
and BACKUP servers
|
and BACKUP servers
|
||||||
|
@ -109,6 +112,9 @@ You can setup env through `.env` file or environment variables with defined as d
|
||||||
- remove `opnsense_main_ha_state` and `opnsense_backup_ha_state`
|
- remove `opnsense_main_ha_state` and `opnsense_backup_ha_state`
|
||||||
metrics marked as deprecated on version 0.5.0 and replace
|
metrics marked as deprecated on version 0.5.0 and replace
|
||||||
by `opnsense_server_ha_state` and `role` label
|
by `opnsense_server_ha_state` and `role` label
|
||||||
|
- allow empty string interfaces to **not** call diagnostic
|
||||||
|
traffic REST API
|
||||||
|
|
||||||
|
|
||||||
### Version 0.5.1 (2023-09-04)
|
### Version 0.5.1 (2023-09-04)
|
||||||
|
|
||||||
|
|
|
@ -106,6 +106,8 @@ class OPNSenseAPI:
|
||||||
return OPNSenseHAState.UNAVAILABLE
|
return OPNSenseHAState.UNAVAILABLE
|
||||||
|
|
||||||
def get_traffic(self, interfaces):
|
def get_traffic(self, interfaces):
|
||||||
|
if not interfaces:
|
||||||
|
return []
|
||||||
try:
|
try:
|
||||||
data = self.get(f"/api/diagnostics/traffic/top/{interfaces}", timeout=15)
|
data = self.get(f"/api/diagnostics/traffic/top/{interfaces}", timeout=15)
|
||||||
except RequestException as ex:
|
except RequestException as ex:
|
||||||
|
@ -120,7 +122,7 @@ class OPNSenseAPI:
|
||||||
for interface in interfaces.split(","):
|
for interface in interfaces.split(","):
|
||||||
traffic_in = OPNSenseTraffic(interface, OPNSenseTrafficMetric.IN)
|
traffic_in = OPNSenseTraffic(interface, OPNSenseTrafficMetric.IN)
|
||||||
traffic_out = OPNSenseTraffic(interface, OPNSenseTrafficMetric.OUT)
|
traffic_out = OPNSenseTraffic(interface, OPNSenseTrafficMetric.OUT)
|
||||||
for record in data.get(interface, []).get("records", []):
|
for record in data.get(interface, {}).get("records", []):
|
||||||
traffic_in.value += record.get(OPNSenseTrafficMetric.IN.value, 0)
|
traffic_in.value += record.get(OPNSenseTrafficMetric.IN.value, 0)
|
||||||
traffic_out.value += record.get(OPNSenseTrafficMetric.OUT.value, 0)
|
traffic_out.value += record.get(OPNSenseTrafficMetric.OUT.value, 0)
|
||||||
traffics.extend([traffic_in, traffic_out])
|
traffics.extend([traffic_in, traffic_out])
|
||||||
|
|
|
@ -131,7 +131,9 @@ def run():
|
||||||
type=str,
|
type=str,
|
||||||
dest="interfaces",
|
dest="interfaces",
|
||||||
default=os.environ.get("OPNSENSE_INTERFACES", "wan,lan"),
|
default=os.environ.get("OPNSENSE_INTERFACES", "wan,lan"),
|
||||||
help="OPNsense interfaces (coma separated) list to export trafic rates (bytes/s)",
|
help="OPNsense interfaces (coma separated) list to export trafic rates (bytes/s). "
|
||||||
|
"An empty string '' means not calling the traffic diagnostic REST API so no "
|
||||||
|
"`opnsense_active_server_traffic_rate` metric.",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--opnsense-password",
|
"--opnsense-password",
|
||||||
|
|
|
@ -129,6 +129,19 @@ def test_get_traffic_none():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@responses.activate
|
||||||
|
def test_get_traffic_empty_string():
|
||||||
|
rsp = responses.add(
|
||||||
|
responses.GET,
|
||||||
|
f"https://{MAIN_HOST}/api/diagnostics/traffic/top/",
|
||||||
|
json={"not": "called"},
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD).get_traffic("") == []
|
||||||
|
)
|
||||||
|
assert rsp.call_count == 0
|
||||||
|
|
||||||
|
|
||||||
def test_labels():
|
def test_labels():
|
||||||
assert OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD).labels == {
|
assert OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD).labels == {
|
||||||
"role": "main",
|
"role": "main",
|
||||||
|
|
Loading…
Reference in a new issue