2019-12-02 04:19:40 +01:00
|
|
|
"""Mikrotik Controller for Mikrotik Router."""
|
|
|
|
|
|
|
|
from datetime import timedelta
|
2019-12-12 23:01:57 +01:00
|
|
|
import asyncio
|
2019-12-02 04:19:40 +01:00
|
|
|
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-12 13:37:09 +01:00
|
|
|
from .helper import from_entry, parse_api
|
2019-12-12 23:01:57 +01:00
|
|
|
from .exceptions import ApiEntryNotFound
|
2019-12-02 04:19:40 +01:00
|
|
|
|
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-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': {},
|
|
|
|
'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-12 23:01:57 +01:00
|
|
|
self.lock = asyncio.Lock()
|
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 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()
|
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()
|
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-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-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-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-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-12 23:01:57 +01:00
|
|
|
try:
|
|
|
|
await asyncio.wait_for(self.lock.acquire(), timeout=10)
|
|
|
|
except:
|
|
|
|
return
|
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-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-05 20:45:04 +01:00
|
|
|
|
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-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-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
|
|
|
# ---------------------------
|
2019-12-05 20:42:14 +01:00
|
|
|
# get_interface
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
2019-12-12 23:01:57 +01:00
|
|
|
def get_interface(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Get all interfaces data from Mikrotik"""
|
2019-12-12 23:01:57 +01:00
|
|
|
self.data['interface'] = parse_api(
|
2019-12-10 22:32:59 +01:00
|
|
|
data=self.data['interface'],
|
2019-12-12 23:01:57 +01:00
|
|
|
source=self.api.path("/interface"),
|
2019-12-10 22:32:59 +01:00
|
|
|
key='default-name',
|
|
|
|
vals=[
|
|
|
|
{'name': 'default-name'},
|
2019-12-11 08:40:40 +01:00
|
|
|
{'name': 'name', 'default_val': 'default-name'},
|
2019-12-10 22:32:59 +01:00
|
|
|
{'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=[
|
|
|
|
{'name': 'client-ip-address'},
|
|
|
|
{'name': 'client-mac-address'},
|
|
|
|
{'name': 'rx-bits-per-second', 'default': 0},
|
|
|
|
{'name': 'tx-bits-per-second', 'default': 0}
|
|
|
|
]
|
|
|
|
)
|
2019-12-08 22:49:36 +01:00
|
|
|
|
2019-12-08 11:20:02 +01:00
|
|
|
# ---------------------------
|
|
|
|
# get_interface_traffic
|
|
|
|
# ---------------------------
|
2019-12-12 23:01:57 +01:00
|
|
|
def get_interface_traffic(self):
|
2019-12-11 09:13:35 +01:00
|
|
|
"""Get traffic for all interfaces from Mikrotik"""
|
|
|
|
interface_list = ""
|
|
|
|
for uid in self.data['interface']:
|
2019-12-11 15:34:35 +01:00
|
|
|
interface_list += self.data['interface'][uid]['name'] + ","
|
2019-12-10 22:32:59 +01:00
|
|
|
|
2019-12-11 15:34:35 +01:00
|
|
|
interface_list = interface_list[:-1]
|
2019-12-08 11:20:02 +01:00
|
|
|
|
2019-12-12 23:01:57 +01:00
|
|
|
self.data['interface'] = parse_api(
|
2019-12-11 09:13:35 +01:00
|
|
|
data=self.data['interface'],
|
2019-12-12 23:01:57 +01:00
|
|
|
source=self.api.get_traffic(interface_list),
|
2019-12-11 09:13:35 +01:00
|
|
|
key_search='name',
|
|
|
|
vals=[
|
|
|
|
{'name': 'rx-bits-per-second', 'default': 0},
|
|
|
|
{'name': 'tx-bits-per-second', 'default': 0},
|
|
|
|
]
|
|
|
|
)
|
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-12 23:01:57 +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"
|
2019-12-12 09:06:15 +01:00
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# 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")
|
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-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-05 20:45:04 +01:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
# ---------------------------
|
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")
|
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-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-05 20:45:04 +01:00
|
|
|
|
2019-12-12 13:02:11 +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
|
|
|
|
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-10 22:32:59 +01:00
|
|
|
self.data['arp'][uid]['address'] = mac2ip[self.data['arp'][uid]['mac-address']] if self.data['arp'][uid]['mac-address'] in mac2ip else ""
|
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-12 23:01:57 +01:00
|
|
|
def get_iface_from_entry(self, entry):
|
2019-12-10 22:32:59 +01:00
|
|
|
"""Get interface default-name using name from interface dict"""
|
2019-12-02 19:23:09 +01:00
|
|
|
uid = None
|
|
|
|
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-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
|
|
|
|
# ---------------------------
|
2019-12-12 23:01:57 +01:00
|
|
|
def get_nat(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Get NAT data from Mikrotik"""
|
2019-12-12 23:01:57 +01:00
|
|
|
self.data['nat'] = parse_api(
|
2019-12-12 09:06:15 +01:00
|
|
|
data=self.data['nat'],
|
2019-12-12 23:01:57 +01:00
|
|
|
source=self.api.path("/ip/firewall/nat"),
|
2019-12-12 09:06:15 +01:00
|
|
|
key='.id',
|
|
|
|
vals=[
|
|
|
|
{'name': '.id'},
|
2019-12-21 14:58:41 +01:00
|
|
|
{'name': 'protocol', 'default': 'any'},
|
|
|
|
{'name': 'dst-port', 'default': 'any'},
|
2019-12-12 09:06:15 +01:00
|
|
|
{'name': 'in-interface', 'default': 'any'},
|
|
|
|
{'name': 'to-addresses'},
|
|
|
|
{'name': 'to-ports'},
|
|
|
|
{'name': 'comment'},
|
|
|
|
{'name': 'enabled', 'source': 'disabled', 'type': 'bool', 'reverse': True}
|
|
|
|
],
|
|
|
|
val_proc=[
|
|
|
|
[
|
|
|
|
{'name': 'name'},
|
|
|
|
{'action': 'combine'},
|
|
|
|
{'key': 'protocol'},
|
|
|
|
{'text': ':'},
|
|
|
|
{'key': 'dst-port'}
|
|
|
|
]
|
2019-12-12 13:02:11 +01:00
|
|
|
],
|
2019-12-12 09:06:15 +01:00
|
|
|
only=[
|
|
|
|
{'key': 'action', 'value': 'dst-nat'}
|
|
|
|
]
|
|
|
|
)
|
2019-12-05 20:45:04 +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):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Get routerboard data from Mikrotik"""
|
2019-12-12 23:01:57 +01:00
|
|
|
self.data['routerboard'] = parse_api(
|
2019-12-11 15:34:35 +01:00
|
|
|
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=[
|
|
|
|
{'name': 'routerboard', 'type': 'bool'},
|
|
|
|
{'name': 'model', 'default': 'unknown'},
|
|
|
|
{'name': 'serial-number', 'default': 'unknown'},
|
2019-12-12 13:02:11 +01:00
|
|
|
{'name': 'firmware', 'default': 'unknown'}
|
2019-12-11 15:34:35 +01:00
|
|
|
]
|
|
|
|
)
|
2019-12-05 20:45:04 +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):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Get system resources data from Mikrotik"""
|
2019-12-12 23:01:57 +01:00
|
|
|
self.data['resource'] = parse_api(
|
2019-12-11 15:34:35 +01:00
|
|
|
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=[
|
|
|
|
{'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-08 22:49:36 +01:00
|
|
|
|
2019-12-12 13:02:11 +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)
|
2019-12-12 08:37:28 +01:00
|
|
|
else:
|
|
|
|
self.data['resource']['memory-usage'] = "unknown"
|
|
|
|
|
2019-12-12 13:02:11 +01:00
|
|
|
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)
|
2019-12-12 08:37:28 +01:00
|
|
|
else:
|
|
|
|
self.data['resource']['hdd-usage'] = "unknown"
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-04 16:09:30 +01:00
|
|
|
# ---------------------------
|
|
|
|
# get_system_routerboard
|
|
|
|
# ---------------------------
|
2019-12-12 23:01:57 +01:00
|
|
|
def get_firmware_update(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Check for firmware update on Mikrotik"""
|
2019-12-12 23:01:57 +01:00
|
|
|
self.data['fw-update'] = parse_api(
|
2019-12-12 13:02:11 +01:00
|
|
|
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=[
|
|
|
|
{'name': 'status'},
|
|
|
|
{'name': 'channel', 'default': 'unknown'},
|
|
|
|
{'name': 'installed-version', 'default': 'unknown'},
|
|
|
|
{'name': 'latest-version', 'default': 'unknown'}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
else:
|
|
|
|
self.data['fw-update']['available'] = False
|
|
|
|
|
2019-12-04 20:13:11 +01:00
|
|
|
# ---------------------------
|
|
|
|
# get_script
|
|
|
|
# ---------------------------
|
2019-12-12 23:01:57 +01:00
|
|
|
def get_script(self):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Get list of all scripts from Mikrotik"""
|
2019-12-12 23:01:57 +01:00
|
|
|
self.data['script'] = parse_api(
|
2019-12-11 15:34:35 +01:00
|
|
|
data=self.data['script'],
|
2019-12-12 23:01:57 +01:00
|
|
|
source=self.api.path("/system/script"),
|
2019-12-11 15:34:35 +01:00
|
|
|
key='name',
|
|
|
|
vals=[
|
|
|
|
{'name': 'name'},
|
|
|
|
{'name': 'last-started', 'default': 'unknown'},
|
|
|
|
{'name': 'run-count', 'default': 'unknown'}
|
|
|
|
]
|
|
|
|
)
|