-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinspector.py
More file actions
610 lines (518 loc) · 21.2 KB
/
inspector.py
File metadata and controls
610 lines (518 loc) · 21.2 KB
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
import importlib
import os
import sys
import uuid
from datetime import datetime
from enum import Enum, unique
import marshmallow_dataclass
import numpy as np
from streamad.util import StreamGenerator, CustomDS
sys.path.append(os.getcwd())
from src.base.clickhouse_kafka_sender import ClickHouseKafkaSender
from src.base.data_classes.batch import Batch
from src.base.utils import setup_config
from src.base.kafka_handler import (
ExactlyOnceKafkaConsumeHandler,
ExactlyOnceKafkaProduceHandler,
KafkaMessageFetchException,
)
from src.base.log_config import get_logger
module_name = "data_inspection.inspector"
logger = get_logger(module_name)
config = setup_config()
MODE = config["pipeline"]["data_inspection"]["inspector"]["mode"]
ENSEMBLE = config["pipeline"]["data_inspection"]["inspector"]["ensemble"]
MODELS = config["pipeline"]["data_inspection"]["inspector"]["models"]
ANOMALY_THRESHOLD = config["pipeline"]["data_inspection"]["inspector"][
"anomaly_threshold"
]
SCORE_THRESHOLD = config["pipeline"]["data_inspection"]["inspector"]["score_threshold"]
TIME_TYPE = config["pipeline"]["data_inspection"]["inspector"]["time_type"]
TIME_RANGE = config["pipeline"]["data_inspection"]["inspector"]["time_range"]
CONSUME_TOPIC = config["environment"]["kafka_topics"]["pipeline"][
"prefilter_to_inspector"
]
PRODUCE_TOPIC = config["environment"]["kafka_topics"]["pipeline"][
"inspector_to_detector"
]
KAFKA_BROKERS = ",".join(
[
f"{broker['hostname']}:{broker['port']}"
for broker in config["environment"]["kafka_brokers"]
]
)
VALID_UNIVARIATE_MODELS = [
"KNNDetector",
"SpotDetector",
"SRDetector",
"ZScoreDetector",
"OCSVMDetector",
"MadDetector",
"SArimaDetector",
]
VALID_MULTIVARIATE_MODELS = [
"xStreamDetector",
"RShashDetector",
"HSTreeDetector",
"LodaDetector",
"OCSVMDetector",
"RrcfDetector",
]
VALID_ENSEMBLE_MODELS = ["WeightEnsemble", "VoteEnsemble"]
STATIC_ZEROS_UNIVARIATE = np.zeros((100, 1))
STATIC_ZEROS_MULTIVARIATE = np.zeros((100, 2))
@unique
class EnsembleModels(str, Enum):
WEIGHT = "WeightEnsemble"
VOTE = "VoteEnsemble"
class Inspector:
"""Finds anomalies in a batch of requests and produces it to the ``Detector``."""
def __init__(self) -> None:
self.batch_id = None
self.X = None
self.key = None
self.begin_timestamp = None
self.end_timestamp = None
self.messages = []
self.anomalies = []
self.kafka_consume_handler = ExactlyOnceKafkaConsumeHandler(CONSUME_TOPIC)
self.kafka_produce_handler = ExactlyOnceKafkaProduceHandler()
# databases
self.batch_timestamps = ClickHouseKafkaSender("batch_timestamps")
self.suspicious_batch_timestamps = ClickHouseKafkaSender(
"suspicious_batch_timestamps"
)
self.suspicious_batches_to_batch = ClickHouseKafkaSender(
"suspicious_batches_to_batch"
)
self.logline_timestamps = ClickHouseKafkaSender("logline_timestamps")
self.fill_levels = ClickHouseKafkaSender("fill_levels")
self.fill_levels.insert(
dict(
timestamp=datetime.now(),
stage=module_name,
entry_type="total_loglines",
entry_count=0,
)
)
def get_and_fill_data(self) -> None:
"""Consumes data from KafkaConsumeHandler and stores it for processing."""
if self.messages:
logger.warning(
"Inspector is busy: Not consuming new messages. Wait for the Inspector to finish the "
"current workload."
)
return
key, data = self.kafka_consume_handler.consume_as_object()
if data:
self.batch_id = data.batch_id
self.begin_timestamp = data.begin_timestamp
self.end_timestamp = data.end_timestamp
self.messages = data.data
self.key = key
self.batch_timestamps.insert(
dict(
batch_id=self.batch_id,
stage=module_name,
status="in_process",
timestamp=datetime.now(),
is_active=True,
message_count=len(self.messages),
)
)
self.fill_levels.insert(
dict(
timestamp=datetime.now(),
stage=module_name,
entry_type="total_loglines",
entry_count=len(self.messages),
)
)
if not self.messages:
logger.info(
"Received message:\n"
f" ⤷ Empty data field: No unfiltered data available. Belongs to subnet_id {key}."
)
else:
logger.info(
"Received message:\n"
f" ⤷ Contains data field of {len(self.messages)} message(s). Belongs to subnet_id {key}."
)
def clear_data(self):
"""Clears the data in the internal data structures."""
self.messages = []
self.anomalies = []
self.X = []
self.begin_timestamp = None
self.end_timestamp = None
logger.debug("Cleared messages and timestamps. Inspector is now available.")
def _mean_packet_size(self, messages: list, begin_timestamp, end_timestamp):
"""Returns mean of packet size of messages between two timestamps given a time step.
By default, 1 ms time step is applied. Time steps are adjustable by "time_type" and "time_range"
in config.yaml.
Args:
messages (list): Messages from KafkaConsumeHandler.
begin_timestamp (datetime): Begin timestamp of batch.
end_timestamp (datetime): End timestamp of batch.
Returns:
numpy.ndarray: 2-D numpy.ndarray including all steps.
"""
logger.debug("Convert timestamps to numpy datetime64")
timestamps = np.array(
[
np.datetime64(datetime.fromisoformat(item["timestamp"]))
for item in messages
]
)
# Extract and convert the size values from "111b" to integers
sizes = np.array([int(str(item["size"]).replace("b", "")) for item in messages])
logger.debug("Sort timestamps and count occurrences")
sorted_indices = np.argsort(timestamps)
timestamps = timestamps[sorted_indices]
sizes = sizes[sorted_indices]
logger.debug("Set min_date and max_date")
min_date = np.datetime64(begin_timestamp)
max_date = np.datetime64(end_timestamp)
logger.debug(
"Generate the time range from min_date to max_date with 1ms interval"
)
time_range = np.arange(
min_date,
max_date + np.timedelta64(TIME_RANGE, TIME_TYPE),
np.timedelta64(TIME_RANGE, TIME_TYPE),
)
logger.debug(
"Initialize an array to hold counts for each timestamp in the range"
)
counts = np.zeros(time_range.shape, dtype=np.float64)
size_sums = np.zeros(time_range.shape, dtype=np.float64)
mean_sizes = np.zeros(time_range.shape, dtype=np.float64)
# Handle empty messages.
if len(messages) > 0:
logger.debug(
"Count occurrences of timestamps and fill the corresponding index in the counts array"
)
_, unique_indices, unique_counts = np.unique(
timestamps, return_index=True, return_counts=True
)
# Sum the sizes at each unique timestamp
for idx, count in zip(unique_indices, unique_counts):
time_index = (
((timestamps[idx] - min_date) // TIME_RANGE)
.astype(f"timedelta64[{TIME_TYPE}]")
.astype(int)
)
size_sums[time_index] = np.sum(sizes[idx : idx + count])
counts[time_index] = count
# Calculate the mean packet size for each millisecond (ignore division by zero warnings)
with np.errstate(divide="ignore", invalid="ignore"):
mean_sizes = np.divide(
size_sums, counts, out=np.zeros_like(size_sums), where=counts != 0
)
else:
logger.warning("Empty messages to inspect.")
logger.debug("Reshape into the required shape (n, 1)")
return mean_sizes.reshape(-1, 1)
def _count_errors(self, messages: list, begin_timestamp, end_timestamp):
"""Counts occurances of messages between two timestamps given a time step.
By default, 1 ms time step is applied. Time steps are adjustable by "time_type" and "time_range"
in config.yaml.
Args:
messages (list): Messages from KafkaConsumeHandler.
begin_timestamp (datetime): Begin timestamp of batch.
end_timestamp (datetime): End timestamp of batch.
Returns:
numpy.ndarray: 2-D numpy.ndarray including all steps.
"""
logger.debug("Convert timestamps to numpy datetime64")
timestamps = np.array(
[
np.datetime64(datetime.fromisoformat(item["timestamp"]))
for item in messages
]
)
logger.debug("Sort timestamps and count occurrences")
sorted_indices = np.argsort(timestamps)
timestamps = timestamps[sorted_indices]
logger.debug("Set min_date and max_date")
min_date = np.datetime64(begin_timestamp)
max_date = np.datetime64(end_timestamp)
logger.debug(
"Generate the time range from min_date to max_date with 1ms interval"
)
# Adding np.timedelta adds end time to time_range
time_range = np.arange(
min_date,
max_date + np.timedelta64(TIME_RANGE, TIME_TYPE),
np.timedelta64(TIME_RANGE, TIME_TYPE),
)
logger.debug(
"Initialize an array to hold counts for each timestamp in the range"
)
counts = np.zeros(time_range.shape, dtype=np.float64)
# Handle empty messages.
if len(messages) > 0:
logger.debug(
"Count occurrences of timestamps and fill the corresponding index in the counts array"
)
unique_times, _, unique_counts = np.unique(
timestamps, return_index=True, return_counts=True
)
time_indices = (
((unique_times - min_date) // TIME_RANGE)
.astype(f"timedelta64[{TIME_TYPE}]")
.astype(int)
)
counts[time_indices] = unique_counts
else:
logger.warning("Empty messages to inspect.")
logger.debug("Reshape into the required shape (n, 1)")
return counts.reshape(-1, 1)
def inspect(self):
"""Runs anomaly detection on given StreamAD Model on either univariate, multivariate data, or as an ensemble."""
if MODELS == None or len(MODELS) == 0:
logger.warning("No model ist set!")
raise NotImplementedError(f"No model is set!")
if len(MODELS) > 1:
logger.warning(
f"Model List longer than 1. Only the first one is taken: {MODELS[0]['model']}!"
)
self._get_models(MODELS)
match MODE:
case "univariate":
self._inspect_univariate()
case "multivariate":
self._inspect_multivariate()
case "ensemble":
self._get_ensemble()
self._inspect_ensemble()
case _:
logger.warning(f"Mode {MODE} is not supported!")
raise NotImplementedError(f"Mode {MODE} is not supported!")
def _inspect_multivariate(self):
"""
Method to inspect multivariate data for anomalies using a StreamAD Model
Errors are count in the time window and fit model to retrieve scores.
Args:
model (str): Model name (should be capable of handling multivariate data)
"""
logger.debug("Inspecting data...")
X_1 = self._mean_packet_size(
self.messages, self.begin_timestamp, self.end_timestamp
)
X_2 = self._count_errors(
self.messages, self.begin_timestamp, self.end_timestamp
)
self.X = np.concatenate((X_1, X_2), axis=1)
# TODO Append zeros to avoid issues when model is reused.
# self.X = np.vstack((STATIC_ZEROS_MULTIVARIATE, X))
ds = CustomDS(self.X, self.X)
stream = StreamGenerator(ds.data)
for x in stream.iter_item():
score = self.models[0].fit_score(x)
# noqa
if score != None:
self.anomalies.append(score)
else:
self.anomalies.append(0)
def _inspect_ensemble(self):
"""
Method to inspect data for anomalies using ensembles of two StreamAD models
Errors are count in the time window and fit model to retrieve scores.
"""
self.X = self._count_errors(
self.messages, self.begin_timestamp, self.end_timestamp
)
# TODO Append zeros to avoid issues when model is reused.
# self.X = np.vstack((STATIC_ZEROS_UNIVARIATE, X))
ds = CustomDS(self.X, self.X)
stream = StreamGenerator(ds.data)
for x in stream.iter_item():
scores = []
# Fit all models in ensemble
for model in self.models:
scores.append(model.fit_score(x))
# TODO Calibrators are missing
score = self.ensemble.ensemble(scores)
# noqa
if score != None:
self.anomalies.append(score)
else:
self.anomalies.append(0)
def _inspect_univariate(self):
"""Runs anomaly detection on given StreamAD Model on univariate data.
Errors are count in the time window and fit model to retrieve scores.
Args:
model (str): StreamAD model name.
"""
logger.debug("Inspecting data...")
self.X = self._count_errors(
self.messages, self.begin_timestamp, self.end_timestamp
)
# TODO Append zeros to avoid issues when model is reused.
# self.X = np.vstack((STATIC_ZEROS_UNIVARIATE, X))
ds = CustomDS(self.X, self.X)
stream = StreamGenerator(ds.data)
for x in stream.iter_item():
score = self.models[0].fit_score(x)
# noqa
if score is not None:
self.anomalies.append(score)
else:
self.anomalies.append(0)
def _get_models(self, models):
if hasattr(self, "models") and self.models != None and self.models != []:
logger.info("All models have been successfully loaded!")
return
self.models = []
for model in models:
if MODE == "univariate" or MODE == "ensemble":
logger.debug(f"Load Model: {model['model']} from {model['module']}.")
if not model["model"] in VALID_UNIVARIATE_MODELS:
logger.error(
f"Model {models} is not a valid univariate or ensemble model."
)
raise NotImplementedError(
f"Model {models} is not a valid univariate or ensemble model."
)
if MODE == "multivariate":
logger.debug(f"Load Model: {model['model']} from {model['module']}.")
if not model["model"] in VALID_MULTIVARIATE_MODELS:
logger.error(f"Model {model} is not a valid multivariate model.")
raise NotImplementedError(
f"Model {model} is not a valid multivariate model."
)
module = importlib.import_module(model["module"])
module_model = getattr(module, model["model"])
self.models.append(module_model(**model["model_args"]))
def _get_ensemble(self):
logger.debug(f"Load Model: {ENSEMBLE['model']} from {ENSEMBLE['module']}.")
if not ENSEMBLE["model"] in VALID_ENSEMBLE_MODELS:
logger.error(f"Model {ENSEMBLE} is not a valid ensemble model.")
raise NotImplementedError(
f"Model {ENSEMBLE} is not a valid ensemble model."
)
if hasattr(self, "ensemble") and self.ensemble != None:
logger.info("Ensemble have been successfully loaded!")
return
module = importlib.import_module(ENSEMBLE["module"])
module_model = getattr(module, ENSEMBLE["model"])
self.ensemble = module_model(**ENSEMBLE["model_args"])
def send_data(self):
"""Pass the anomalous data for the detector unit for further processing"""
total_anomalies = np.count_nonzero(
np.greater_equal(np.array(self.anomalies), SCORE_THRESHOLD)
)
if total_anomalies / len(self.X) > ANOMALY_THRESHOLD: # subnet is suspicious
logger.info("Sending anomalies to detector for further analysis.")
buckets = {}
for message in self.messages:
if message["client_ip"] in buckets.keys():
buckets[message["client_ip"]].append(message)
else:
buckets[message["client_ip"]] = []
buckets.get(message["client_ip"]).append(message)
for key, value in buckets.items():
logger.info(f"Sending anomalies to detector for {key}.")
logger.info(f"Sending anomalies to detector for {value}.")
suspicious_batch_id = uuid.uuid4() # generate new suspicious_batch_id
self.suspicious_batches_to_batch.insert(
dict(
suspicious_batch_id=suspicious_batch_id,
batch_id=self.batch_id,
)
)
data_to_send = {
"batch_id": suspicious_batch_id,
"begin_timestamp": self.begin_timestamp,
"end_timestamp": self.end_timestamp,
"data": value,
}
batch_schema = marshmallow_dataclass.class_schema(Batch)()
self.suspicious_batch_timestamps.insert(
dict(
suspicious_batch_id=suspicious_batch_id,
client_ip=key,
stage=module_name,
status="finished",
timestamp=datetime.now(),
is_active=True,
message_count=len(value),
)
)
self.kafka_produce_handler.produce(
topic=PRODUCE_TOPIC,
data=batch_schema.dumps(data_to_send),
key=key,
)
else: # subnet is not suspicious
self.batch_timestamps.insert(
dict(
batch_id=self.batch_id,
stage=module_name,
status="filtered_out",
timestamp=datetime.now(),
is_active=False,
message_count=len(self.messages),
)
)
logline_ids = set()
for message in self.messages:
logline_ids.add(message["logline_id"])
for logline_id in logline_ids:
self.logline_timestamps.insert(
dict(
logline_id=logline_id,
stage=module_name,
status="filtered_out",
timestamp=datetime.now(),
is_active=False,
)
)
self.fill_levels.insert(
dict(
timestamp=datetime.now(),
stage=module_name,
entry_type="total_loglines",
entry_count=0,
)
)
def main(one_iteration: bool = False):
"""
Creates the :class:`Inspector` instance. Starts a loop that continuously fetches data. Actual functionality
follows.
Args:
one_iteration (bool): For testing purposes: stops loop after one iteration
Raises:
KeyboardInterrupt: Execution interrupted by user. Closes down the :class:`LogCollector` instance.
"""
logger.info("Starting Inspector...")
inspector = Inspector()
logger.info(f"Inspector is running.")
iterations = 0
while True:
if one_iteration and iterations > 0:
break
iterations += 1
try:
logger.debug("Before getting and filling data")
inspector.get_and_fill_data()
logger.debug("After getting and filling data")
logger.debug("Start anomaly detection")
inspector.inspect()
logger.debug("Send data to detector")
inspector.send_data()
except KafkaMessageFetchException as e: # pragma: no cover
logger.debug(e)
except IOError as e:
logger.error(e)
raise e
except ValueError as e:
logger.debug(e)
except KeyboardInterrupt:
logger.info("Closing down Inspector...")
break
finally:
inspector.clear_data()
if __name__ == "__main__": # pragma: no cover
main()