RouterOS Italia

Versione completa: [Mikrotik] Resilience/Monitoring- Monitoring Script
Al momento stai visualizzando i contenuti in una versione ridotta. Visualizza la versione completa e formattata.
Actually there are two scripts. The first one is the main script for monitoring arbitrary RouterOS parameters. It uses the second auxiliary script for logging.

Add both scripts to your RouterOS. Make sure they are named as "monitoring" and "log-up-down". Add the monitoring script to the system scheduler:

Codice:
/system scheduler add name=monitoring on-event=monitoring interval=5s \
   start-date=jan/01/1970 start-time=00:00:00
Check the log messages. You will see the following lines:
Codice:
Check the log messages. You will see the following lines:


Edit the monitoring script to adjust it for your own RouterOS configuration. By default the script monitors the state of three routes and makes log entries when the state is changed, assuming that a failover between two ISP connections is configured. But you can monitor arbitrary parameters and do other actions than just logging.

If you want to receive monitoring notifications by e-mail, just set up RouterOS system logging for that.

Codice:
############################################################################

Codice:
# Script name:   monitoring

Codice:
# Last changed:  25 Feb 2011

Codice:
# ROS version:   5.0rc10

Codice:
# Author:        Vadim Guchenko [yhw at relost dot net]

Codice:
#

Codice:
# Monitors one or more parameters of RouterOS and performs specified actions

Codice:
# when parameters change their values. A parameter is any expression of the

Codice:
# RouterOS scripting language that can return a value of any data type to

Codice:
# which the = operator can be applied. When the parameter value is changed,

Codice:
# the specified action is executed. An action is any statement of the

Codice:
# RouterOS scripting language that doesn't return a value (usually it's

Codice:
# invocation of another script). Before executing an action the following

Codice:
# global variables are set:

Codice:
#

Codice:
#   monParamName         The name of the parameter that has been changed.

Codice:
#                        Can be used to distinguish different parameters in

Codice:
#                        one action script.

Codice:
#   monParamOldValue     The old parameter value.

Codice:
#   monParamNewValue     The new parameter value.

Codice:
#   monParamOldValueAge  How long (seconds) the parameter had the old value.

Codice:
#                        Calculated without using the system clock.

Codice:
#

Codice:
# This script must be run from the scheduler at small intervals. If a # The names of the parameters. Every array element corresponds to an

Codice:
# individual parameter. Dimensions of the arrays "paramNames", "paramValues"

Codice:
# and "paramActions" must be the same.

Codice:
:local paramNames {

Codice:
       "ISP1"

Codice:
       "ISP2"

Codice:
       "Internet"

Codice:
}

Codice:
#

Codice:
# The expressions to compute parameter values. Note that the expressions

Codice:
# must NOT be enclosed in double quotes.

Codice:
:local paramValues {

Codice:
       ([/ip route find dst-address=127.127.127.1/32 active] != "")

Codice:
       ([/ip route find dst-address=127.127.127.2/32 active] != "")

Codice:
       ([/ip route find dst-address=0.0.0.0/0 active] != "")

Codice:
}

Codice:
#

Codice:
# The actions to be executed when parameter values are changed. Note that

Codice:
# all actions must be enclosed in double quotes.

Codice:
:local paramActions {

Codice:
       "/system script run log-up-down""/system script run log-up-down"

Codice:
       "/system script run log-up-down"

Codice:
}

Codice:
#

Codice:
# The action to be executed when the script is run for the first time after

Codice:
# RouterOS is booted up.

Codice:
:local initAction ":log info \"Monitoring started\""

Codice:
#

Codice:
############################################################################

Codice:
# Don't change anything below this line.

Codice:
############################################################################


Codice:
:global monTimeCounter

Codice:
:global monParamValues

Codice:
:global monParamValuesChangedAt


Codice:
:local newParamValues

Codice:
:local newParamValuesChangedAt


Codice:
:if ($monTimeCounter > 0) do={

Codice:
} else={

Codice:
       :set monTimeCounter 0

Codice:
       :local action [:parse $initAction]

Codice:
       $action:for i from=0 to=([:len $paramNames] - 1) do={

Codice:
       :if ([:pick $paramValues $i] = [:pick $monParamValues $i]) do={

Codice:
               :set newParamValues ($newParamValues, [:pick $monParamValues $i])

Codice:
               :set newParamValuesChangedAt ($newParamValuesChangedAt, [:pick $monParamValuesChangedAt $i])

Codice:
       } else={

Codice:
               :set newParamValues ($newParamValues, [:pick $paramValues $i])

Codice:
               :set newParamValuesChangedAt ($newParamValuesChangedAt, $monTimeCounter)

Codice:
               :global monParamName [:pick $paramNames $i]

Codice:
               :global monParamOldValue [:pick $monParamValues $i]

Codice:
               :global monParamNewValue [:pick $paramValues $i]

Codice:
               :global monParamOldValueAge ($monTimeCounter - [:pick $monParamValuesChangedAt $i])

Codice:
               :local action [:parse [:pick $paramActions $i]]

Codice:
               $action

Codice:
       }

Codice:
}


Codice:
:set monTimeCounter ($monTimeCounter + $runInterval)

Codice:
:set monParamValues $newParamValues

Codice:
:set monParamValuesChangedAt $newParamValuesChangedAt


Codice:
############################################################################

Codice:
# Script name:   log-up-down

Codice:
# Last changed:  25 Feb 2011

Codice:
# ROS version:   5.0rc10

Codice:
# Author:        Vadim Guchenko [yhw at relost dot net]

Codice:
#

Codice:
# Logs the new value of the parameter in terms of "up" and "down". Also logs

Codice:
# uptime/downtime. Used in conjunction with the monitoring script.

Codice:
#

Codice:
# There is nothing to configure.

Codice:
############################################################################


Codice:
:global monParamName

Codice:
:global monParamNewValue

Codice:
:global monParamOldValueAge


Codice:
:local status

Codice:
:local time


Codice:
:if ($monParamNewValue = true) do={

Codice:
       :set status "up"

Codice:
       :set time "downtime"

Codice:
} else={

Codice:
       :set status "down":set time "uptime"

Codice:
}


Codice:
:log info "$monParamName is $status ($time=$[:totime $monParamOldValueAge])"