tomaae.homeassistant-mikrot.../custom_components/mikrotik_router/mikrotik_controller.py

562 lines
18 KiB
Python
Raw Normal View History

"""Mikrotik Controller for Mikrotik Router."""
from datetime import timedelta
2019-12-12 23:01:57 +01:00
import asyncio
import logging
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from .const import (
2019-12-02 18:13:55 +01:00
DOMAIN,
CONF_TRACK_ARP,
DEFAULT_TRACK_ARP,
CONF_SCAN_INTERVAL,
CONF_UNIT_OF_MEASUREMENT,
2019-12-02 18:13:55 +01:00
DEFAULT_SCAN_INTERVAL,
DEFAULT_TRAFFIC_TYPE,
)
from .mikrotikapi import MikrotikAPI
from .helper import from_entry, parse_api
2019-12-12 23:01:57 +01:00
from .exceptions import ApiEntryNotFound
2019-12-02 17:59:49 +01:00
_LOGGER = logging.getLogger(__name__)
2019-12-02 17:59:49 +01:00
# ---------------------------
# MikrotikControllerData
2019-12-02 17:59:49 +01:00
# ---------------------------
2020-03-16 04:51:41 +01:00
class MikrotikControllerData:
"""MikrotikController Class"""
2020-03-16 04:51:41 +01:00
def __init__(
self,
hass,
config_entry,
name,
host,
port,
username,
password,
use_ssl,
traffic_type,
):
"""Initialize MikrotikController."""
2019-12-02 18:13:55 +01:00
self.name = name
self.hass = hass
self.config_entry = config_entry
self.traffic_type = traffic_type
2020-03-16 04:51:41 +01:00
self.data = {
"routerboard": {},
"resource": {},
"interface": {},
"arp": {},
"nat": {},
"fw-update": {},
"script": {},
}
2019-12-02 18:13:55 +01:00
self.listeners = []
2019-12-12 23:01:57 +01:00
self.lock = asyncio.Lock()
2019-12-02 18:13:55 +01:00
self.api = MikrotikAPI(host, username, password, port, use_ssl)
2020-03-16 04:51:41 +01:00
async_track_time_interval(
self.hass, self.force_update, self.option_scan_interval
)
async_track_time_interval(
self.hass, self.force_fwupdate_check, timedelta(hours=1)
)
2019-12-02 19:23:09 +01:00
# ---------------------------
# force_update
# ---------------------------
2019-12-05 22:10:42 +01:00
async def force_update(self, _now=None):
"""Trigger update by timer"""
2019-12-02 18:13:55 +01:00
await self.async_update()
# ---------------------------
# force_fwupdate_check
# ---------------------------
2019-12-05 22:10:42 +01:00
async def force_fwupdate_check(self, _now=None):
"""Trigger hourly update by timer"""
await self.async_fwupdate_check()
2019-12-02 18:13:55 +01:00
# ---------------------------
# option_track_arp
# ---------------------------
@property
def option_track_arp(self):
"""Config entry option to not track ARP."""
return self.config_entry.options.get(CONF_TRACK_ARP, DEFAULT_TRACK_ARP)
2019-12-02 18:13:55 +01:00
# ---------------------------
# option_scan_interval
# ---------------------------
@property
def option_scan_interval(self):
"""Config entry option scan interval."""
2020-03-16 04:51:41 +01:00
scan_interval = self.config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
)
2019-12-02 18:13:55 +01:00
return timedelta(seconds=scan_interval)
# ---------------------------
# option_traffic_type
# ---------------------------
@property
def option_traffic_type(self):
"""Config entry option to not track ARP."""
2020-03-16 04:51:41 +01:00
return self.config_entry.options.get(
CONF_UNIT_OF_MEASUREMENT, DEFAULT_TRAFFIC_TYPE
)
2019-12-02 18:13:55 +01:00
# ---------------------------
# signal_update
# ---------------------------
@property
def signal_update(self):
"""Event to signal new data."""
return f"{DOMAIN}-update-{self.name}"
2019-12-02 18:13:55 +01:00
# ---------------------------
# connected
# ---------------------------
def connected(self):
"""Return connected state"""
2019-12-02 18:13:55 +01:00
return self.api.connected()
2019-12-02 18:13:55 +01:00
# ---------------------------
# hwinfo_update
# ---------------------------
async def hwinfo_update(self):
"""Update Mikrotik hardware info"""
2019-12-12 23:01:57 +01:00
try:
await asyncio.wait_for(self.lock.acquire(), timeout=10)
except:
return
await self.hass.async_add_executor_job(self.get_system_routerboard)
await self.hass.async_add_executor_job(self.get_system_resource)
self.lock.release()
2019-12-04 16:09:30 +01:00
# ---------------------------
# async_fwupdate_check
# ---------------------------
async def async_fwupdate_check(self):
"""Update Mikrotik data"""
2019-12-12 23:01:57 +01:00
await self.hass.async_add_executor_job(self.get_firmware_update)
2019-12-04 16:09:30 +01:00
async_dispatcher_send(self.hass, self.signal_update)
2019-12-02 18:13:55 +01:00
# ---------------------------
# async_update
# ---------------------------
async def async_update(self):
"""Update Mikrotik data"""
2019-12-12 23:01:57 +01:00
try:
await asyncio.wait_for(self.lock.acquire(), timeout=10)
except:
return
2020-03-16 04:51:41 +01:00
if "available" not in self.data["fw-update"]:
2019-12-04 16:09:30 +01:00
await self.async_fwupdate_check()
2019-12-12 23:01:57 +01:00
await self.hass.async_add_executor_job(self.get_interface)
await self.hass.async_add_executor_job(self.get_interface_traffic)
await self.hass.async_add_executor_job(self.get_interface_client)
await self.hass.async_add_executor_job(self.get_nat)
await self.hass.async_add_executor_job(self.get_system_resource)
await self.hass.async_add_executor_job(self.get_script)
2019-12-02 18:13:55 +01:00
async_dispatcher_send(self.hass, self.signal_update)
2019-12-12 23:01:57 +01:00
self.lock.release()
2019-12-02 18:13:55 +01:00
# ---------------------------
# async_reset
# ---------------------------
async def async_reset(self):
"""Reset dispatchers"""
2019-12-02 18:13:55 +01:00
for unsub_dispatcher in self.listeners:
unsub_dispatcher()
2019-12-02 18:13:55 +01:00
self.listeners = []
return True
2019-12-03 01:47:38 +01:00
# ---------------------------
2019-12-04 20:13:11 +01:00
# set_value
2019-12-03 01:47:38 +01:00
# ---------------------------
def set_value(self, path, param, value, mod_param, mod_value):
"""Change value using Mikrotik API"""
2019-12-03 01:47:38 +01:00
return self.api.update(path, param, value, mod_param, mod_value)
2019-12-04 20:13:11 +01:00
# ---------------------------
# run_script
# ---------------------------
def run_script(self, name):
"""Run script using Mikrotik API"""
2019-12-12 23:01:57 +01:00
try:
self.api.run_script(name)
except ApiEntryNotFound as error:
_LOGGER.error("Failed to run script: %s", error)
2019-12-02 18:13:55 +01:00
# ---------------------------
# get_interface
2019-12-02 18:13:55 +01:00
# ---------------------------
2019-12-12 23:01:57 +01:00
def get_interface(self):
"""Get all interfaces data from Mikrotik"""
2020-03-16 04:51:41 +01:00
self.data["interface"] = parse_api(
data=self.data["interface"],
2019-12-12 23:01:57 +01:00
source=self.api.path("/interface"),
2020-03-16 04:51:41 +01:00
key="default-name",
vals=[
2020-03-16 04:51:41 +01:00
{"name": "default-name"},
{"name": "name", "default_val": "default-name"},
{"name": "type", "default": "unknown"},
{"name": "running", "type": "bool"},
{
"name": "enabled",
"source": "disabled",
"type": "bool",
"reverse": True,
},
{"name": "port-mac-address", "source": "mac-address"},
{"name": "comment"},
{"name": "last-link-down-time"},
{"name": "last-link-up-time"},
{"name": "link-downs"},
{"name": "tx-queue-drop"},
{"name": "actual-mtu"},
],
ensure_vals=[
2020-03-16 04:51:41 +01:00
{"name": "client-ip-address"},
{"name": "client-mac-address"},
{"name": "rx-bits-per-second", "default": 0},
{"name": "tx-bits-per-second", "default": 0},
],
)
# ---------------------------
# get_interface_traffic
# ---------------------------
2019-12-12 23:01:57 +01:00
def get_interface_traffic(self):
"""Get traffic for all interfaces from Mikrotik"""
interface_list = ""
2020-03-16 04:51:41 +01:00
for uid in self.data["interface"]:
interface_list += self.data["interface"][uid]["name"] + ","
2019-12-11 15:34:35 +01:00
interface_list = interface_list[:-1]
2020-03-16 04:51:41 +01:00
self.data["interface"] = parse_api(
data=self.data["interface"],
2019-12-12 23:01:57 +01:00
source=self.api.get_traffic(interface_list),
2020-03-16 04:51:41 +01:00
key_search="name",
vals=[
2020-03-16 04:51:41 +01:00
{"name": "rx-bits-per-second", "default": 0},
{"name": "tx-bits-per-second", "default": 0},
],
)
traffic_type = self.option_traffic_type
if traffic_type == "Kbps":
traffic_div = 0.001
elif traffic_type == "Mbps":
traffic_div = 0.000001
elif traffic_type == "B/s":
traffic_div = 0.125
elif traffic_type == "KB/s":
traffic_div = 0.000125
elif traffic_type == "MB/s":
traffic_div = 0.000000125
else:
traffic_type = "bps"
traffic_div = 1
2020-03-16 04:51:41 +01:00
for uid in self.data["interface"]:
self.data["interface"][uid]["rx-bits-per-second-attr"] = traffic_type
self.data["interface"][uid]["tx-bits-per-second-attr"] = traffic_type
self.data["interface"][uid]["rx-bits-per-second"] = round(
self.data["interface"][uid]["rx-bits-per-second"] * traffic_div
2020-03-16 04:51:41 +01:00
)
self.data["interface"][uid]["tx-bits-per-second"] = round(
self.data["interface"][uid]["tx-bits-per-second"] * traffic_div
2020-03-16 04:51:41 +01:00
)
2020-03-11 23:25:51 +01:00
2019-12-02 18:13:55 +01:00
# ---------------------------
2019-12-05 22:29:25 +01:00
# get_interface_client
2019-12-02 18:13:55 +01:00
# ---------------------------
2019-12-12 23:01:57 +01:00
def get_interface_client(self):
"""Get ARP data from Mikrotik"""
2020-03-16 04:51:41 +01:00
self.data["arp"] = {}
2019-12-05 22:29:25 +01:00
# Remove data if disabled
2019-12-02 18:13:55 +01:00
if not self.option_track_arp:
2020-03-16 04:51:41 +01:00
for uid in self.data["interface"]:
self.data["interface"][uid]["client-ip-address"] = "disabled"
self.data["interface"][uid]["client-mac-address"] = "disabled"
2019-12-12 09:06:15 +01:00
return
2019-12-02 18:13:55 +01:00
mac2ip = {}
bridge_used = False
2019-12-12 23:01:57 +01:00
mac2ip, bridge_used = self.update_arp(mac2ip, bridge_used)
2019-12-05 22:29:25 +01:00
if bridge_used:
2019-12-12 23:01:57 +01:00
self.update_bridge_hosts(mac2ip)
2019-12-05 22:29:25 +01:00
# Map ARP to ifaces
2020-03-16 04:51:41 +01:00
for uid in self.data["interface"]:
if uid not in self.data["arp"]:
continue
2020-03-16 04:51:41 +01:00
self.data["interface"][uid]["client-ip-address"] = from_entry(
self.data["arp"][uid], "address"
)
self.data["interface"][uid]["client-mac-address"] = from_entry(
self.data["arp"][uid], "mac-address"
)
2019-12-05 22:29:25 +01:00
# ---------------------------
# update_arp
# ---------------------------
2019-12-12 23:01:57 +01:00
def update_arp(self, mac2ip, bridge_used):
2019-12-05 22:29:25 +01:00
"""Get list of hosts in ARP for interface client data from Mikrotik"""
2019-12-12 23:01:57 +01:00
data = self.api.path("/ip/arp")
if not data:
return mac2ip, bridge_used
2019-12-02 18:13:55 +01:00
for entry in data:
# Ignore invalid entries
2020-03-16 04:51:41 +01:00
if entry["invalid"]:
2019-12-02 18:13:55 +01:00
continue
2020-03-16 04:51:41 +01:00
if "interface" not in entry:
2020-03-11 23:39:43 +01:00
continue
2019-12-02 18:13:55 +01:00
# Do not add ARP detected on bridge
2020-03-16 04:51:41 +01:00
if entry["interface"] == "bridge":
2019-12-02 18:13:55 +01:00
bridge_used = True
# Build address table on bridge
2020-03-16 04:51:41 +01:00
if "mac-address" in entry and "address" in entry:
mac2ip[entry["mac-address"]] = entry["address"]
2019-12-02 18:13:55 +01:00
continue
2019-12-02 18:13:55 +01:00
# Get iface default-name from custom name
2019-12-12 23:01:57 +01:00
uid = self.get_iface_from_entry(entry)
2019-12-02 18:13:55 +01:00
if not uid:
continue
2019-12-21 08:21:17 +01:00
_LOGGER.debug("Processing entry %s, entry %s", "/ip/arp", entry)
2019-12-02 18:13:55 +01:00
# Create uid arp dict
2020-03-16 04:51:41 +01:00
if uid not in self.data["arp"]:
self.data["arp"][uid] = {}
2019-12-02 18:13:55 +01:00
# Add data
2020-03-16 04:51:41 +01:00
self.data["arp"][uid]["interface"] = uid
self.data["arp"][uid]["mac-address"] = (
from_entry(entry, "mac-address")
if "mac-address" not in self.data["arp"][uid]
else "multiple"
)
self.data["arp"][uid]["address"] = (
from_entry(entry, "address")
if "address" not in self.data["arp"][uid]
else "multiple"
)
2019-12-05 22:29:25 +01:00
return mac2ip, bridge_used
2019-12-02 18:13:55 +01:00
# ---------------------------
# update_bridge_hosts
# ---------------------------
2019-12-12 23:01:57 +01:00
def update_bridge_hosts(self, mac2ip):
2019-12-05 22:29:25 +01:00
"""Get list of hosts in bridge for interface client data from Mikrotik"""
2019-12-12 23:01:57 +01:00
data = self.api.path("/interface/bridge/host")
if not data:
return
2019-12-02 18:13:55 +01:00
for entry in data:
# Ignore port MAC
2020-03-16 04:51:41 +01:00
if entry["local"]:
2019-12-02 18:13:55 +01:00
continue
2019-12-02 18:13:55 +01:00
# Get iface default-name from custom name
2019-12-12 23:01:57 +01:00
uid = self.get_iface_from_entry(entry)
2019-12-02 18:13:55 +01:00
if not uid:
continue
2020-03-16 04:51:41 +01:00
_LOGGER.debug(
"Processing entry %s, entry %s", "/interface/bridge/host", entry
)
2019-12-02 18:13:55 +01:00
# Create uid arp dict
2020-03-16 04:51:41 +01:00
if uid not in self.data["arp"]:
self.data["arp"][uid] = {}
2019-12-02 18:13:55 +01:00
# Add data
2020-03-16 04:51:41 +01:00
self.data["arp"][uid]["interface"] = uid
if "mac-address" in self.data["arp"][uid]:
self.data["arp"][uid]["mac-address"] = "multiple"
self.data["arp"][uid]["address"] = "multiple"
2019-12-02 18:13:55 +01:00
else:
2020-03-16 04:51:41 +01:00
self.data["arp"][uid]["mac-address"] = from_entry(entry, "mac-address")
self.data["arp"][uid]["address"] = (
mac2ip[self.data["arp"][uid]["mac-address"]]
if self.data["arp"][uid]["mac-address"] in mac2ip
else ""
)
2019-12-02 19:23:09 +01:00
# ---------------------------
# get_iface_from_entry
2019-12-02 19:23:09 +01:00
# ---------------------------
2019-12-12 23:01:57 +01:00
def get_iface_from_entry(self, entry):
"""Get interface default-name using name from interface dict"""
2019-12-02 19:23:09 +01:00
uid = None
2020-03-16 04:51:41 +01:00
for ifacename in self.data["interface"]:
if self.data["interface"][ifacename]["name"] == entry["interface"]:
2019-12-11 15:34:35 +01:00
uid = ifacename
2019-12-02 19:23:09 +01:00
break
2019-12-02 19:23:09 +01:00
return uid
2019-12-03 18:29:05 +01:00
# ---------------------------
# get_nat
# ---------------------------
2019-12-12 23:01:57 +01:00
def get_nat(self):
"""Get NAT data from Mikrotik"""
2020-03-16 04:51:41 +01:00
self.data["nat"] = parse_api(
data=self.data["nat"],
2019-12-12 23:01:57 +01:00
source=self.api.path("/ip/firewall/nat"),
2020-03-16 04:51:41 +01:00
key=".id",
2019-12-12 09:06:15 +01:00
vals=[
2020-03-16 04:51:41 +01:00
{"name": ".id"},
{"name": "protocol", "default": "any"},
{"name": "dst-port", "default": "any"},
{"name": "in-interface", "default": "any"},
{"name": "to-addresses"},
{"name": "to-ports"},
{"name": "comment"},
{
"name": "enabled",
"source": "disabled",
"type": "bool",
"reverse": True,
},
2019-12-12 09:06:15 +01:00
],
val_proc=[
[
2020-03-16 04:51:41 +01:00
{"name": "name"},
{"action": "combine"},
{"key": "protocol"},
{"text": ":"},
{"key": "dst-port"},
2019-12-12 09:06:15 +01:00
]
2019-12-12 13:02:11 +01:00
],
2020-03-16 04:51:41 +01:00
only=[{"key": "action", "value": "dst-nat"}],
2019-12-12 09:06:15 +01:00
)
2019-12-02 18:13:55 +01:00
# ---------------------------
# get_system_routerboard
# ---------------------------
2019-12-12 23:01:57 +01:00
def get_system_routerboard(self):
"""Get routerboard data from Mikrotik"""
2020-03-16 04:51:41 +01:00
self.data["routerboard"] = parse_api(
data=self.data["routerboard"],
2019-12-12 23:01:57 +01:00
source=self.api.path("/system/routerboard"),
2019-12-11 15:34:35 +01:00
vals=[
2020-03-16 04:51:41 +01:00
{"name": "routerboard", "type": "bool"},
{"name": "model", "default": "unknown"},
{"name": "serial-number", "default": "unknown"},
{"name": "firmware", "default": "unknown"},
],
2019-12-11 15:34:35 +01:00
)
2019-12-02 18:13:55 +01:00
# ---------------------------
# get_system_resource
# ---------------------------
2019-12-12 23:01:57 +01:00
def get_system_resource(self):
"""Get system resources data from Mikrotik"""
2020-03-16 04:51:41 +01:00
self.data["resource"] = parse_api(
data=self.data["resource"],
2019-12-12 23:01:57 +01:00
source=self.api.path("/system/resource"),
2019-12-11 15:34:35 +01:00
vals=[
2020-03-16 04:51:41 +01:00
{"name": "platform", "default": "unknown"},
{"name": "board-name", "default": "unknown"},
{"name": "version", "default": "unknown"},
{"name": "uptime", "default": "unknown"},
{"name": "cpu-load", "default": "unknown"},
{"name": "free-memory", "default": 0},
{"name": "total-memory", "default": 0},
{"name": "free-hdd-space", "default": 0},
{"name": "total-hdd-space", "default": 0},
],
2019-12-11 15:34:35 +01:00
)
2020-03-16 04:51:41 +01:00
if self.data["resource"]["total-memory"] > 0:
self.data["resource"]["memory-usage"] = round(
(
(
self.data["resource"]["total-memory"]
- self.data["resource"]["free-memory"]
)
/ self.data["resource"]["total-memory"]
)
* 100
)
else:
2020-03-16 04:51:41 +01:00
self.data["resource"]["memory-usage"] = "unknown"
if self.data["resource"]["total-hdd-space"] > 0:
self.data["resource"]["hdd-usage"] = round(
(
(
self.data["resource"]["total-hdd-space"]
- self.data["resource"]["free-hdd-space"]
)
/ self.data["resource"]["total-hdd-space"]
)
* 100
)
else:
2020-03-16 04:51:41 +01:00
self.data["resource"]["hdd-usage"] = "unknown"
2019-12-04 16:09:30 +01:00
# ---------------------------
# get_system_routerboard
# ---------------------------
2019-12-12 23:01:57 +01:00
def get_firmware_update(self):
"""Check for firmware update on Mikrotik"""
2020-03-16 04:51:41 +01:00
self.data["fw-update"] = parse_api(
data=self.data["fw-update"],
2019-12-12 23:01:57 +01:00
source=self.api.path("/system/package/update"),
2019-12-12 13:02:11 +01:00
vals=[
2020-03-16 04:51:41 +01:00
{"name": "status"},
{"name": "channel", "default": "unknown"},
{"name": "installed-version", "default": "unknown"},
{"name": "latest-version", "default": "unknown"},
],
2019-12-12 13:02:11 +01:00
)
2020-03-16 04:51:41 +01:00
if "status" in self.data["fw-update"]:
self.data["fw-update"]["available"] = (
True
if self.data["fw-update"]["status"] == "New version is available"
else False
)
2019-12-12 13:02:11 +01:00
else:
2020-03-16 04:51:41 +01:00
self.data["fw-update"]["available"] = False
2019-12-12 13:02:11 +01:00
2019-12-04 20:13:11 +01:00
# ---------------------------
# get_script
# ---------------------------
2019-12-12 23:01:57 +01:00
def get_script(self):
"""Get list of all scripts from Mikrotik"""
2020-03-16 04:51:41 +01:00
self.data["script"] = parse_api(
data=self.data["script"],
2019-12-12 23:01:57 +01:00
source=self.api.path("/system/script"),
2020-03-16 04:51:41 +01:00
key="name",
2019-12-11 15:34:35 +01:00
vals=[
2020-03-16 04:51:41 +01:00
{"name": "name"},
{"name": "last-started", "default": "unknown"},
{"name": "run-count", "default": "unknown"},
],
2019-12-11 15:34:35 +01:00
)