-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnarwhal.py
652 lines (509 loc) · 19.7 KB
/
narwhal.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
from twisted.web.server import Site
from twisted.web.static import File
from twisted.internet import endpoints, reactor, task
from pri import PRI
import datetime
import os
import re
import socket
import time
import dateutil.parser
import msgpack
import numpy as np
# import orjson
import json
import redis
import tablib
import zstd
from klein import Klein
from klein import route
VERSION = "0.2"
DASH_LINE = "-----------------------------------------------------------------"
configuration = {
"SERVER_NAME": "Narwhal",
"REMOTE_REDIS_HOST": "localhost",
"REMOTE_REDIS_PORT": 6379,
"REDIS_MAIN_DB": 0,
"SYSLOG_CACHE_PROCESS_INTERVAL": 2,
"SYSLOG_CACHE_DB_NES": 1,
"COMPRESSION_TYPE": 3,
"NETWORK_INTERFACE": "0.0.0.0",
"DASHBOARD_WEB_PORT": 3000,
"ENABLE_HTTPS": "Y",
"PRIVATE_KEY": "localhost.pem",
"SEVERITY_TO_RETURN": "0 1 2 3",
"DASHBOARD_SHOW_HOURS": 4,
"DASHBOARD_DATA_REFRESH_SECONDS": 3,
"ENDPOINT_SYSLOG_TRANSMISSION_INTERVAL_SECONDS": 1,
"SEVERITY_PATTERN": "sev*"
}
LOG_MESSAGE_START = "Narwhal server started"
LOG_MESSAGE_STOP = "Narwhal server stopped"
# as per RFC 5424: https://tools.ietf.org/html/rfc5424
SEVERITY_CODE = [
"emerg",
"alert",
"crit",
"err",
"warning",
"notice",
"info",
"debug"
]
redis_main_db = ""
narwhal_log_facility = []
chart_severity = 0
def display_console_banner():
base_url = "https://" + \
configuration["NETWORK_INTERFACE"] + ":" + \
str(configuration["DASHBOARD_WEB_PORT"])
print(DASH_LINE)
print(" Narwhal server v." + VERSION)
print("Access dashboard at " + base_url)
print(DASH_LINE)
return
def load_env_variable(env_variable):
global configuration
env_variable_value = os.getenv(env_variable)
if env_variable_value:
print(env_variable_value)
configuration[env_variable] = env_variable_value
return
def load_configuration():
global configuration
try:
load_env_variable("SERVER_NAME")
load_env_variable("REMOTE_REDIS_HOST")
load_env_variable("REMOTE_REDIS_PORT")
load_env_variable("REDIS_MAIN_DB")
load_env_variable("SYSLOG_CACHE_PROCESS_INTERVAL")
load_env_variable("SYSLOG_CACHE_DB_NES")
load_env_variable("COMPRESSION_TYPE")
load_env_variable("NETWORK_INTERFACE")
load_env_variable("DASHBOARD_WEB_PORT")
load_env_variable("ENABLE_HTTPS")
load_env_variable("PRIVATE_KEY")
load_env_variable("SEVERITY_TO_RETURN")
load_env_variable("DASHBOARD_SHOW_HOURS")
load_env_variable("DASHBOARD_DATA_REFRESH_SECONDS")
load_env_variable("ENDPOINT_SYSLOG_TRANSMISSION_INTERVAL_SECONDS")
load_env_variable("SEVERITY_PATTERN")
except Exception:
print(DASH_LINE)
print("ERROR: Enviroment variable is not available.")
print(DASH_LINE)
raise
def redis_connect(redis, database):
global configuration
redis_connection = redis.Redis(
host=configuration["REMOTE_REDIS_HOST"],
port=configuration["REMOTE_REDIS_PORT"],
db=database)
return redis_connection
def decode_syslog_pri(pos):
line = int(pos)
if line < 192:
facility = PRI[line][0]
severity = PRI[line][1]
else:
facility = 999
severity = 99
return facility, severity
def narwhal_log(message):
global narwhal_log_facility
global configuration
ip = socket.gethostbyname(socket.gethostname())
narwhal_log_facility.append(
[
datetime.datetime.now().replace(microsecond=0).isoformat(),
ip,
5,
16,
ip,
configuration['SERVER_NAME'],
datetime.datetime.utcnow().isoformat(),
message
])
def syslog_cache_processor(redis_syslog_cache, redis_main_db):
global narwhal_log_facility
date_field = ""
redis_cache_data = np.array([])
dt = []
ip = []
endpoint = []
raw_message = []
redis_data = []
data = np.empty([8])
data_to_redis = []
cache_stored_keys = list(map(
decode_bytes, (redis_syslog_cache.hkeys("raw_message_block"))))
redis_syslog_cache_pipeline = redis_syslog_cache.pipeline()
for key in cache_stored_keys:
redis_syslog_cache_pipeline.hget("raw_message_block", key)
for compressed_data in redis_syslog_cache_pipeline.execute():
decompressed_data = zstd.decompress(compressed_data)
redis_cache_data = msgpack.unpackb(decompressed_data, raw=False)
for entry in redis_cache_data:
ip += entry['ip']
endpoint += entry['ep']
raw_message += entry['ms']
for key in cache_stored_keys:
redis_syslog_cache_pipeline.hdel("raw_message_block", key)
redis_syslog_cache_pipeline.execute()
for index in range(len(raw_message)):
if raw_message[index].startswith("<"):
facility, severity = decode_syslog_pri(
raw_message[index].split(">", 1)[0].strip("<"))
timestamp = re.search(
r"[A-Z][a-z]{2}\s{1,2}\d{1,2}(?:\s\d{4})?\s\d{2}[:]\d{2}[:]\d{2}(?:\.\d{1,6})?(?:\s[A-Z]{3})?",
raw_message[index].split(">", 1)[1],
).group()
right_part_of_the_raw_message = raw_message[index].split(
">", 1)[1].replace(timestamp, '')
try:
system, message = right_part_of_the_raw_message.split(
": ", 1)
except ValueError:
system = str(right_part_of_the_raw_message)
message = ""
new_date_field = timestamp[:16]
if date_field == "":
date_field = new_date_field
if new_date_field != date_field:
data = np.reshape(data, (-1, 8))
redis_main_db_pipeline = redis_main_db.pipeline()
for severity_key in range(8):
for index in range(len(data)):
if (data[index][2] == str(severity_key)):
data_to_redis.append(data[index].tolist())
if data_to_redis:
data_compressed = zstd.compress(
msgpack.packb(data_to_redis),
configuration['COMPRESSION_TYPE'])
compressed_records_count = len(data_to_redis)
redis_main_db_pipeline.hset(
severity_key, date_field, data_compressed)
redis_main_db_pipeline.hincrby(
severity_key, 'total', compressed_records_count)
keys_to_score = {date_field: compressed_records_count}
redis_main_db_pipeline.zadd(
(str(severity_key) + "T"), keys_to_score)
data_to_redis = []
redis_main_db_pipeline.execute()
date_field = new_date_field
data = np.empty([8])
data_row = np.array([timestamp[:16],
endpoint[index],
severity,
facility,
ip[index],
system,
timestamp,
message])
data = np.concatenate((data, data_row), axis=0)
if narwhal_log_facility:
for log_row in narwhal_log_facility:
data = np.concatenate((data, log_row), axis=0)
narwhal_log_facility.clear()
calculate_statistic(redis_main_db)
return
def available_severity_keys(redis_connection):
severity_keys = []
redis_pipeline = redis_connection.pipeline()
for index in range(7):
redis_pipeline.exists(str(index))
sev_key = 0
for item in redis_pipeline.execute():
if item:
severity_keys.append(str(sev_key))
sev_key += 1
return severity_keys
def calculate_statistic(redis_connection):
messages_by_severity = [0, 0, 0, 0, 0, 0, 0, 0]
severity_keys = available_severity_keys(redis_connection)
for severity_key in severity_keys:
messages_by_severity[int(severity_key)] = int((redis_connection.hget(
severity_key, 'total')).decode('UTF-8'))
print(" Messages by severity (1-8):" + str(messages_by_severity) +
" Total messages: " + str(sum(messages_by_severity)),
end="\r", flush=True)
return
def decode_bytes(item):
return item.decode('UTF-8')
def truncate_timestamp(item):
return item.decode('UTF-8')[:16]
def truncate_timestamp_for_chart(item):
return (item[:15] + "0")
def prepare_chart_data(item):
global chart_severity
return({"x": item[0].decode('UTF-8'), "y": chart_severity, "z": item[1].decode('UTF-8')})
def prepare_timeline(item):
return(item[0].decode('UTF-8'))
def respond_to_dashboard_data_request(redis_connection):
global chart_severity
dashboard = {}
dashboard['ChartData0'] = []
dashboard['ChartData1'] = []
dashboard['ChartData2'] = []
dashboard['ChartData3'] = []
dashboard['ChartData4'] = []
dashboard['ChartData5'] = []
dashboard['ChartData6'] = []
dashboard['ChartData7'] = []
timeline = []
events = []
messages_by_severity = [0, 0, 0, 0, 0, 0, 0, 0]
severity_keys = available_severity_keys(redis_connection)
for severity_key in severity_keys:
chart_severity = severity_key
events = redis_connection.zrange(
(str(severity_key)+"T"), 0, -1, withscores=True)
events = np.atleast_2d(events)
dashboard[('ChartData' + str(severity_key))
] = list(map(prepare_chart_data, events))
timeline += list(map(prepare_timeline, events))
messages_by_severity[int(severity_key)] = int((redis_connection.hget(
severity_key, "total")).decode('UTF-8'))
# timeline = np.reshape(timeline, (-1, 2))
timeline.sort()
first_timestamp = timeline[0]
last_timestamp = timeline[-1]
dashboard["total_events"] = sum(messages_by_severity)
dashboard["messages_per_second"] = 0
dashboard["seconds_between_messages"] = 0
# sev_key = []
# for severity in range(len(SEVERITY_CODE)):
# sev_key += SEVERITY_CODE[severity]
delta = (dateutil.parser.parse(
last_timestamp) -
dateutil.parser.parse(first_timestamp))
dashboard["messages_per_second"] = round(
(dashboard["total_events"] / delta.total_seconds()), 2
)
dashboard["seconds_between_messages"] = round(
(delta.total_seconds() / dashboard["total_events"]), 2
)
dashboard["logAlertCount"] = messages_by_severity[0] + \
messages_by_severity[1] + \
messages_by_severity[2] + messages_by_severity[3]
dashboard["logWarningsCount"] = messages_by_severity[4]
dashboard["logMessageCount"] = messages_by_severity[5] + \
messages_by_severity[6] + messages_by_severity[7]
redis_config = redis_connection.info("memory")
dashboard["redis_used_memory_human"] = redis_config["used_memory_human"]
dashboard["redis_used_memory"] = redis_config["used_memory"]
dashboard["redis_total_system_memory_human"] = redis_config[
"total_system_memory_human"
]
dashboard["redis_total_system_memory"] = redis_config[
"total_system_memory"
]
return json.dumps(dashboard)
def respond_to_events_data_request(redis_connection, events_to_return, mode):
global configuration
dt = []
endpoint = []
severity = []
facility = []
ip = []
system = []
time = []
event = []
timeline = np.array([])
timestamps = np.array([])
if events_to_return == "alerts":
severity_to_return = configuration['SEVERITY_TO_RETURN'].split()
if events_to_return == "all":
severity_to_return = list(available_severity_keys(redis_connection))
redis_pipeline = redis_connection.pipeline()
for severity_key in severity_to_return:
if events_to_return == "alerts":
severity_key = (str(severity_key))
redis_pipeline.hgetall(severity_key)
for redis_data in redis_pipeline.execute():
if redis_data:
redis_data_keys = redis_data.keys()
for key in redis_data_keys:
if key.decode() != "total":
data_block = np.reshape(
(msgpack.unpackb(zstd.decompress(redis_data[key]))),
(-1, 8))
for data in data_block:
dt.append(data[0].decode())
endpoint.append(data[1].decode())
severity.append(data[2].decode())
facility.append(data[3].decode())
ip.append(data[4].decode())
system.append(data[5].decode())
time.append(data[6].decode())
event.append(data[7].decode())
if mode == "json":
num = [str(n) for n in range(np.count_nonzero(dt))]
return (json.dumps({
"n": list(num),
"dt": dt,
"endpoint": endpoint,
"severity": severity,
"facility": facility,
"ip": ip,
"system": system,
"timestamp": time,
"event": event
}))
if mode == "csv":
csv_dataset = tablib.Dataset()
csv_dataset.append_col(list(dt), header='dt')
csv_dataset.append_col(list(endpoint), header='endpoint')
csv_dataset.append_col(list(severity), header='severity')
csv_dataset.append_col(list(facility), header='facility')
csv_dataset.append_col(list(ip), header='ip')
csv_dataset.append_col(list(system), header='system')
csv_dataset.append_col(list(time), header='timestamp')
csv_dataset.append_col(list(event), header='event')
return csv_dataset.export('csv')
def read_and_return_file(path):
file = ""
file_handler = open(path, "r")
for readIndexLine in file_handler:
file += readIndexLine
file_handler.close()
return file
nserv = Klein()
class NotFound(Exception):
pass
def enable_cors(request):
request.responseHeaders.addRawHeader("Access-Control-Allow-Origin", "*")
request.responseHeaders.addRawHeader("Access-Control-Allow-Methods",
"PUT, GET, POST, DELETE, OPTIONS")
request.responseHeaders.addRawHeader(
"Access-Control-Allow-Headers",
"Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token",
)
return request
@nserv.route("/")
def home(request):
return File("build/")
# @route('/')
# def home(request):
# return File("/build/")
@nserv.route("/static/", branch=True)
def static(request):
return File("build/static/")
@route("/src/")
def static(request):
return File("/src/")
# @nserv.route("/manifest.json", branch=True)
# def static(request):
# return File("/manifest.json")
# @nserv.route("/static/img/", branch=True)
# def img(request):
# return File("..narwhal/build/static/img")
# @nserv.route("/public/", branch=True)
# def static(request):
# return File("../narwhal/public")
@nserv.route("/dashboard", branch=True)
def return_dashboard_data(request):
request = enable_cors(request)
dashboard = respond_to_dashboard_data_request(redis_main_db)
data = respond_to_events_data_request(redis_main_db, "alerts", "json")
jsonMerged = {**json.loads(dashboard), **json.loads(data)}
return json.dumps(jsonMerged)
@nserv.route("/server_data")
def server_data_req(request):
request = enable_cors(request)
# return respond_to_dashboard_data_request(redis_main_db)
return respond_to_events_data_request(redis_main_db, "all", "json")
@nserv.route("/server_events")
def server_events_req(request):
request = enable_cors(request)
return respond_to_events_data_request(redis_main_db, "alerts", "json")
@nserv.route("/csv_alerts", branch=True)
def export_csv_alerts(request):
request.responseHeaders.addRawHeader(
b"content-type", b"application/csv")
request.responseHeaders.addRawHeader(b"content-disposition", b"attachment")
content = respond_to_events_data_request(redis_main_db, "alerts", "csv")
return content
@nserv.route("/csv_all", branch=True)
def export_csv_all(request):
request.responseHeaders.addRawHeader(
b"content-type", b"application/csv")
request.responseHeaders.addRawHeader(b"content-disposition", b"attachment")
content = respond_to_events_data_request(redis_main_db, "all", "csv")
return content
@nserv.route("/json_alerts", branch=True)
def export_json_alerts(request):
request.responseHeaders.addRawHeader(b"content-type", b"application/json")
content = respond_to_events_data_request(redis_main_db, "alerts", "json")
return content
@nserv.route("/json_all", branch=True)
def export_json_all(request):
request.responseHeaders.addRawHeader(b"content-type", b"application/json")
content = respond_to_events_data_request(redis_main_db, "all", "json")
return content
@nserv.handle_errors(NotFound)
@nserv.handle_errors(FileNotFoundError)
def error(self, request):
# request.setResponseCode(404)
error_file = ""
error_file_handler = open("build/error.html", "r")
for readErrorLine in error_file_handler:
error_file += readErrorLine
error_file_handler.close()
return error_file
def main_server_loop_failed(failure):
print("ERROR - main server loop failed:")
print(DASH_LINE)
print(failure.getBriefTraceback())
reactor.stop()
resource = nserv.resource
if __name__ == "__main__":
try:
load_configuration()
try:
display_console_banner()
redis_syslog_cache = redis_connect(
redis, configuration['SYSLOG_CACHE_DB_NES'])
redis_main_db = redis_connect(redis,
configuration["REDIS_MAIN_DB"])
connected_to_redis = True
print("Connected to Redis.")
except redis.ConnectionError:
connected_to_redis = False
print(DASH_LINE)
print("Error - check configuration or status of the Redis server.")
print(DASH_LINE)
if connected_to_redis:
narwhal_log(LOG_MESSAGE_START)
print("Processing cache...")
start_time = time.time()
syslog_cache_processor(redis_syslog_cache, redis_main_db)
print("Processed cache on server start in %s seconds. " %
round(time.time() - start_time))
if configuration["ENABLE_HTTPS"] == "Y":
endpoint_description = (
"ssl:" + str(configuration["DASHBOARD_WEB_PORT"]) +
":interface=" + configuration["NETWORK_INTERFACE"] +
":privateKey="+configuration["PRIVATE_KEY"])
else:
endpoint_description = ("tcp:port=" + str(configuration["DASHBOARD_WEB_PORT"]) +
":interface=" + configuration["NETWORK_INTERFACE"])
endpoint = endpoints.serverFromString(reactor,
endpoint_description)
endpoint.listen(Site(nserv.resource()))
syslog_cache_processor_task = task.LoopingCall(
syslog_cache_processor, redis_syslog_cache, redis_main_db)
main_server_processor_loop = syslog_cache_processor_task.start(
configuration["SYSLOG_CACHE_PROCESS_INTERVAL"])
main_server_processor_loop.addErrback(main_server_loop_failed)
# reactor.suggestThreadPoolSize(30)
reactor.run()
except (IOError, SystemExit):
raise
except KeyboardInterrupt:
narwhal_log(LOG_MESSAGE_STOP)
print("Shutting down server...")
syslog_cache_processor(redis_syslog_cache, redis_main_db)
reactor.callFromThread(reactor.stop)
# reactor.stop
print("done.")