← Back to list

Monitoring MikroTik Routers via SNMP and MQTT Discovery — A POC

Why This Project Exists

Antonio Francesco Gentile · 2026-02-10 11:36 · 1 claps · 1.8 min read
#mqtt #mikrotik #home-assistant #iot #snmp
Open on Medium ↗
Wiki topics: 📟 · Gadgets & IoT

Monitoring MikroTik Routers via SNMP and MQTT Discovery — A POC

Why This Project Exists

After building a Python-based SNMP-to-MQTT bridge for my switches, NAS devices, and OpenWrt routers, I realized my MikroTik routers could fit perfectly into the same telemetry system.

MikroTik has an excellent SNMP implementation that lets you easily expose CPU, memory, and interface statistics. Thanks to MQTT autodiscovery, all metrics are automatically displayed in Home Assistant. No YAML configuration needed.

Enable SNMP on MikroTik

Open WinBox or SSH into your MikroTik and run:

/snmp set enabled=yes
/snmp community add name=public addresses=192.168.1.0/24
/snmp set contact="admin@home" location="Server Room"

You can verify SNMP is working:

/snmp print

And test it from your Home Assistant server:

snmpwalk -v2c -c public 192.168.1.2 1.3.6.1.2.1.1

You should get something like:

SNMPv2-MIB::sysDescr.0 = STRING: RouterOS 7.x …

Create the Configuration File

File: mikrotik_config.yaml

mqtt:
  host: 192.168.1.100
  port: 1883
  username: mqtt_user
  password: mqtt_pass
  base_topic: homeassistant/sensor/mikrotik

snmp:
  version: 2
  community: public

devices:
  - name: MikroTik-Core
    ip: 192.168.1.2
  - name: MikroTik-Office
    ip: 192.168.1.3

The Python Script (SNMP + MQTT Discovery)

File: mikrotik_snmp_mqtt.py

#!/usr/bin/env python3
import yaml, json, time
from easysnmp import Session
import paho.mqtt.client as mqtt

MIKROTIK_OIDS = {
    "1.3.6.1.2.1.1.3.0": "System Uptime",
    "1.3.6.1.2.1.25.3.3.1.2": "CPU Load",
    "1.3.6.1.2.1.25.2.3.1.5": "Memory Total",
    "1.3.6.1.2.1.25.2.3.1.6": "Memory Used",
    "1.3.6.1.2.1.2.2.1.10": "Interface In Octets",
    "1.3.6.1.2.1.2.2.1.16": "Interface Out Octets"
}

def load_config(path="mikrotik_config.yaml"):
    with open(path, "r") as f:
        return yaml.safe_load(f)

def publish_discovery(client, device_name, base_topic, var):
    full_oid = f"{var.oid}.{var.oid_index}" if var.oid_index else var.oid
    sensor_id = f"{device_name.lower()}_{full_oid.replace('.', '_')}"
    meaning = next((desc for oid, desc in MIKROTIK_OIDS.items() if full_oid.startswith(oid)), "Unknown")

    cfg_topic = f"{base_topic}/{sensor_id}/config"
    state_topic = f"{base_topic}/{sensor_id}/state"

    payload_config = {
        "name": f"{device_name} {meaning}",
        "state_topic": state_topic,
        "unique_id": sensor_id,
        "device": {
            "identifiers": [f"mikrotik_{device_name}"],
            "name": f"MikroTik {device_name}",
            "manufacturer": "MikroTik"
        }
    }

    client.publish(cfg_topic, json.dumps(payload_config), retain=True)
    client.publish(state_topic, var.value, retain=True)

def main():
    cfg = load_config()
    mqtt_cfg, snmp_cfg = cfg["mqtt"], cfg["snmp"]

    client = mqtt.Client()
    client.username_pw_set(mqtt_cfg["username"], mqtt_cfg["password"])
    client.connect(mqtt_cfg["host"], mqtt_cfg["port"], 60)

    for device in cfg["devices"]:
        print(f"Scanning {device['name']} ({device['ip']}) ...")
        session = Session(hostname=device["ip"], community=snmp_cfg["community"], version=snmp_cfg["version"])
        for var in session.walk("1.3.6.1.2.1"):
            if var.value:
                publish_discovery(client, device["name"], mqtt_cfg["base_topic"], var)

    client.disconnect()

if __name__ == "__main__":
    main()

Step 4 — Automate via systemd (optional)

Create the user-level timer and service.

~/.config/systemd/user/mikrotik_snmp.service

Unit]
Description=MikroTik SNMP MQTT Updater

[Service]
Type=oneshot
ExecStart=/usr/bin/python3 %h/ha_snmp/mikrotik_snmp_mqtt.py
WorkingDirectory=%h/ha_snmp

~/.config/systemd/user/mikrotik_snmp.timer

[Timer]

[Timer]
OnBootSec=2min
OnUnitActiveSec=5min
Persistent=true

[Install]
WantedBy=timers.target

Enable:

systemctl --user daemon-reload
systemctl --user enable --now mikrotik_snmp.timer

What Happens Next

  • Every 5 minutes, your MikroTik routers are scanned via SNMP.
  • Each metric (uptime, CPU load, memory usage, traffic counters) is published to MQTT.
  • Home Assistant automatically discovers and creates sensors like:

sensor.mikrotik_core_cpu_load

sensor.mikrotik_office_memory_used

sensor.mikrotik_office_uptime

All automatically generated via MQTT discovery, without a single line of YAML.


메타데이터
post_id
c84f2ecbc325
slug
monitoring-mikrotik-routers-via-snmp-and-mqtt-discovery-a-poc-c84f2ecbc325
url
https://medium.com/@antoniofrancesco.gentile/monitoring-mikrotik-routers-via-snmp-and-mqtt-discovery-a-poc-c84f2ecbc325
canonical_url
https://medium.com/@antoniofrancesco.gentile/monitoring-mikrotik-routers-via-snmp-and-mqtt-discovery-a-poc-c84f2ecbc325
author_url
https://medium.com/@antoniofrancesco.gentile
status
ok
fetched_at
2026-06-21 12:17:11