net watch

3
I wrote this script to improve basic RouterOs netwatch tool, but my difference f rom others netwatch scripts is that my own use also the oroginal routeros tool n etwhatch. This script has some advantages: 1) it is completely independent from devices to control, as it take informations directly from netwatch table, so you only have to compile /tool netwatch with y our devices and don't take care of the script; 2) it sends only ONE e-mail with a complete report of netwatch table, instead of one e-mail for each device; 3) it's able to send only ONE e-mail at router startup and only if there is any down device in netwatch table; 4) it takes care of disabled netwatch entries, so you can disable control on dev ices you know are switched off or not present; 5) it can reduce the number of e-mail alerts, as it checks netwatch table at fix ed intervals (scheduled) and the probability to found a false down device (becau se of one ping lost in netwatch) at those intervals is surely lower than the inf amous netwatch's false negatives and positives; 6) it's lot simpler than other netwatch scripts; The script is composed by two different scripts: a startup-alarm (scheduled at s tartup) script used to initialize the global variables (control variable, e-mail address variable, identity variable) and to send a mail when router has been re booted; and the netwatch script (scheduled at 5, 10, 20, or more minutes) . This is the startup-alarm script: Code: # Schedule this script to run at startup and only once # "net" variable is the "netwatch" script control variable and it's initialized at "0" by this script # "recipient" variable contains the mail address used to send and receive e-mail s (it can be the same!) # "thisbox" variable is used to put device identity in mail subject # first disable "netwatch" script in order to leave time (120 sec) to the router to complete boot up /system scheduler set netwatch disabled yes :global net 0 :global recipient [email protected] :global thisbox [/system identity get name] :delay 120 # wait to leave time to NTP client to update time and date :local thisdate [/system clock get date] :local thistime [/system clock get time] # send now a mail to "$recipient" and enable "netwatch" script /tool e-mail send to="$recipient" \ subject="$thisbox started on $thisdate at $thistime"; /system scheduler set netwatch disabled no and this is the netwatch script: Code: :global net :global recipient :global thisbox

Upload: garry-moore

Post on 19-Jul-2016

4 views

Category:

Documents


0 download

DESCRIPTION

Script Netwatch

TRANSCRIPT

Page 1: Net Watch

I wrote this script to improve basic RouterOs netwatch tool, but my difference from others netwatch scripts is that my own use also the oroginal routeros tool netwhatch.This script has some advantages:

1) it is completely independent from devices to control, as it take informations directly from netwatch table, so you only have to compile /tool netwatch with your devices and don't take care of the script;2) it sends only ONE e-mail with a complete report of netwatch table, instead of one e-mail for each device;3) it's able to send only ONE e-mail at router startup and only if there is any down device in netwatch table;4) it takes care of disabled netwatch entries, so you can disable control on devices you know are switched off or not present;5) it can reduce the number of e-mail alerts, as it checks netwatch table at fixed intervals (scheduled) and the probability to found a false down device (because of one ping lost in netwatch) at those intervals is surely lower than the infamous netwatch's false negatives and positives;6) it's lot simpler than other netwatch scripts;The script is composed by two different scripts: a startup-alarm (scheduled at startup) script used to initialize the global variables (control variable, e-mail address variable, identity variable) and to send a mail when router has been rebooted; and the netwatch script (scheduled at 5, 10, 20, or more minutes) .This is the startup-alarm script:

Code:# Schedule this script to run at startup and only once# "net" variable is the "netwatch" script control variable and it's initialized at "0" by this script# "recipient" variable contains the mail address used to send and receive e-mails (it can be the same!)# "thisbox" variable is used to put device identity in mail subject# first disable "netwatch" script in order to leave time (120 sec) to the router to complete boot up

/system scheduler set netwatch disabled yes:global net 0:global recipient [email protected]:global thisbox [/system identity get name]

:delay 120

# wait to leave time to NTP client to update time and date

:local thisdate [/system clock get date]:local thistime [/system clock get time]

# send now a mail to "$recipient" and enable "netwatch" script

/tool e-mail send to="$recipient" \subject="$thisbox started on $thisdate at $thistime";/system scheduler set netwatch disabled no

and this is the netwatch script:Code::global net:global recipient:global thisbox

Page 2: Net Watch

:local A 0

# check if there is anything "down" in /tool netwatch and check only enabled entries# variable "A" is used to save this check result

:foreach i in=[/tool netwatch find where (disabled=no)] do={if ([/tool netwatch get number=$i status]="down" && $A=0) do={:set A 1}}

# if there is a device down, send a report and set 1 the control global variable "net"# if all device are up and "net" variable is =1, send a report and set 0 the control global variable "net"

:if ($A=1) do={ :if ($net =0) do={ :local report ""; :foreach h in=[/tool netwatch find] do={ :if ( $report != "" ) do={ :set report "$report;"; }; :set report ( $report . "\n" . "$[/tool netwatch get number=$h status]" . " " \ . "$[/tool netwatch get number=$h comment]" . " from " . "$[/tool netwatch get number=$h since]"); } /tool e-mail send to="$recipient" subject="$thisbox controllo rete" body="$report"; :set net 1; }} \else={ :if ($net =1) do={ :local report ""; :foreach h in=[/tool netwatch find] do={ :if ( $report != "" ) do={ :set report "$report;"; }; :set report ( $report . "\n" . "$[/tool netwatch get number=$h status]" . " " \ . "$[/tool netwatch get number=$h comment]" . " from " . "$[/tool netwatch get number=$h since]"); } /tool e-mail send to="$recipient" subject="$thisbox network check" body="$report"; :set net 0; }}

To show script's operation I put there my netwatch print:

Code:[admin@WIFI_CASTELLO] /tool netwatch> printFlags: X - disabled # HOST TIMEOUT INTERVAL STATUS 0 ;;; WIFI_Municipio 192.168.2.2 1s 1m up 1 ;;; WIFI_Casa_A 192.168.2.3 1s 1m up 2 ;;; WIFI_Cast_nano 192.168.2.19 1s 1m up

Page 3: Net Watch

And here is the mail content if "WIFI_Casa_A" was down:

subject: WIFI_CASTELLO network check

body: up WIFI_Municipio from mar/29/2014 04:42:00;down WIFI_Casa_A from mar/29/2014 11:35:03;up WIFI_Cast_nano from mar/28/2014 19:34:55

http://forum.mikrotik.com/viewtopic.php?f=9&t=83539