Community discussions

MikroTik App
 
Josephny
Member
Member
Topic Author
Posts: 495
Joined: Tue Sep 20, 2022 12:11 am

Netwatch down script can't email

Fri Apr 12, 2024 12:39 pm

I use the script below and I notice that when Netwatch runs it's "Down" script and tries to send the notification email, it fails due to a "timeout." I understand that it can't send an email if the connection is down, but I'm wondering what the solution is for getting notified (via email) that a there was a Netwatch down condition at xx:xx:xx time?

Note: If the status goes down and back up quickly (within a couple of seconds, I'm guessing), the script works fine (i.e., the email is sent). So, it is only those times when the connection is down and stays down while the script is trying to send the email that this problem occurs.

DOWN:

/system
:local cdate [clock get date] 
:local yyyy  [:pick $cdate 0  4]
:local MM    [:pick $cdate 5  7]
:local dd    [:pick $cdate 8 10]
:local identitydate "$[identity get name]_$yyyy-$MM-$dd"
:local identity "$[identity get name]"
/tool netwatch
:local Host $host
:local Status [get [find where host="$Host"] status]
:local Interval [get [find where host="$Host"] interval]

:log info "script=netwatch watch_host=$Host comment=\"$Comment\" status=$Status interval=$Interval"

:tool e-mail send to=joseph@xxxx.com subject="$identity $Status" body=( "$Status $Host $identitydate" )
 
abbio90
Member Candidate
Member Candidate
Posts: 234
Joined: Fri Aug 27, 2021 9:16 pm
Contact:

Re: Netwatch down script can't email

Fri Apr 12, 2024 1:11 pm

if there is no internet the email cannot be sent. The solution would be a backup connection
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12030
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Netwatch down script can't email

Fri Apr 12, 2024 5:30 pm

Send email on status up instead of on status down.
When go down memorize the datetime in a global variable,
when up send mail with the global variable value
 
Josephny
Member
Member
Topic Author
Posts: 495
Joined: Tue Sep 20, 2022 12:11 am

Re: Netwatch down script can't email

Sat Apr 13, 2024 12:06 am

Send email on status up instead of on status down.
When go down memorize the datetime in a global variable,
when up send mail with the global variable value
Fantastic solution!

Will attempt to create.

Thank you.
 
Josephny
Member
Member
Topic Author
Posts: 495
Joined: Tue Sep 20, 2022 12:11 am

Re: Netwatch down script can't email

Tue Apr 16, 2024 2:01 pm

I'm batting around .500 (my usual), which in baseball is fantastic, but in programming, not so much....

Here's my down script:

/system
:local cdate [clock get date] 
:local yyyy  [:pick $cdate 0  4]
:local MM    [:pick $cdate 5  7]
:local dd    [:pick $cdate 8 10]
:local identitydate "$[identity get name]_$yyyy-$MM-$dd"
:local identity "$[identity get name]"
/tool netwatch
:local Host $host
:local Status [get [find where host="$Host"] status]
:local Interval [get [find where host="$Host"] interval]

:log info "script=netwatch watch_host=$Host comment=\"$Comment\" status=$Status interval=$Interval"

:tool e-mail send to=joseph@xxxx.com subject="$identity $Status" body=( "$Status $Host $identitydate" )

:global globalstoreddown "$identitydate_Down_$Status"


Here's my up script:

/system
:local cdate [clock get date] 
:local yyyy  [:pick $cdate 0  4]
:local MM    [:pick $cdate 5  7]
:local dd    [:pick $cdate 8 10]
:local identitydate "$[identity get name]_$yyyy-$MM-$dd"
:local identity "$[identity get name]"
/tool netwatch
:local Host $host
:local Status [get [find where host="$Host"] status]
:local Interval [get [find where host="$Host"] interval]

:log info "script=netwatch watch_host=$Host comment=\"$Comment\" status=$Status interval=$Interval"

:if ([:len [$globalstoreddown]] > 1) do={
:tool e-mail send to=joseph@xxxx.com subject="$identity Previously Netwatch Down" body=( "$globalstoreddown" )
} 

:tool e-mail send to=joseph@xxxx.com subject="$identity $Status" body=( "$Status $Host $identitydate" )

I confirmed that globalstoreddown gets created by the down script with

:log info $globalstoreddown

But the up script does not send the email in the line:
:if([:len [$globalstoreddown] > 1)
Also, the idea was that an email would be send that includes not just the date but the time it went down (and the time it went up).

Thanks!

Who is online

Users browsing this forum: daldana2940 and 4 guests