Mikrotik Nightly Usage E-mail Script

by Jim Bouse 7. December 2017 08:12

One of my buddies asked for a script that would e-mail him the usage from his Mikrotik users.

I whipped this up to help him take care of it.

It assumes that you have simple queues for each user.

# Nightly E-mail Usage - Jim Bouse - Brazos WiFi - 2017-12-7

:local identity [/system identity get name];
:local email "asdf@asdf.com";
:local subject "Usage Report - $identity";
:local body "";

:put "Starting E-mail Usage Script";

:foreach queue in=[/queue simple find] do={
  :local bytes [/queue simple get $queue bytes];
  :local target [/queue simple get $queue target];
  :local name [/queue simple get $queue name];
  :local ul [:pick $bytes 0 [:find $bytes "/"]];
  :local dl [:pick $bytes ([:find $bytes "/"]+1) [:len $bytes] ];

  # Divide bytes by 1000 to get kB
  :set ul ($ul/1000);
  :set dl ($dl/1000);

  # Divide kB by 1000 to get MB
  :set ul ($ul/1000);
  :set dl ($dl/1000);

  :local line "$target - $name - $dl/$ul\n";
  :set body "$line$body";

  /queue simple reset-counters $queue;
:put "Sending e-mail";
/tool e-mail send to=$email subject=$subject body=$body;
:put "Sent e-mail";


Edit this text file to include your e-mail instead of asdf@asdf.com.  Then paste the whole TXT file contents into a terminal window for your Mikrotik.  It will create the script and set it to send daily at 11:50PM.

Nightly E-mail Usage.txt (1.46 kb)



Powered by BlogEngine.NET
Original Design by Laptop Geek, Adapted by onesoft