2019-12-02 04:19:40 +01:00
|
|
|
"""Mikrotik Controller for Mikrotik Router."""
|
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
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,
|
|
|
|
DEFAULT_SCAN_INTERVAL,
|
2019-12-02 04:19:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
from .mikrotikapi import MikrotikAPI
|
|
|
|
|
2019-12-02 17:59:49 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-12-02 04:19:40 +01:00
|
|
|
|
2019-12-02 16:22:01 +01:00
|
|
|
|
2019-12-05 22:10:42 +01:00
|
|
|
# ---------------------------
|
|
|
|
# from_entry
|
|
|
|
# ---------------------------
|
|
|
|
def from_entry(entry, param, default=""):
|
|
|
|
"""Validate and return a value from a Mikrotik API dict"""
|
|
|
|
if param not in entry:
|
|
|
|
return default
|
|
|
|
|
|
|
|
return entry[param]
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# from_entry_bool
|
|
|
|
# ---------------------------
|
|
|
|
def from_entry_bool(entry, param, default=False, reverse=False):
|
|
|
|
"""Validate and return a bool value from a Mikrotik API dict"""
|
|
|
|
if param not in entry:
|
|
|
|
return default
|
|
|
|
|
|
|
|
if not reverse:
|
|
|
|
ret = entry[param]
|
|
|
|
else:
|
|
|
|
if entry[param]:
|
|
|
|
ret = False
|
|
|
|
else:
|
|
|
|
ret = True
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 04:19:40 +01:00
|
|
|
# MikrotikControllerData
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 04:19:40 +01:00
|
|
|
class MikrotikControllerData():
|
2019-12-05 20:42:14 +01:00
|
|
|
"""MikrotikController Class"""
|
2019-12-02 18:13:55 +01:00
|
|
|
def __init__(self, hass, config_entry, name, host, port, username, password, use_ssl):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Initialize MikrotikController."""
|
2019-12-02 18:13:55 +01:00
|
|
|
self.name = name
|
|
|
|
self.hass = hass
|
|
|
|
self.config_entry = config_entry
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-06 01:22:34 +01:00
|
|
|
self.data = {'routerboard': {},
|
|
|
|
'resource': {},
|
|
|
|
'interface': {},
|
2019-12-08 11:20:02 +01:00
|
|
|
'interface_map': {},
|
2019-12-06 01:22:34 +01:00
|
|
|
'arp': {},
|
|
|
|
'nat': {},
|
|
|
|
'fw-update': {},
|
|
|
|
'script': {}
|
|
|
|
}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
self.listeners = []
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
self.api = MikrotikAPI(host, username, password, port, use_ssl)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
async_track_time_interval(self.hass, self.force_update, self.option_scan_interval)
|
2019-12-04 18:53:28 +01:00
|
|
|
async_track_time_interval(self.hass, self.force_fwupdate_check, timedelta(hours=1))
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
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):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Trigger update by timer"""
|
2019-12-02 18:13:55 +01:00
|
|
|
await self.async_update()
|
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-04 18:53:28 +01:00
|
|
|
# ---------------------------
|
|
|
|
# force_fwupdate_check
|
|
|
|
# ---------------------------
|
2019-12-05 22:10:42 +01:00
|
|
|
async def force_fwupdate_check(self, _now=None):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Trigger hourly update by timer"""
|
2019-12-04 18:53:28 +01:00
|
|
|
await self.async_fwupdate_check()
|
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
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-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# option_scan_interval
|
|
|
|
# ---------------------------
|
|
|
|
@property
|
|
|
|
def option_scan_interval(self):
|
|
|
|
"""Config entry option scan interval."""
|
|
|
|
scan_interval = self.config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
|
|
|
return timedelta(seconds=scan_interval)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# signal_update
|
|
|
|
# ---------------------------
|
|
|
|
@property
|
|
|
|
def signal_update(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Event to signal new data."""
|
2019-12-06 01:22:34 +01:00
|
|
|
return "{}-update-{}".format(DOMAIN, self.name)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# connected
|
|
|
|
# ---------------------------
|
|
|
|
def connected(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Return connected state"""
|
2019-12-02 18:13:55 +01:00
|
|
|
return self.api.connected()
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# hwinfo_update
|
|
|
|
# ---------------------------
|
|
|
|
async def hwinfo_update(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Update Mikrotik hardware info"""
|
2019-12-02 18:13:55 +01:00
|
|
|
self.get_system_routerboard()
|
|
|
|
self.get_system_resource()
|
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-04 16:09:30 +01:00
|
|
|
# ---------------------------
|
|
|
|
# async_fwupdate_check
|
|
|
|
# ---------------------------
|
|
|
|
async def async_fwupdate_check(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Update Mikrotik data"""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-04 16:09:30 +01:00
|
|
|
self.get_firmare_update()
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-04 16:09:30 +01:00
|
|
|
async_dispatcher_send(self.hass, self.signal_update)
|
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# async_update
|
|
|
|
# ---------------------------
|
|
|
|
async def async_update(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Update Mikrotik data"""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-04 16:09:30 +01:00
|
|
|
if 'available' not in self.data['fw-update']:
|
|
|
|
await self.async_fwupdate_check()
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-05 20:42:14 +01:00
|
|
|
self.get_interface()
|
2019-12-05 22:29:25 +01:00
|
|
|
self.get_interface_client()
|
2019-12-03 18:29:05 +01:00
|
|
|
self.get_nat()
|
2019-12-03 03:09:30 +01:00
|
|
|
self.get_system_resource()
|
2019-12-04 20:13:11 +01:00
|
|
|
self.get_script()
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
async_dispatcher_send(self.hass, self.signal_update)
|
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# async_reset
|
|
|
|
# ---------------------------
|
|
|
|
async def async_reset(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Reset dispatchers"""
|
2019-12-02 18:13:55 +01:00
|
|
|
for unsub_dispatcher in self.listeners:
|
|
|
|
unsub_dispatcher()
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
self.listeners = []
|
|
|
|
return True
|
2019-12-05 20:45:04 +01:00
|
|
|
|
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):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""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-05 20:45:04 +01:00
|
|
|
|
2019-12-04 20:13:11 +01:00
|
|
|
# ---------------------------
|
|
|
|
# run_script
|
|
|
|
# ---------------------------
|
|
|
|
def run_script(self, name):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Run script using Mikrotik API"""
|
2019-12-04 20:13:11 +01:00
|
|
|
return self.api.run_script(name)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
2019-12-05 20:42:14 +01:00
|
|
|
# get_interface
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
2019-12-05 20:42:14 +01:00
|
|
|
def get_interface(self):
|
|
|
|
"""Get all interfaces data from Mikrotik"""
|
2019-12-08 11:20:02 +01:00
|
|
|
interface_list = ""
|
2019-12-05 22:10:42 +01:00
|
|
|
data = self.api.path("/interface")
|
2019-12-08 22:49:36 +01:00
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
|
2019-12-05 22:10:42 +01:00
|
|
|
for entry in data:
|
|
|
|
if 'default-name' not in entry:
|
2019-12-02 18:13:55 +01:00
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-05 22:10:42 +01:00
|
|
|
uid = entry['default-name']
|
2019-12-02 18:13:55 +01:00
|
|
|
if uid not in self.data['interface']:
|
|
|
|
self.data['interface'][uid] = {}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-05 22:10:42 +01:00
|
|
|
self.data['interface'][uid]['default-name'] = from_entry(entry, 'default-name')
|
2019-12-08 19:19:26 +01:00
|
|
|
self.data['interface'][uid]['name'] = from_entry(entry, 'name', default=entry['default-name'])
|
2019-12-05 22:10:42 +01:00
|
|
|
self.data['interface'][uid]['type'] = from_entry(entry, 'type', 'unknown')
|
|
|
|
self.data['interface'][uid]['running'] = from_entry_bool(entry, 'running')
|
|
|
|
self.data['interface'][uid]['enabled'] = from_entry_bool(entry, 'disabled', reverse=True)
|
|
|
|
self.data['interface'][uid]['port-mac-address'] = from_entry(entry, 'mac-address')
|
|
|
|
self.data['interface'][uid]['comment'] = from_entry(entry, 'comment')
|
|
|
|
self.data['interface'][uid]['last-link-down-time'] = from_entry(entry, 'last-link-down-time')
|
|
|
|
self.data['interface'][uid]['last-link-up-time'] = from_entry(entry, 'last-link-up-time')
|
|
|
|
self.data['interface'][uid]['link-downs'] = from_entry(entry, 'link-downs')
|
|
|
|
self.data['interface'][uid]['tx-queue-drop'] = from_entry(entry, 'tx-queue-drop')
|
|
|
|
self.data['interface'][uid]['actual-mtu'] = from_entry(entry, 'actual-mtu')
|
2019-12-08 11:20:02 +01:00
|
|
|
|
|
|
|
self.data['interface_map'][self.data['interface'][uid]['name']] = self.data['interface'][uid]['default-name']
|
|
|
|
|
|
|
|
if interface_list:
|
|
|
|
interface_list += ","
|
|
|
|
|
|
|
|
interface_list += self.data['interface'][uid]['name']
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
if 'client-ip-address' not in self.data['interface'][uid]:
|
|
|
|
self.data['interface'][uid]['client-ip-address'] = ""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
if 'client-mac-address' not in self.data['interface'][uid]:
|
|
|
|
self.data['interface'][uid]['client-mac-address'] = ""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-08 11:20:02 +01:00
|
|
|
if 'rx-bits-per-second' not in self.data['interface'][uid]:
|
|
|
|
self.data['interface'][uid]['rx-bits-per-second'] = 0
|
|
|
|
|
|
|
|
if 'tx-bits-per-second' not in self.data['interface'][uid]:
|
|
|
|
self.data['interface'][uid]['tx-bits-per-second'] = 0
|
|
|
|
|
|
|
|
self.get_interface_traffic(interface_list)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# get_interface_traffic
|
|
|
|
# ---------------------------
|
|
|
|
def get_interface_traffic(self, interface_list):
|
|
|
|
data = self.api.get_traffic(interface_list)
|
|
|
|
for entry in data:
|
|
|
|
uid = self.data['interface_map'][from_entry(entry, 'name')]
|
|
|
|
self.data['interface'][uid]['rx-bits-per-second'] = from_entry(entry, 'rx-bits-per-second', default=0)
|
|
|
|
self.data['interface'][uid]['tx-bits-per-second'] = from_entry(entry, 'tx-bits-per-second', default=0)
|
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
return
|
2019-12-05 20:45:04 +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-05 22:29:25 +01:00
|
|
|
def get_interface_client(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Get ARP data from Mikrotik"""
|
2019-12-02 18:13:55 +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:
|
|
|
|
for uid in self.data['interface']:
|
|
|
|
self.data['interface'][uid]['client-ip-address'] = "disabled"
|
|
|
|
self.data['interface'][uid]['client-mac-address'] = "disabled"
|
|
|
|
return False
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
mac2ip = {}
|
|
|
|
bridge_used = False
|
2019-12-05 22:29:25 +01:00
|
|
|
mac2ip, bridge_used = self.update_arp(mac2ip, bridge_used)
|
|
|
|
|
|
|
|
if bridge_used:
|
|
|
|
self.update_bridge_hosts(mac2ip)
|
|
|
|
|
|
|
|
# Map ARP to ifaces
|
|
|
|
for uid in self.data['interface']:
|
2019-12-09 00:31:43 +01:00
|
|
|
if uid not in self.data['arp']:
|
|
|
|
continue
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# update_arp
|
|
|
|
# ---------------------------
|
|
|
|
def update_arp(self, mac2ip, bridge_used):
|
|
|
|
"""Get list of hosts in ARP for interface client data from Mikrotik"""
|
2019-12-02 18:13:55 +01:00
|
|
|
data = self.api.path("/ip/arp")
|
2019-12-08 22:49:36 +01:00
|
|
|
if not data:
|
2019-12-08 23:03:05 +01:00
|
|
|
return mac2ip, bridge_used
|
2019-12-08 22:49:36 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
for entry in data:
|
|
|
|
# Ignore invalid entries
|
|
|
|
if entry['invalid']:
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# Do not add ARP detected on bridge
|
|
|
|
if entry['interface'] == "bridge":
|
|
|
|
bridge_used = True
|
|
|
|
# Build address table on bridge
|
|
|
|
if 'mac-address' in entry and 'address' in entry:
|
|
|
|
mac2ip[entry['mac-address']] = entry['address']
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# Get iface default-name from custom name
|
2019-12-05 20:42:14 +01:00
|
|
|
uid = self.get_iface_from_entry(entry)
|
2019-12-02 18:13:55 +01:00
|
|
|
if not uid:
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# Create uid arp dict
|
|
|
|
if uid not in self.data['arp']:
|
|
|
|
self.data['arp'][uid] = {}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# Add data
|
|
|
|
self.data['arp'][uid]['interface'] = uid
|
2019-12-09 00:31:43 +01:00
|
|
|
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-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# update_bridge_hosts
|
|
|
|
# ---------------------------
|
|
|
|
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-02 18:13:55 +01:00
|
|
|
data = self.api.path("/interface/bridge/host")
|
2019-12-08 22:49:36 +01:00
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
for entry in data:
|
|
|
|
# Ignore port MAC
|
|
|
|
if entry['local']:
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# Get iface default-name from custom name
|
2019-12-05 20:42:14 +01:00
|
|
|
uid = self.get_iface_from_entry(entry)
|
2019-12-02 18:13:55 +01:00
|
|
|
if not uid:
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# Create uid arp dict
|
|
|
|
if uid not in self.data['arp']:
|
|
|
|
self.data['arp'][uid] = {}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# Add data
|
|
|
|
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"
|
|
|
|
else:
|
2019-12-09 00:31:43 +01:00
|
|
|
self.data['arp'][uid]['mac-address'] = from_entry(entry, 'mac-address')
|
2019-12-02 18:13:55 +01:00
|
|
|
self.data['arp'][uid]['address'] = ""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
if self.data['arp'][uid]['address'] == "" and self.data['arp'][uid]['mac-address'] in mac2ip:
|
|
|
|
self.data['arp'][uid]['address'] = mac2ip[self.data['arp'][uid]['mac-address']]
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 19:23:09 +01:00
|
|
|
# ---------------------------
|
2019-12-05 20:42:14 +01:00
|
|
|
# get_iface_from_entry
|
2019-12-02 19:23:09 +01:00
|
|
|
# ---------------------------
|
2019-12-05 20:42:14 +01:00
|
|
|
def get_iface_from_entry(self, entry):
|
|
|
|
"""Get interface name from Mikrotik"""
|
2019-12-02 19:23:09 +01:00
|
|
|
uid = None
|
|
|
|
for ifacename in self.data['interface']:
|
|
|
|
if self.data['interface'][ifacename]['name'] == entry['interface']:
|
|
|
|
uid = self.data['interface'][ifacename]['default-name']
|
|
|
|
break
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 19:23:09 +01:00
|
|
|
return uid
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 18:29:05 +01:00
|
|
|
# ---------------------------
|
|
|
|
# get_nat
|
|
|
|
# ---------------------------
|
|
|
|
def get_nat(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Get NAT data from Mikrotik"""
|
2019-12-03 18:29:05 +01:00
|
|
|
data = self.api.path("/ip/firewall/nat")
|
2019-12-08 22:49:36 +01:00
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
|
2019-12-03 18:29:05 +01:00
|
|
|
for entry in data:
|
|
|
|
if entry['action'] != 'dst-nat':
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 18:29:05 +01:00
|
|
|
uid = entry['.id']
|
|
|
|
if uid not in self.data['nat']:
|
|
|
|
self.data['nat'][uid] = {}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-07 21:18:27 +01:00
|
|
|
self.data['nat'][uid]['name'] = "{}:{}".format(entry['protocol'], entry['dst-port'])
|
|
|
|
self.data['nat'][uid]['.id'] = from_entry(entry, '.id')
|
2019-12-05 22:10:42 +01:00
|
|
|
self.data['nat'][uid]['protocol'] = from_entry(entry, 'protocol')
|
|
|
|
self.data['nat'][uid]['dst-port'] = from_entry(entry, 'dst-port')
|
|
|
|
self.data['nat'][uid]['in-interface'] = from_entry(entry, 'in-interface', 'any')
|
|
|
|
self.data['nat'][uid]['to-addresses'] = from_entry(entry, 'to-addresses')
|
|
|
|
self.data['nat'][uid]['to-ports'] = from_entry(entry, 'to-ports')
|
|
|
|
self.data['nat'][uid]['comment'] = from_entry(entry, 'comment')
|
|
|
|
self.data['nat'][uid]['enabled'] = from_entry_bool(entry, 'disabled', default=True, reverse=True)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 18:29:05 +01:00
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# get_system_routerboard
|
|
|
|
# ---------------------------
|
|
|
|
def get_system_routerboard(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Get routerboard data from Mikrotik"""
|
2019-12-02 18:13:55 +01:00
|
|
|
data = self.api.path("/system/routerboard")
|
2019-12-08 22:49:36 +01:00
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
for entry in data:
|
2019-12-05 22:10:42 +01:00
|
|
|
self.data['routerboard']['routerboard'] = from_entry_bool(entry, 'routerboard')
|
|
|
|
self.data['routerboard']['model'] = from_entry(entry, 'model', 'unknown')
|
|
|
|
self.data['routerboard']['serial-number'] = from_entry(entry, 'serial-number', 'unknown')
|
|
|
|
self.data['routerboard']['firmware'] = from_entry(entry, 'current-firmware', 'unknown')
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# get_system_resource
|
|
|
|
# ---------------------------
|
|
|
|
def get_system_resource(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Get system resources data from Mikrotik"""
|
2019-12-02 18:13:55 +01:00
|
|
|
data = self.api.path("/system/resource")
|
2019-12-08 22:49:36 +01:00
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
for entry in data:
|
2019-12-05 22:10:42 +01:00
|
|
|
self.data['resource']['platform'] = from_entry(entry, 'platform', 'unknown')
|
|
|
|
self.data['resource']['board-name'] = from_entry(entry, 'board-name', 'unknown')
|
|
|
|
self.data['resource']['version'] = from_entry(entry, 'version', 'unknown')
|
|
|
|
self.data['resource']['uptime'] = from_entry(entry, 'uptime', 'unknown')
|
|
|
|
self.data['resource']['cpu-load'] = from_entry(entry, 'cpu-load', 'unknown')
|
2019-12-03 03:09:30 +01:00
|
|
|
if 'free-memory' in entry and 'total-memory' in entry:
|
|
|
|
self.data['resource']['memory-usage'] = round(((entry['total-memory'] - entry['free-memory']) / entry['total-memory']) * 100)
|
|
|
|
else:
|
|
|
|
self.data['resource']['memory-usage'] = "unknown"
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
if 'free-hdd-space' in entry and 'total-hdd-space' in entry:
|
|
|
|
self.data['resource']['hdd-usage'] = round(((entry['total-hdd-space'] - entry['free-hdd-space']) / entry['total-hdd-space']) * 100)
|
|
|
|
else:
|
|
|
|
self.data['resource']['hdd-usage'] = "unknown"
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-04 16:09:30 +01:00
|
|
|
# ---------------------------
|
|
|
|
# get_system_routerboard
|
|
|
|
# ---------------------------
|
|
|
|
def get_firmare_update(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Check for firmware update on Mikrotik"""
|
2019-12-04 16:09:30 +01:00
|
|
|
data = self.api.path("/system/package/update")
|
2019-12-08 22:49:36 +01:00
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
|
2019-12-04 16:09:30 +01:00
|
|
|
for entry in data:
|
2019-12-09 00:31:43 +01:00
|
|
|
if 'status' in entry:
|
2019-12-08 22:36:10 +01:00
|
|
|
self.data['fw-update']['available'] = True if entry['status'] == "New version is available" else False
|
2019-12-08 22:49:36 +01:00
|
|
|
elif 'available' not in self.data['fw-update']:
|
2019-12-08 22:36:10 +01:00
|
|
|
self.data['fw-update']['available'] = False
|
2019-12-05 22:10:42 +01:00
|
|
|
self.data['fw-update']['channel'] = from_entry(entry, 'channel', 'unknown')
|
|
|
|
self.data['fw-update']['installed-version'] = from_entry(entry, 'installed-version', 'unknown')
|
|
|
|
self.data['fw-update']['latest-version'] = from_entry(entry, 'latest-version', 'unknown')
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-04 16:09:30 +01:00
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-04 20:13:11 +01:00
|
|
|
# ---------------------------
|
|
|
|
# get_script
|
|
|
|
# ---------------------------
|
|
|
|
def get_script(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Get list of all scripts from Mikrotik"""
|
2019-12-04 20:13:11 +01:00
|
|
|
data = self.api.path("/system/script")
|
2019-12-08 22:49:36 +01:00
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
|
2019-12-04 20:13:11 +01:00
|
|
|
for entry in data:
|
|
|
|
if 'name' not in entry:
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-08 19:19:26 +01:00
|
|
|
if not entry['name']:
|
2019-12-09 00:17:49 +01:00
|
|
|
_LOGGER.error("Mikrotik %s found a script without a name. It will not be available in UI.")
|
2019-12-08 19:19:26 +01:00
|
|
|
continue
|
|
|
|
|
2019-12-04 20:13:11 +01:00
|
|
|
uid = entry['name']
|
|
|
|
if uid not in self.data['script']:
|
|
|
|
self.data['script'][uid] = {}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-05 22:10:42 +01:00
|
|
|
self.data['script'][uid]['name'] = from_entry(entry, 'name')
|
|
|
|
self.data['script'][uid]['last-started'] = from_entry(entry, 'last-started', 'unknown')
|
|
|
|
self.data['script'][uid]['run-count'] = from_entry(entry, 'run-count', 'unknown')
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-04 20:13:11 +01:00
|
|
|
return
|