mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-12 18:34:30 +02:00
Cleanup interface bandwidth tracking, ref #166
This commit is contained in:
parent
aee405a9f0
commit
4fbd5a4280
2 changed files with 30 additions and 103 deletions
|
@ -613,9 +613,6 @@ class MikrotikControllerData:
|
||||||
if self.api.connected():
|
if self.api.connected():
|
||||||
await self.async_process_host()
|
await self.async_process_host()
|
||||||
|
|
||||||
if self.api.connected() and self.option_sensor_port_traffic:
|
|
||||||
await self.hass.async_add_executor_job(self.get_interface_traffic)
|
|
||||||
|
|
||||||
if self.api.connected():
|
if self.api.connected():
|
||||||
await self.hass.async_add_executor_job(self.process_interface_client)
|
await self.hass.async_add_executor_job(self.process_interface_client)
|
||||||
|
|
||||||
|
@ -679,6 +676,8 @@ class MikrotikControllerData:
|
||||||
{"name": "tx-queue-drop"},
|
{"name": "tx-queue-drop"},
|
||||||
{"name": "actual-mtu"},
|
{"name": "actual-mtu"},
|
||||||
{"name": "about", "source": ".about", "default": ""},
|
{"name": "about", "source": ".about", "default": ""},
|
||||||
|
{"name": "rx-current", "source": "rx-byte", "default": 0.0},
|
||||||
|
{"name": "tx-current", "source": "tx-byte", "default": 0.0},
|
||||||
],
|
],
|
||||||
ensure_vals=[
|
ensure_vals=[
|
||||||
{"name": "client-ip-address"},
|
{"name": "client-ip-address"},
|
||||||
|
@ -699,6 +698,34 @@ class MikrotikControllerData:
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if self.option_sensor_port_traffic:
|
||||||
|
uom_type, uom_div = self._get_unit_of_measurement()
|
||||||
|
for uid, vals in self.data["interface"].items():
|
||||||
|
self.data["interface"][uid]["rx-attr"] = uom_type
|
||||||
|
self.data["interface"][uid]["tx-attr"] = uom_type
|
||||||
|
|
||||||
|
current_tx = vals["tx-current"]
|
||||||
|
previous_tx = vals["tx-previous"]
|
||||||
|
if not previous_tx:
|
||||||
|
previous_tx = current_tx
|
||||||
|
|
||||||
|
delta_tx = max(0, current_tx - previous_tx) * 8
|
||||||
|
self.data["interface"][uid]["tx"] = round(
|
||||||
|
delta_tx / self.option_scan_interval.seconds * uom_div, 2
|
||||||
|
)
|
||||||
|
self.data["interface"][uid]["tx-previous"] = current_tx
|
||||||
|
|
||||||
|
current_rx = vals["rx-current"]
|
||||||
|
previous_rx = vals["rx-previous"]
|
||||||
|
if not previous_rx:
|
||||||
|
previous_rx = current_rx
|
||||||
|
|
||||||
|
delta_rx = max(0, current_rx - previous_rx) * 8
|
||||||
|
self.data["interface"][uid]["rx"] = round(
|
||||||
|
delta_rx / self.option_scan_interval.seconds * uom_div, 2
|
||||||
|
)
|
||||||
|
self.data["interface"][uid]["rx-previous"] = current_rx
|
||||||
|
|
||||||
self.data["interface"] = parse_api(
|
self.data["interface"] = parse_api(
|
||||||
data=self.data["interface"],
|
data=self.data["interface"],
|
||||||
source=self.api.path("/interface/ethernet"),
|
source=self.api.path("/interface/ethernet"),
|
||||||
|
@ -774,50 +801,6 @@ class MikrotikControllerData:
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---------------------------
|
|
||||||
# get_interface_traffic
|
|
||||||
# ---------------------------
|
|
||||||
def get_interface_traffic(self):
|
|
||||||
"""Get traffic for all interfaces from Mikrotik"""
|
|
||||||
tmp_data = parse_api(
|
|
||||||
data={},
|
|
||||||
source=self.api.get_traffic(),
|
|
||||||
key="default-name",
|
|
||||||
vals=[
|
|
||||||
{"name": "rx-byte", "default": 0.0},
|
|
||||||
{"name": "tx-byte", "default": 0.0},
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
uom_type, uom_div = self._get_unit_of_measurement()
|
|
||||||
for uid in self.data["interface"]:
|
|
||||||
self.data["interface"][uid]["rx-attr"] = uom_type
|
|
||||||
self.data["interface"][uid]["tx-attr"] = uom_type
|
|
||||||
if uid not in tmp_data:
|
|
||||||
continue
|
|
||||||
|
|
||||||
current_tx = tmp_data[uid]["tx-byte"]
|
|
||||||
previous_tx = self.data["interface"][uid]["tx-previous"]
|
|
||||||
if not previous_tx:
|
|
||||||
previous_tx = current_tx
|
|
||||||
|
|
||||||
delta_tx = max(0, current_tx - previous_tx) * 8
|
|
||||||
self.data["interface"][uid]["tx"] = round(
|
|
||||||
delta_tx / self.option_scan_interval.seconds * uom_div, 2
|
|
||||||
)
|
|
||||||
self.data["interface"][uid]["tx-previous"] = current_tx
|
|
||||||
|
|
||||||
current_rx = tmp_data[uid]["rx-byte"]
|
|
||||||
previous_rx = self.data["interface"][uid]["rx-previous"]
|
|
||||||
if not previous_rx:
|
|
||||||
previous_rx = current_rx
|
|
||||||
|
|
||||||
delta_rx = max(0, current_rx - previous_rx) * 8
|
|
||||||
self.data["interface"][uid]["rx"] = round(
|
|
||||||
delta_rx / self.option_scan_interval.seconds * uom_div, 2
|
|
||||||
)
|
|
||||||
self.data["interface"][uid]["rx-previous"] = current_rx
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# get_route
|
# get_route
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
|
|
@ -434,62 +434,6 @@ class MikrotikAPI:
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# ---------------------------
|
|
||||||
# get_traffic
|
|
||||||
# ---------------------------
|
|
||||||
def get_traffic(self) -> Optional(list):
|
|
||||||
"""Get traffic stats"""
|
|
||||||
if not self.connection_check():
|
|
||||||
return None
|
|
||||||
|
|
||||||
response = self.path("/interface", return_list=False)
|
|
||||||
if response is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
args = {"stats": True}
|
|
||||||
self.lock.acquire()
|
|
||||||
try:
|
|
||||||
_LOGGER.debug("API query: %s", "/interface/print stats")
|
|
||||||
traffic = response("print", **args)
|
|
||||||
except librouteros.exceptions.ConnectionClosed:
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
|
||||||
return None
|
|
||||||
except (
|
|
||||||
librouteros.exceptions.TrapError,
|
|
||||||
librouteros.exceptions.MultiTrapError,
|
|
||||||
librouteros.exceptions.ProtocolError,
|
|
||||||
librouteros.exceptions.FatalError,
|
|
||||||
ssl.SSLError,
|
|
||||||
socket_timeout,
|
|
||||||
socket_error,
|
|
||||||
BrokenPipeError,
|
|
||||||
OSError,
|
|
||||||
ValueError,
|
|
||||||
) as api_error:
|
|
||||||
self.disconnect("get_traffic", api_error)
|
|
||||||
self.lock.release()
|
|
||||||
return None
|
|
||||||
except:
|
|
||||||
self.disconnect("get_traffic")
|
|
||||||
self.lock.release()
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
|
||||||
traffic = list(traffic)
|
|
||||||
except librouteros.exceptions.ConnectionClosed as api_error:
|
|
||||||
self.disconnect("get_traffic", api_error)
|
|
||||||
self.lock.release()
|
|
||||||
return None
|
|
||||||
except:
|
|
||||||
self.disconnect("get_traffic")
|
|
||||||
self.lock.release()
|
|
||||||
return None
|
|
||||||
|
|
||||||
self.lock.release()
|
|
||||||
|
|
||||||
return traffic if traffic else None
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# get_sfp
|
# get_sfp
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue