Syed Jahanzaib – Personal Blog to Share Knowledge !

May 28, 2014

PTCL vDSL modem hang issue and its workaround

Filed under: Mikrotik Related — Tags: , , , — Syed Jahanzaib / Pinochio~:) @ 2:35 PM

modem

Updated: October, 2014

The script is well tested with mikrotik 5.2x only

Recently at a network, the operator was facing PTCL 50mb via vDSL modem (HUAWEI HG622 & CELLPIPE model) hangs frequently. The interval between hanging was different , sometimes 3-4 times daily or after 14-16 hours. Ping to the modem also timeout and when the operator restart the modem, every thing works fine, but its painful to do it manually specially in late night hours when no one at help desk to do the stupid job of resetting.

[It is also observed that PTA is actively blocking users public ips from which suspected grey traffic (Like VPN, HOTSPOT SHIELD and tunneling type applications, and specially VOIP) is passing through, so disconnecting , and reconnecting again assigns you new public ip and internet starts working again.)

The workaround I made was to

  • Try Using Good Quality UPS with automatic voltage control,
  • Configured the modem in BRIDGE mode,
  • Add pppoe client dialer in the Mikrotik ppp section,so mikrotik handles the wan dialing itself, not the modem
  • Then add a script with scheduler which will keep checking two internet hosts connectivity at 1 minute interval or as required) , and if it found no reply from both host , then it disables the default dialer (pppoe-out1) and redial the connection after 10 seconds of PAUSE/DELAY (to prevent any dial-flood).
  • It also sends an email to admin [configurable] so that he should be aware of what happening behind his back 😛 . You can skip email section if you don’t require notifications.

SCRIPT FOR MULTIPLE HOST MONITORING

 

Following is an EXPORT version of the script, name it wanmonitor. You should modify it as per your local need.

# Following script is a modified version from the Mikrotik forum.
# http://forum.mikrotik.com/viewtopic.php?f=9&t=85505
# Modified few contents to suite local requirements and description added
# Regard's / Syed Jahanzaib / https://aacable.wordpress.com

# Script Starts here...
# DUAL Internet Host (to avoid false failure) to be checked You can modify them as per required, JZ

# set GMAIL smtp IP auto
:global gmailsmtp
:set gmailsmtp [:resolve "smtp.gmail.com"];

# Add two hosts for monitoring internet connectivity
:local host1   "8.8.8.8"
:local host2   "208.67.222.123"
 
# Do not modify data below without proper understanding.
:local i 0;
:local F 0;
:local date;
:local time;
:global InternetStatus;
:global InternetLastChange;
 
# PING each host 5 times
:for i from=1 to=5 do={
if ([/ping $host1 count=1]=0) do={:set F ($F + 1)}
if ([/ping $host2 count=1]=0) do={:set F ($F + 1)}
:delay 1;
};
 
# If both links are down and all replies are timeout, then link is considered down
:if (($F=10)) do={
:if (($InternetStatus="UP")) do={
:log error "WARNING : The INTERNET link seems to be DOWN. Please Check";
:set InternetStatus "DOWN";
 
## ADD YOUR RULES HERE, LIKE ROUTE CHANGE OR WHAT EVER IS REQUIRED, Example is below ...
:log error "PTCL LINK SEEMS TO BE DOWN, Resetting PPPoE Dialer and wait for at least 10 seconds before redialing / zaib"
 /interface pppoe-client disable pppoe-out1
/sys script run siren
:delay 10
 /interface pppoe-client enable pppoe-out1

:set date [/system clock get date];
:set time [/system clock get time];
:set InternetLastChange ($time . " " . $date);
} else={:set InternetStatus "DOWN";}
} else={
 
## If reply is received , then consider the Link is UP
:if (($InternetStatus="DOWN")) do={
:log warning "WARNING :The INTERNET link have been restored";
:set InternetStatus "UP";
 
## ADD YOUR RULES HERE, LIKE ROUTE CHANGE OR WHAT EVER IS REQUIRED, Example is below ...
:local currentIP
:local externalInterface "pppoe-out1"

## get the current IP address from the external interface
:set currentIP [/ip address get [find interface="$externalInterface"] address]

## Strip netmask
:for i from=( [:len $currentIP] - 1) to=0 step=-1 do={
:if ( [:pick $currentIP $i] = "/") do={
:set currentIP [:pick $currentIP 0 $i]

# Show warning in Mikrotik LOG window
:log warning "PTCL LINK RE - CONNECTED with new WAN IP = $currentIP, Please check and confirm / zaib"

# Disable or Enable EMAIL alert feature as required
#/tool e-mail send to="aacable@hotmail.com.com" password=YOURMAILPASSWD subject="$[/system clock get date] $[/system clock get time] -- PTCL DSL pppoe Dialer RE-CONNECTED AND UP NOW, New WAN IP is $currentIP / zaib" from=aacable79@gmail.com server=$gmailsmtp tls=yes body="$[/system clock get date] $[/system clock get time] : PTCL Link was down, so the wan monitor script disconnected the pppoe-out1 dialer and reconnected after 10 seconds of delay. New WAN IP is $currentIP  . Thank you / aacable@hotmail.com"

}
}
 
:set date [/system clock get date];
:set time [/system clock get time];
:set InternetLastChange ($time . " " . $date);
} else={:set InternetStatus "UP";}
}
 
# Script Ends Here.
# Thank you / zaib

.

SCHEDULER CONFIGURATION (to check wan connectivity every minute)


/system scheduler
add comment="Schedular for WAN Monitor tun run every minute on MT" disabled=no interval=1m name="wan monitor script 1 minute check" on-event=wanmonitor policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-date=jul/10/2014 start-time=16:34:32

 

EMAIL CONFIGURATION (if email alerts are required)

You can skip this email config section if you don’t want to receive notifications via email. also you have to remove it from the main script also. Modify the gmail smtp as required.


/tool e-mail
set address=173.194.69.109 from=your_email@gmail.com password=your_password port=587 starttls=no user=your_username

Done.


 

 

When there will be no response from the internet (Google dns or hosts you have set) , then netwatch will trigger down script section, which will disconnect the active pppoe-out1 dialer connection, wait 10 seconds, then redial the connection again and log alert and shoots email.

As showed in the image below …

1- link up


 

 

1.

.

.


 

NOTES

  • You can increase the interval and timeout value as per your requirement, ideally it should be a bit higher.
  • Its recommended to monitor your ISP gateway + one internet host to avoid False result.

Second Version of the above script with sms / email support, and re connect wan pppoe-out1 dialer


# WAN 1 Monitor Script for mikrotik by using two internet host
# With optional Email and SMS Alert, modem is attached to local mikrotik in this example
# but i have added kannel sms gateway and gmail option too. Added many variables
# by Syed Jahanzaib
# https://aacable.wordpress.com
# Email : aacable at hotmail dot com
# Script Last Modified : 25-NOV-2015 / 13:00

# Set variables
:local i 0;
:local F 0;
:local date;
:local time;
:global "DSL1netstatus"
:global "dsl1netlastchange"

# Check dsl-1 GATEWAY to be monitored (Currently we are monitoring internet host AROOT DNS)
:global dsl1host1 "4.2.2.1"
:global dsl1host2 "199.7.83.42"

#:global dsl1host1 "4.2.2.8"
#:global dsl1host2 "199.7.83.8"


# Number of Ping Count, how many times each host should ping
:local PINGCOUNT "30"

# PING HTRESHOLD, Multiply above value with 2 , so if you have select 5 in above example, then set below $PINGTS to 10
:local PINGTS "60"

# Setting SMS Number for admin
:local cell1 "03333021909"
# Company Name
:local COMPANY "ZAIB"

#If you dont have kannel sms gateway ignore this.
:local KURL "http://KANNEL_IP:13013/cgi-bin/sendsms"
:local KID "kannel"
:local KPASS "kpass"

# SMS Msg format for localy attached modem in mikrotik
:local MSGDOWN "$COMPANY WAN ALERT: DSL-1 is DOWN."
:local MSGUP "$COMPANY WAN INFO: DSL-1 is UP"

# Set USB Port on which usb modme is connected in Mikrotik
:local PORT "usb3"

# SMS Msg format for Kannel SMS gateway
#:local MSGDOWN "$COMPANY+WAN+ALERT:+DSL-1+is+DOWN."
#:local MSGUP "$COMPANY+WAN+INFO:+DSL-1+is+UP"


# Gmail SMTP Address / If you dont want to send email disable these
:global "gmailsmtp"
:set gmailsmtp [:resolve "smtp.gmail.com"];
:local GMAILID "YOURGMAILID@gmail.com"
:local GMAILPASS "GMAILPASS"

# Alerts n info m Send to following Support addressess
:local TO1 "ADMINMAIL@hotmail.com"
:local TO2 "ADMINMAIL2@gmail.com"

# Email Subject for DOWN Alert
:local SUBDOWN "$[/system clock get date] $[/system clock get time] -- $COMPANY_ALERT: dsl-1 Primary Internet Link DOWN"
# Email Body for DOWN Alert
:local BODYDOWN "-- dsl-1 Primary Internet Link  at $COMPANY DATA CENTER not responding, \n\nPlease check conectivity... \n\n>>>  Script Designed by ***   Syed_Jahanzaib   ****  <<<"

# Email Subject for UP Alert
:local SUBUP "$[/system clock get date] $[/system clock get time] -- $COMPANY_INFO: dsl-1 Primary Internet Link is now UP"
# Email Body for UP Alert
:local BODYUP "-- dsl-1 Primary Internet Link  at $COMPANY DATA CENTER have been restored \n\n>>>  Script Designed by ***   Syed_Jahanzaib   ****  <<<"



#######################################
#######################################
########## Start the SCRIPT ###########
########## DONOT EDIT BELOW ###########
#######################################
#######################################

# PING each host $PINGCOUNT times
# IF NOT A SINGLE PING SUCCESSFULL THEN CONSIDER LINK DOWN ## ZAIB
:for i from=1 to=$PINGCOUNT do={
if ([/ping $dsl1host1 count=1]=0) do={:set F ($F + 1)}
if ([/ping $dsl1host2 count=1]=0) do={:set F ($F + 1)}
:delay 1;
};


# If no response (all ping counts fails for both hosts, Time out, then LOG down status and take action
:if (($F=$PINGTS)) do={
:if (($DSL1netstatus="UP")) do={
:set DSL1netstatus "DOWN";

# Also add status in global variables to be used as tracking
:set date [/system clock get date];
:set time [/system clock get time];
:set dsl1netlastchange ($time . " " . $date);


##################################################
####### FOR DOWN STATUS, CHANGE THE RULES ########
##################################################

# If the link is down, then LOG info and warning in Mikrotik LOG window [Zaib]
:log error "DSL1  Gateway Not Responding..."
:log error "DSL1  Gateway Not Responding..."
:log error "DSL1  Gateway Not Responding..."

# "Emailing the DOWN status. . . "
/tool e-mail send to="$TO1" subject="$SUBDOWN" body="$[/system clock get date] $[/system clock get time]:$BODYDOWN" from=$GMAILID password=$GMAILPASS  server=$gmailsmtp start-tls=yes
/tool e-mail send to="$TO2" subject="$SUBDOWN" body="$[/system clock get date] $[/system clock get time]:$BODYDOWN" from=$GMAILID password=$GMAILPASS  server=$gmailsmtp start-tls=yes

# Send SMS via KANNEL
#/tool fetch url="$KURL\?username=$KID&password=$KPASSto=$cell1&text=$MSGDOWN"

# Send SMS via Mikrotik attached modem
/tool sms send port=$PORT phone-number=$cell1 message="$MSGDOWN" channel=0

# Disable pppoe-out1 wan1 dialer and try to re-connect one time only
:log error "DISABLING PPPOE-OUT1 WAN DIALER"
/interface pppoe-client disable pppoe-out1
:delay 10
:log warning "EN-ABLING PPPOE-OUT1 WAN DIALER"
/interface pppoe-client enable pppoe-out1

##################################################
####### FOR UP STATUS, CHANGE THE RULES ########
##################################################
# If ping is ok for 1 host as well reply received, then LOG UP and take action as required
} else={:set DSL1netstatus "DOWN";}
} else={
:if (($DSL1netstatus="DOWN")) do={

:set DSL1netstatus "UP";
# If link is UP, then LOG info and warning in Mikrotik LOG window [Zaib]
log warning "DSL1  Gateway RESTORED ..."
log warning "DSL1  Gateway RESTORED ..."
log warning "DSL1  Gateway RESTORED ..."


# "Emailing the UP status. . . "
/tool e-mail send to="$TO1" subject="$SUBUP" body="$[/system clock get date] $[/system clock get time] $BODYUP" from=$GMAILID password=$GMAILPASS server=$gmailsmtp start-tls=yes
/tool e-mail send to="$TO2" subject="$SUBUP" body="$[/system clock get date] $[/system clock get time] $BODYUP" from=$GMAILID password=$GMAILPASS server=$gmailsmtp start-tls=yes

# Send SMS via KANNEL Gateway
#/tool fetch url="$KURL\?username=$KID&password=$KPASSto=$cell1&text=$MSGUP"

# Send SMS via Mikrotik attached modem
/tool sms send port=$PORT phone-number=$cell1 message="$MSGUP" channel=0

:set date [/system clock get date];
:set time [/system clock get time];
:set dsl1netlastchange ($time . " " . $date);

} else={:set DSL1netstatus "UP";}
}


 

Howto create WAN PPPOE DIALER in Mikrotik

1- mt pppoe

 

2- pppo 2

 

3- pppo 3

 

4-connectred

DONE.


 

Regard’s
Syed Jahanzaib