For the complete documentation index, see llms.txt. This page is also available as Markdown.

elastalert.py

SUMMARY

An elastalert rule tester built using python. Tests could be set-up uniquely and could be run by batch. Specified logs are indexed using Elasticsearch 7.4.0 and are used with a custom elastalert alerter. This program covers testing for single matches and log aggregation with field mapping capabilities. (source code)

ENVIRONMENT

ELASTICSEARCH

VIRTUAL ENVIRONMENT

Python 3.6

OPERATING SYSTEM

Linux (in this case: Ubuntu 18.04)

IMPORTANT NOTES

  • This probably won't work on Windows Machines.

  • The program manually creates a basic config file if none is specified.

  • The program manually sets the rule's alert to elastalerter.alerter.Alert.

  • If mappings are not specified during aggregation, key fields are automatically set as keyword fields:

    query_key: field_name.keyword
    metric_agg_key: field_name.keyword
  • Logs to be used in a specific test could be a list of files or a directory containing all logs to be indexed.

    "log": ["log1.json", "log2.json", ...]
    
     or
    
    "log": "/dir"
  • If a directory is specified, the program will check if it exists as is, in the directory specified in --logs, or in the current working directory.

  • Test results are laid out as follows in results.json:

    {
        "total": 6,
        "pass": 3,
        "fail": 3,
        "tests": {
            "test_2": {
                "result": "PASSED",
                "message": []
            },
            "test_2": {
                "result": "PASSED",
                "message": []
            },
            "test_3": {
                "result": "FAILED",
                "message": [
                    "1 LOG(S) MATCHED: log001.json"
                ]
            },
            "test_4": {
                "result": "FAILED",
                "message": [
                    "7 LOG(S) DID NOT MATCH: agg_log001.json, agg_log002.json, agg_log003.json, agg_log004.json, agg_log005.json, log001.json, log003.json"
                ]
            },
            "test_agg_1": {
                "result": "FAILED",
                "message": [
                    "HITS (5) EXCEEDED THE THRESHOLD"
                ]
            }
        }
    }

SET-UP

  1. Download and run elasticsearch 7.4.0 for Linux.

  2. Set-up a python virtual environment:

  3. Download and install dependencies for elastalerter.py

    download and unzip the files

    install dependencies

    install the alerter

EXECUTION (w/ sample output)

help (-h, --help)

default output

mappings (--mappings)

> sample mappings.json

> execution

verbose (--verbose)

Last updated