Script: Monitor de status de las interfaces

El siguiente script permite conocer cuando hay un cambio en el estado de las interfaces, por ejemplo, si estamos en una interface ethernet podremos saber cuando deja de hacer link.

Los datos son mostrados en el log cada cierto intervalo:

# Detects changes to interface running state

:global InterfaceID
:global InterfaceState

:local findindex
:local prevstate
:local curstate
:local tmpstate

/interface {

   :foreach i in=[find] do={
      :set curstate [:tostr [get $i running]]
      :if ([:len $curstate] = 0) do={ :set curstate [:tostr false] }

      :set findindex [:find [:toarray $InterfaceID] [:tostr $i]]
      :if ([:len $findindex] > 0) do={
#      interface found. compare it's current state to previous state
         :set prevstate [:tostr [:pick [:toarray $InterfaceState] $findindex]]

         :if ( $prevstate != $curstate ) do={
#         --- start interface state change action ---

            :put ([get $i name] . " running state changed: " . $prevstate . " -> " . $curstate)
            :log info ([get $i name] . " running state changed: " . $prevstate . " -> " . $curstate)

#         --- end interface state change action ---

#         update interface record with new state
            :set tmpstate ""
            :for x from=0 to=([:len [:toarray $InterfaceState]] - 1) do={
               :set prevstate [:tostr [:pick [:toarray $InterfaceState] $x]]
               :if ($x = $findindex) do={
                  :set tmpstate ([:tostr $tmpstate] . $curstate . ",") } else={
                     :set tmpstate ([:tostr $tmpstate] . $prevstate . ",") }
            }
            :set InterfaceState [:tostr $tmpstate]

#      end if ( $prevstate != $curstate )
         }

      } else={

#      interface wasn't found, record it's state
         :set InterfaceID ([:tostr $InterfaceID] . [:tostr $i] . ",")
         :set InterfaceState ([:tostr $InterfaceState] . [get $i running] . ",")

      }

# end foreach i
   }
# /interface
}

Hay que realizar el scheduler para que se ejecute cada cierto tiempo:

/system scheduler add name="Interface State Check" on-event="<script name>" interval=5s

Via: MikroTIk