| 
									
										
										
										
											2020-09-18 11:00:27 +02:00
										 |  |  | #!rsc by RouterOS | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | # RouterOS script: log-forward | 
					
						
							| 
									
										
										
										
											2022-01-01 21:38:15 +01:00
										 |  |  | # Copyright (c) 2020-2022 Christian Hesse <mail@eworm.de> | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | # 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 15:14:10 +01:00
										 |  |  | :local 0 "log-forward"; | 
					
						
							| 
									
										
										
										
											2021-02-18 14:52:47 +01:00
										 |  |  | :global GlobalFunctionsReady; | 
					
						
							|  |  |  | :while ($GlobalFunctionsReady != true) do={ :delay 500ms; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | :global Identity; | 
					
						
							|  |  |  | :global LogForwardFilter; | 
					
						
							| 
									
										
										
										
											2020-09-25 22:21:59 +02:00
										 |  |  | :global LogForwardFilterMessage; | 
					
						
							| 
									
										
										
										
											2021-06-09 14:32:52 +02:00
										 |  |  | :global LogForwardInclude; | 
					
						
							|  |  |  | :global LogForwardIncludeMessage; | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | :global LogForwardLast; | 
					
						
							| 
									
										
										
										
											2020-09-28 20:20:19 +02:00
										 |  |  | :global LogForwardRateLimit; | 
					
						
							| 
									
										
										
										
											2021-03-04 18:47:55 +01:00
										 |  |  | :global NotificationsWithSymbols; | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 08:55:33 +01:00
										 |  |  | :global EscapeForRegEx; | 
					
						
							| 
									
										
										
										
											2021-06-15 14:39:42 +02:00
										 |  |  | :global HexToNum; | 
					
						
							| 
									
										
										
										
											2020-10-18 20:13:58 +02:00
										 |  |  | :global IfThenElse; | 
					
						
							| 
									
										
										
										
											2021-02-22 15:14:10 +01:00
										 |  |  | :global LogPrintExit2; | 
					
						
							| 
									
										
										
										
											2021-03-15 00:48:38 +01:00
										 |  |  | :global QuotedPrintable; | 
					
						
							| 
									
										
										
										
											2020-07-17 20:52:52 +02:00
										 |  |  | :global ScriptLock; | 
					
						
							| 
									
										
										
										
											2021-04-27 20:45:51 +02:00
										 |  |  | :global SendNotification2; | 
					
						
							| 
									
										
										
										
											2020-07-17 08:07:12 +02:00
										 |  |  | :global SymbolForNotification; | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | :global WaitFullyConnected; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 19:52:20 +01:00
										 |  |  | $ScriptLock $0; | 
					
						
							| 
									
										
										
										
											2020-07-17 20:52:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:20:19 +02:00
										 |  |  | :if ([ :typeof $LogForwardRateLimit ] = "nothing") do={ | 
					
						
							|  |  |  |   :set LogForwardRateLimit 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :if ($LogForwardRateLimit > 30) do={ | 
					
						
							|  |  |  |   :set LogForwardRateLimit ($LogForwardRateLimit - 1); | 
					
						
							| 
									
										
										
										
											2021-02-22 15:14:10 +01:00
										 |  |  |   $LogPrintExit2 info $0 ("Rate limit in action, not forwarding logs, if any!") true; | 
					
						
							| 
									
										
										
										
											2020-09-28 20:20:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | $WaitFullyConnected; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :local Count 0; | 
					
						
							| 
									
										
										
										
											2021-03-04 18:42:57 +01:00
										 |  |  | :local Duplicates false; | 
					
						
							| 
									
										
										
										
											2021-12-17 11:48:25 +01:00
										 |  |  | :local Last [ $IfThenElse ([ :len $LogForwardLast ] > 0) [ $HexToNum $LogForwardLast ] -1 ]; | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | :local Messages ""; | 
					
						
							|  |  |  | :local MessageVal; | 
					
						
							| 
									
										
										
										
											2021-03-04 18:42:57 +01:00
										 |  |  | :local MessageDups [ :toarray "" ]; | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 08:55:33 +01:00
										 |  |  | :local LogForwardFilterLogForwarding ("^" . [ $EscapeForRegEx ("Error sending e-mail <" . \ | 
					
						
							|  |  |  |     [ $QuotedPrintable ("[" . $Identity . "] " . [ $SymbolForNotification "warning-sign" ] . \ | 
					
						
							|  |  |  |     "Log Forwarding") ] . ">:") ]); | 
					
						
							| 
									
										
										
										
											2022-05-10 15:08:30 +02:00
										 |  |  | :foreach Message in=[ /log/find where (!(message="") and !(message~$LogForwardFilterLogForwarding) and \ | 
					
						
							| 
									
										
										
										
											2021-06-09 14:32:52 +02:00
										 |  |  |     !(topics~$LogForwardFilter) and !(message~$LogForwardFilterMessage)) or \ | 
					
						
							|  |  |  |     topics~$LogForwardInclude or message~$LogForwardIncludeMessage ] do={ | 
					
						
							| 
									
										
										
										
											2022-05-10 15:08:30 +02:00
										 |  |  |   :set MessageVal [ /log/get $Message ]; | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 14:39:42 +02:00
										 |  |  |   :if ($Last < [ $HexToNum ($MessageVal->".id") ]) do={ | 
					
						
							| 
									
										
										
										
											2021-03-04 18:42:57 +01:00
										 |  |  |     :local DupCount ($MessageDups->($MessageVal->"message")); | 
					
						
							|  |  |  |     :if ($DupCount < 3) do={ | 
					
						
							| 
									
										
										
										
											2021-03-04 18:47:55 +01:00
										 |  |  |       :set Messages ($Messages . "\n" . [ $IfThenElse ($NotificationsWithSymbols = true) (" \E2\97\8F ") ] . \ | 
					
						
							|  |  |  |         $MessageVal->"time" . " " . [ :tostr ($MessageVal->"topics") ] . " " . $MessageVal->"message"); | 
					
						
							| 
									
										
										
										
											2021-03-04 18:42:57 +01:00
										 |  |  |     } else={ | 
					
						
							|  |  |  |       :set Duplicates true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     :set ($MessageDups->($MessageVal->"message")) ($DupCount + 1); | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  |     :set Count ($Count + 1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :if ($Count > 0) do={ | 
					
						
							| 
									
										
										
										
											2021-04-27 21:50:44 +02:00
										 |  |  |   $SendNotification2 ({ origin=$0; \ | 
					
						
							|  |  |  |     subject=([ $SymbolForNotification "warning-sign" ] . "Log Forwarding"); \ | 
					
						
							| 
									
										
										
										
											2021-04-27 20:45:51 +02:00
										 |  |  |     message=("The log on " . $Identity . " contains " . [ $IfThenElse ($Count = 1) \ | 
					
						
							|  |  |  |       "this message" ("these " . $Count . " messages") ] . " after " . \ | 
					
						
							| 
									
										
										
										
											2022-05-10 15:08:30 +02:00
										 |  |  |       [ /system/resource/get uptime ] . " uptime." . [ $IfThenElse ($Duplicates = true) \ | 
					
						
							| 
									
										
										
										
											2021-04-27 20:45:51 +02:00
										 |  |  |       (" Multi-repeated messages have been skipped.") ] . "\n" . $Messages) }); | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:20:19 +02:00
										 |  |  |   :set LogForwardRateLimit ($LogForwardRateLimit + 10); | 
					
						
							| 
									
										
										
										
											2020-07-15 14:43:26 +02:00
										 |  |  |   :set LogForwardLast ($MessageVal->".id"); | 
					
						
							| 
									
										
										
										
											2020-09-28 20:20:19 +02:00
										 |  |  | } else={ | 
					
						
							|  |  |  |   :if ($LogForwardRateLimit > 0) do={ | 
					
						
							|  |  |  |     :set LogForwardRateLimit ($LogForwardRateLimit - 1); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-07-15 12:22:55 +02:00
										 |  |  | } |