-
Notifications
You must be signed in to change notification settings - Fork 6
/
cpu-monitoring
36 lines (36 loc) · 1.4 KB
/
cpu-monitoring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
local cpuarray
local rebootIfHigh false
set cpuarray ([/system resource get value-name=cpu-load])
#Change the value below to change the number of CPU load sample to average
local cpusample 20
for i from=1 to=[$cpusample -1] do={
delay 950ms;
set cpuarray ([/system resource get value-name=cpu-load],$cpuarray)
}
put $cpuarray
local totalcpuarray 0
#calculating the average
foreach value in=$cpuarray do={ set totalcpuarray ($totalcpuarray + $value)}
:put $totalcpuarray
local avgcpu ($totalcpuarray / [:len $cpuarray])
if ($avgcpu > 65) do={
:global ConnectionAvailable
$ConnectionAvailable
if ($ConnectionAvailable=true) do={
global CHATID
global BOTID
local maxcpu ($cpuarray->0)
local mincpu ($cpuarray->0)
foreach i in=$cpuarray do={
if ($i > $maxcpu) do={ set maxcpu $i}
if ($i < $mincpu) do={ set mincpu $i}
}
local sendToTelegram ("Mikrotik ".[/system resource get value-name=board-name]."(".[/sy id get value-name=name].") :%0A\E2\9A\A0 \E2\9A\A0 <b>CPU Load Report</b>%0AMax: $maxcpu%0AMin: $mincpu%0AAverage: $avgcpu%25%0ACPU Samples: $cpusample%0AReboot if CPU sample show highload: $rebootIfHigh")
/tool fetch url="https://api.telegram.org/bot$BOTID/sendMessage\?chat_id=$CHATID&text=$sendToTelegram&parse_mode=html" keep-result=no;
} else={
log warning message="Cannot send message to telegram. is internet connected?"
}
if ($rebootIfHigh = true) do={/system reboot}
} else={
nothing
}