Push RouterOS metrics to InfluxDB

Here is a really simple and easy way to push metrics (or any value) from RouterOS right into InfluxDB:

#this script grabs some simple measurements
#and POSTs them to an influxDB server using the HTTP API.

#influx details

:local server 12.34.56;
:local port 8086;
:local host routername;

#collect up the measurements and store for performance reasons

:local userCount [/ppp active print count-only];

:local pppoeCount [/interface pppoe-server print count-only];
:local l2tpCount [/interface l2tp-server print count-only];
:local someDHCP [/ip dhcp-server lease print count-only where server="some-dhcp"];
:local connCount [/ip firewall connection print count-only];
:local vlanHosts [/interface bridge host print count-only where on-interface="someVLAN"];

#push measurement name, host, value to influxdb

/tool fetch mode=http url="http://$server/write\3Fdb=mtscripts" port=$port http-method=post http-content-type="application/json" http-data="

active.sessions.total,host=$host\_value=$userCount\n
active.sessions.pppoe,host=$host\_value=$pppoeCount\n
dhcp.someserver,host=$host\_value=$someDHCP\n
hosts.VLAN,host=$host\_value=$vlanHosts\n
firewall.connections,host=$host\_value=$connCount\n
active.sessions.l2tp,host=$host\_value=$l2tpCount"}";

I needed this to grab some simple numbers to put on a Grafana dashboard so I set the script above to run every 5 minutes. So far this has worked really well.

Leave a Reply

Your email address will not be published. Required fields are marked *