MikroTik script: Bandwidth excess notification

This MikroTik script will send a notification when the bandwidth is exceeded on the interface (exceeding the download or upload speed). The script will send an email or Telegram message, but you can schedule other events to be executed.

Speed Limit Exceeded Email Notification
Example email

You may have a lot of Internet bandwidth, but even with traffic control policies configured, the bandwidth may be depleted. Use the Bandwidth Exceeded script to determine the cause of the problem in good time by setting the trigger to 85% bandwidth.

The script is diagnostic, notifying about problems with the exhaustion of the communication channel width, there is no need to run it with high frequency.

Content

  1. Script description
  2. Create trigger
  3. Create script
  4. Script code
  5. Fixing issues

Article in other languages:
🇪🇸 – Script MikroTik: Notificación de exceso de ancho de banda
🇷🇺 – MikroTik скрипт: Уведомление о превышении полосы пропускания
🇫🇷 – Script MikroTik: Notification d’excès de bande passante
🇩🇪 – MikroTik-Script: Benachrichtigung über Bandbreite Überschritten
🇳🇱 – MikroTik-script: Melding van overmatige bandbreedte

Script description

The script uses the functions:

Telegram message - notification of exceeding the speed limit
Telegram message example

The script uses the configured trigger in Traffic Monitor, where we specify the interface and traffic exceeding threshold.

The trigger fires every time the threshold is exceeded and can generate a large number of notifications, so the script controls how often notifications are sent. The script will send a notification when the threshold value is exceeded, if the notification has not been sent yet for the specified time period (NotifyPeriod).

Specify values in the script:

  • NotifyPeriod – interval for sending notifications;
  • Interface – check interface (specify value from Traffic Monitor trigger);
  • Threshold – trigger response threshold (specify value from Traffic Monitor trigger).

Create trigger

In the example, the name of the Internet connection interface is MyInternetProvider, the full bandwidth is 40 Mbits/s, the notification when the threshold value is exceeded (above) is 30 Mbits/s (30M).

[Tools] -> [Traffic Monitor] -> [+] -> [Name=MyISP, Interface=MyInternetProvider, Traffic=received, Trigger=above, Threshold=30M, OnEvents=/system script run TrafficMonitorExtIntefaceNotify;]
Create trigger in Traffic Monitor
Trigger creation in Traffic Monitor

Create script

The following permissions are required to run the script: read, write, test, policy.

[System] -> [Scripts] -> [+] -> [Name: TrafficMonitorExtIntefaceNotify] -> [Policy: read, write, test, policy]
MikroTik script: Bandwidth excess notification
Script notification about the traffic load

Script code

# Name: BandwidthExceededNotification v1
# Description: Bandwidth Exceeded Notification
# Author: Yun Sergey, MHelp.pro 2020
# License: GPL-3.0 License
# Description, purpose and questions: https://mhelp.pro/mikrotik-script-bandwidth-excess-notification/
# More scripts Mikrotik: https://mhelp.pro/tag/mikrotik-scripts/

:local NotifyPeriod [:totime "00:05:00"];
:local Interface "MyInternetProvider";
:local Threshold "30M";

:local DeviceName [/system identity get name];
:local Time [/system clock get time];
:local Date [/system clock get date];
:global TMonExtIntefaceNotifyLastTime;
:local CurrentTime [/system clock get time];
:local TimeShift ($CurrentTime - $TMonExtIntefaceNotifyLastTime);

#:log info "Script BandwidthExceededNotification - start.";

:if ($TimeShift > $NotifyPeriod) do={

    #:log info "Script BandwidthExceededNotification - Bandwidth excess, send message.";

    :local EmailMessageText "Interface: $Interface - bandwidth exceeded ($Threshold). See Traffic Monitor";
    :local TelegramMessageText "Interface: $Interface - bandwidth exceeded ($Threshold). See Traffic Monitor";

    # START SEND EMAIL
    :local SendTo "notify@mhelp.pro";
    :local Subject "\F0\9F\93\88 Bandwidth Exceeded: $DeviceName [$Date $Time]";
    :local MessageText "$EmailMessageText";
    :local FileName "";
    :local SendEmail [:parse [/system script get EMailSendMessageFunction source]];
    $SendEmail SendTo=$SendTo TextMail=$MessageText Subject=$Subject FileName=$FileName;
    # END SEND EMAIL

    # START SEND TELEGRAM
    :local MessageText "\F0\9F\93\88 <b>$DeviceName: Bandwidth Exceeded</b> %0D%0A $TelegramMessageText";
    :local SendTelegramMessage [:parse [/system script  get TGBotSendMessageFunction source]];
    $SendTelegramMessage MessageText=$MessageText;
    # END SEND TELEGRAM

    :set TMonExtIntefaceNotifyLastTime $CurrentTime;
};
#:log info "Script BandwidthExceededNotification - end.";

Fixing issues

The script does not work at startup

Check the script launch rights, they must correspond to those specified in the article (an error was noticed in ROS 6.47.8 – the maximum rights set by default may cause an access error).

Diagnostics of the script

Uncomment the lines :log info (remove the # at the beginning of the line).

  1. Script BandwidthExceededNotification – start.” – the script was launched successfully;
  2. Script BandwidthExceededNotification – Bandwidth excess, send message.” – limitation detected, notification period expired, notifications sending;
  3. Script BandwidthExceededNotification – end.” – script work completed successfully.

If you receive messages 1 and 3, the script is working fine, the notification interval has not expired.

If you received notifications 1 and 2, but did not receive 3, there is a problem with the text of the sent message (the use of special characters in the device name or the text of the message).


🟢 MikroTik script – bandwidth exceeded (download speed, upload speed) notification, discussed in this article. I hope that controlling the use of the Internet channel will help you improve the performance of your MikroTik devices. However, if you run into any problems while setting up, feel free to write in the comments. I will try to help.

✅ The script is verified: MikroTik hAP ac lite (RouterBOARD 952Ui-5ac2nD), RouterOS 6.48 (stable).

📒 When copying materials from the site, you must indicate the author (Yun Sergey) and a hyperlink to the source (MHelp.pro).

Leave a Reply

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