Community discussions

MikroTik App
 
User avatar
anav
Forum Guru
Forum Guru
Topic Author
Posts: 19103
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

SCRIPT Works in System Script but no in NETWATCH???

Mon Mar 22, 2021 12:20 am

This script works well for me when used in the SYSTEM SCRIPT location to send me telegram notices if the temperatures on the Router go out of whack. In particular I am interested in using the /tool fetch portions of the script to similarly report on my netwatch devices status on telegram:

A. successful temperature script:
:global "tempstatus"
:global "templaststatus"
:global "cputempstatus"
:global "cputemplaststatus"
:global "systemtemp" [/system health get temperature]
:global "cputemp" [/system health get cpu-temperature]
:if (systemtemp > "45") do={:set "tempstatus" "system temp is too high"}
:if (systemtemp > "60") do={:set "tempstatus" "system temp is critical"}
:if (systemtemp < "45") 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 fetch "https://api.telegram.org/bot1111111111:rAndomStrInGof lettersandnumbers/sendMessage?chat_id=-222222222&text=Router $[/system identity get name] has a temperature notification. $[/system clock get date] $[/system clock get time] -> $tempstatus - $systemtemp* Celsius)"
:log info "Telegram notification sent about system $tempstatus status"
:set "templaststatus" $"tempstatus"
}
:if ($"cputempstatus" != $"cputemplaststatus") do {
/ tool fetch "https://api.telegram.org/bot1111111111:rAndomStrInGof lettersandnumbers/sendMessage?chat_id=-222222222&text=Router $[/system identity get name] has a temperature notification. $[/system clock get date] $[/system clock get time] -> $cputempstatus - $cputemp* Celsius)"
:log info "Telegram notification sent about system $cputempstatus status"
:set "cputemplaststatus" $"cputempstatus"
}
/file remove number=[find where name~"Celsius"]
Currently for Netwatch, I am successfully sending emails for notifications. Now I also want Netwatch as per temp settings script above, to send me a TELEGRAM message.
This is what works for email quite nicely in netwatch for the DOWN condition, very simple and works!
B. successful email script in netwatch
:local sub1 ([/system clock get time])
/tool e-mail send from="myemail@address.com.ca" body="At $sub1 HP Printer is Offline" subject="HP Printer is offline" to="myemail@address.com" 
:log info "CHECK HP printer stat!!"
I thought I could simply add the relevant part of the health/temperature script to also send me a telegram message as per the above example.
SO I tried TWO iterations and both FAILED. :-( BASICALLY ONE WITH AND ONE WITHOUT closing { and } brackets.

So after the last line of the email part of the script above B., I added:
(1) With brackets
{
/ tool fetch "https://api.telegram.org/bot1111111111:rAndomStrInGof lettersandnumbers/sendMessage?chat_id
=-222222222&text=Router $[/system identity get name] has detected the HP Printer Offline"
:log info "Telegram notification sent about HP Printer offline"
}
and
(2) Without brackets
/ tool fetch "https://api.telegram.org/bot1111111111:t rAndomStrInGof lettersandnumbers/sendMessage?chat_id
=-222222222&text=Router $[/system identity get name] has detected the HP Printer Offline"
:log info "Telegram notification sent about HP Printer offline"

Not only did I not get telegram messages but both FAILED to produce a router log entry about the Telegram Notification, which worked just fine for health/temp script.

PLEASE ADVISE!
 
2frogs
Forum Veteran
Forum Veteran
Posts: 713
Joined: Fri Dec 03, 2010 1:38 am

Re: SCRIPT Works in System Script but no in NETWATCH???

Mon Mar 22, 2021 1:51 pm

Netwatch (also DHCP, PPP, etc.) lacks permissions to use global variables.

You can create a script where permissions are not required and then call on that script.
 
User avatar
anav
Forum Guru
Forum Guru
Topic Author
Posts: 19103
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: SCRIPT Works in System Script but no in NETWATCH???

Mon Mar 22, 2021 3:58 pm

Netwatch (also DHCP, PPP, etc.) lacks permissions to use global variables.

You can create a script where permissions are not required and then call on that script.
2Frogs, luv you for responding. Others read but no response, should get a life LOL.
Okay just to clarify I do not use any of the global settings in netwatch
I ONLY TAKE THE /TOOL FETCH part of the script for my netwatch portion!!!!

Are you saying that Router $[/system identity get name] is more like global type of command that would not be recognized in Netwatch??

Are you saying that FETCH does not work in Netwatch???

Finally how can I get this to work?????????
Do you mean I can from netwatch call a script that is sitting in my SERVER SCRIPT config?? If so HOW??
 
2frogs
Forum Veteran
Forum Veteran
Posts: 713
Joined: Fri Dec 03, 2010 1:38 am

Re: SCRIPT Works in System Script but no in NETWATCH???

Mon Mar 22, 2021 4:17 pm

Fetch may also needs permissions not available to Netwatch. These changes were made in response to the 2019 security issues, I believe.
 
2frogs
Forum Veteran
Forum Veteran
Posts: 713
Joined: Fri Dec 03, 2010 1:38 am

Re: SCRIPT Works in System Script but no in NETWATCH???  [SOLVED]

Mon Mar 22, 2021 4:32 pm

Correction: it was 2018. VER 6.42

https://wiki.mikrotik.com/wiki/Manual:Tools/Netwatch

There is an example in the wiki. Create the script with dont-require-permissions=yes.

Who is online

Users browsing this forum: No registered users and 23 guests