Community discussions

MikroTik App
 
elico
Member Candidate
Member Candidate
Topic Author
Posts: 143
Joined: Mon Nov 07, 2016 3:23 am

Releasing Prometheus RouterOS exporter container

Wed Nov 16, 2022 11:40 pm

Prometheus is known for it's metric scrapping graphing and alerting capabilities.
When combined with grafana it can be a very powerful tool to visualize IT systems and routers statistics and to know about trends in a system over time.

I have created a container for Mikrotik RouterOS devices metric scrapping.
The sources are at:
https://github.com/elico/mikrotik-exporter-container
The exporter is based on the repository at:
https://github.com/nshttpd/mikrotik-exporter

An example of spinning a container:
/interface/bridge/add name=dockers
/ip/address/add address=172.17.0.254/24 interface=dockers

/interface/veth/add name=veth5 address=172.17.0.5/24 gateway=172.17.0.254
/interface/bridge/port add bridge=dockers interface=veth5

/ip/firewall/filter/add chain=input protocol=tcp dst-port=8728 connection-state=new src-address=172.17.0.5 action=accept place-before=0

/container/config/set registry-url=https://registry-1.docker.io tmpdir=disk1/pull

/container/envs/add name=prom_exporter_envs key=TZ value="Asia/Jerusalem"
/container/envs/add name=prom_exporter_envs key=ROUTER_NAME value="RB4011"
/container/envs/add name=prom_exporter_envs key=ROUTER_ADDRESS value="172.17.0.254"
/container/envs/add name=prom_exporter_envs key=USER value="prometheus"
/container/envs/add name=prom_exporter_envs key=PASSWORD value="changeme"
/container/envs/add name=prom_exporter_envs key=PORT value="8728"

/container/envs/add name=prom_exporter_envs key=CONNTRACK_METRICS value="1"
/container/envs/add name=prom_exporter_envs key=DHCP_METRICS value="1"
/container/envs/add name=prom_exporter_envs key=DHCPL_METRICS value="1"
/container/envs/add name=prom_exporter_envs key=FIRMWARE_METRICS value="1"
/container/envs/add name=prom_exporter_envs key=HEALTH_METRICS value="1"
/container/envs/add name=prom_exporter_envs key=ROUTES_METRICS value="1"
/container/envs/add name=prom_exporter_envs key=NETWATCH_METRICS value="1"

/container/add dns=172.17.0.254 remote-image=elicro/mikrotik-exporter:latest interface=veth5 root-dir=disk1/mt-exporter envlist=prom_exporter_envs start-on-boot=yes
And don't forget to create and change the password accordingly:
/user group add name=prometheus policy=api,read,winbox
/user add name=prometheus group=prometheus password=changeme
The exporter is listening at tcp port: 9436
so in the case you will want to access/scrap it you would need to access the target:
172.17.0.5:9436
ie:
http://172.17.0.5:9436/metrics

The container is based on alpine linux to allow the option of debugging in shell if needed.

The exporter is written to handle two scenarios:
to export a single system or more then one system.
It can be a very powerfull tool if used properly.

The container contains a default config file at: /config/config.yaml
https://github.com/elico/mikrotik-expor ... onfig.yaml
If you wish to use your customized config.yaml file to export more then one system or for any other reason you can use a mount directory for that.
The mount directory inside the container is: /config
and it should contain two files:
/config/config.yaml
/config/custom

The file: /config/custom
is being used to instruct the container to spin with the plain configuration and to not transform the container default config.yaml which is really a single node template.

An example for multi RouterOS devices scrapping via a single config file can be seen here:
https://github.com/nshttpd/mikrotik-exp ... g.test.yml

By default all special metrics are disabled unless enabled using environment variable.
The start script with all the available variables can be seen at:
https://github.com/elico/mikrotik-expor ... r/start.sh

The exporter features can be seen at the sources config file at:
https://github.com/nshttpd/mikrotik-exp ... fig.go#L13
To enable a specific metric you need to set the variable with any value, ie not empty like:
/container/envs/add name=prom_exporter_envs key=CONNTRACK_METRICS value="1"
And the key name is composed of "feature_METRICS" while the feature is capitalized.
So for example the feature name for wireless interfaces metrics is listed at:
https://github.com/nshttpd/mikrotik-exp ... fig.go#L28
and is: wlanif
So to enable the wireless interface metrics exporting you would need to define the next key value
/container/envs/add name=prom_exporter_envs key=WLANIF_METRICS value="1"
and instead of "1" you can define any string you want.

Just pay attention that this and other Prometheus exporters do not support authentication and as long they are not firewalled with strict rules anyone can get these metrics, so be aware of what you are running.

I have not tried to run Prometheus on-top of a RouterOS device but I assume that it's possible as well.
 
geebee63
just joined
Posts: 6
Joined: Tue Nov 10, 2020 6:43 pm

Re: Releasing Prometheus RouterOS exporter container

Tue Dec 13, 2022 2:27 pm

Helllo
I tried your container.
My router logs show that the api has logged in successfully.
When I open x.x.x.x:9436/metrics(I had to port forward because im running a container on chr) it all works fine.
I also have grafana running as container on my chr. I set the url for prometheus data source in grafana as http://x.x.x.x:9436/metrics. I tried adjusting the url but no luck. It keeps on giving me this error "Error reading Prometheus: An error occurred within the plugin".
Can you help please?

Edit: Just realise i had to go through a prometheus server. All working now. Thank you so much.
 
tdabasinskas
just joined
Posts: 3
Joined: Wed Feb 08, 2023 12:34 pm

Re: Releasing Prometheus RouterOS exporter container

Wed Feb 08, 2023 12:36 pm

Thanks!

It might be a good idea to add parts of this forum post to the README.md of the repository.

Who is online

Users browsing this forum: No registered users and 6 guests