Improved interface bandwidth tracking, fixes #166

This commit is contained in:
Tomaae 2022-03-03 10:12:05 +01:00
parent 25a09498d6
commit 5d04872032
3 changed files with 43 additions and 29 deletions

View file

@ -679,8 +679,10 @@ class MikrotikControllerData:
ensure_vals=[ ensure_vals=[
{"name": "client-ip-address"}, {"name": "client-ip-address"},
{"name": "client-mac-address"}, {"name": "client-mac-address"},
{"name": "rx-bits-per-second", "default": 0}, {"name": "rx-previous", "default": 0.0},
{"name": "tx-bits-per-second", "default": 0}, {"name": "tx-previous", "default": 0.0},
{"name": "rx", "default": 0.0},
{"name": "tx", "default": 0.0},
], ],
skip=[ skip=[
{"name": "type", "value": "bridge"}, {"name": "type", "value": "bridge"},
@ -773,33 +775,44 @@ class MikrotikControllerData:
# --------------------------- # ---------------------------
def get_interface_traffic(self): def get_interface_traffic(self):
"""Get traffic for all interfaces from Mikrotik""" """Get traffic for all interfaces from Mikrotik"""
interface_list = "" tmp_data = parse_api(
for uid in self.data["interface"]: data={},
interface_list += str(self.data["interface"][uid]["name"]) + "," source=self.api.get_traffic(),
key="default-name",
interface_list = interface_list[:-1]
self.data["interface"] = parse_api(
data=self.data["interface"],
source=self.api.get_traffic(interface_list),
key_search="name",
vals=[ vals=[
{"name": "rx-bits-per-second", "default": 0}, {"name": "rx-byte", "default": 0.0},
{"name": "tx-bits-per-second", "default": 0}, {"name": "tx-byte", "default": 0.0},
], ],
) )
uom_type, uom_div = self._get_unit_of_measurement() uom_type, uom_div = self._get_unit_of_measurement()
for uid in self.data["interface"]: for uid in self.data["interface"]:
self.data["interface"][uid]["rx-bits-per-second-attr"] = uom_type self.data["interface"][uid]["rx-attr"] = uom_type
self.data["interface"][uid]["tx-bits-per-second-attr"] = uom_type self.data["interface"][uid]["tx-attr"] = uom_type
self.data["interface"][uid]["rx-bits-per-second"] = round( if uid not in tmp_data:
self.data["interface"][uid]["rx-bits-per-second"] * uom_div 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-bits-per-second"] = round( self.data["interface"][uid]["tx-previous"] = current_tx
self.data["interface"][uid]["tx-bits-per-second"] * uom_div
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_bridge # get_bridge

View file

@ -437,7 +437,7 @@ class MikrotikAPI:
# --------------------------- # ---------------------------
# get_traffic # get_traffic
# --------------------------- # ---------------------------
def get_traffic(self, interfaces) -> Optional(list): def get_traffic(self) -> Optional(list):
"""Get traffic stats""" """Get traffic stats"""
if not self.connection_check(): if not self.connection_check():
return None return None
@ -446,11 +446,11 @@ class MikrotikAPI:
if response is None: if response is None:
return None return None
args = {"interface": interfaces, "once": True} args = {"stats": True}
self.lock.acquire() self.lock.acquire()
try: try:
_LOGGER.debug("API query: %s", "/interface/monitor-traffic") _LOGGER.debug("API query: %s", "/interface/print stats")
traffic = response("monitor-traffic", **args) traffic = response("print", **args)
except librouteros.exceptions.ConnectionClosed: except librouteros.exceptions.ConnectionClosed:
self.disconnect() self.disconnect()
self.lock.release() self.lock.release()
@ -487,6 +487,7 @@ class MikrotikAPI:
return None return None
self.lock.release() self.lock.release()
return traffic if traffic else None return traffic if traffic else None
# --------------------------- # ---------------------------

View file

@ -281,7 +281,7 @@ SENSOR_TYPES = {
key="traffic_tx", key="traffic_tx",
name="TX", name="TX",
icon="mdi:upload-network-outline", icon="mdi:upload-network-outline",
native_unit_of_measurement="data__tx-bits-per-second-attr", native_unit_of_measurement="data__tx-attr",
device_class=None, device_class=None,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_category=None, entity_category=None,
@ -289,7 +289,7 @@ SENSOR_TYPES = {
ha_connection=CONNECTION_NETWORK_MAC, ha_connection=CONNECTION_NETWORK_MAC,
ha_connection_value="data__port-mac-address", ha_connection_value="data__port-mac-address",
data_path="interface", data_path="interface",
data_attribute="tx-bits-per-second", data_attribute="tx",
data_name="name", data_name="name",
data_uid="", data_uid="",
data_reference="default-name", data_reference="default-name",
@ -299,7 +299,7 @@ SENSOR_TYPES = {
key="traffic_rx", key="traffic_rx",
name="RX", name="RX",
icon="mdi:download-network-outline", icon="mdi:download-network-outline",
native_unit_of_measurement="data__rx-bits-per-second-attr", native_unit_of_measurement="data__rx-attr",
device_class=None, device_class=None,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_category=None, entity_category=None,
@ -307,7 +307,7 @@ SENSOR_TYPES = {
ha_connection=CONNECTION_NETWORK_MAC, ha_connection=CONNECTION_NETWORK_MAC,
ha_connection_value="data__port-mac-address", ha_connection_value="data__port-mac-address",
data_path="interface", data_path="interface",
data_attribute="rx-bits-per-second", data_attribute="rx",
data_name="name", data_name="name",
data_uid="", data_uid="",
data_reference="default-name", data_reference="default-name",