aboutsummaryrefslogtreecommitdiffstats
path: root/log-forward
blob: 813fd060f4e7aaebbf828f5184084249301b05f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!rsc by RouterOS
# RouterOS script: log-forward
# Copyright (c) 2020-2022 Christian Hesse <mail@eworm.de>
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
#
# forward log messages via notification
# https://git.eworm.de/cgit/routeros-scripts/about/doc/log-forward.md

:local 0 "log-forward";
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }

:global Identity;
:global LogForwardFilter;
:global LogForwardFilterMessage;
:global LogForwardInclude;
:global LogForwardIncludeMessage;
:global LogForwardLast;
:global LogForwardRateLimit;
:global NotificationsWithSymbols;

:global EscapeForRegEx;
:global HexToNum;
:global IfThenElse;
:global LogPrintExit2;
:global QuotedPrintable;
:global ScriptLock;
:global SendNotification2;
:global SymbolByUnicodeName;
:global SymbolForNotification;

$ScriptLock $0;

:if ([ :typeof $LogForwardRateLimit ] = "nothing") do={
  :set LogForwardRateLimit 0;
}

:if ($LogForwardRateLimit > 30) do={
  :set LogForwardRateLimit ($LogForwardRateLimit - 1);
  $LogPrintExit2 info $0 ("Rate limit in action, not forwarding logs, if any!") true;
}

:local Count 0;
:local Duplicates false;
:local Last [ $IfThenElse ([ :len $LogForwardLast ] > 0) [ $HexToNum $LogForwardLast ] -1 ];
:local Messages "";
:local Warning false;
:local MessageVal;
:local MessageDups [ :toarray "" ];

:local LogForwardFilterLogForwarding ("^" . [ $CharacterReplace [ $EscapeForRegEx ("Error sending e-mail <" . \
    [ $QuotedPrintable ("[" . $Identity . "] %SYMBOLREGEX%Log Forwarding") ] . ">:") ] "%SYMBOLREGEX%" \
    ("((" . [ $SymbolByUnicodeName "memo" ] . "|" . [ $SymbolByUnicodeName "warning-sign" ] . ") )?") ]);
:foreach Message in=[ /log/find where (!(message="") and !(message~$LogForwardFilterLogForwarding) and \
    !(topics~$LogForwardFilter) and !(message~$LogForwardFilterMessage)) or \
    topics~$LogForwardInclude or message~$LogForwardIncludeMessage ] do={
  :set MessageVal [ /log/get $Message ];

  :if ($Last < [ $HexToNum ($MessageVal->".id") ]) do={
    :local DupCount ($MessageDups->($MessageVal->"message"));
    :if ($MessageVal->"topics" ~ "(emergency|alert|critical|error|warning)") do={
      :set Warning true;
    }
    :if ($DupCount < 3) do={
      :set Messages ($Messages . "\n" . [ $IfThenElse ($NotificationsWithSymbols = true) (" \E2\97\8F ") ] . \
        $MessageVal->"time" . " " . [ :tostr ($MessageVal->"topics") ] . " " . $MessageVal->"message");
    } else={
      :set Duplicates true;
    }
    :set ($MessageDups->($MessageVal->"message")) ($DupCount + 1);
    :set Count ($Count + 1);
  }
}

:if ($Count > 0) do={
  $SendNotification2 ({ origin=$0; \
    subject=([ $SymbolForNotification [ $IfThenElse ($Warning = true) "warning-sign" "memo" ] ] . \
      "Log Forwarding"); \
    message=("The log on " . $Identity . " contains " . [ $IfThenElse ($Count = 1) \
      "this message" ("these " . $Count . " messages") ] . " after " . \
      [ /system/resource/get uptime ] . " uptime." . [ $IfThenElse ($Duplicates = true) \
      (" Multi-repeated messages have been skipped.") ] . "\n" . $Messages) });

  :set LogForwardRateLimit ($LogForwardRateLimit + 10);
  :set LogForwardLast ($MessageVal->".id");
} else={
  :if ($LogForwardRateLimit > 0) do={
    :set LogForwardRateLimit ($LogForwardRateLimit - 1);
  }
}