MikroTikスクリプト:デバイスまたは単純なMikroTikログパーサーへの正常なログインの通知

この記事は、少数のデバイスを使用しているスペシャリスト(監視またはロギングシステム用に個別のサーバーを使用していない)、ホームユーザー、デバイススクリプトを初めて作成し始めているユーザー、およびそれを理解する時間がない/希望がないユーザーにとってより興味深いものになります。





ユーザーのログイン/ログアウトイベントを含むメールの例
ユーザーのログイン/ログアウトイベントを含むメールの例

このリクエストに対してインターネット上で見つかる巨大なスクリプトを単純化し、この単純なアクション(MikroTik Wikiのスクリプトの)を実行し、ロンドンの居住者でない場合にMikroTikエンジニアが単純な解析方法を不可能にした理由を示すために、独自のスクリプトを作成するように促されました。:)





この記事では、MikroTikデバイスからのユーザーのログインとログアウトに関する通知の例を解析しますが、例も示しています。





  1. デバイスログの時間の編成。





  2. デバイスログを解析し、基準でイベントを検索します。





  3. 電子メール通知の送信。





  4. テレグラムメッセージの送信。





バックグラウンド。MikroTikログ解析スクリプトが「巨大」なのはなぜですか?

怪物とは、大量のスクリプトロジックとフォームの構築を意味します。





:set tmpstring ([:pick [:tostr $tmpstring] 0 $findindex] . [:pick [:tostr $tmpstring] ($findindex + [:len [:tostr $ruleop]]) [:len [:tostr $tmpstring]]])
      
      



"" , .





MIkroTik, . :)





: " "account", (UTC+06)?





/log find where time > $LastRunTime topics ~ "account"
      
      



, 23:59:59 . 12 , 00:00:00 . ?





MikroTik : , , , , "" . , /, , .





? , MikroTik 00:00:00 UTC±0:00. , .. (UTC+06), 6 , . 06:00:00 .





(UTC±0:00), , .





( ), .





, , MikroTik id , (.id , , 0).





  • ParseLogAccountEndArrayID - .id ;





  • IDsEventsAccount .id , "account" - (: , ). 1000 , ;





  • LenArrayIDs - , StartArrayID - ( ID ), EndArrayID - 1( 0).





  • .id (IDsEventsAccount) .id (ParseLogAccountEndArrayID) (.. "account") (ParseLogAccountEndArrayID) - ( / ) ;





  • "account", (IDsEventsAccount) , " +1" ( ) " -1" (.. 0);





  • .id (IDMessage) ;





  • email, MikroTik;





  • Telegram , %0D%0A ;





  • ParseLogAccountEndArrayID ID "account" (EndArrayID).





  • email;





  • Telegram;





: read, write, test, policy.





[System] -> [Scripts] -> [+] -> [Name: ParseLogAccountEvents] -> [Policy: read, write, test, policy]





:local DeviceName [/system identity get name];
:local Time [/system clock get time];
:local Date [/system clock get date];
:local EmailMessageText;
: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={

    :local StartArray [:find $IDsEventsAccount $ParseLogAccountLastRunID];

    :for KeyArray from=($StartArrayID+1) to=($LenArrayIDs-1) do={
        :local IDMessage ($IDsEventsAccount ->$KeyArray );
        :set EmailMessageText "$EmailMessageText \n\r  $[/log get number=$IDMessage time] - $[/log get number=$IDMessage message];";
        :set TelegramMessageText "$TelegramMessageText %0D%0A  $[/log get number=$IDMessage time] - $[/log get number=$IDMessage message];";
        }

    :set ParseLogAccountEndArrayID $EndArrayID;

    # START SEND EMAIL
    :local SendFrom "ToMail@mail.ru";
    :local PasswordMail "yourpassword";
    :local SmtpServer [:resolve "smtp.mail.ru"];
    :local UserName "FromMail@mail.ru";
    :local SmtpPort 465;
    :local UseTLS "tls-only";
    :local SendTo "ToMail@mail.ru";
    :local Subject "\F0\9F\94\93 AUTH: $DeviceName [$Date $Time]";
    :local MessageText $EmailMessageText;
    /tool e-mail send to=$SendTo server=$SmtpServer port=$SmtpPort start-tls=$UseTLS user=$SendFrom password=$PasswordMail  from=$SendFrom subject=$Subject body=$MessageText;
    # END SEND EMAIL

    # START SEND TELEGRAM MESSAGE
    :local BotToken "YourBotID";
    :local ChatID "YourChatID";
    :local ParseMode "html";
    :local DisableWebPagePreview True;
    :local SendText "\F0\9F\94\93 <b>$DeviceName: AUTH</b> $TelegramMessageText";
    :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;
    # END SEND TELEGRAM MESSAGE

}
      
      



: read, write, test, policy.





[System] -> [Schedule] -> [+] -> [Name: ParseLogAccountEvents] —> [Interval: 00:05:00] -> [Policy: read, write, policy, test]





:





/system scheduler add name=ParseLogAccountEvents policy=read,write,policy,test on-event="/system script run ParseLogAccountEvents" interval=5m
      
      



, MikroTik , .





テレグラムメッセージの例
Telegram

, Logging:





[System] -> [Logging] -> [Rules] -> [+] -> [Topics]





:





[/log find where message ~ "log"]





, / , ( Firewall , MikroTik Safe Mode) .





, , .





, Telegram , , . : " Email" " Telegram", , MikroTik.





MikroTik, - .





: hAP ac lite, RouterOS 6.47.8 (stable).





PSこれはハブレに関する私の最初の記事です。厳密に、しかし公平に判断することができます。この記事では、監視システムまたは個別のログサーバーを使用するスペシャリストに新しいことは何も提供しません。しかし、初心者、ホームユーザー、ネットワークデバイスの数が少ない管理者にとっては、これが役立つことを願っています。








All Articles