Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mikrotik Monitoring Alert to Telegram

Goals

  • Make anything alerting model on Mikrotik to Telegram

Monitoring User Connect

This script will be monitor every user connect / disconnect to your telegram.

IP > DHCP SERVER > INTERFACE > SCRIPT

:if ($leaseBound = "1") do={/tool netwatch add down-script=":local BotID \"5416297136:AAHAx3p-yFsDr4nG8h5YNBI6nRyPe6XXXXX\
    \";\r\
    \n:local CHID \"-74638XXXX\";\r\
    \n:local avgRtt;\r\
    \n:local pin\r\
    \n:local pout\r\
    \n:local datetime \"\$[/system clock get date] \$[/system clock get time]\
    \"\r\
    \n:local hostname [[/ip dhcp-server lease get [find address=\$host] host-n\
    ame]]\r\
    \n:local mac [[/ip dhcp-server lease get [find address=\$host] mac-address\
    ]]\r\
    \n:local server [[/ip dhcp-server lease get [find address=\$host] active-s\
    erver]]\r\
    \n#Ping it real good\r\
    \n/tool flood-ping \$host count=10 do={\r\
    \n:if (\$sent = 10) do={\r\
    \n:set avgRtt \$\"avg-rtt\"\r\
    \n:set pout \$sent\r\
    \n:set pin \$received\r\
    \n}\r\
    \n}\r\
    \n:local ploss (100 - ((\$pin * 100) / \$pout))\r\
    \n:local logmsg (\"Ping Average for \$host - \".[:tostr \$avgRtt].\"ms - p\
    acket loss: \".[:tostr \$ploss].\"%\")\r\
    \n:log info \$logmsg\r\
    \n:local text \"Router Id:* \$[/system identity get name] * %0A\\\r\
    \nTanggal : _\$datetime_%0A\\\r\
    \nPing : _\$host_%0A\\\r\
    \nHost Name : _\$hostname_%0A\\\r\\
    \nMAC : _\$mac_%0A\\\r\\
    \nServer : _\$server_%0A\\\r\\
    \nLog : _\$logmsg_\"\r\
    \n/tool fetch url=\"https://api.telegram.org/bot\$BotID/sendmessage\?chat_\
    id=\$CHID&text=\$text\" keep-result=no;\r\
    \n" host=$leaseActIP up-script=":local BotID \"980842675:AAFLvPe4GuY25He\
    SRcxARu_BbSCx5OTf7Z4\";\r\
    \n:local CHID \"550242591\";\r\
    \n:local avgRtt;\r\
    \n:local pin\r\
    \n:local pout\r\
    \n:local datetime \"\$[/system clock get date] \$[/system clock get time]\
    \"\r\
    \n:local hostname [[/ip dhcp-server lease get [find address=\$host] host-n\
    ame]]\r\
    \n:local mac [[/ip dhcp-server lease get [find address=\$host] mac-address\
    ]]\r\
    \n:local server [[/ip dhcp-server lease get [find address=\$host] server]]\
    \r\
    \n#Ping it real good\r\
    \n/tool flood-ping \$host count=10 do={\r\
    \n:if (\$sent = 10) do={\r\
    \n:set avgRtt \$\"avg-rtt\"\r\
    \n:set pout \$sent\r\
    \n:set pin \$received\r\
    \n}\r\
    \n}\r\
    \n:local ploss (100 - ((\$pin * 100) / \$pout))\r\
    \n:local logmsg (\"Ping Average for \$host - \".[:tostr \$avgRtt].\"ms - p\
    acket loss: \".[:tostr \$ploss].\"%\")\r\
    \n:log info \$logmsg\r\
    \n:local text \"Router Id:* \$[/system identity get name] * %0A\\\r\
    \nTanggal : _\$datetime_%0A\\\r\
    \nPing : _\$host_%0A\\\r\
    \nHost Name : _\$hostname_%0A\\\r\\
    \nMAC : _\$mac_%0A\\\r\\
    \nServer : _\$server_%0A\\\r\\
    \nLog : _\$logmsg_\"\r\
    \n/tool fetch url=\"https://api.telegram.org/bot\$BotID/sendmessage\?chat_\
    id=\$CHID&text=\$text\" keep-result=no;\r\
    \n"
} else={
/tool netwatch remove [find host=$leaseActIP]
}

Monitoring Up/Down Netwatch

This script will be monitor every user connect / disconnect to your telegram for manual technique.

Tools > Netwatch

  • when UP
/tool fetch url="https://api.telegram.org/bot5416297136:AAHAx3p-yFsDr4nG8h5YNBI6nRyPeXXXXX/sendMessage?chat_id=-74638XXXX&text=Ping <b>Server</b> terpantau UP&parse_mode=html" keep-result=no
  • when Down
/tool fetch url="https://api.telegram.org/bot5416297136:AAHAx3p-yFsDr4nG8h5YNBI6nRyPeXXXXX/sendMessage?chat_id=-74638XXXX&text=Ping <b>Server</b> terpantau UP&parse_mode=html" keep-result=no

Monitoring Login/Logout Mikrotik

This script will be monitor every user connect / disconnect to your Mikrotik Device.

  • Create Script for Fetch Account System > Script > ParseLogAccountEvents
:local DeviceName [/system identity get name];
:local Time [/system clock get time];
:local Date [/system clock get date];
:local TelegramMessageText;

:global ParseLogAccountEndArrayID;

:local IDsEventsAccount [/log find where  topics ~ "account"];

:local LenArrayIDs [:len $IDsEventsAccount];
:local StartArrayID [:find $IDsEventsAccount $ParseLogAccountEndArrayID];
:local EndArrayID ($IDsEventsAccount -> ($LenArrayIDs-1));

:if ($EndArrayID != $ParseLogAccountEndArrayID and [:tobool $ParseLogAccountEndArrayID] ) do={

    :for KeyArray from=($StartArrayID+1) to=($LenArrayIDs-1) do={
        :local IDMessage ($IDsEventsAccount ->$KeyArray );
        :set TelegramMessageText "$TelegramMessageText %0D%0A  $[/log get number=$IDMessage time] - $[/log get number=$IDMessage message];";
        }
    :local MessageText "\F0\9F\94\93 <b>$DeviceName: AUTH</b> $TelegramMessageText";
    :local SendTelegramMessage [:parse [/system script get MyTGBotSendMessage source]];
    $SendTelegramMessage MessageText=$MessageText;
}

:set ParseLogAccountEndArrayID $EndArrayID;
  • Create Telegram Send Bastion System > Script > MyTGBotSendMessage
:local BotToken "5416297136:AAHAx3p-yFsDr4nG8h5YNBI6nRyPe6XXXX";
:local ChatID "-74638XXXX";
:local ParseMode "html";
:local DisableWebPagePreview True;
:local SendText $MessageText;

:local tgUrl "https://api.telegram.org/bot$BotToken/sendMessage?chat_id=$ChatID&text=$SendText&parse_mode=$ParseMode&disable_web_page_preview=$DisableWebPagePreview";
/tool fetch http-method=get url=$tgUrl keep-result=no;
:log info "Send Telegram Message: $MessageText";

Brief

Dont forget to re-check CHAT_ID & BOT TOKEN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment