-
Notifications
You must be signed in to change notification settings - Fork 1
/
Alert2Telegram.rsc
126 lines (111 loc) · 4.73 KB
/
Alert2Telegram.rsc
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Script for log's alert (to Telegram)
# Written by Konkere <amorph@konkere.ru>
# Tested on MikroTik RouterOS 6.48.6 long-term (CHR and RouterBoard)
:global TlgrmBotID
:global TlgrmChatIDlog
:local SchedulerName "Alert2Telegram"
:local CurrentDate [/system clock get date]
:local CurrentHour [:pick [/system clock get time] 0 2]
:local GMToffset [:totime [/system clock get gmt-offset ]]
:local YesterdayDate [/system scheduler get [find name="YesterdayDate"] comment]
# Topics for alert
:local Messages [:toarray [/log find topics~"warning" || topics~"critical" || topics~"error" || topics~"firewall"]]
# Patterns to ignore messages with text
:local MessagesIgnore {"First ignore";"Second ignore"}
:local LastAlertTime [/system scheduler get [find name="$SchedulerName"] comment]
# Trigger for convertation in output messages jan/20/1970 00:00:00 to 00:00:00 20.01.1970
# set false for default view
:local ConvertDateTime true
# Display name of device in output messsage
# set true to show
:local DeviceNameToOutput false
:local DeviceName
:local MessageDateTime
:local message
:local output
:local NewLogs false
:local count 0
# Function for convert jan/20/1970 00:00:00 to 00:00:00 20.01.1970
:local DefConvertTime do={
if ($convertDT = true) do={
:local arrayMonths {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"}
:local mDate [:pick $MessageDT 4 6]
:local mYear [:pick $MessageDT 7 11]
:local mTime [:pick $MessageDT 12 20]
:local mMonth ($arrayMonths->[:pick $MessageDT 0 3])
:local MessageDT "$mTime $mDate.$mMonth.$mYear"
:return $MessageDT
} else={
:return $MessageDT
}
}
:if ([:len $LastAlertTime] = 0) do={
:set NewLogs true
}
if ( [:len $GMToffset] != 8 ) do={
# Convert negative GMToffset to readable (7101w3d03:28:16 to -03)
:set $GMToffset [pick [:totime ([/system clock get gmt-offset ] - 4294967296)] 0 3]
} else={
:set $GMToffset [:pick [$GMToffset] 0 2]
}
:if ([:len [/system scheduler find name="$SchedulerName"]] = 0) do={
/log warning "$SchedulerName does not exist, create scheduler first"
}
# Messages loop
:foreach MessageCheck in=$Messages do={
:local LogEntry true
:foreach MessageIgnore in=$MessagesIgnore do={
:if ([/log get $MessageCheck message] ~ "$MessageIgnore") do={
:set LogEntry false
}
}
:if ($LogEntry = true) do={
:set message [/log get $MessageCheck message]
# Log date jan/20/1970 00:00:00 (full and default)
:set MessageDateTime [/log get $MessageCheck time]
# Log date 00:00:00
:if ([:len $MessageDateTime] = 8) do={
if ($CurrentHour >= $GMToffset) do={
# Current date format
:set MessageDateTime ([:pick [/system clock get date] 0 11]." ".$MessageDateTime)
} else={
# Yesterday date format (from 00 to GMToffset hours, BUG with GMT+offset)
:set MessageDateTime ($YesterdayDate." ".$MessageDateTime)
}
} else={
# Log date jan/20 00:00:00 for yesterday and today (from 00 to GMToffset hours)
:if ([:len $MessageDateTime] = 15 ) do={
:set MessageDateTime ([:pick $MessageDateTime 0 6]."/".[:pick [/system clock get date] 7 11]." ".[:pick $MessageDateTime 7 15])
}
}
:if ($NewLogs = true) do={
:set output ($output.[$DefConvertTime MessageDT=$MessageDateTime convertDT=$ConvertDateTime]."%0A%F0%9F%9A%A9".$message."%0A%0A")
}
:if ($MessageDateTime = $LastAlertTime) do={
:set NewLogs true
:set output ""
}
}
:if ($count = ([:len $Messages]-1)) do={
:if ($NewLogs = false) do={
:if ([:len $message] > 0) do={
:set output ($output.[$DefConvertTime MessageDT=$MessageDateTime convertDT=$ConvertDateTime]."%0A%F0%9F%9A%A9".$message."%0A%0A")
}
}
}
:set count ($count + 1)
}
# Update var YesterdayDate in scheduler's comment (if YesterdayDate not used at current time)
if (($CurrentHour >= $GMToffset) && ($YesterdayDate != $CurrentDate)) do={
/system scheduler set [find name="YesterdayDate"] comment=$CurrentDate
}
if ($DeviceNameToOutput = true) do={
:set DeviceName ("at %F0%9F%93%B6".[/system identity get name])
}
if ([:len $output] > 0) do={
#Final output message for send
:set output ("%E2%9D%97Alert%E2%9D%97".$DeviceName."%0A%0A".$output)
/system scheduler set [find name="$SchedulerName"] comment=$MessageDateTime
/tool fetch url="https://api.telegram.org/bot$TlgrmBotID/sendmessage?chat_id=$TlgrmChatIDlog&text=$output" keep-result=no;
/log info "$SchedulerName - New logs found, send to Telegram"
}