forked from logpai/Drain3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drain_stdin_demo.py
74 lines (60 loc) · 2.36 KB
/
drain_stdin_demo.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
# SPDX-License-Identifier: MIT
import json
import logging
import sys
from os.path import dirname
from drain3 import TemplateMiner
from drain3.template_miner_config import TemplateMinerConfig
# persistence_type = "NONE"
# persistence_type = "REDIS"
# persistence_type = "KAFKA"
persistence_type = "FILE"
logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(message)s')
if persistence_type == "KAFKA":
from drain3.kafka_persistence import KafkaPersistence
persistence = KafkaPersistence("drain3_state", bootstrap_servers="localhost:9092")
elif persistence_type == "FILE":
from drain3.file_persistence import FilePersistence
persistence = FilePersistence("drain3_state.bin")
elif persistence_type == "REDIS":
from drain3.redis_persistence import RedisPersistence
persistence = RedisPersistence(redis_host='',
redis_port=25061,
redis_db=0,
redis_pass='',
is_ssl=True,
redis_key="drain3_state_key")
else:
persistence = None
config = TemplateMinerConfig()
config.load(dirname(__file__) + "/drain3.ini")
config.profiling_enabled = False
template_miner = TemplateMiner(persistence, config)
print(f"Drain3 started with '{persistence_type}' persistence")
print(f"Starting training mode. Reading from std-in ('q' to finish)")
while True:
log_line = input("> ")
if log_line == 'q':
break
result = template_miner.add_log_message(log_line)
result_json = json.dumps(result)
print(result_json)
template = result["template_mined"]
params = template_miner.extract_parameters(template, log_line)
print("Parameters: " + str(params))
print("Training done. Mined clusters:")
for cluster in template_miner.drain.clusters:
print(cluster)
print(f"Starting inference mode, matching to pre-trained clusters. Input log lines or 'q' to finish")
while True:
log_line = input("> ")
if log_line == 'q':
break
cluster = template_miner.match(log_line)
if cluster is None:
print(f"No match found")
else:
template = cluster.get_template()
print(f"Matched template #{cluster.cluster_id}: {template}")
print(f"Parameters: {template_miner.get_parameter_list(template, log_line)}")