Hello ,
Does anyone have a script that checked ping Down and Up and when is down sending SMS, and where is up to send sms.
I`m using USB modem .
10x in advance
# 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
# http://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"
# 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 "JAHAN.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 "yourid@gmail.com"
:local GMAILPASS "yourpassword"
# Alerts n info m Send to following Support addresses
:local TO1 "aacable @ hotmail . 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..."
# "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
# 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 "ENABLING 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 ..."
# "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
# 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";}
}
# SENDING SMS TO ADMIN FOR DAILY MORNING INFO
:local date;
:local time;
:local PPP ([/ppp active print count-only])
:local UPTIME [/system resource get uptime]
:local CPU [/system resource get cpu-load]
:set date [/system clock get date];
:set time [/system clock get time];
:local cell1 "03333021909"
:log warning "INFO: Daily info for MIKROTIK @ $date $time\nActive PPPOE Users = $PPP\nUptime is $UPTIME\nCPU Load = $CPU\nPowered by J."
:log warning "Sending DAILY MORNING ALERT SMS on $cell1 ... by jz"
/tool sms send port=usb3 phone-number=$cell1 message="INFO: Daily info for AA-Johar @ $date $time\nActive PPPOE Users = $PPP\nUptime = $UPTIME\nCPU Load = $CPU\nPowered by J." channel=0
# SENDING SMS TO ADMINS FOR RESTART ALERT
:delay 30s
:log error "Sending RESTART ALERT SMS ... by jz"
/tool sms send port=usb3 phone-number=03xxxxx message="ALERT: Mikrotik Routerboard CCR restarted now." channel=0
:local result [/ping 192.168.88.1 count=3]
:global status
:if ($result = 3 && $status != true) do={
/tool sms send message="link up" phone-number=12345
:set status true
} else={
:if ($result = 0 && $status != false) do={
/tool sms send message="link down" phone-number=12345
:set status false
}
}
ping to what? you can use netwatch tool and configure up n down scripts as required. or use separate scripts for more customized control and send sms as required.
Few scripts for your reference.
Script#1, to send alert when WAN goes down.Scrip#2, to send sms daily including mikrotik health report.Code: Select all# 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 # http://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" # 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 "JAHAN.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 "yourid@gmail.com" :local GMAILPASS "yourpassword" # Alerts n info m Send to following Support addresses :local TO1 "aacable @ hotmail . 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..." # "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 # 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 "ENABLING 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 ..." # "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 # 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";} }
Script#2, to send reboot alert, when mikrotik reboots. schedule it to run on reboot.Code: Select all# SENDING SMS TO ADMIN FOR DAILY MORNING INFO :local date; :local time; :local PPP ([/ppp active print count-only]) :local UPTIME [/system resource get uptime] :local CPU [/system resource get cpu-load] :set date [/system clock get date]; :set time [/system clock get time]; :local cell1 "03333021909" :log warning "INFO: Daily info for MIKROTIK @ $date $time\nActive PPPOE Users = $PPP\nUptime is $UPTIME\nCPU Load = $CPU\nPowered by J." :log warning "Sending DAILY MORNING ALERT SMS on $cell1 ... by jz" /tool sms send port=usb3 phone-number=$cell1 message="INFO: Daily info for AA-Johar @ $date $time\nActive PPPOE Users = $PPP\nUptime = $UPTIME\nCPU Load = $CPU\nPowered by J." channel=0
Some other references.Code: Select all# SENDING SMS TO ADMINS FOR RESTART ALERT :delay 30s :log error "Sending RESTART ALERT SMS ... by jz" /tool sms send port=usb3 phone-number=03xxxxx message="ALERT: Mikrotik Routerboard CCR restarted now." channel=0
https://aacable.wordpress.com/tag/mikrotik-sms/