Mikrotik RoS 7.x monitorovací skript s notifikací na Telegram

MikroTik (oficiálně SIA „Mikrotīkls“) je lotyšský výrobce síťových zařízení. Společnost vyvíjí a prodává drátové a bezdrátové síťové směrovače, síťové přepínače, přístupové body a také operační systémy a pomocný software. Společnost byla založena v roce 1996 se zaměřením na prodej zařízení na rozvíjejících se trzích. V srpnu 2019 bylo na webových stránkách společnosti uvedeno odhadem 280 zaměstnanců.

V roce 2021 byla společnost Mikrotik s hodnotou 1,24 miliardy EUR třetí největší společností v Lotyšsku a první soukromou společností, která v Lotyšsku překročila hodnotu 1 milionu EUR. Společnost Mikrotik byla založena v roce 1996 v lotyšské Rize jako společnost zabývající se softwarem pro PC. V roce 2002 začal MikroTik vyrábět vlastní hardware.

Dne 23. května 2018 oznámila společnost Cisco Talos Intelligence Group, že některá zařízení MikroTik byla shledána zranitelnými vůči malwaru VPNFilter. Dne 3. srpna 2018 bylo zjištěno, že směrovače MikroTik byly napadeny malwarem pro kryptoměny Coinhive.

Mikrotik + Telegram notifikace

Aktualizace: Skript funguje i na Mikrotik RoS 7.x stable. Skript monitorující aktivitu na Mikrotik routeru s Telegram notifikací. Věnujeme pozornost zejména proměnným

Konfigurujeme local bot, local ChatID a local startBuf

:local bot "1234567890:ABCDfaImL2L_BKmeo5GGGifUGIcXPKwJiGA"
:local ChatID "-9002345109561"
:local startBuf [:toarray [/log find message~" fail" || message~"loop"|| message~"sent" || message~"down" || message~"fcs" || message~"excessive" || message~"logged in" || message~"respond"]]

Scheduler se vytvoří automaticky po prvním manuálním spuštění skriptu

# LOG FILTER TO TELEGRAM BY AHMED MOUSELLY//UPD.09.07.2020BergDev
# BEGIN SETUP Edit Here
:local myserver ([/system identity get name])
:local scheduleName "LogFilter"
:local bot "1234567890:ABCDfaImL2L_BKmeo5GGGifUGIcXPKwJiGA"
:local ChatID "-9002345109561"
#Here you can specify which events from the logs you need to send.
:local startBuf [:toarray [/log find message~" fail" || message~"loop"|| message~"sent" || message~"down" || message~"fcs" || message~"excessive" || message~"logged in" || message~"respond"]]

# END SETUP

# warn if schedule does not exist
:if ([:len [/system scheduler find name="$scheduleName"]] = 0) do={
  /log warning "[LogFilter] Alert : Schedule does not exist. Creating schedule ...."

 /system scheduler add name=$scheduleName interval=60s start-date=Oct/10/2020 start-time=startup on-event=LogFilter

  /log warning "[LogFilter] Alert : Schedule created ."
}

# get last time
:local lastTime [/system scheduler get [find name="$scheduleName"] comment]
# for checking time of each log entry
:local currentTime
# log message
:local message
 
# final output
:local output

:local keepOutput false
# if lastTime is empty, set keepOutput to true
:if ([:len $lastTime] = 0) do={
  :set keepOutput true
}

:local counter 0
# loop through all log entries that have been found
:foreach i in=$startBuf do={
 
# loop through all removeThese array items
  :local keepLog true
  :foreach j in=$removeThese do={
#   if this log entry contains any of them, it will be ignored
    :if ([/log get $i message] ~ "$j") do={
      :set keepLog false
    }
  }
  :if ($keepLog = true) do={
   
   :set message [/log get $i message]

#   LOG DATE
#   depending on log date/time, the format may be different. 3 known formats
#   format of jan/01/2002 00:00:00 which shows up at unknown date/time. Using as default
    :set currentTime [ /log get $i time ]
#   format of 00:00:00 which shows up on current day's logs
   :if ([:len $currentTime] = 8 ) do={
     :set currentTime ([:pick [/system clock get date] 0 11]." ".$currentTime)
    } else={
#     format of jan/01 00:00:00 which shows up on previous day's logs
     :if ([:len $currentTime] = 15 ) do={
        :set currentTime ([:pick $currentTime 0 6]."/".[:pick [/system clock get date] 7 11]." ".[:pick $currentTime 7 15])
      }
   }
    
#   if keepOutput is true, add this log entry to output
   :if ($keepOutput = true) do={
     :set output ($output.$currentTime." %0A%0A ".$message."\r\n")
   }

    :if ($currentTime = $lastTime) do={
     :set keepOutput true
     :set output ""
   }
  }
  :if ($counter = ([:len $startBuf]-1)) do={
   :if ($keepOutput = false) do={    
     :if ([:len $message] > 0) do={
        :set output ($output.$currentTimer." ".$message."\r\n")
      }
    }
  }
  :set counter ($counter + 1)
}

if ([:len $output] > 0) do={
  /system scheduler set [find name="$scheduleName"] comment=$currentTime
  /tool fetch url="https://api.telegram.org/bot$bot/sendmessage?chat_id=$ChatID&text=$myserver%0A$output" keep-result=no;
}

Užitečné

Domovská stránka Mikrotik.
Návod Mikrotik RoS 7.x mark routing

ICTIS.CZ