Page 1 of 1

Script to monitor temperature

Posted: Wed Jan 10, 2018 1:44 pm
by simeitg
Hi! I have used a lot this forum to solve a bunch of problems, and today i would like to contribute a little.
Here is a small script to monitore the temperature of a CCR 1036, and send a email if its ok, or send a sms if its above the limit.
Here in brazil temperatures can rise very quick in the summer, so i want to know if the A/C its ok!
After the script just make a scheduler to run every 30min.
________________________________________________________________________________
:global tcasinha [/system health get temperature]
:local emessage ("Temperature is " . [:pick $tcasinha 0 2] . "." . [:pick $tcasinha 2 3])
:if ($tcasinha >= 40) do={
:log info $tcasinha
/tool sms send usb3 "yourcellphonehere" message=$emessage
} else={
/tool e-mail send to="youremailhere" subject="TEMP_CASINHA" body=$emessage start-tls=yes
}
________________________________________________________________________________

Re: Script to monitor temperature

Posted: Sun Jan 14, 2018 6:02 pm
by frank333
Here in brazil temperatures can rise very quick in the summer, so i want to know if the A/C its ok!
works wonderfully!!
typical Brazilian temperature 40C° :-P :-P
Obrigado!

Re: Script to monitor temperature

Posted: Fri Feb 02, 2018 2:50 am
by plisken
Working like a charm thanks :)

Re: Script to monitor temperature

Posted: Fri Feb 02, 2018 5:55 am
by nichky
nice :)

Thanks

Re: Script to monitor temperature

Posted: Wed Feb 07, 2018 9:42 am
by nikpefan
In a situation where you have other schedulers on your router, how will the scheduler know which script to run??

Re: Script to monitor temperature

Posted: Sat Mar 03, 2018 6:18 pm
by aacable
Seems Good.
I would like to provide some feedback on the script.

Let's say what if we schedule it to run every 5 minutes? It will run, & do following
- Check temperature, compare it with threshold value , if condition true, LOG & Send SMS Alert.

Now after 5 minutes , the script executes again, & do the following ...
- Check temperature, compare it with threshold value , if condition true, LOG & Send SMS Alert AGAIN.

& repeats actions after every 5 minutes. What if the temperature remains above threshold value all day long & no one available at support to rectify that? You will receive annoying messages many many times or specific action many times.

To prevent this, what you can do is to add IF statement . like IF the Threshold value reaches, send SMS, and then create an empty file name sms_sent.txt
Now you should add a check option before sending sms that if "sms_sent.txt" file is available, then Don't sms send, because sms already sent , just log warning in LOG window. once temperature goes ok, then just delete sms_sent.txt file, there are other tuning you might want to do like once temperate comes ok, then send OK sms once only by adding second check as well.

Re: Script to monitor temperature

Posted: Fri Sep 06, 2019 3:58 pm
by theycallmetimmy
I know this post is well over a year old, but since no one has responded to it I thought I would.
This is a reply to aacable.

These are a few sample scripts I created that looks for the system temperature on a MikroTik, logs the current temp, and sends an alert at different temperature checkpoints. It also resets itself so it only sends one alert per temperature checkpoint until it is finally back within spec.
The first script runs on a RB3011 and is a direct export from the MikroTik running WinBox 6.45.3. The script only reads the system temperature, not the CPU temperature.

Code: Select all

/system script
add dont-require-permissions=no name=Check-system-Temp owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive source=":global\
\_\"tempstatus\"\r\
\n:global \"templaststatus\"\r\
\n:global \"systemtemp\" [/system health get temperature]\r\
\n:if (systemtemp > \"50\") do={:set \"tempstatus\" \"system temp is too high\"}\
\r\
\n:if (systemtemp > \"60\") do={:set \"tempstatus\" \"system temp is critical\"}\
\r\
\n:if (systemtemp < \"40\") do={:set \"tempstatus\" \"system temp is within spec\
\"}\r\
\n:if (\$\"tempstatus\" != \$\"templaststatus\") do {\r\
\n/tool e-mail send to=\"your-email-here@email.com\" subject=(\"This router \
\$[/system identity get name] has a temperature alert. \$[/system clock ge\
t date] \$tempstatus\") body=(\"This router \$[/system identity get name] \
has a temperature alert. \$[/system clock get date] \$tempstatus - \$syste\
mtemp Celcius\")\r\
\n:log info \"Email sent about system \$tempstatus status\"\r\
\n:set \"templaststatus\" \$\"tempstatus\"\r\
\n}"
Here is the same script but for "us simple folk", it does not include the permissions policies or script name.

Code: Select all

:global "tempstatus"
:global "templaststatus"
:global "systemtemp" [/system health get temperature]
:if (systemtemp > "50") do={:set "tempstatus" "system temp is too high"}
:if (systemtemp > "60") do={:set "tempstatus" "system temp is critical"}
:if (systemtemp < "40") do={:set "tempstatus" "system temp is within spec"}
:if ($"tempstatus" != $"templaststatus") do {
/tool e-mail send to="your-email-here@email.com" subject=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $tempstatus") body=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $tempstatus - $systemtemp Celcius")
:log info "Email sent about system $tempstatus status"
:set "templaststatus" $"tempstatus"
}
Here is the scheduler to run the script every 10 minutes to check for changes. This is a direct export from the MikroTik.

Code: Select all

/system scheduler
add interval=10m name="Check system Temp" on-event=Check-system-Temp policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive start-time=\
startup
This below script checks the system temp and cpu temp, logs the data and emails you alerts at each temperature checkpoint. This was run on a CCR1036 running WinBox 6.42.6.

Code: Select all

:global "tempstatus"
:global "templaststatus"
:global "cputempstatus"
:global "cputemplaststatus"
:global "systemtemp" [/system health get temperature]
:global "cputemp" [/system health get cpu-temperature]
:if (systemtemp > "50") do={:set "tempstatus" "system temp is too high"}
:if (systemtemp > "60") do={:set "tempstatus" "system temp is critical"}
:if (systemtemp < "40") do={:set "tempstatus" "system temp is within spec"}
:if (cputemp > "65") do={:set "cputempstatus" "cpu temp is too high"}
:if (cputemp > "75") do={:set "cputempstatus" "cpu temp is critical"}
:if (cputemp < "64") do={:set "cputempstatus" "cpu temp is within spec"}
:if ($"tempstatus" != $"templaststatus") do {
/tool e-mail send to="your-email-here@email.com" subject=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $tempstatus") body=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $tempstatus - $systemtemp Celcius")
:log info "Email sent about system $tempstatus status"
:set "templaststatus" $"tempstatus"
}
:if ($"cputempstatus" != $"cputemplaststatus") do {
/tool e-mail send to="your-email-here@email.com" subject=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $cputempstatus") body=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $cputempstatus - $cputemp Celcius")
:log info "Email sent about system $cputempstatus status"
:set "cputemplaststatus" $"cputempstatus"
}

You could build the same scheduler for the system temp and cpu temp script and have it run every 10 minutes as well.
I have altered the script so many times from the original one I found online that I forgot where it came from. Obviously the sample script I found online was a lot simpler than mine and was for something else entirely.

Re: Script to monitor temperature

Posted: Mon Sep 09, 2019 8:05 am
by Jotne
I know this post is well over a year old, but since no one has responded to it I thought I would.
Please edit your post.
Select code block and click the </> button above the text field to code tag scripts.
like this
:global "systemtemp" [/system health get temperature]
:global "cputemp" [/system health get cpu-temperature]
Scipt should also test if these two gives a value in return since hAP lite does not give none, hex gives temperature but not cpu-temperature, so they will fail.

Why use global variable? her could local variable be used.

Re: Script to monitor temperature

Posted: Mon Sep 09, 2019 2:31 pm
by theycallmetimmy
Thanks for the tip on the code tags Jotne, I have edited my original post.

As for using :global instead of :local, there wasn't a reason besides being able to see the script output on the script>Environment tab. That way you can check your work to make sure it's outputting the correct data. Also since I'm using global, I can use the temperature readings for other scripts if I needed to.

My script doesn't check for returned values because I wrote it for the RB3011 and CCR1036. Anyone can alter it for whatever MikroTik they choose to use. I wrote two sample scripts, one to check for system temperature and one to check for system&cpu temperature.

I know this post is well over a year old, but since no one has responded to it I thought I would.
Please edit your post.
Select code block and click the </> button above the text field to code tag scripts.
like this
:global "systemtemp" [/system health get temperature]
:global "cputemp" [/system health get cpu-temperature]
Scipt should also test if these two gives a value in return since hAP lite does not give none, hex gives temperature but not cpu-temperature, so they will fail.

Why use global variable? her could local variable be used.

Re: Script to monitor temperature

Posted: Mon Sep 09, 2019 3:42 pm
by Jotne
As for using :global instead of :local, there wasn't a reason besides being able to see the script output on the script>Environment tab.
I do always run the script from command line using :put to see whats going on like this:
{
:local systemtemp [/system health get temperature]
:put $systemtemp 
}

PS Do not quote the complete post above you. To reply on a post click "Post Reply" below the post.

Re: Script to monitor temperature

Posted: Mon Sep 09, 2019 3:54 pm
by theycallmetimmy
Okay. Like I said, you can configure the script however you want for whatever MikroTik you use.

Re: Script to monitor temperature

Posted: Wed Sep 11, 2019 9:46 am
by aacable
I know this post is well over a year old, but since no one has responded to it I thought I would.
This is a reply to aacable.

These are a few sample scripts I created that looks for the system temperature on a MikroTik, logs the current temp, and sends an alert at different temperature checkpoints. It also resets itself so it only sends one alert per temperature checkpoint until it is finally back within spec.
The first script runs on a RB3011 and is a direct export from the MikroTik running WinBox 6.45.3. The script only reads the system temperature, not the CPU temperature.

Code: Select all

/system script
add dont-require-permissions=no name=Check-system-Temp owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive source=":global\
\_\"tempstatus\"\r\
\n:global \"templaststatus\"\r\
\n:global \"systemtemp\" [/system health get temperature]\r\
\n:if (systemtemp > \"50\") do={:set \"tempstatus\" \"system temp is too high\"}\
\r\
\n:if (systemtemp > \"60\") do={:set \"tempstatus\" \"system temp is critical\"}\
\r\
\n:if (systemtemp < \"40\") do={:set \"tempstatus\" \"system temp is within spec\
\"}\r\
\n:if (\$\"tempstatus\" != \$\"templaststatus\") do {\r\
\n/tool e-mail send to=\"your-email-here@email.com\" subject=(\"This router \
\$[/system identity get name] has a temperature alert. \$[/system clock ge\
t date] \$tempstatus\") body=(\"This router \$[/system identity get name] \
has a temperature alert. \$[/system clock get date] \$tempstatus - \$syste\
mtemp Celcius\")\r\
\n:log info \"Email sent about system \$tempstatus status\"\r\
\n:set \"templaststatus\" \$\"tempstatus\"\r\
\n}"
Here is the same script but for "us simple folk", it does not include the permissions policies or script name.

Code: Select all

:global "tempstatus"
:global "templaststatus"
:global "systemtemp" [/system health get temperature]
:if (systemtemp > "50") do={:set "tempstatus" "system temp is too high"}
:if (systemtemp > "60") do={:set "tempstatus" "system temp is critical"}
:if (systemtemp < "40") do={:set "tempstatus" "system temp is within spec"}
:if ($"tempstatus" != $"templaststatus") do {
/tool e-mail send to="your-email-here@email.com" subject=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $tempstatus") body=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $tempstatus - $systemtemp Celcius")
:log info "Email sent about system $tempstatus status"
:set "templaststatus" $"tempstatus"
}
Here is the scheduler to run the script every 10 minutes to check for changes. This is a direct export from the MikroTik.

Code: Select all

/system scheduler
add interval=10m name="Check system Temp" on-event=Check-system-Temp policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive start-time=\
startup
This below script checks the system temp and cpu temp, logs the data and emails you alerts at each temperature checkpoint. This was run on a CCR1036 running WinBox 6.42.6.

Code: Select all

:global "tempstatus"
:global "templaststatus"
:global "cputempstatus"
:global "cputemplaststatus"
:global "systemtemp" [/system health get temperature]
:global "cputemp" [/system health get cpu-temperature]
:if (systemtemp > "50") do={:set "tempstatus" "system temp is too high"}
:if (systemtemp > "60") do={:set "tempstatus" "system temp is critical"}
:if (systemtemp < "40") do={:set "tempstatus" "system temp is within spec"}
:if (cputemp > "65") do={:set "cputempstatus" "cpu temp is too high"}
:if (cputemp > "75") do={:set "cputempstatus" "cpu temp is critical"}
:if (cputemp < "64") do={:set "cputempstatus" "cpu temp is within spec"}
:if ($"tempstatus" != $"templaststatus") do {
/tool e-mail send to="your-email-here@email.com" subject=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $tempstatus") body=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $tempstatus - $systemtemp Celcius")
:log info "Email sent about system $tempstatus status"
:set "templaststatus" $"tempstatus"
}
:if ($"cputempstatus" != $"cputemplaststatus") do {
/tool e-mail send to="your-email-here@email.com" subject=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $cputempstatus") body=("This router $[/system identity get name] has a temperature alert. $[/system clock get date] $cputempstatus - $cputemp Celcius")
:log info "Email sent about system $cputempstatus status"
:set "cputemplaststatus" $"cputempstatus"
}

You could build the same scheduler for the system temp and cpu temp script and have it run every 10 minutes as well.
I have altered the script so many times from the original one I found online that I forgot where it came from. Obviously the sample script I found online was a lot simpler than mine and was for something else entirely.
Nice One mate !