MikroTik Script: Push Router Stats to dweet.io

MikroTik Script: Push Router Stats to dweet.io

In this tutorial, I will show you how to create a RouterOS script to push Router statistics up to dweet.io and then use that data to create a nice monitoring dashboard with slick gauges and graphs over at freeboard.io.

Disclaimer: dweet.io data feeds are public so consider this before pushing any sensitive data up to dweet.io. They do have a commercial accounts in which you can lock (make private) data feeds. freeboard.io dashboards are public by default but can be make private. Also, this script is provided as-is, use at your own risk.

MikroTik Script to push router stats to dweet.io

#
#   MikroTik RouterOS Script
#   This script has two parts:  1) RouterOS statistics collector 2) Push router stats up to dweet.io
#
#   This script can be adapted to send the collected statistics to any NMS system that supports
#    receiving data via http get/post requests.
#
#   Tested on RouterOS v6.40.3
#   Learn more about this script and dweet.io by reading...
#   http://jcutrer.com/howto/networking/mikrotik/mikrotik-script-dweet-io-stats
# 
#   Script Revision:  1.0.2
#   Author Name:     Jonathan Cutrer
#   Author URL:       http://jcutrer.com
#
#   Change Log
#
#   1.0.2      Reworked stats collection in groups, you can turn each on/off below
#              Changed dweet.io call to use SSL (https://)
#              Added VPN stats collection
#              Added Health stats collection
#              Added Routing Protocol stats collection
#
#   1.0.1      Added keep-result=no to last line
#
#   1.0.0      Initial Release
#
#
# Begin Setup
#
:local interfaceWAN         "ether1";
:local interfaceWLAN        "wlan1";
:local interfaceWLANGuest   "wlan2";
:local prefix               "mikrotik-stats";
:local dweetURL             "https://dweet.io/dweet/for/";
:local dataParams;

#
# Collection Groups
# Define what gets groups of statistics get collected and sent
#
:local enBoard true
:local enPerf true
:local enHealth true
:local enRouter true
:local enRouting true
:local enFirewall true
:local enWireless true
:local enVPN true
#
# End Setup


#
# Collect Stats: Board
#
:local boardData; :local identity; :local model; :local serial;
if ( $enBoard ) do={
:put "Collecting Board data..."
    :set identity             [/system identity get name];
    :set model                [/system routerboard get model];
    :set serial         [/system routerboard get serial-number];
    :set boardData        "identity=$identity&model=$model&serial=$serial"
    :set dataParams $boardData;

}

# Set the thing name
:local thing          "$prefix-$serial"

#
# Collect Stats: Perf
#
:local perfData; :local cpuLoad; :local memFree; :local uptime;
if ( $enPerf ) do={
    :put "Collecting Performance data..."
    :delay 5
    :set cpuLoad [/system resource get cpu-load];
    :set memFree [/system resource get free-memory];
    :set uptime [/system resource get uptime];
    :set perfData "cpu-load=$cpuLoad&uptime=$uptime&mem-free=$memFree"
    :set dataParams ( $dataParams . "&" . $perfData);
}

#
# Collect Stats: Health
#
:local healthData; :local volts; :local amps; :local watts; :local temp; :local cpuTemp; :local fanSpeed;
if ( $enHealth ) do={
    :put "Collecting Health data..."
    :set volts [/system health get voltage];
    :set amps [/system health get current];
    :set watts [/system health get power-consumption];
    :set temp [/system health get temperature];
    :set cpuTemp [/system health get cpu-temperature];
    :set fanSpeed [/system health get fan1-speed];
    :set healthData "volts=$volts&amps=$amps&watts=$watts&temp=$temp&cpu-temp=$cpuTemp&fan-speed=$fanSpeed"
    :set dataParams ( $dataParams . "&" . $healthData);
}


#
# Collect Stats: Router
#
:local routerData; :local ipRoutes;
if ( $enRouter ) do={
    :put "Collecting Router data..."
    :set ipRoutes [:len [/ip route find]];
    :set routerData  "ip-routes=$ipRoutes"
    :set dataParams ( $dataParams . "&" . $routerData);
}

#
# Collect Stats: Routing
#
:local routingData; :local bgpPeers; :local ospfNeighbors;
if ( $enRouting ) do={
    : put "Collecting Routing Protocol data..." ;
    :set bgpPeers [:len [/routing bgp peer find]];
    :set ospfNeighbors [:len [/routing ospf neighbor find]];
    :set routingData  "bgp-peers=$bgpPeers&ospf-neighbors=$ospfNeighbors";
    :set dataParams ( $dataParams . "&" . $routingData);
}

#
# Collect Stats: Wireless
#
:local wirelessData; :local wlanClients; :local wlanGuests;
if ( $enWireless ) do={
    :put "Collecting Wireless data...";
    :set wlanClients [/interface wireless registration-table print count-only where interface="$interfaceWLAN"];
    :set wlanGuests [/interface wireless registration-table print count-only where interface="$interfaceWLANGuest"];
    :set wirelessData "wlan-clients=$wlanClients&wlan-guests=$wlanGuests";
    :set dataParams ( $dataParams . "&" . $wirelessData);
}

#
# Collect Stats: Firewall
#
:local firewallData; :local ipFwConx;
if ( $enFirewall ) do={
    :put "Collecting Firewall data...";
    :set ipFwConx [/ip firewall connection tracking get total-entries];
    :set firewallData "ip-fw-conx=$ipFwConx";
    :set dataParams ( $dataParams . "&" . $firewallData);
}

#
# Collect Stats: VPN
#
:local vpnData; :local vpnPppConx; :local vpnIpsecPeers; :local vpnIpsecPolicy;
if ( $enFirewall ) do={
    :put "Collecting VPN data...";
    :set vpnPppConx [:len [/ppp active find]];
    :set vpnIpsecPeers [:len [/ip ipsec remote-peers find]];
    :set vpnIpsecPolicy [:len [/ip ipsec policy find]];
    :set vpnData "vpn-ppp-conx=$vpnPppConx&vpn-ipsec-peers=$vpnIpsecPeers&vpn-ipsec-policys=$vpnIpsecPolicy";
    :set dataParams ( $dataParams . "&" . $vpnData);
}


#
# Test Output of Collected Data
#
:put $boardData
:put $perfData
:put $healthData
:put $routerData
:put $routingData
:put $wirelessData
:put $firewallData
:put $vpnData


#
# Build the Final Request URL
#
:local finalURL;
:set finalURL "$dweetURL$thing?$dataParams" 

# print the final url
:put $finalURL


#
# Push data to dweet.io
#
/tool fetch url="$finalURL" mode=https keep-result=no

# end of script

Install the script

  1. In Winbox, click System | Scripts
  2. From the Script List, click the Add (+) toolbar button
  3. Paste in the above script and name it “dweet-stats”mikrotik-script-push-dweet-stats

Before we create the schedule, you will want to adjust the Setup section of the scripts and make sure it executes without error.  My WAN interface name is ether1 but some RouterOS configs name it ether1-gateway or you may be using a different interface for your WAN port.  The above script also assumes that your wireless interface is named wlan1 and that you have a guest wireless interface wlan2. If you run into problems or have no wireless interfaces on your router just comment out those lines and adjust the finalurl variable to exclude that data.

Test Execute the script

  1. In WinBox, click New Terminal
  2. Paste in /system script run dweet-stats and press [enter]

The script should execute without syntax errors and give you output similar to this…

[admin@R1] > /system script run dweet-stats              
Collecting Board data...
Collecting Performance data...
Collecting Health data...
Collecting Router data...
Collecting Routing Protocol data...
Collecting Wireless data...
Collecting Firewall data...
Collecting VPN data...
identity=R1&model=1100AHx2&serial=341102911117
cpu-load=2&uptime=2w3d08:18:34&mem-free=1557213184
volts=118&amps=801&watts=94&temp=23&cpu-temp=33&fan-speed=3199
ip-routes=190
bgp-peers=0&ospf-neighbors=3
wlan-clients=0&wlan-guests=0
ip-fw-conx=30
vpn-ppp-conx=0&vpn-ipsec-peers=2&vpn-ipsec-policys=5
https://dweet.io/dweet/for/mikrotik-stats-341102911117?identity=R1&model=1100AHx2&serial=3411029CC647&cpu-load=2&uptime=2w3d08:18:34&mem-free=1557213184
&volts=118&amps=801&watts=94&temp=23&cpu-temp=33&fan-speed=3199&ip-routes=190&bgp-peers=0&ospf-neighbors=3&wlan-clients=0&wlan-guests=0&ip-fw-conx=30&vpn-ppp-
conx=0&vpn-ipsec-peers=2&vpn-ipsec-policys=5
      status: finished
  downloaded: 0KiBC-z pause]
       total: 0KiB
    duration: 0s

[admin@R1] >

Schedule the script to run every X minutes or seconds

  1. In Winbox, click System | Scheduler
  2. From the Script List, click the Add (+) toolbar button
  3. Name the schedule “run-dweet-stats” and set the Interval: ie. 00:01:00 = Every 1 minute
  4. Paste /system script run dweet-stats into the On Event: boxmikrotik-schedule-dweet-script

View your router stats on dweet.io

Your router’s data will land at http://dweet.io/follow/mikrotik-stats-<serial-number>

Here is what you can expect to see http://dweet.io/follow/mikrotik-stats-3E2E02190C64

mikrotik-iot-data-stream-dweet
Click to Enlarge

You can also see the raw data that is being submitted by clicking on the Raw tab.

mikrotik-dweet.io-raw-data

A Real Time Dashboard for your Router Stats

Next, we can work with the dweet data to build a dashboard with freeboard.io.  Checkout the one I created here https://freeboard.io/board/5a20d586511f114a61002d82.

mikrotik-router-freeboard-dashboard
Click to Enlarge

To create your own dashboard you can click the big orange button on your dweet.io thing page or you can clone my dashboard and change the datasource to your dweet.io thing name.

create-clone-freeboard-io

Future Enhancements

I plan on adding WAN interface TX/RX utilization stats to the script.  What else do you think would be useful? comment below

Reference

NetScout LinkRunner G2

LinkRunner G2 is the ultimate network cable test tool


CAT5 Cable Tester, Measure Cable Length,
PoE Voltage, Network Connectivity, Switch Port ID
Optional Wireless & Fiber Optics Modules
Check Price on Amazon

4 Replies to “MikroTik Script: Push Router Stats to dweet.io”

  1. I need some help here
    am supposed to customize the freeboard to track the network status of the two routers in the company but am totally green on how to go about it

    1. hi there, nice scripts, i wonder if you can help with a script that collects wifi password entries when someone is trying to access the ssid of the routerboard, wrong and correct data entries. look fw for your reply,

  2. Wondering if this needs an update?

    :local boardData; :local identity; :local model; :local serial;
    [admin@DWEET-TEST-ROUTER] > if ( $enBoard ) do={
    {… :put “Collecting Board data…”
    {… :set identity [/system identity get name];
    syntax error (line 3 column 10)
    [admin@DWEET-TEST-ROUTER] > :set model [/system routerboard get model];
    syntax error (line 1 column 10)
    [admin@DWEET-TEST-ROUTER] > :set serial [/system routerboard get serial-number];
    syntax error (line 1 column 10)
    [admin@DWEET-TEST-ROUTER] > :set boardData “identity=$identity&model=$model&serial=$serial”
    syntax error (line 1 column 10)
    [admin@DWEET-TEST-ROUTER] > :set dataParams $boardData;
    syntax error (line 1 column 10)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.