Notes:
This exporter uses the MikroTik REST API to query metrics and exposes them in a Prometheus‑scrapable format, following the multi‑target exporter pattern where the target device is passed as a parameter to a “probe” endpoint.
debug = false; if ($API->connect('192.168.88.1', 'api_user', 'SecurePassword123')) // Add a new firewall drop rule at the top of the chain $API->comm("/ip/firewall/filter/add", array( "chain" => "forward", "src-address" => "198.51.100.45", "action" => "drop", "comment" => "Malicious IP blocked via Web Portal API" )); echo "Firewall rule successfully injected."; $API->disconnect(); else echo "Connection failed. Check network routing or user credentials."; ?> Use code with caution. Go: Advanced Real-Time Traffic Monitoring
logs = api.path('log').select('time', 'message') for log in logs: if 'ssh' in log['message'].lower() and 'failure' in log['message'].lower(): # Extract IP using regex (pseudo) import re match = re.search(r'from (\d+.\d+.\d+.\d+)', log['message']) if match: ip = match.group(1) # Check if already listed existing = list(address_list.select(list='ssh_block', address=ip)) if not existing: address_list.add(list='ssh_block', address=ip, timeout='1h') print(f"Banned ip for 1 hour")
import requests from requests.auth import HTTPBasicAuth mikrotik api examples
MikroTik API over TLS (port 8729) with curl:
For RouterOS 7 REST API in Go, the routerosv7_restfull_api package offers a clean interface:
The evilfreelancer/routeros-api-php package provides a modern PHP client with Laravel support:
The API syntax mirrors CLI commands – use ? for filters, = for parameters, and empty line to terminate. Always test in a lab before deploying to production. Notes: This exporter uses the MikroTik REST API
api = librouteros.connect( host='192.168.88.1', username='admin', password='', port=8728 )
The timeout parameter automatically removes the entry after the specified time (e.g., 1h , 30m , 2d ), which is much more efficient than writing custom cleanup scripts.
Before adding a rule, check if it exists:
B) Using librouteros for binary API (v6/v7) Go: Advanced Real-Time Traffic Monitoring logs = api
This library simplifies the low-level byte encoding.
print(mikrotik_cmd('192.168.88.1', 8728, 'admin', '', '/interface/print'))
Change queue max-limit at 9 AM and 6 PM.