In this method we will define some functions which will calculate the status of our ISPs by pinging some targets which we will define them according to some important factors. Then we will take action based on output of these functions. Also to keep track of what was happened, we need a logging system and to know what is going on, we need a notification of sort.
I’m assuming that we have 2 Internet connection and are using “Mangle” to load balance between them. Also I will call ISPs via number (ISP1 and IPS2); so their “Routing Mark” will be “to_ISP1” and “to_ISP2”.
I’m going to explain my script piece by piece and at the end you can find the full script.
Log & Notification
One of the most important aspect of our work is to be notify and be able to check what happened in our network, so first of all we need to define a way to have these features.
Log
As for logging, we can use our own MikroTik logging tool. To do this, we should define a logging action and then define a logging rule:
system logging action add name="SDISP" target=disk disk-file-name="disk1/ISP" disk-lines-per-file=1000 disk-file-count=10 disk-stop-on-full=no
As you can see, I’m using a SD card which is connected directly to my MikroTik as my target but we can use any other target as long as “disk-file-name” is defined. Also you may want to change “disk-lines-per-file” and “disk-file-count” according to your design.
system logging add topics=script prefix="ISP Status" action=SDISP
Function: Notification
As for notification, we will use built-in email tool as a function as bellow. In this function, we have 2 global variables which must define outside of the function and 8 local variables which should define inside the function. Global variables are essential but local ones are used to make the script more legible and their value can be written directly to the function.
Global variables:
:global EmailDescription ""
:global EmailBody ""
“EmailDescription” is using to describe the notification which is describing what kind of action is performing.
“EmailBody” is what we want to know about the status of our ISPs. This variable contains the amount of another variable which will create in another function ($StatusOfISPs) but because of the MikroTik scripting language structure, it should be define when we are using this function otherwise it will not be set correctly!!!
Local variable for “SendEmail” function:
:local EmailServer "74.125.127.109"
:local EmailPort "587"
:local EmailTLS "yes"
:local EmailAddress "YOUR_ACCOUNT@gmail.com"
:local EmailPassword "YOUR_ACCOUNT_PASSWORD"
:local EmailTo "YOUR_EMAIL_ADDRESS"
:local EmailCC "CC_EMAIL_ADDRESS(ES)_IF_NEEDED"
:local Attach "disk1/ISP.0.txt"
As you can see, these variables are defining what “MikroTik Email Tool” is require. In this case, we are using Gmail as our email server.
“Attach” variable is used to attach the latest log file which we are creating for this scenario.
“SendEmail” function:
:global SendEmail do={
:local EmailServer "74.125.127.109"
:local EmailPort "587"
:local EmailTLS "yes"
:local EmailAddress "YOUR_ACCOUNT@gmail.com"
:local EmailPassword "YOUR_ACCOUNT_PASSWORD"
:local EmailTo "YOUR_EMAIL_ADDRESS"
:local EmailCC "CC_EMAIL_ADDRESS(ES)_IF_NEEDED"
:local Attach "disk1/ISP.0.txt"
/tool e-mail send to=$EmailTo cc=$EmailCC file=$Attach subject="$[/system identity get name] Internet Status - $EmailDescription" body="$EmailBody" from=$EmailAddress server=$EmailServer port=$EmailPort user=$EmailAddress password=$EmailPassword start-tls=$EmailTLS
}
How to use it:
$SendEmail EmailDescription="We're testing the function" EmailBody=”Hello from MikroTik”;
Function: Calculating status of ISP
To define the status of an ISP, we should be defining 3 hosts to check our internet with them via ping. These hosts which we will call them “Ping Targets”, must meet some conditions that I will explain it later in a different section in detail. Before pinging these hosts, we will ping our gateway too. The results will be put in an array to be kept.
In this function, we will define 3 global variables that must define before the function, like what we had in “SendEmail” function which should be define when we want to use the function. Also there is 8 local variables in this function.
Global variables:
:global Interface ""
:global ISPName ""
:global GatewayISP ""
“Interface” is determining which interface should be used to check our ping targets.
“ISPName” is using to define which “Routing Table” should be used. Because I’m assuming that you define a meaningful name, we will use this variable for logging purposes too.
The reason for using both “Interface” and “ISPName” together is that our action against loosing connection through each ISP is to disable its interface (Will explain later); so if we didn’t define both of them, our results will not be accurate.
As for “GatewayISP”, it should contain the IP address of our ISP.
Local variable for “ISPStatus” function:
:local PingTarget1 [:resolve "HOST1"]
:local PingTarget2 [:resolve "HOST2"]
:local PingTarget3 [:resolve "HOST3"]
:local PingGW [ping $GatewayISP count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult1 [ping $PingTarget1 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult2 [ping $PingTarget2 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult3 [ping $PingTarget3 count=1 routing-table="to_$ISPName" interface=$Interface]
:local StatusOfISP
The first 3 variables are used to resolve our ping targets hostname to their IPs. Next one is to ping our gateway. The next 3 variables are using to ping our ping targets. The last one is using to keep the results within the function, so at the end we “put” it as the output of the function.
“ISPStatus” function:
:global ISPStatus do={
:local PingTarget1 [:resolve "HOST1"]
:local PingTarget2 [:resolve "HOST2"]
:local PingTarget3 [:resolve "HOST3"]
:local PingGW [ping $GatewayISP count=1 routing-table="to_$ISPName" interface=$Interface]
:delay delay-time=1;
:local PingResult1 [ping $PingTarget1 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult2 [ping $PingTarget2 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult3 [ping $PingTarget3 count=1 routing-table="to_$ISPName" interface=$Interface]
:local StatusOfISP
:if ($PingGW = 0) do={
:log error "$ISPName gateway is not reachable"
:set $StatusOfISP "000";
} else={
:set $StatusOfISP "$PingResult1$PingResult2$PingResult3"
}
:log warning "$ISPName is $StatusOfISP";
:put $StatusOfISP;
}
As you can see, we will check the ping result of our gateway first, then ping targets. By understanding the whole function, now you may want to write it like this to speed it up a little bit:
:global ISPStatus do={
:local PingGW [ping $GatewayISP count=1 routing-table="to_$ISPName" interface=$Interface]
:local StatusOfISP
:delay delay-time=1;
:if ($PingGW = 0) do={
:log error "$ISPName gateway is not reachable"
:set $StatusOfISP "000";
} else={
:local PingTarget1 [:resolve "google.com"]
:local PingTarget2 [:resolve "varzesh3.com"]
:local PingTarget3 [:resolve "example.com"]
:local PingResult1 [ping $PingTarget1 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult2 [ping $PingTarget2 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult3 [ping $PingTarget3 count=1 routing-table="to_$ISPName" interface=$Interface]
:set $StatusOfISP "$PingResult1$PingResult2$PingResult3"
}
:log warning "$ISPName is $StatusOfISP";
:put $StatusOfISP;
}
In almost all cases PingGW will be 1 because of all SLA thing; so there is no different between this two handwriting.
How to use it:
:global StatusOfISP1 [$ISPStatus Interface=ether1 GatewayISP=xx.xx.xx.xx ISPName=ISP1]
:global StatusOfISP2 [$ISPStatus Interface=ether2 GatewayISP=yy.yy.yy.yy ISPName=ISP2]
The output will be like this: “;nnn”
“n” is 0 or 1.
Function: Disabling ISP
In some cases, which we will discuss them later, we will force to disable ISP interface to maintain our connectivity to the Internet. But before we do so, we need to be sure that our Internet is really down and it’s not temporary. To reach this goal, we are going to triple check our ping results before disabling the interface.
In this function, we need to use “ISPStatus” function but because of MikroTik scripting language structure, we couldn’t just call a function in another function and we have to define it again within this function too, but locally (Because all of these function will be defined at the top of our script). To be able to decide based on the result of each recheck, we are going to put each step result in a “Check#” local variable; so there will be 3 local variables. Also there is another local variable to keep the result within the function, so at the end we “put” it as the output of the function.
Beside local variables, we need one global variable called “StatusCheck” which is using to inject the value of “StatusOfISP1” or “StatusOfISP2” -based on which ISP is going to recheck- to the function; this variable will be used in all 3 check steps as reference.
Now because we are going to use “ISPStatus” function, we should define “Interface”, “GatewayISP” and “ISPName” which are requirements of this function.
Before every steps of recheck process, we will use 20 seconds delay to ensure that the ping results are not temporary.
Let’s take a look at the function:
:global DisableISP do={
#ISP Status Function:
:local ISPStatus do={
:local PingTarget1 [:resolve "google.com"]
:local PingTarget2 [:resolve "varzesh3.com"]
:local PingTarget3 [:resolve "example.com"]
:local PingGW [ping $GatewayISP count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult1 [ping $PingTarget1 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult2 [ping $PingTarget2 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult3 [ping $PingTarget3 count=1 routing-table="to_$ISPName" interface=$Interface]
:local StatusOfISP
:if ($PingGW = 0) do={
:set $StatusOfISP "000";
} else={
:set $StatusOfISP "$PingResult1$PingResult2$PingResult3"
};
:put $StatusOfISP;
};
#Now the function itselfe:
:local Disable "";
#Check 1:
:delay delay-time=20;
:local Check1 [$ISPStatus Interface=$Interface GatewayISP=$GatewayISP ISPName=$ISPName];
:if ($Check1=$StatusCheck) do={
:log warning "$ISPName Strike 1";
#Check 2:
:delay delay-time=20;
:local Check2 [$ISPStatus Interface=$Interface GatewayISP=$GatewayISP ISPName=$ISPName];
:if ($Check2=$StatusCheck) do={
:log warning "$ISPName Strike 2";
#Check 3:
:delay delay-time=20;
:local Check3 [$ISPStatus Interface=$Interface GatewayISP=$GatewayISP ISPName=$ISPName];
:if ($Check3=$StatusCheck) do={
:log warning "$ISPName Strike 3";
:set Disable 1;
} else={:set Disable 0}
} else={:set Disable 0}
} else={:set Disable 0};
:put $Disable;
}
How to use it:
:global DisableISP1 [$DisableISP Interface=ether1 GatewayISP=xx.xx.xx.xx ISPName=ISP1 StatusCheck=$StatusOfISP1]
:global DisableISP2 [$DisableISP Interface=ether2 GatewayISP=yy.yy.yy.yy ISPName=ISP2 StatusCheck=$StatusOfISP2]
The output will be like this: “;n”
If output is “;1”, it’s true and the ISP Interface should be disabled, otherwise it’s false and the results was temporary.
Ping targets determination
As I mentioned before, we are deciding about Internet connectivity based on 3 ping targets. Before we heading to script itself, we need to know what are each “HOST”s stands for.
This section is very important because every company has different needs and conditions which should be met. Also because we are taking actions automatically based on some ping results, ping targets must have defined with maximum precision.
The reason for having 3 host is obvious; one or two is not enough to reach an accurate perception and any number more than 3 will only make it more complicated than it really is.
HOST1:
This ping target must present a worldwide service which has no downtime at all such as “Google”. The reason is very simple; we need a ping target that if there is a problem in some of its servers, it could be able to reroute incoming requests to another server and never let you down.
I will use “www.google.com” as my 1st ping target.
HOST2:
This ping target must present a local service in your country which almost has no downtime. The reason for this one is that we need to know that if there is a connectivity issue over our Internet, is it affecting on all connections or only on connections which is going outside of our country.
I will use “www.varzesh3.com” as my 2nd ping target which is one of the most stable services in Iran.
HOST3:
This ping target must present a service that your employees are using every day; in other words, this can be your company service if there is one. The reason to have such ping target is that you can determine that this service is down or not because if this service goes down, your network clients will thing that there is an issue over the Internet.
Let’s use “www.example.com” as 3rd ping.
Ping results evaluation
In this part we will discuss the meaning of “StatusOfISP1” and “StatusOfISP2”. As I mentioned before, their values store as an array which is 3 bits. So there are 8 different possibilities and because there is 2 different array, we are looking at 64 possibilities at total. We will keep logging for all of these possibilities but there are other actions that we will take about some of them too.
By assuming that our both ISPs are stable, some of these possibilities are not probable to be happen and if they happen, there is nothing can be done.
Also there are some possibilities which there is no action to fix the situation like when our both ISPs are down or when internal connections are OK; when any ping target (HOSTs) are unreachable, we reach the same conclusion as well. Although in such cases, we will notify via email.
But we are here to find out when ONE of our ISPs are down, so we can disable that one and reroute all connections to another one.
Now take a look at this table. In this table we are going to use a simple color code to determine what we are going to do about situation. No color means that we don’t do anything but keep the log; Blue means that we will notify by email beside logging; Red means that we are going to disable one of our ISPs; we will keep the log and notify by email as well.


Script
In this script, we’re going to evaluate the situation and take action. In the script, we will use 3 global variables which contain status of our ISPs; “StatusOfISP1”, “StatusOfISP2” and “StatusOfISPs”:
:global StatusOfISP1 [$ISPStatus Interface=ether1 GatewayISP=xx.xx.xx.xx ISPName=ISP1]
:global StatusOfISP2 [$ISPStatus Interface=ether2 GatewayISP=yy.yy.yy.yy ISPName=ISP2]
:global StatusOfISPs ($StatusOfISP1,$StatusOfISP2)
The first two variables are described before. The last one is contain these two variables value and its output is like: ;nnn;;nnn
Now we start checking this output and react. In this script, we are using “if () do={…} else={ if() do={…} else={ … }}” structure.
There are “delay” command in several part of script. These delays are make sure that the ping result was not temporary.
Please note the commends in the script:
#Calculating Status:
:global StatusOfISP1 [$ISPStatus Interface=ether1 GatewayISP=xx.xx.xx.xx ISPName=ISP1]
:global StatusOfISP2 [$ISPStatus Interface=ether2 GatewayISP=yy.yy.yy.yy ISPName=ISP2]
:global StatusOfISPs ($StatusOfISP1,$StatusOfISP2)
#Checking calculated statuses and perform desire actions:
#Only Notify:
:if (($StatusOfISPs=";000;;000")or($StatusOfISPs=";010;;010")or($StatusOfISPs=";010;;110")or($StatusOfISPs=";100;;100")or($StatusOfISPs=";101;;101")or($StatusOfISPs=";110;;010")or($StatusOfISPs=";110;;111")or($StatusOfISPs=";111;;110")) do={
#Check:
:delay delay-time=5;
:local CheckISP1 [$ISPStatus Interface=ether1 GatewayISP=xx.xx.xx.xx ISPName=ISP1];
:local CheckISP2 [$ISPStatus Interface=ether2 GatewayISP=yy.yy.yy.yy ISPName=ISP2];
:if (($CheckISP1=$StatusOfISP1)and($CheckISP2=$StatusOfISP2)) do={
:log warning "$StatusOfISPs";
$SendEmail EmailDescription="Warning" EmailBody=$StatusOfISPs;
}
} else={
#Example is Unreachable
:if (($StatusOfISPs=";110;;110")or($StatusOfISPs=";100;;110")or($StatusOfISPs=";110;;100")) do={
#Check:
:delay delay-time=10;
:local CheckISP1 [$ISPStatus Interface=ether1 GatewayISP=xx.xx.xx.xx ISPName=ISP1];
:local CheckISP2 [$ISPStatus Interface=ether2 GatewayISP=yy.yy.yy.yy ISPName=ISP2];
:if (($CheckISP1=$StatusOfISP1)and($CheckISP2=$StatusOfISP2)) do={
:log warning "Example is Unreachable";
:log warning "$StatusOfISPs";
$SendEmail EmailDescription="Example is Unreachable" EmailBody=$StatusOfISPs;
}
} else={
#Disable ISP1:
:if (($StatusOfISPs=";000;;101")or($StatusOfISPs=";000;;110")or($StatusOfISPs=";000;;111")or($StatusOfISPs=";010;;101")or($StatusOfISPs=";010;;111")or($StatusOfISPs=";100;;101")or($StatusOfISPs=";100;;111")) do={
#Check:
:delay delay-time=5;
:local DisableISP1 [$DisableISP Interface=ether1 GatewayISP=xx.xx.xx.xx ISPName=ISP1 StatusCheck=$StatusOfISP1];
:if ($DisableISP1=";1") do={
:log error "ISP1 has been disabled";
:log error "$StatusOfISPs";
/interface ethernet disable ether1;
:delay delay-time=1;
$SendEmail EmailDescription="ISP1 has been disabled" EmailBody=$StatusOfISPs;
}
} else={
#Disable ISP2:
:if (($StatusOfISPs=";101;;000")or($StatusOfISPs=";101;;010")or($StatusOfISPs=";101;;100")or($StatusOfISPs=";110;;000")or($StatusOfISPs=";111;;000")or($StatusOfISPs=";111;;010")or($StatusOfISPs=";111;;100")) do={
#Check:
:delay delay-time=5;
:local DisableISP2 [$DisableISP Interface=ether2 GatewayISP=yy.yy.yy.yy ISPName=ISP2 StatusCheck=$StatusOfISP2];
:if ($DisableISP2=";1") do={
:log error "ISP2 has been disabled";
:log error "$StatusOfISPs";
/interface ethernet disable ether2;
:delay delay-time=1;
$SendEmail EmailDescription="ISP2 has been disabled" EmailBody=$StatusOfISPs;
}
} else={
#No Action:
:log warning "No action is required";
:log warning "$StatusOfISPs";
}
}
}
}
}
There is a little problem about this script which we should taking care of. In 2 part of this script, we’re disabling one of our interfaces which connect us to the Internet. By doing that, we can’t get result by running this script again. So we need to change something in there.
To avoid such problem, we’re going to use 2 another global variables; one for counter and another one for setting a “DO NOT RUN THE SCRIPT” value, so we can check it at the beginning of the script.
:global InitiateStatus
:global Counter
Now after disabling an interface, we’re going to set “InitiateStatus” to “0” and “Counter” to “-1” and we need to change the script to be like as bellow. Since I set the run time interval for this script to 2 minutes, I will count 8 times (16 minutes) before running the script again.
The reason for setting “Counter” to “-1” is that if the script goes for disabling an interface, it will take a little less than 2 minutes because of delay times in the script; now because we’re increasing its value at first, then checking the “if” statement, we will face its true value (which is “0”) in the right time.
#Checking that is there any disabled intefaces cuased by previous run:
:global InitiateStatus
:global Counter
:if ($InitiateStatus="0") do={
:set $Counter ($Counter+1);
:if ($Counter!=8) do={
:log warning "Initiate Status is set to 0 and script will not initiate for 16 minutes. $(2*$Counter) minute(s) past.";
} else={
:set $InitiateStatus 1;
$SendEmail EmailDescription="Initiate Status is reset to 1" EmailBody=$StatusOfISPs;
:delay delay-time=100;
/interface ethernet enable ether1;
/interface ethernet enable ether2;
}
} else={
#Calculating Status:
:global StatusOfISP1 [$ISPStatus Interface=ether1 GatewayISP=xx.xx.xx.xx ISPName=ISP1]
:global StatusOfISP2 [$ISPStatus Interface=ether2 GatewayISP=yy.yy.yy.yy ISPName=ISP2]
:global StatusOfISPs ($StatusOfISP1,$StatusOfISP2)
---{Script continues}---
#Disable ISP1:
:if (($StatusOfISPs=";000;;101")or($StatusOfISPs=";000;;110")or($StatusOfISPs=";000;;111")or($StatusOfISPs=";010;;101")or($StatusOfISPs=";010;;111")or($StatusOfISPs=";100;;101")or($StatusOfISPs=";100;;111")) do={
#Check:
:delay delay-time=5;
:local DisableISP1 [$DisableISP Interface=ether1 GatewayISP=xx.xx.xx.xx ISPName=ISP1 StatusCheck=$StatusOfISP1];
:if ($DisableISP1=";1") do={
:set $InitiateStatus 0;
:set $Counter -1;
:log error "ISP1 has been disabled";
:log error "$StatusOfISPs";
/interface ethernet disable ether1;
:delay delay-time=1;
$SendEmail EmailDescription="ISP1 has been disabled" EmailBody=$StatusOfISPs;
}
---{Same changes happened in “#Disable ISP2” section too}---
---{Script continues}---
These added lines will enable us to wait a wise time before running the script again and before running it, we will enable the interfaces, so the result will accurate.
The reason to have 100 seconds delay before enabling the interfaces is that we set “InitiateStatus” value to 1 but we’re not going to run the script right then and the next run will be in 2 minutes. Also enabling an interface might take few seconds so the interface should be enabled before running the script again. So to not loose Internet connection in these 2 minutes (because MikroTik will sends some connections through newly enabled ISP), we will wait 100 seconds.
That’s it.
Full Script
:global EmailDescription ""
:global EmailBody ""
:global Interface ""
:global ISPName ""
:global GatewayISP ""
:global StatusCheck ""
:global InitiateStatus
:global Counter
:global SendEmail do={
:local EmailServer "74.125.127.109"
:local EmailPort "587"
:local EmailTLS "yes"
:local EmailAddress "YOUR_ACCOUNT@gmail.com"
:local EmailPassword "YOUR_ACCOUNT_PASSWORD"
:local EmailTo "YOUR_EMAIL_ADDRESS"
:local EmailCC "CC_EMAIL_ADDRESS(ES)_IF_NEEDED"
:local Attach "disk1/ISP.0.txt"
/tool e-mail send to=$EmailTo cc=$EmailCC file=$Attach subject="$[/system identity get name] Internet Status - $EmailDescription" body="$EmailBody" from=$EmailAddress server=$EmailServer port=$EmailPort user=$EmailAddress password=$EmailPassword start-tls=$EmailTLS
}
:global ISPStatus do={
:local PingTarget1 [:resolve "google.com"]
:local PingTarget2 [:resolve "varzesh3.com"]
:local PingTarget3 [:resolve "example.com"]
:local PingGW [ping $GatewayISP count=1 routing-table="to_$ISPName" interface=$Interface]
:delay delay-time=1;
:local PingResult1 [ping $PingTarget1 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult2 [ping $PingTarget2 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult3 [ping $PingTarget3 count=1 routing-table="to_$ISPName" interface=$Interface]
:local StatusOfISP
:if ($PingGW = 0) do={:log error "$ISPName gateway is not reachable";:set $StatusOfISP "000"} else={:set $StatusOfISP "$PingResult1$PingResult2$PingResult3"};
:log warning "$ISPName is $StatusOfISP";
:put $StatusOfISP};
:global DisableISP do={
:local ISPStatus do={
:local PingTarget1 [:resolve "google.com"]
:local PingTarget2 [:resolve "varzesh3.com"]
:local PingTarget3 [:resolve "example.com"]
:local PingGW [ping $GatewayISP count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult1 [ping $PingTarget1 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult2 [ping $PingTarget2 count=1 routing-table="to_$ISPName" interface=$Interface]
:local PingResult3 [ping $PingTarget3 count=1 routing-table="to_$ISPName" interface=$Interface]
:local StatusOfISP
:if ($PingGW = 0) do={
:set $StatusOfISP "000";
} else={:set $StatusOfISP "$PingResult1$PingResult2$PingResult3"};
:put $StatusOfISP;};
:local Disable;
:delay delay-time=20;
:local Check1 [$ISPStatus Interface=$Interface GatewayISP=$GatewayISP ISPName=$ISPName];
:if ($Check1=$StatusCheck) do={
:log warning "$ISPName Strike 1";
:delay delay-time=20;
:local Check2 [$ISPStatus Interface=$Interface GatewayISP=$GatewayISP ISPName=$ISPName];
:if ($Check2=$StatusCheck) do={
:log warning "$ISPName Strike 2";
:delay delay-time=20;
:local Check3 [$ISPStatus Interface=$Interface GatewayISP=$GatewayISP ISPName=$ISPName];
:if ($Check3=$StatusCheck) do={
:log warning "$ISPName Strike 3";:set Disable 1;} else={:set Disable 0}} else={:set Disable 0}} else={:set Disable 0};
:put $Disable;}
:if ($InitiateStatus="0") do={
:set $Counter ($Counter+1);
:if ($Counter!=8) do={
:log warning "Initiate Status is set to 0 and script will not initiate for 16 minutes. $(2*$Counter) minute(s) past.";
} else={
:set $InitiateStatus 1;
$SendEmail EmailDescription="Initiate Status is reset to 1" EmailBody=$StatusOfISPs;
:delay delay-time=100;
/interface ethernet enable ether1;
/interface ethernet enable ether2;
}
} else={
:global StatusOfISP1 [$ISPStatus Interface=ether1 GatewayISP=xz.xz.xz.xz ISPName=ISP1]
:global StatusOfISP2 [$ISPStatus Interface=ether2 GatewayISP=yz.yz.yz.yz ISPName=ISP2]
:global StatusOfISPs ($StatusOfISP1,$StatusOfISP2)
:if (($StatusOfISPs=";000;;000")or($StatusOfISPs=";010;;010")or($StatusOfISPs=";010;;110")or($StatusOfISPs=";100;;100")or($StatusOfISPs=";101;;101")or($StatusOfISPs=";110;;010")or($StatusOfISPs=";110;;111")or($StatusOfISPs=";111;;110")) do={
:delay delay-time=5;
:local CheckISP1 [$ISPStatus Interface=ether1 GatewayISP=xz.xz.xz.xz ISPName=ISP1];
:local CheckISP2 [$ISPStatus Interface=ether2 GatewayISP=yz.yz.yz.yz ISPName=ISP2];
:if (($CheckISP1=$StatusOfISP1)and($CheckISP2=$StatusOfISP2)) do={
:log warning "$StatusOfISPs";
$SendEmail EmailDescription="Warning" EmailBody=$StatusOfISPs;
}
} else={
:if (($StatusOfISPs=";110;;110")or($StatusOfISPs=";100;;110")or($StatusOfISPs=";110;;100")) do={
:delay delay-time=10;
:local CheckISP1 [$ISPStatus Interface=ether1 GatewayISP=xz.xz.xz.xz ISPName=ISP1];
:local CheckISP2 [$ISPStatus Interface=ether2 GatewayISP=yz.yz.yz.yz ISPName=ISP2];
:if (($CheckISP1=$StatusOfISP1)and($CheckISP2=$StatusOfISP2)) do={
:log warning "Example is Unreachable";
:log warning "$StatusOfISPs";
$SendEmail EmailDescription="Example is Unreachable" EmailBody=$StatusOfISPs;
}
} else={
:if (($StatusOfISPs=";000;;101")or($StatusOfISPs=";000;;110")or($StatusOfISPs=";000;;111")or($StatusOfISPs=";010;;101")or($StatusOfISPs=";010;;111")or($StatusOfISPs=";100;;101")or($StatusOfISPs=";100;;111")) do={
:delay delay-time=5;
:local DisableISP1 [$DisableISP Interface=ether1 GatewayISP=xz.xz.xz.xz ISPName=ISP1 StatusCheck=$StatusOfISP1];
:if ($DisableISP1=";1") do={
:set $InitiateStatus 0;
:set $Counter -1;
:log error "ISP1 has been disabled";
:log error "$StatusOfISPs";
/interface ethernet disable ether1;
:delay delay-time=1;
$SendEmail EmailDescription="ISP1 has been disabled" EmailBody=$StatusOfISPs;
}
} else={
:if (($StatusOfISPs=";101;;000")or($StatusOfISPs=";101;;010")or($StatusOfISPs=";101;;100")or($StatusOfISPs=";110;;000")or($StatusOfISPs=";111;;000")or($StatusOfISPs=";111;;010")or($StatusOfISPs=";111;;100")) do={
:delay delay-time=5;
:local DisableISP2 [$DisableISP Interface=ether2 GatewayISP=yz.yz.yz.yz ISPName=ISP2 StatusCheck=$StatusOfISP2];
:if ($DisableISP2=";1") do={
:set $InitiateStatus 0;
:set $Counter -1;
:log error "ISP2 has been disabled";
:log error "$StatusOfISPs";
/interface ethernet disable ether2;
:delay delay-time=1;
$SendEmail EmailDescription="ISP2 has been disabled" EmailBody=$StatusOfISPs;
}
} else={
:log warning "No action is required";
:log warning "$StatusOfISPs";
}
}
}
}
}