Drain3
A robust streaming log template miner based on the Drain algorithm
Install / Use
/learn @logpai/Drain3README
Drain3
Important Update
Drain3 was moved to the logpai GitHub organization (which is also the home for the original Drain implementation). We always welcome more contributors and maintainers to join us and push the project forward. We welcome more contributions and variants of implementations if you find practical enhancements to the algorithm in production scenarios.
Introduction
Drain3 is an online log template miner that can extract templates (clusters) from a stream of log messages in a timely manner. It employs a parse tree with fixed depth to guide the log group search process, which effectively avoids constructing a very deep and unbalanced tree.
Drain3 continuously learns on-the-fly and extracts log templates from raw log entries.
Example:
For the input:
connected to 10.0.0.1
connected to 192.168.0.1
Hex number 0xDEADBEAF
user davidoh logged in
user eranr logged in
Drain3 extracts the following templates:
ID=1 : size=2 : connected to <:IP:>
ID=2 : size=1 : Hex number <:HEX:>
ID=3 : size=2 : user <:*:> logged in
Full sample program output:
Starting Drain3 template miner
Checking for saved state
Saved state not found
Drain3 started with 'FILE' persistence
Starting training mode. Reading from std-in ('q' to finish)
> connected to 10.0.0.1
Saving state of 1 clusters with 1 messages, 528 bytes, reason: cluster_created (1)
{"change_type": "cluster_created", "cluster_id": 1, "cluster_size": 1, "template_mined": "connected to <:IP:>", "cluster_count": 1}
Parameters: [ExtractedParameter(value='10.0.0.1', mask_name='IP')]
> connected to 192.168.0.1
{"change_type": "none", "cluster_id": 1, "cluster_size": 2, "template_mined": "connected to <:IP:>", "cluster_count": 1}
Parameters: [ExtractedParameter(value='192.168.0.1', mask_name='IP')]
> Hex number 0xDEADBEAF
Saving state of 2 clusters with 3 messages, 584 bytes, reason: cluster_created (2)
{"change_type": "cluster_created", "cluster_id": 2, "cluster_size": 1, "template_mined": "Hex number <:HEX:>", "cluster_count": 2}
Parameters: [ExtractedParameter(value='0xDEADBEAF', mask_name='HEX')]
> user davidoh logged in
Saving state of 3 clusters with 4 messages, 648 bytes, reason: cluster_created (3)
{"change_type": "cluster_created", "cluster_id": 3, "cluster_size": 1, "template_mined": "user davidoh logged in", "cluster_count": 3}
Parameters: []
> user eranr logged in
Saving state of 3 clusters with 5 messages, 644 bytes, reason: cluster_template_changed (3)
{"change_type": "cluster_template_changed", "cluster_id": 3, "cluster_size": 2, "template_mined": "user <:*:> logged in", "cluster_count": 3}
Parameters: [ExtractedParameter(value='eranr', mask_name='*')]
> q
Training done. Mined clusters:
ID=1 : size=2 : connected to <:IP:>
ID=2 : size=1 : Hex number <:HEX:>
ID=3 : size=2 : user <:*:> logged in
This project is an upgrade of the original Drain project by LogPAI from Python 2.7 to Python 3.6 or later with additional features and bug-fixes.
Read more information about Drain from the following paper:
- Pinjia He, Jieming Zhu, Zibin Zheng, and Michael R. Lyu. Drain: An Online Log Parsing Approach with Fixed Depth Tree, Proceedings of the 24th International Conference on Web Services (ICWS), 2017.
A Drain3 use case is presented in this blog post: Use open source Drain3 log-template mining project to monitor for network outages .
New features
- Persistence. Save and load Drain state into an Apache Kafka topic, Redis or a file.
- Streaming. Support feeding Drain with messages one-be-one.
- Masking. Replace some message parts (e.g numbers, IPs, emails) with wildcards. This improves the accuracy of template mining.
- Packaging. As a pip package.
- Configuration. Support for configuring Drain3 using an
.inifile or a configuration object. - Memory efficiency. Decrease the memory footprint of internal data structures and introduce cache to control max memory consumed (thanks to @StanislawSwierc)
- Inference mode. In case you want to separate training and inference phase, Drain3 provides a function for fast matching against already-learned clusters (templates) only, without the usage of regular expressions.
- Parameter extraction. Accurate extraction of the variable parts from a log message as an ordered list, based on its mined template and the defined masking instructions (thanks to @Impelon).
Expected Input and Output
Although Drain3 can be ingested with full raw log message, template mining accuracy can be improved if you feed it with only the unstructured free-text portion of log messages, by first removing structured parts like timestamp, hostname. severity, etc.
The output is a dictionary with the following fields:
change_type- indicates either if a new template was identified, an existing template was changed or message added to an existing cluster.cluster_id- Sequential ID of the cluster that the log belongs to.cluster_size- The size (message count) of the cluster that the log belongs to.cluster_count- Count clusters seen so far.template_mined- the last template of above cluster_id.
Configuration
Drain3 is configured using configparser. By default, config
filename is drain3.ini in working directory. It can also be configured passing
a TemplateMinerConfig object to the TemplateMiner
constructor.
Primary configuration parameters:
[DRAIN]/sim_th- similarity threshold. if percentage of similar tokens for a log message is below this number, a new log cluster will be created (default 0.4)[DRAIN]/depth- max depth levels of log clusters. Minimum is 3. (default 4)[DRAIN]/max_children- max number of children of an internal node (default 100)[DRAIN]/max_clusters- max number of tracked clusters (unlimited by default). When this number is reached, model starts replacing old clusters with a new ones according to the LRU cache eviction policy.[DRAIN]/extra_delimiters- delimiters to apply when splitting log message into words (in addition to whitespace) ( default none). Format is a Python list e.g.['_', ':'].[MASKING]/masking- parameters masking - in json format (default "")[MASKING]/mask_prefix&[MASKING]/mask_suffix- the wrapping of identified parameters in templates. By default, it is<and>respectively.[SNAPSHOT]/snapshot_interval_minutes- time interval for new snapshots (default 1)[SNAPSHOT]/compress_state- whether to compress the state before saving it. This can be useful when using Kafka persistence.
Masking
This feature allows masking of specific variable parts in log message with keywords, prior to passing to Drain. A well-defined masking can improve template mining accuracy.
Template parameters that do not match any custom mask in the preliminary masking phase are replaced with <*> by Drain
core.
Use a list of regular expressions in the configuration file with the format {'regex_pattern', 'mask_with'} to set
custom masking.
For example, following masking instructions in drain3.ini will mask IP addresses and integers:
[MASKING]
masking = [
{"regex_pattern":"((?<=[^A-Za-z0-9])|^)(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})((?=[^A-Za-z0-9])|$)", "mask_with": "IP"},
{"regex_pattern":"((?<=[^A-Za-z0-9])|^)([\\-\\+]?\\d+)((?=[^A-Za-z0-9])|$)", "mask_with": "NUM"},
]
]
Persistence
The persistence feature saves and loads a snapshot of Drain3 state in a (compressed) json format. This feature adds restart resiliency to Drain allowing continuation of activity and maintain learned knowledge across restarts.
Drain3 state includes the search tree and all the clusters that were identified up until snapshot time.
The snapshot also persist number of log messages matched each cluster, and it's cluster_id.
An example of a snapshot:
{
"clusters": [
{
"cluster_id": 1,
"log_template_tokens": [
"aa",
"aa",
"<*>"
],
"py/object": "drain3_core.LogCluster",
"size": 2
},
{
"cluster_id": 2,
"log_template_tokens": [
"My",
"IP",
"is",
"<IP>"
],
"py/object": "drain3_core.LogCluster",
"size": 1
}
]
}
This example snapshot persist two clusters with the templates:
["aa", "aa", "<*>"] - occurs twice
["My", "IP", "is", "<IP>"] - occurs once
Snapshots are created in the following events:
cluster_created- in any new templatecluster_template_changed- in any update of a templateperiodic- after n minutes from the last snapshot. This is intended to save cluster sizes even if no new template was identified.
Drain3 currently supports the following persistence modes:
-
Kafka - The snapshot is saved in a dedicated topic used only for snapshots - the last message in this topic is the last snapshot that will be loaded after restart. For Kafka persistence, you need to provide:
topic_name. You may also provide otherkwargsthat are supported bykafka.KafkaConsumerandkafka.Producere.gbootstrap_serversto change Kafka endpoint (default islocalhost:9092). -
Redis - The snapshot is saved to a key in Redis database (contributed by @matabares).
-
File - The snapshot is saved to a file.
-
Memory - The snapshot is saved
Related Skills
YC-Killer
2.7kA library of enterprise-grade AI agents designed to democratize artificial intelligence and provide free, open-source alternatives to overvalued Y Combinator startups. If you are excited about democratizing AI access & AI agents, please star ⭐️ this repository and use the link in the readme to join our open source AI research team.
best-practices-researcher
The most comprehensive Claude Code skills registry | Web Search: https://skills-registry-web.vercel.app
groundhog
398Groundhog's primary purpose is to teach people how Cursor and all these other coding agents work under the hood. If you understand how these coding assistants work from first principles, then you can drive these tools harder (or perhaps make your own!).
isf-agent
a repo for an agent that helps researchers apply for isf funding
