Fixed imports, changed primary key for accounting hosts from ip address to mac address

This commit is contained in:
Ivan Pavlina 2020-04-07 11:32:38 +02:00
parent aa8f41f6fe
commit e51304a5e3
2 changed files with 118 additions and 89 deletions

View file

@ -3,7 +3,7 @@
import asyncio import asyncio
import logging import logging
from datetime import timedelta from datetime import timedelta
import ipaddress from ipaddress import ip_address, IPv4Network
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.dispatcher import async_dispatcher_send
@ -719,6 +719,7 @@ class MikrotikControllerData:
{"name": "status", "default": "unknown"}, {"name": "status", "default": "unknown"},
{"name": "last-seen", "default": "unknown"}, {"name": "last-seen", "default": "unknown"},
{"name": "server", "default": "unknown"}, {"name": "server", "default": "unknown"},
{"name": "comment"},
], ],
ensure_vals=[ ensure_vals=[
{"name": "interface"}, {"name": "interface"},
@ -732,33 +733,38 @@ class MikrotikControllerData:
self.data["dhcp"][uid]['available'] = \ self.data["dhcp"][uid]['available'] = \
self.api.arp_ping(self.data["dhcp"][uid]['address'], self.data["dhcp"][uid]['interface']) self.api.arp_ping(self.data["dhcp"][uid]['address'], self.data["dhcp"][uid]['interface'])
def build_accounting_hosts(self): def build_accounting_hosts(self):
# Build hosts from DHCP Server leases and ARP list # Build hosts from already retrieved DHCP Server leases
self.data["accounting"] = parse_api( for mac in self.data["dhcp"]:
data=self.data["accounting"], if mac not in self.data["accounting"]:
source=self.api.path("/ip/dhcp-server/lease", return_list=True), self.data["accounting"][mac] = self.data["dhcp"][mac]
key="address",
vals=[ # self.data["accounting"] = parse_api(
{"name": "address"}, # data=self.data["accounting"],
{"name": "mac-address"}, # source=self.api.path("/ip/dhcp-server/lease", return_list=True),
{"name": "host-name"}, # key="address",
{"name": "comment"}, # vals=[
{"name": "disabled", "default": True}, # {"name": "address"},
], # {"name": "mac-address"},
only=[ # {"name": "host-name"},
{"key": "disabled", "value": False}, # {"name": "comment"},
], # {"name": "disabled", "default": True},
ensure_vals=[ # ],
{"name": "address"}, # only=[
{"name": "mac-address"}, # {"key": "disabled", "value": False},
] # ],
) # ensure_vals=[
# {"name": "address"},
# {"name": "mac-address"},
# ]
# )
# Also retrieve all entries in ARP table. If some hosts are missing, build it here # Also retrieve all entries in ARP table. If some hosts are missing, build it here
arp_hosts = parse_api( arp_hosts = parse_api(
data={}, data={},
source=self.api.path("/ip/arp", return_list=True), source=self.api.path("/ip/arp", return_list=True),
key="address", key="mac-address",
vals=[ vals=[
{"name": "address"}, {"name": "address"},
{"name": "mac-address"}, {"name": "mac-address"},
@ -775,15 +781,14 @@ class MikrotikControllerData:
] ]
) )
for addr in arp_hosts: for mac in arp_hosts:
if addr not in self.data["accounting"]: if mac not in self.data["accounting"]:
self.data["accounting"][addr] = { self.data["accounting"][mac] = {
"address": arp_hosts[addr]['address'], "address": arp_hosts[mac]['address'],
"mac-address": arp_hosts[addr]['address'] "mac-address": arp_hosts[mac]['address']
} }
# Build name for host. First try getting DHCP lease comment, then entry in DNS (only static entries) # Build name for host.
# and then device's host-name. If everything fails use hosts IP address as name
dns_data = parse_api( dns_data = parse_api(
data={}, data={},
source=self.api.path("/ip/dns/static", return_list=True), source=self.api.path("/ip/dns/static", return_list=True),
@ -794,17 +799,22 @@ class MikrotikControllerData:
], ],
) )
for addr in self.data["accounting"]: for mac, vals in self.data["accounting"].items():
if str(self.data["accounting"][addr].get('comment', '').strip()): # First try getting DHCP lease comment
self.data["accounting"][addr]['name'] = self.data["accounting"][addr]['comment'] if str(vals.get('comment', '').strip()):
elif addr in dns_data and str(dns_data[addr].get('name', '').strip()): self.data["accounting"][mac]['name'] = vals['comment']
self.data["accounting"][addr]['name'] = dns_data[addr]['name'] # Then entry in static DNS entry
elif str(self.data["accounting"][addr].get('host-name', '').strip()): elif vals['address'] in dns_data and str(dns_data[vals['address']].get('name', '').strip()):
self.data["accounting"][addr]['name'] = self.data["accounting"][addr]['host-name'] self.data["accounting"][mac]['name'] = dns_data[vals['address']]['name']
# And then DHCP lease host-name
elif str(vals.get('host-name', '').strip()):
self.data["accounting"][mac]['name'] = vals['host-name']
# If everything fails use hosts IP address as name
else: else:
self.data["accounting"][addr]['name'] = self.data["accounting"][addr]['address'] self.data["accounting"][mac]['name'] = vals['address']
_LOGGER.debug(f"Generated {len(self.data['accounting'])} accounting devices") _LOGGER.debug(f"Generated {len(self.data['accounting'])} accounting devices")
_LOGGER.debug(self.data['accounting'])
# Build list of local networks # Build list of local networks
dhcp_networks = parse_api( dhcp_networks = parse_api(
@ -819,30 +829,36 @@ class MikrotikControllerData:
] ]
) )
self.local_dhcp_networks = [ipaddress.IPv4Network(network) for network in dhcp_networks] self.local_dhcp_networks = [IPv4Network(network) for network in dhcp_networks]
def _address_part_of_local_network(self, address): def _address_part_of_local_network(self, address):
address = ipaddress.ip_address(address) address = ip_address(address)
for network in self.local_dhcp_networks: for network in self.local_dhcp_networks:
if address in network: if address in network:
return True return True
return False return False
def _get_accounting_mac_by_ip(self, requested_ip):
for mac, vals in self.data['accounting'].items():
if vals.get('address') is requested_ip:
return mac
return None
def get_accounting(self): def get_accounting(self):
"""Get Accounting data from Mikrotik""" """Get Accounting data from Mikrotik"""
traffic_type, traffic_div = self._get_traffic_type_and_div() traffic_type, traffic_div = self._get_traffic_type_and_div()
# Build temp accounting values dict with all known addresses # Build temp accounting values dict with ip address as key
# Also set traffic type for each item # Also set traffic type for each item
tmp_accounting_values = {} tmp_accounting_values = {}
for addr in self.data['accounting']: for mac, vals in self.data['accounting'].items():
tmp_accounting_values[addr] = { tmp_accounting_values[vals['address']] = {
"wan-tx": 0, "wan-tx": 0,
"wan-rx": 0, "wan-rx": 0,
"lan-tx": 0, "lan-tx": 0,
"lan-rx": 0 "lan-rx": 0
} }
self.data['accounting'][addr]["tx-rx-attr"] = traffic_type self.data['accounting'][mac]["tx-rx-attr"] = traffic_type
time_diff = self.api.take_accounting_snapshot() time_diff = self.api.take_accounting_snapshot()
if time_diff: if time_diff:
@ -883,28 +899,38 @@ class MikrotikControllerData:
# Now that we have sum of all traffic in bytes for given period # Now that we have sum of all traffic in bytes for given period
# calculate real throughput and transform it to appropriate unit # calculate real throughput and transform it to appropriate unit
for addr in tmp_accounting_values: for addr in tmp_accounting_values:
self.data['accounting'][addr]['wan-tx'] = round( mac = self._get_accounting_mac_by_ip(addr)
if not mac:
_LOGGER.debug(f"Address {addr} not found in accounting data, skipping update")
continue
self.data['accounting'][mac]['wan-tx'] = round(
tmp_accounting_values[addr]['wan-tx'] / time_diff * traffic_div, 2) tmp_accounting_values[addr]['wan-tx'] / time_diff * traffic_div, 2)
self.data['accounting'][addr]['wan-rx'] = round( self.data['accounting'][mac]['wan-rx'] = round(
tmp_accounting_values[addr]['wan-rx'] / time_diff * traffic_div, 2) tmp_accounting_values[addr]['wan-rx'] / time_diff * traffic_div, 2)
if self.api.is_accounting_local_traffic_enabled(): if self.api.is_accounting_local_traffic_enabled():
self.data['accounting'][addr]['lan-tx'] = round( self.data['accounting'][mac]['lan-tx'] = round(
tmp_accounting_values[addr]['lan-tx'] / time_diff * traffic_div, 2) tmp_accounting_values[addr]['lan-tx'] / time_diff * traffic_div, 2)
self.data['accounting'][addr]['lan-rx'] = round( self.data['accounting'][mac]['lan-rx'] = round(
tmp_accounting_values[addr]['lan-rx'] / time_diff * traffic_div, 2) tmp_accounting_values[addr]['lan-rx'] / time_diff * traffic_div, 2)
else: else:
# If local traffic was enabled earlier and then disabled return counters for LAN traffic to 0 # If local traffic was enabled earlier and then disabled return counters for LAN traffic to 0
if 'lan-tx' in self.data['accounting'][addr]: if 'lan-tx' in self.data['accounting'][mac]:
self.data['accounting'][addr]['lan-tx'] = 0.0 self.data['accounting'][mac]['lan-tx'] = 0.0
if 'lan-rx' in self.data['accounting'][addr]: if 'lan-rx' in self.data['accounting'][mac]:
self.data['accounting'][addr]['lan-rx'] = 0.0 self.data['accounting'][mac]['lan-rx'] = 0.0
else: else:
# No time diff, just initialize/return counters to 0 for all # No time diff, just initialize/return counters to 0 for all
for addr in tmp_accounting_values: for addr in tmp_accounting_values:
self.data['accounting'][addr]['wan-tx'] = 0.0 mac = self._get_accounting_mac_by_ip(addr)
self.data['accounting'][addr]['wan-rx'] = 0.0 if not mac:
_LOGGER.debug(f"Address {addr} not found in accounting data, skipping update")
continue
self.data['accounting'][mac]['wan-tx'] = 0.0
self.data['accounting'][mac]['wan-rx'] = 0.0
if self.api.is_accounting_local_traffic_enabled(): if self.api.is_accounting_local_traffic_enabled():
self.data['accounting'][addr]['lan-tx'] = 0.0 self.data['accounting'][mac]['lan-tx'] = 0.0
self.data['accounting'][addr]['lan-rx'] = 0.0 self.data['accounting'][mac]['lan-rx'] = 0.0

View file

@ -466,26 +466,32 @@ class MikrotikAPI:
return False return False
@staticmethod @staticmethod
def _current_milliseconds(): def _current_milliseconds():
from time import time from time import time
return int(round(time() * 1000)) return int(round(time() * 1000))
def is_accounting_enabled(self) -> bool: def is_accounting_enabled(self) -> bool:
accounting = self.path("/ip/accounting", return_list=True) if not self.connection_check():
if accounting is None:
return False return False
for item in accounting: response = self.path("/ip/accounting")
if response is None:
return False
for item in response:
if 'enabled' not in item: if 'enabled' not in item:
continue continue
if item['enabled']: if item['enabled']:
return True return True
return False return False
def is_accounting_local_traffic_enabled(self) -> bool: def is_accounting_local_traffic_enabled(self) -> bool:
accounting = self.path("/ip/accounting", return_list=True) if not self.connection_check():
return False
accounting = self.path("/ip/accounting")
if accounting is None: if accounting is None:
return False return False
@ -502,48 +508,45 @@ class MikrotikAPI:
# --------------------------- # ---------------------------
def take_accounting_snapshot(self) -> float: def take_accounting_snapshot(self) -> float:
"""Get accounting data""" """Get accounting data"""
if not self._connected or not self._connection: if not self.connection_check():
if self._connection_epoch > time.time() - self._connection_retry_sec:
return 0 return 0
if not self.connect(): accounting = self.path("/ip/accounting", return_list=False)
return 0
accounting = self.path("/ip/accounting")
self.lock.acquire() self.lock.acquire()
try: try:
# Prepare command # Prepare command
take = accounting('snapshot/take') take = accounting('snapshot/take')
# Run command on Mikrotik
tuple(take)
except librouteros_custom.exceptions.ConnectionClosed: except librouteros_custom.exceptions.ConnectionClosed:
if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s connection closed", self._host)
self.connection_error_reported = True
self.disconnect() self.disconnect()
self.lock.release() self.lock.release()
return 0 return 0
if not self.connection_error_reported: except (
_LOGGER.error( librouteros_custom.exceptions.TrapError,
"Mikrotik %s error while take_accounting_snapshot %s -> %s - %s", self._host, librouteros_custom.exceptions.MultiTrapError,
type(api_error), api_error.args librouteros_custom.exceptions.ProtocolError,
) librouteros_custom.exceptions.FatalError,
self.connection_error_reported = True ssl.SSLError,
BrokenPipeError,
self.disconnect() OSError,
ValueError,
) as api_error:
self.disconnect("accounting_snapshot", api_error)
self.lock.release()
return 0
except:
self.disconnect("accounting_snapshot")
self.lock.release() self.lock.release()
return 0 return 0
except Exception as e:
if not self.connection_error_reported:
_LOGGER.error(
"% -> %s error on %s host while take_accounting_snapshot",
type(e), e.args, self._host,
)
self.connection_error_reported = True
self.disconnect() try:
list(take)
except librouteros_custom.exceptions.ConnectionClosed as api_error:
self.disconnect("accounting_snapshot", api_error)
self.lock.release()
return 0
except:
self.disconnect("accounting_snapshot")
self.lock.release() self.lock.release()
return 0 return 0