generated from fuchs-fabian/simbashlog-example-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simbashlog_matrix_notifier.py
232 lines (189 loc) · 8.45 KB
/
simbashlog_matrix_notifier.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
'''
NOTES:
For more information:
- https://github.com/fuchs-fabian/matrix-notify-py
'''
import sys
from enum import Enum
import simbashlog_notify_helper as snh # type: ignore
# simbashlog-notifier specific imports
import matrix_notify as mn # type: ignore
SIMBASHLOG_NOTIFIER_NAME = "simbashlog-matrix-notifier"
class NotifierConfigField(Enum):
# General
MIN_REQUIRED_LOG_LEVEL = "min_required_log_level"
SHOW_IN_CONSOLE_SENT_MESSAGE = "show_in_console_sent_message"
# Header
SHOW_IN_HEADER_PID = "show_in_header_pid"
# Body
SHOW_IN_BODY_LOG_FILE_RESULT = "show_in_body_log_file_result"
SHOW_IN_BODY_LOG_FILE_CONTENT = "show_in_body_log_file_content"
SHOW_IN_BODY_SUMMARY_FOR_PID = "show_in_body_summary_for_pid"
SHOW_IN_BODY_SUMMARY_FOR_LOG_FILE = "show_in_body_summary_for_log_file"
# Footer
SHOW_IN_FOOTER_LOG_FILE_NAMES = "show_in_footer_log_file_names"
SHOW_IN_FOOTER_HOST = "show_in_footer_host"
SHOW_IN_FOOTER_NOTIFIER_NAME = "show_in_footer_notifier_name"
# Notifier specific
USE_E2E = "use_e2e"
ROOM_ID = "room_id"
ACCESS_TOKEN = "access_token"
HOMESERVER_URL = "homeserver_url"
def __str__(self):
return self.value
def load_config() -> snh.NotifierConfig:
try:
config_data = snh.NotifierConfig.get_data(SIMBASHLOG_NOTIFIER_NAME, NotifierConfigField)
except Exception as e:
print(f"An error occurred while the config file was being processed:\n{e}")
sys.exit(1)
return snh.NotifierConfig(
# General
min_required_log_level=config_data[
NotifierConfigField.MIN_REQUIRED_LOG_LEVEL.value
],
show_in_console_sent_message=config_data[
NotifierConfigField.SHOW_IN_CONSOLE_SENT_MESSAGE.value
].lower() == "true",
# Header
show_in_header_pid=config_data[
NotifierConfigField.SHOW_IN_HEADER_PID.value
].lower() == "true",
# Body
show_in_body_log_file_result=config_data[
NotifierConfigField.SHOW_IN_BODY_LOG_FILE_RESULT.value
].lower() == "true",
show_in_body_log_file_content=config_data[
NotifierConfigField.SHOW_IN_BODY_LOG_FILE_CONTENT.value
].lower() == "true",
show_in_body_summary_for_pid=config_data[
NotifierConfigField.SHOW_IN_BODY_SUMMARY_FOR_PID.value
].lower() == "true",
show_in_body_summary_for_log_file=config_data[
NotifierConfigField.SHOW_IN_BODY_SUMMARY_FOR_LOG_FILE.value
].lower() == "true",
# Footer
show_in_footer_log_file_names=config_data[
NotifierConfigField.SHOW_IN_FOOTER_LOG_FILE_NAMES.value
].lower() == "true",
show_in_footer_host=config_data[
NotifierConfigField.SHOW_IN_FOOTER_HOST.value
].lower() == "true",
show_in_footer_notifier_name=config_data[
NotifierConfigField.SHOW_IN_FOOTER_NOTIFIER_NAME.value
].lower() == "true",
# Notifier specific
use_e2e=config_data[
NotifierConfigField.USE_E2E.value
].lower() == "true",
room_id=config_data[
NotifierConfigField.ROOM_ID.value
],
access_token=config_data[
NotifierConfigField.ACCESS_TOKEN.value
],
homeserver_url=config_data[
NotifierConfigField.HOMESERVER_URL.value
]
)
def get_min_required_log_level(config: snh.NotifierConfig) -> int:
if config.min_required_log_level is not None:
min_required_log_level = int(config.min_required_log_level)
if not min_required_log_level in range(0, 8):
print(f"Invalid minimum required log level'{min_required_log_level}'")
sys.exit(1)
return min_required_log_level
return 7
def create_message(config: snh.NotifierConfig, stored_log_info: snh.StoredLogInfo) -> str:
builder = snh.MessageBuilder(
stored_log_info = stored_log_info,
notifier_name = SIMBASHLOG_NOTIFIER_NAME,
apply_heading = lambda content: mn.Helper.HTML.Tag.H3.format(content),
apply_subheading = lambda content: mn.Helper.HTML.Tag.H4.format(content),
apply_paragraph = lambda content: mn.Helper.HTML.Tag.PARAGRAPH.format(content),
apply_code = lambda content: mn.Helper.HTML.Tag.CODE.format(content),
apply_bold = lambda content: mn.Helper.HTML.Tag.BOLD.format(content),
apply_italic = lambda content: mn.Helper.HTML.Tag.ITALIC.format(content)
)
builder.add_header(
show_pid=config.show_in_header_pid
).add_body(
show_log_file_result=config.show_in_body_log_file_result,
show_log_file_content=config.show_in_body_log_file_content,
show_summary_for_pid=config.show_in_body_summary_for_pid,
show_summary_for_log_file=config.show_in_body_summary_for_log_file
).add_footer(
show_log_file_names=config.show_in_footer_log_file_names,
show_host=config.show_in_footer_host,
show_notifier_name=config.show_in_footer_notifier_name
)
return builder.build()
def perform_action(config: snh.NotifierConfig, stored_log_info: snh.StoredLogInfo, message: str):
notifier = mn.MatrixNotifier(
room_id=config.room_id,
access_token=config.access_token,
homeserver_url=config.homeserver_url
)
html_message = mn.Helper.HTML.replace_spaces_and_new_lines(message)
if config.show_in_console_sent_message:
notifier.send(html_message, config.use_e2e)
else:
with snh.suppress_output():
notifier.send(html_message, config.use_e2e)
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
# ░░ ░░
# ░░ ░░
# ░░ DO NOT MODIFY ANYTHING BELOW! ░░
# ░░ ░░
# ░░ ░░
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
@snh.unexpected_exception_handler
def notify(config: snh.NotifierConfig, stored_log_info: snh.StoredLogInfo):
# Check if there are any log entries
if len(stored_log_info.data_df) == 0:
print("No matching log entries found. Nothing to notify.")
sys.exit(0)
# Create message
message = create_message(config, stored_log_info)
# Perform action
perform_action(config, message)
@snh.unexpected_exception_handler
def filter_log_data_by_min_required_log_level(config: snh.NotifierConfig, stored_log_info: snh.StoredLogInfo) -> None:
min_required_log_level = get_min_required_log_level(config)
if min_required_log_level == 7:
return
initial_count = len(stored_log_info.data_df)
stored_log_info.data_df[snh.DataFrameField.SEVERITY_CODE.value] = stored_log_info.data_df[snh.LogField.LEVEL.value].apply(
lambda level_name: snh.Severity.get_by_name(level_name).rfc_5424_numerical_code
)
stored_log_info.data_df.drop(
stored_log_info.data_df[stored_log_info.data_df[snh.DataFrameField.SEVERITY_CODE.value] > min_required_log_level].index,
inplace=True
)
stored_log_info.data_df.drop(columns=[snh.DataFrameField.SEVERITY_CODE.value], inplace=True)
final_count = len(stored_log_info.data_df)
# Set count for severity to 0 if the severity is greater than the minimum required log level
for column in stored_log_info.summary_df.columns:
try:
if column == snh.DataFrameField.PID.value:
continue
severity_code = snh.Severity.get_by_name(column).rfc_5424_numerical_code
if severity_code > min_required_log_level:
stored_log_info.summary_df[column] = 0
except AttributeError:
continue
if initial_count != final_count:
print(f"Ignored {initial_count - final_count} log entries with severity greater than {min_required_log_level} (Original count: {initial_count} | Final count: {final_count})")
@snh.unexpected_exception_handler
def main():
# Load configuration
config = load_config()
# Process command-line arguments
stored_log_info = snh.process_arguments()
# Filter log data by minimum required log level
filter_log_data_by_min_required_log_level(config, stored_log_info)
# Notify
notify(config, stored_log_info)
if __name__ == "__main__":
main()