Subscribe For Free Updates!

We'll not spam mate! We promise.

Sunday 24 November 2013

Mikrotik: Netwatch Script to Detect Target Server Link & Act Accordingly


matrix
Someone requested me to create a script that can check Squid Proxy or Target Server Link state from Mikrotik, and if the Squid Proxy / Target Server is not responding, then it should Disable the redirect NAT rule so all load can be handle by Mikrotik.You can modify this script as per your requirements. you can modify it to check WAN link, and if the WAN link is down, it can trigger SMS Script / Sound Alaram, or shift to backup WAN link by enabling diabling ROUTE’s or it can also be used to detect any wan link failure detection and change the rules accordingly.
Possibilities are endless.
So here it is.

Adding Comments in Redirect Rule

First add comments in required NAT rule, i.e Rule that redirects port 80 traffic to squid. You can also disable/enable rules based on the numbers, BUT its not recommended BECAUSE if you add any new rule and move it somewhere, all your scripts will disable/enable wrong rules, that is why you have to use find command and get rules this way.
You can use GUI to add comment in the rule, I am showing you an example below . . .
/ip firewall nat
add action=dst-nat chain=dstnat comment=”Redirect to SQUID” disabled=no dst-port=80 protocol=tcp to-addresses=192.168.6.1 to-ports=8080
add action=masquerade chain=srcnat comment=”Default NAT Rule for Internet Access / zaib” disabled=no to-addresses=0.0.0.0
1- nat rule
Change the host ip address as your local requirements. Its jsut an example to show you.
.
.

Adding EMAIL account in mikrotik

1
/tool e-mail set address=73.194.78.109 from=yourgmailid@gmail.com password=your_password port=587 starttls=no user=your_gmailid

Adding Scripts to Enable/Disable Rule

After adding comments in the dst-nt rule, you have to create two scripts that can enable and disable the dst-nat rule. We can name them UP and DOWN.
 Copy paste the below scripts on Mikrotik Terminal. It will create two scripts in System/Scripts with the name of UP and DOWN.

DOWN SCRIPT
1
2
3
4
:log info "Squid Proxy Server is not responding. Please Check Connectivity"
/ip firewall nat disable [find comment="Redirect to SQUID"]
:log info "Emailing the DOWN status. . . "
/tool e-mail send to="aacable@hotmail.com" password=yourgmailid subject="Squid Proxy Server is not responding. Please Check Connectivity"  body="Squid Proxy Server is not responding. Please Check Connectivity" from=yourgmailid@gmail.com server=173.194.69.109 tls=yes
UP SCRIPT
1
2
3
4
:log info "Squid Proxy Server is OK."
/ip firewall nat enable [find comment="Redirect to SQUID"]
:log info "Emailing the UP status. . . "
/tool e-mail send to="aacable@hotmail.com" password=yourgmailid subject="Squid Proxy Server is OK."  body="Squid Proxy Server is OK." from=yourgmailid@gmail.com server=173.194.69.109 tls=yes
2- Script-List
.

Adding NETWATCH to monitor Target Server [Squid] Link Detection

Use the below script to add netwatch entry for squid / target server link detection. It will also add the UP and DOWN script for the appropriate action. For example. When the target server link is down, it will execute script DOWN which will disable the NAT redirect rule , when the target server link is up and working, it will execute the UP script which will re-enable the redirect rule.
/tool netwatch add comment="Netwatch to Check SQUID Server Link state / zaib" disabled=no down-script="/sys script run down" host=192.168.6.1 interval=5m timeout=1s \     up-script="/sys script run up"
3 - Squid Link Detection
(Replace the host ip to match your local target server ip)
.
.

Testing THE Scripts . . .   :) ~

Now test the scripts, Unplug the Target Server LAN link, and you will see something like below in the LOG.
4- link down
.
Now plugin the Target Server LAN link, and you will see something like below in the LOG.
5- link up
.
.

SCRIPT TO CHECK SQUID STATUS AND START IT IF REQUIRED

Following script [checksquid.sh] will check SQUID service status , and if it found squid stop, it will start it auto, if the squid is already running , it will do nothing :)
First create file in any folder or temp folder by
mkdir /temp touch /temp/checksquid.sh chmod +x  /temp/checksquid.sh
Now edit this file
nano /temp/checksquid.sh
& paste the following code
1
2
3
4
5
6
7
8
#!/bin/bash
pid=`pidof $1`
if [ "$pid" == "" ]; then
echo $1 service is NOT running, Trying to start again . . .
service $1 start
else
echo $1 service is Running OK , no further action required, EXITING  . . .
fi
Save & Exit.
Test it by running
/temp/checksquid.sh squid
You can check any service status with above command, if the service is not running , it will try to start with.
You can create its cron entry to run it after every 5 minutes.
For example, add it using CRON by following
crontab -e
and add following line
*/5 * * * * /temp/checksquid.sh
Now save and exit.
Regard's
Naveed Ahmad

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT → , ,
FOLLOW US →
SHARE IT →

1 comments:

 
".