Community discussions

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

Critique please

Wed Nov 23, 2022 1:13 pm

I'm totally brand new to MT scripting (and not a programmer).

I found a great script to test for changes in the status of a route but needed to make a couple of changes:

1) Check for changes in multiple routes -- so I made the variables unique for each route by add 3 numbers that have significance to me to the ends of the var names
2) Change TRUE and FALSE to UP and DOWN

Here's what I came up with (after hiking the learning curve):
{
:global prevstatus355;
:global updown355;
:global status355 [:ip route get value-name=active [:ip route find comment="355"]]
:global true true
:global false false

:log info $status355;
:log info $prevstatus355;
:log info $true
:log info $false

:if ( "$status355" != "$prevstatus355" ) do={ 

:if ( "$status355" = $true ) do={

:log info "works"; 
:set $updown355 UP;
log info $status355;

}

:log warn "355 connectivity changed status from \"$prevstatus355\" -> \"$status355\" (true=up, false=down, nothing=unintialized)";
:tool e-mail send to=xxxxxxxx subject="355 Connectivity now \"$updown355\"" body=( [ :system clock get date ] . " " . [ :system clock get time ] . " 355 connectivity changed status from \"$prevstatus355\" -> \"$updown355\" (true=up, false=down, nothing=unintialized)" )
}
:set $prevstatus $updownstatus
:global prevstatus355 $status355


}

How bad is this?

Also, will this check for connectivity (that's really what I was looking for)?

Thanks!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Critique please

Wed Nov 23, 2022 2:36 pm

:global true true
:global false false

Enough, I'll stop here.
I thought I'd seen them all...
 
Josephny
Member
Member
Topic Author
Posts: 434
Joined: Tue Sep 20, 2022 12:11 am

Re: Critique please

Wed Nov 23, 2022 2:39 pm

You're welcome (;-)!

I was trying to figure out what my 'if' statements comparing variable contents weren't working.

But, please go on...
Last edited by BartoszP on Fri Nov 25, 2022 12:34 am, edited 1 time in total.
Reason: removed excessive quotting of preceding post; be wise, quote smart.
 
Josephny
Member
Member
Topic Author
Posts: 434
Joined: Tue Sep 20, 2022 12:11 am

Re: Critique please

Wed Nov 23, 2022 9:47 pm

This section (a nested if) breaks the scripts and I'm having no luck figuring out how to fix it:
:

if ( "$status355" = $true ) do={

:log info "works"; 
:set $updown355 UP;
log info $status355;

}
I just wanted to the email notification to read "Up" instead of "true"
 
Josephny
Member
Member
Topic Author
Posts: 434
Joined: Tue Sep 20, 2022 12:11 am

Re: Critique please

Thu Nov 24, 2022 1:02 pm

Okay, I got it working -- Fun learning this stuff!

{
:global prevstatus355;
:global updown355;
:global status355 [:ip route get value-name=active [:ip route find comment="355"]]

:log info ("status355 is $status355");
:log info ("prevstatus355 is $prevstatus355");

:if ( "$status355" = true ) do={:set updown355 UP} else= {:set updown355 DOWN}

:log info ("updown355 is $updown355");

:if ( "$status355" != "$prevstatus355" ) do={ 

:log warn "355 connectivity is now \"$updown355\" ";
:tool e-mail send to=xx@xx.com subject="355 Connectivity now \"$updown355\"" body=( [ :system clock get date ] . " " . [ :system clock get time ] . " 355 connectivity changed status from \"$prevstatus355\" -> \"$updown355\" " )

:set prevstatus355 $status355

}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Critique please

Thu Nov 24, 2022 7:52 pm

Okay, I got it working -- Fun learning this stuff!

Learn from this.
:global prevstatus355
{
    /ip route
    :local status355 [get [find where comment="355"] active]
    :if ($status355) do={:set status355 "UP"} else={:set status355 "DOWN"}
    :log info "status355 is $status355 and prevstatus355 is $prevstatus355"
    :if ($status355 != $prevstatus355) do={ 
        :log warning "355 connectivity is now $status355"
        /tool e-mail send to=email@example.com subject="355 Connectivity is now $status355" \
                     body="$[/system clock get date] $[/system clock get time] 355 connectivity changed status $prevstatus355 -> $status355"
        :set prevstatus355 $status355
    }
}

One of the assumptions needed to not block this script is: There must be for sure one and only one route with that comment.

Who is online

Users browsing this forum: No registered users and 16 guests