Compare commits

..

No commits in common. "f497d3a1d17f8a8b727e39578b8887cd2fcc9135" and "6546d319ae75ab2c7f1a764799216e813e06e059" have entirely different histories.

376
README.md
View file

@ -1,219 +1,159 @@
# MikroTik Monitoring with Grafana Dashboard # MikroTik Monitoring with Grafana Dashboard
Grafana Dashboard in MikroTik Container to monitor a MikroTik device (using SNMP).\
A self-contained monitoring stack (Grafana + Prometheus + SNMP Exporter) deployed inside a MikroTik RouterOS v7+ container environment to collect and visualize device metrics using SNMP. [See preview images](#dashboard-screen).
> This solution leverages the [MikroTik Container](https://help.mikrotik.com/docs/display/ROS/Container) feature introduced in RouterOS v7 to avoid the need for external servers. In this project we will use MikroTik Container to monitor the MikroTik itself (using SNMP).\
Using it reduce the costs and complexity derived from using/deploying a dedicated machine for the same objective.
---
DISCLAIMER: [MikroTik Container](https://help.mikrotik.com/docs/display/ROS/Container) is a MikroTik feature available from RouterOS v7, you can [update](https://help.mikrotik.com/docs/display/ROS/Upgrading+and+installation#:~:text=using%20this%20method.-,Manual%20upgrade%20process,-First%20step%20%2D%20visit) yours if lower.
## Table of Contents
We can monitor MikroTik using 3 Containers (SNMP Exporter + Prometheus + Grafana).\
* [Overview](#overview) [SNMP Exporter](https://hub.docker.com/r/prom/snmp-exporter), [Prometheus](https://hub.docker.com/r/prom/prometheus) and [Grafana](https://hub.docker.com/r/grafana/grafana) are a well known stack of tools that enable to monitor any type of device with appropriate configurations.\
* [Prerequisites](#prerequisites) In this scenario the metrics are retrieved in real time with SNMP, Prometheus act as timeseries database with query and Grafana had that sweet dashboards.
* [Setup](#setup)
* [1. Enable SNMP on MikroTik](#1-enable-snmp-on-mikrotik) In my configuration I have a MikroTik RouterBoard L009UiGS (ARM) with ROS v7.12 but the steps are very general and almost equal for every version.
* [2. Enable Container Support](#2-enable-container-support) # Setup steps
* [3. Configure VETH and Network](#3-configure-veth-and-network) First of all Enable SNMP on MikroTik in IP->SNMP, tick the box and leave others as default.
* [4. Prepare Configuration Files](#4-prepare-configuration-files) ## 1. MikroTik activate Container feature
* [5. Define Environment Variables and Mounts](#5-define-environment-variables-and-mounts) First of all we have to activate the *Container* package on MikroTik, to default it is disabled.\
* [6. Deploy and Start Containers](#6-deploy-and-start-containers) To check if it is also activated in WinBox, open *New Terminal* and run:
* [Accessing the Services](#accessing-the-services) `system/device-mode/pr`\
* [Known Issues & Troubleshooting](#known-issues--troubleshooting) After the mode (e.g. `mode: enterprise`) should be compare the string `container: yes`\
* [References](#references) If you have already activated it, go next to the step 2.\
Else if not you have to **download, install and activate the package**.
---
1. **Download**
## Overview - Go to https://mikrotik.com/download/archive
- Select your ROS version
This project demonstrates how to deploy a complete monitoring stack directly on a MikroTik router using containers. It consists of: - Download the *all_packages* zip of your architecture
- You can see your architecture in WinBox `System->Resources`
* **SNMP Exporter** for retrieving SNMP metrics.
* **Prometheus** as the time-series database and metrics scraper. 2. **Install**
* **Grafana** for dashboard visualization. - From the downloaded zip extract file *"container-X.Y.npk"* and upload it inside *Files* in MikroTik (you can Drag and Drop the file inside WinBox)
- Now *System->Reboot* the MikroTik in order to install the package
All components run within the MikroTik device itself, avoiding the cost and complexity of external infrastructure. - After that you can already see *Container* features on the left bar of WinBox but you still cannot use it until you activate it.
![Grafana Dashboard Screenshot](https://github.com/IgorKha/Grafana-Mikrotik/blob/master/readme/screen.png) 3. **Activate**
- In WinBox, open *New Terminal* and run:
--- - `system/device-mode/update container=yes`
- After that you have to shutdown the MikroTik as explained in the CLI, turning power off or pressing button of MikroTik device, otherwise the activation is stopped.
## Prerequisites - If the activation worked after the boot you can check running in New Terminal `system/device-mode/pr` again as above.
* MikroTik RouterOS **v7+** with **Container** feature support. ## 2. MikroTik VETH interfaces configuration
* A device with sufficient resources (e.g., RouterBoard L009UiGS ARM). Note: After each following step press before *Apply* and then *OK* buttons.
* Basic familiarity with WinBox or CLI. 1. Go to *Bridge* and in *Bridge* tab create a *New Interface*
* External storage (e.g., USB drive) recommended for container volumes. - Name: docker
- Leave the rest of fields as default
--- 2. Go to *IP->Addresses* add a *New Address*
- Address: 192.168.50.1/24
## Setup - Network: 192.168.50.0
- Interface select "docker"
### 1. Enable SNMP on MikroTik 3. Go to *IP->Firewall* in *NAT* tab create a *New NAT Rule*
- Chain select "srcnat"
* Navigate to **IP > SNMP** - Src. Address: 192.168.50.0/24
* Enable SNMP (tick the checkbox), leave defaults. - (This is the *Addresses* of the *Bridge* "docker")
- Go to *Action* tab and as *Action* select "masquerade"
--- 4. Go to *Interfaces* and in *VETH* tab add 3 *Interfaces* (*Copy* the first to speed up the process)
1. *New Interface* for **Grafana**
### 2. Enable Container Support - Name: grafana
- Address: 192.168.50.100/24
#### a. Check if Container is enabled: - Gateway: 192.168.50.1
2. *New Interface* for **SNMP Exporter**
From a **New Terminal** in WinBox: - Name: snmp_exporter
- Address: 192.168.50.101/24
```bash - Gateway: 192.168.50.1
/system/device-mode/print 3. *New Interface* for **Prometehus**
``` - Name: prometheus
- Address: 192.168.50.102/24
Ensure the output includes `container: yes`. - Gateway: 192.168.50.1
5. Go to *Bridge* and in *Ports* tab **create a *New Bridge Port* for each *VETH interface*** (grafana, snmp_exporter, prometheus) created above and select *"docker"* as Bridge.
#### b. If not enabled: - (In order to Bridge all the interfaces on the same subnet LAN)
1. **Download**: ## 3. Configuration files
Prepare configuration files for containers.
* Visit [MikroTik Download Archive](https://mikrotik.com/download/archive) 1. Download the 3 folders (grafana, prometheus and snmp) from the repository.
* Select your RouterOS version and architecture (from WinBox in System > Resources) 2. Modify files as follow:
* Download the `all_packages` ZIP - grafana/provisioning/datasource.yml
- url: http://192.168.50.102:9090
2. **Install**: - (or your prometheus VETH IP)
- prometheus/prometheus.yml
* Extract `container-*.npk` from the ZIP - in *job_name MikroTik* change targets to your Mikrotik ip (MikroTik IP where SNMP is enabled)
* Upload it to **Files** via WinBox - in *replacement* put the snmp_exporter VETH IP: 192.168.50.101:9116
* Reboot the device 3. Finally load the 3 folders in MikroTik ***Files***.
3. **Activate**: ## 4. MikroTik Containers configuration
First of all we have to to set the Environment variables and Mount path to volume for our Containers.
* Run: `system/device-mode/update container=yes` 1. Go to *Container* and in **Envs** tab.\
* Perform a full shutdown (unplug power or press the power button of the physical device) The Envs interest only the grafana container, we define this 3 envs for it:
* Verify again with: `system/device-mode/print` - Name: `grafana`, Key: `GF_SECURITY_ADMIN_USE`, Value: `admin`
- Name: `grafana`, Key: `GF_SECURITY_ADMIN_PASSWORD`, Value: `mikrotik`
--- - Name: `grafana`, Key: `GF_SECURITY_SIGN_UP`, Value: `false`
### 3. Configure VETH and Network 2. Go to *Container* and in **Mounts** tab.\
Note: all *src path* is at your choice e.g. path to an external usb drive attached to MikroTik.
> After each step press Apply and then OK buttons. There are one Mount for each container:
- Name: `grafana`, Src: `/usb1/grafana/provisioning`, Dst: `/etc/grafana/provisioning/`
#### a. Create a Docker bridge: - Name: `prometheus`, Src: `/usb1/prometheus`, Dst: `/etc/prometheus`
- Name: `snmp`, Src: `/usb1/snmp`, Dst: `/etc/snmp_exporter`
1. **Bridge > Bridge**: Add new interface named `docker`, leave defaults
2. **IP > Addresses**: Add Address `192.168.50.1/24`, Network `192.168.50.0`, Interface `docker` 3. Finally, go to *Container* and in **Container** tab press *Config* button
3. **IP > Firewall > NAT**: - Registry URL: https://registry-1.docker.io
- Tmp Dir: /pull
* Chain: `srcnat` - (This path is at your choice e.g. path to an external usb drive attached to MikroTik)
* Src. Address: `192.168.50.0/24`
* Action: `masquerade` 4. Now in tab Container we can create a container for each VETH created before.\
Note: in each container the `Root Dir` path field is at your choice (e.g. path to an external usb drive attached to MikroTik e.g. `/usb1/grafana_dir`).\
#### b. Add VETH interfaces: **After you fill out the fields of each container press *Apply* and wait for download/extracting of the container until in status appear *"stopped"* (if it appear is all OK) then go to create the next container, else if appear status: "*error*", *Copy* the container, delete the old, and re-try to *Apply*.**
- New Container for Grafana
* Interface → VETH tab, add 3 interfaces (Name → Address, Gateway): - Remote Image: grafana/grafana:9.1.0
- Interface select *"grafana"*
* `grafana``192.168.50.100/24`, `192.168.50.1` - Envslist: grafana
* `snmp_exporter``192.168.50.101/24`, `192.168.50.1` - Mounts select "*grafana*"
* `prometheus``192.168.50.102/24`, `192.168.50.1` - Root Dir: /usb1/grafana_dir
- (Tick *Logging* and *Start On Boot* boxes if you want)
#### c. Bridge the interfaces: - New Container for Prometheus
- Remote Image: prom/prometheus:latest
* Bridge > Ports: Create a **New Bridge Port** for each VETH interface and add it to the `docker` bridge. - Interface select *"prometheus"*
- Cmd: --config.file=/etc/prometheus/prometheus.yml
--- - Root Dir: /usb1/prometheus_dir
- Mounts select *"prometheus"*
### 4. Prepare Configuration Files - (Tick *Logging* and *Start On Boot* boxes if you want)
- New Container for SNMP Exporter
1. Clone this repository and extract `grafana/`, `prometheus/`, `snmp/` folders. - Remote Image: prom/snmp-exporter:latest
2. Edit the following: - Interface select *"snmp_exporter"*
- Cmd: --config.file=/etc/snmp_exporter/snmp.yml
* **Grafana Datasource** (`grafana/provisioning/datasources.yml`): - Root Dir: /usb1/snmp_dir
- Mounts select *"snmp"*
```yaml - (Tick *Logging* and *Start On Boot* boxes if you want)
url: http://192.168.50.102:9090 # change it according with your prometheus VETH IP
``` ## 5. Start Container
* **Prometheus Config** (`prometheus/prometheus.yml`): After all you can Start and Stop the container using the respective button.\
Start all containers in order (SNMP Exporter, Prometheus and Grafana) and enjoy.
* `static_configs.targets`: use your MikroTik LAN IP
* `relabel_configs.replacement`: `192.168.50.101:9116` (change it according with your snmp_exporter VETH IP) ![containers](container_image.png)
3. Upload `grafana/`, `prometheus/`, `snmp/` folders to MikroTik via **Files**. ### Link
If you configured the VETH interfaces and other as above, then:
--- - Grafana: [192.168.50.100:3000](192.168.50.100:3000)
- admin:mikrotik
### 5. Define Environment Variables and Mounts - Prometheus: [192.168.50.102:9090](192.168.50.102:9090)
- SNMP Exporter: [192.168.50.101:9116](192.168.50.101:9116)
#### a. Container > Envs (Grafana only):
Obv, you should assure that they are reachable from your host.
| Key | Value | # Possible issues
| ----------------------------- | -------- | - In some version for ARM Grafana show "No data" to interface in and out:
| GF\_SECURITY\_ADMIN\_USER | admin | - Rapid solution: edit the dashboard, edit the query, change $scrap_intervall to 30s or others. (after you modify the ether1 all other will change automatically)
| GF\_SECURITY\_ADMIN\_PASSWORD | mikrotik | - Solution: from grafana 9.1.0 the problem was resolved
| GF\_SECURITY\_SIGN\_UP | false | - Container store is created only the first time that the Apply button is pressed
- Possible issue: mikrotik container create a directory instead of container store
#### b. Container > Mounts: - If you create or remove container with same Root Dir of another the conflicts is coming to you!
> Use your own `src path` (e.g. path to an external usb drive attached to MikroTik) - Be careful before removing container, when you do it the relative Root Dir is eliminated (even container store)
- Be careful before press Apply button delete all the container that you want to eliminate
| Container | Src Path | Dst Path | - When Start the container check in Files if the Root Dir is created as "container store" type, if not there was a problem (starting the container will fail with "execve: No such file or directory"), copy the container setup and change the Root Dir until the container is started correctly
| ---------- | ---------------------------- | ---------------------------- |
| grafana | `/usb1/grafana/provisioning` | `/etc/grafana/provisioning/` | Thanks [@IgorKha](https://github.com/IgorKha/) for [configuration files](https://github.com/IgorKha/Grafana-Mikrotik/) and [Dashboard](https://grafana.com/grafana/dashboards/14420-mikrotik-monitoring/).
| prometheus | `/usb1/prometheus` | `/etc/prometheus` |
| snmp | `/usb1/snmp` | `/etc/snmp_exporter` | Special thanks to my MikroTik teacher @rolando.
--- ## Dashboard screen
Screen by [@IgorKha](https://github.com/IgorKha/)
### 6. Deploy and Start Containers ![dashboard](https://github.com/IgorKha/Grafana-Mikrotik/blob/master/readme/screen.png)
#### a. Configure Container settings:
**Container > Container > Config:**
* Registry URL: `https://registry-1.docker.io`
* Tmp Dir: `/pull` (customizable e.g. path to an external usb drive attached to MikroTik)
#### b. Create containers:
> After you fill out the fields of each container press Apply and **wait for download/extracting** until in status appear `stopped` (if it appear is all OK) then go to create the next container. If it shows `error`, Copy the container, delete the old, and re-try to Apply.
**Container > New Container**
| Container | Remote Image | Interface | Others |
| ----------------- | --------------------------- | --------------- | -------------------------------------------------------------------- |
| **Grafana** | `grafana/grafana:9.1.0` | `grafana` | Envslist `grafana`, Mounts `grafana`, Root Dir `/usb1/grafana_dir` |
| **Prometheus** | `prom/prometheus:latest` | `prometheus` | Cmd: `--config.file=/etc/prometheus/prometheus.yml`, Mounts `prometheus`, Root Dir `/usb1/prometheus_dir`|
| **SNMP Exporter** | `prom/snmp-exporter:latest` | `snmp_exporter` | Cmd: `--config.file=/etc/snmp_exporter/snmp.yml`, Mounts `snmp`, Root Dir `/usb1/snmp_dir` |
> `Root Dir` is customizable e.g. path to an external usb drive attached to MikroTik
---
## Accessing the Services
> Ensure to start containers in order (SNMP Exporter, Prometheus and Grafana) and enjoy.
> Start and Stop the container using the respective button.
> Ensure the router and host are on the same LAN or routing is configured properly.
| Service | URL |
| ------------- | -------------------------------------------------------- |
| Grafana | [http://192.168.50.100:3000](http://192.168.50.100:3000) |
| Prometheus | [http://192.168.50.102:9090](http://192.168.50.102:9090) |
| SNMP Exporter | [http://192.168.50.101:9116](http://192.168.50.101:9116) |
Grafana credentials: `admin / mikrotik`
---
## Known Issues & Troubleshooting
* **Grafana “No Data” Bug on ARM**:
* Try increasing `$scrape_interval` to 30s in the dashboard query.
* Fixed in Grafana ≥ 9.1.0
* **Container Store Errors**:
* Ensure container root dirs are unique.
* Deleting a container deletes its root directory.
* Always verify the root dir is marked as `container store` in Files. (and **not as `directory`**)
* **Startup Failures**:
* If container fails with `execve: No such file or directory`, verify paths, mounts, and retry with a different root dir.
---
## References
* Configuration and dashboard by [@IgorKha](https://github.com/IgorKha/): [Dashboard #14420](https://grafana.com/grafana/dashboards/14420-mikrotik-monitoring/)
* Special thanks to @rolando for guidance on MikroTik world.