A script to calculate Average CPU Load

Solved it, my stupidity…
Also modified it to accommodate mikrotik bugs.
The following modification monitors router for 20 seconds.
If in 20 seconds the router’s cpu usage stays at 100%, it will reboot it.

In scheduler create job that starts on start-up and runs at one second intervals…

Add code:

# Name : Average CPU Load
# Set up the scheduler to run this at a 1 second intervals (Sample Rate)
# Set info logs to echo to Terminal in System Logging

:local cpuload 0
:global cpuarray
:local arraylen 0
:local arraypos 0
:local arraytot 0
:global avgcpuload 0
:global highavgcpuload

# arraysize is the number of cpu-Load samples to keep
# Experiment with this value to incease or decrease the number of samples
# The greater the value the longer the time that the cpu-load average is calculated for.
:local arraysize 20

# Get cpu-load samples, limit cpuarray to array size
:set cpuload [/system resource get cpu-load]
:set cpuarray ( [:toarray $cpuload] + $cpuarray )
:set cpuarray [:pick $cpuarray 0 $arraysize]

# add up all values in array
:set arraypos 0
:set arraylen [:len $cpuarray]
:while ($arraypos < $arraylen) do={
:set arraytot ($arraytot + [:pick $cpuarray $arraypos] );
:set arraypos ($arraypos +1)}

# divide sum of array values by the number of values in cpuarray
:set avgcpuload ($arraytot / [:len $cpuarray])
:if ([:len $highavgcpuload] = 0) do={:set highavgcpuload $avgcpuload}
:if ([$highavgcpuload] < [$avgcpuload]) do={:set highavgcpuload $avgcpuload}

# Display results in Terminal window
:log info ("CPU Load Captures:")
:log info $cpuarray
:log info ("Array Total: $arraytot")
:log info ("Array size: $arraysize")
:log info ("CPU Load - Avg: $avgcpuload High: $highavgcpuload")

:if ($avgcpuload >= 100)  do={ /system reboot; }