User talk:Giepie

From MikroTik Wiki
Jump to: navigation, search

Netwatcher by Giepie

I created this script to run off a RB with battery backup. The Dude doesn't always run that well on a RB when there's lots of nodes to monitor. A PC or RB1000 is ideal for the DUDE, but when costs are a problem, my script is a good alternative for receiving SMS's or E-Mails when certain sites goes up or down.

The script works very straight forward, but I do suggest using it on a dedicated MT, or at least a MT not making use if Address-lists.

Each site/address you wish to monitor should be added to /ip address-lists. The address-list's name should be the description of the monitored IP, and the address is obviously the address. Do not use the comment field, as the comment field is used for the down count.

Every time a certain address replies with a timeout, the counter in the comment field will add 1 to the number until it reaches 7. Once it reaches 7, the script will automatically change the comment to 8, which 1) sends an e-mail/sms notification 2)not notify about this problem again, except when the site comes online again. Once the IP replies, the counter will be set to 0. For me it is useful doing it this way, as some IP's I monitor are sites on the internet, and those site's sometimes have higher latency, so if at least one decent ping comes through in the 7 attempts, you won't be bothered by e-mail/sms abuse from the netwatcher.

The script is intended to inform you of DOWN sites, not sites with weak links.

A schedule should be added to run this script upon startup. The script will run forever due to the continues loop.

Here is the script:


log info "Starting Netwatcher"
local sitename
local siteip
local sitecount
local endlesscounter
local endless
  1. STARTING ENDLESS LOOP###
set endlesscounter 0
while ($endlesscounter < 5) do={

/ip firewall address-list

set sitecount 0
  1. Main Loop START###
foreach i in=[/ip firewall address-list find (disabled=no)] do={
set sitename [get $i list]
set siteip [get $i address]
set sitecount [get $i comment]
  1. If no comment (new site), then change to 0###
if ($sitecount = "") do={
set sitecount 0
log info "$sitename SiteCount was invalid, set to $sitecount"

/ip firewall address-list set $i comment=$sitecount

}


  1. If Site is ONLINE, ZERO comment###
if ([/ping $siteip count=1]>0) do={
  1. IF Site was down, notify on up###
if (sitecount>0) do={
  1. If sitecount is >0 and =7 only then notify###
if (sitecount=8) do={
log error "SITE UP: $sitename $siteip, notifying"
  1. Send mail to your Cellular Phone's Mailbox (in order to get SMS) with all info in
  2. subject line

/tool e-mail send to=celularnumber@celularprovider.com from=netwatcher@domain.com server=smtpserver subject="UP: $sitename $siteip" body=""

  1. Send mail to your E-Mail address with more info in the body

/tool e-mail send to=user@domain.com from=netwatcher@domain.com server=smtpserver subject="UP: $sitename $siteip" body=" SITE IS ONLINE: $sitename $siteip"}

set sitecount 0

/ip firewall address-list set $i comment=$sitecount

}

  1. End site was down###
  2. Start Site Online###
    log info "$sitename $siteip Online $sitecount"
set sitecount 0
if (sitecount > 7) do={ /ip firewall address-list set $i comment=$sitecount}

} else={


  1. If Site is OFFLINE, add 1 to comment except when sitecount>7###
  1. Start loop to not process sitecount>7###
if (sitecount<7) do={
set sitecount ($sitecount + 1)

/ip firewall address-list set $i comment=$sitecount

log error "$sitename $siteip Offline-Count: $sitecount"}
  1. If sitecount=7 set sitecount to 8 and notify###
if (sitecount=7) do={
log error "SITE DOWN: $sitename $siteip, notifying"
set sitecount 8

/ip firewall address-list set $i comment=$sitecount

  1. Send mail to your Cellular Phone's Mailbox (in order to get SMS) with all info in
  2. subject line

/tool e-mail send to=phonenumber@cellularprovider.com from=netwatcher@domain.com server=smtpaddress subject="DOWN: $sitename $siteip" body=""

  1. Send mail to your E-Mail address with more info in the body

/tool e-mail send to=user@domain.com from=netwatcher@domain.com server=smtpaddress subject="DOWN: $sitename $siteip" body="SITE IS OFF-LINE: $sitename $siteip"

  1. End IF sitecount=7###

} }

  1. End loop not to process sitecount>3####

}

  1. END? ENDLESS LOOP###
set endlesscounter 0
  1. set endlesscounter ($endlesscounter + 1)
    log error "endlesscounter: $endlesscounter"

}

log info "Done"