MikroTik Script: Device processor overload notification

The script measures the processor utilization level of the MikroTik device, after a certain period of time. Calculates the average processor load. Sends a notification by e-mail or Telegram message when the CPU load threshold is exceeded, as well as the dynamics of the CPU load.

Also, the script can be used to monitor the CPU load and write the results to the MikroTik device log. You can add the ability to save data to a file.

Email message about CPU overload
Sample CPU Overload Notification Email

Content

  1. Description of the script
  2. Create script
  3. Add script to scheduler

Article in other languages:
?? – MikroTik Script: Notificación de sobrecarga del procesador del dispositivo
?? – MikroTik Скрипт: Уведомление о перегрузке процессора устройства
?? – MikroTik Script: Notification de surcharge du processeur de périphérique
?? – MikroTik Script: Benachrichtigung über Überlastung des Gerätes Prozessors
?? – MikroTik-script: Melding over overbelasting van apparaat processor

Description of the script

Specify the excess threshold (percentage of CPU load) – DeviceAverageLoadThreshold and the number of measurements – NumberOfMeasurements. Measurement interval 2 seconds. If the threshold value is exceeded, the message will indicate the measurement results to assess the dynamics of the load.

Do not set DeviceAverageLoadThreshold too high – this is an average value, start at 40% and if the device will not cause problems during this load, gradually increase the script’s threshold.

If an already running script instance is found when running the “MikroTik processor load check” script, the script will not be run twice, and a warning will be displayed in the device log.

Each time the script is run, even if the AverageCPULoad threshold is not exceeded, a line like this is written to the device log:

Average CPU load = 6%

This is useful for testing, but you can comment out or remove this line:

#:log info "Average CPU load = $AverageCPULoad%";

The script sends notification to email and Telegram message using the functions:

The message contains:

  • Average processor load (measurements performed);
  • Device CPU model;
  • Script response threshold (CPU load);
  • The number of measurements taken;
  • CPU load for each measurement.
Telegram message about CPU overload
Example of Telegram message with a CPU overload notification

If you do not need any of the types of notifications, remove the call section from the script text.

Create script

To run the script, you need permission: read, write, test, policy.

[System] -> [Scripts] -> [+] -> [Name: CPUOverloadCheck] -> [Policy: read, write, test, policy]

Script code:

:local DeviceAverageLoadThreshold 25;
:local NumberOfMeasurements 5;

:local DeviceName [/system identity get name];
:local Time [/system clock get time];
:local Date [/system clock get date];
:local CPUModel [/system resource get cpu];
:local Load 0;
:local Message "";

:for Measurement from=1 to=$NumberOfMeasurements do={
    :local CPULoad [/system resource get cpu-load];
    :set Load ($Load + $CPULoad);
    :set Message ($Message  . [:tostr $CPULoad]  . "% ");
    :delay 5s;
}
:local AverageCPULoad ($Load / $NumberOfMeasurements);
:log info "Average CPU load = $AverageCPULoad%";

if ($AverageCPULoad > $DeviceAverageLoadThreshold) do={
    
    :set Message "CPU $CPUModel utilization exceeded $DeviceAverageLoadThreshold% threshold. Result of $NumberOfMeasurements CPU load measurements: $Message";

     # START SEND EMAIL
    :local SendTo "notify@mhelp.pro";
    :local Subject "\F0\9F\A5\B5 CPU Average Overload $AverageCPULoad%: $DeviceName [$Date $Time]";
    :local MessageText "$Message";
    :local FileName "";
    :local SendEmail [:parse [/system script get SendEmailFunction source]];
    $SendEmail SendTo=$SendTo TextMail=$MessageText Subject=$Subject FileName=$FileName;
    # END SEND EMAIL

    # START SEND TELEGRAM
    :local MessageText "\F0\9F\A5\B5 <b>$DeviceName: CPU Average Overload $AverageCPULoad%.</b> $Message";
    :local SendTelegramMessage [:parse [/system script  get MyTGBotSendMessage source]];
    $SendTelegramMessage MessageText=$MessageText;
    # END SEND TELEGRAM
}
MikroTik Script: Device processor overload notification

Add script to scheduler

To run the script, you need permission: read, write, test, policy.

In the task script, in the ScriptName variable, specify the name of the script, in the example CPUOverloadCheck. This is necessary to check an already running script.

For normal cases, the check interval is 5 minutes, but you can reduce the check interval to better diagnose the device.

Create task:

[System] -> [Schedule] -> [+] -> [Name: CPUOverloadCheck] —>  [Interval: 00:05:00] -> [Policy: read, write, policy, test]

Script code:

:local ScriptName "CPUOverloadCheck";
:local ScriptRunning [system script job find where script=$ScriptName];
:if ($ScriptRunning) do={
    :log info "The script $ScriptName cannot be run, the script is already running (Change the NumberOfMeasurements to reduce the script)";
} else={
    /system script run $ScriptName;
}
Add script Device processor overload notification to scheduler

? Advice: If you are using a lot of scripts, to reduce the simultaneous load, add an offset to the Start Time parameter (see screenshot).


? How to create a script – checking the processor load of a MikroTik device and sending an email notification or Telegram message has been discussed in this article. Now you can increase control over MikroTik devices and the CPU overload of the device will not occur unexpectedly. However, if you run into any problems while configuring the script, feel free to write in the comments. I will try to help.

✅ The script is checked: hAP ac lite [RouterBOARD 952Ui-5ac2nD], RouterOS 6.47.8 (stable).

1 thought on “MikroTik Script: Device processor overload notification”

Leave a Comment