Skip to content

Instantly share code, notes, and snippets.

@yolleksandr
Created September 21, 2019 20:48
Show Gist options
  • Save yolleksandr/f5ecb2d02a32e3a15c1e7a04a74b10e8 to your computer and use it in GitHub Desktop.
Save yolleksandr/f5ecb2d02a32e3a15c1e7a04a74b10e8 to your computer and use it in GitHub Desktop.
https://wiki.mikrotik.com/wiki/Manual:Scripting
https://wiki.mikrotik.com/wiki/Manual:Scripting-examples#Detect_new_log_entry
Prepare
1. Create bot via botfather and get token
2. Add bot to chat
3. Get chat id
On device
1. Create script sendLogonEventTelegram
:global telegramToken "";
:global telegramChatId "";
:global routerHostName [/system identity get name] ;
:global lastTime;
:global currentBuf [ :toarray [ /log find buffer=logons ] ] ;
:global currentLineCount [ :len $currentBuf ] ;
:global currentTime [ :totime [/log get [ :pick $currentBuf ($currentLineCount -1) ] time ] ];
:global message "";
:if ( $lastTime = "" ) do={
:set lastTime $currentTime ;
:set message [/log get [ :pick $currentBuf ($currentLineCount-1) ] message];
/tool fetch http-method=post url="https://api.telegram.org/bot$telegramToken/sendMessage?chat_id=$telegramChatId&parse_mode=markdown&text=*$routerHostName*```$message```"
} else={
:if ( $lastTime < $currentTime ) do={
:set lastTime $currentTime ;
:set message [/log get [ :pick $currentBuf ($currentLineCount-1) ] message];
/tool fetch http-method=post url="https://api.telegram.org/bot$telegramToken/sendMessage?chat_id=$telegramChatId&parse_mode=markdown&text=*$routerHostName*```$message```"
}
}
2. Create log buffer
/system logging action
add name="logons"
/system logging
add action=logons topics=system,error,critical
add action=logons topics=system,info,account
3. Add scheduler for run every 10 (adjust time period) seconds with On Event: sendLogonEventTelegram
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment