parse eql in python

To parse an EQL query in Python, you can use the Elastic Common Schema (ECS) library. ECS provides a set of data models, fields, and mappings that can be used to standardize document structures across different data sources and platforms.

  1. First, install the Elastic Common Schema (ECS) library using pip:
pip install elastic-apm[ecs-logging]
37 chars
2 lines
  1. Next, import the eql module from the ECS library:
main.py
from ecs-logging import eql
28 chars
2 lines
  1. Define your EQL query as a string:
main.py
query = "process where process.name == 'cmd.exe'"
50 chars
2 lines
  1. Use the eql.parse() method to parse the EQL query string:
main.py
ast = eql.parse(query)
23 chars
2 lines

The parse() method returns an Abstract Syntax Tree (AST) object that represents the structure of the EQL query. You can then use this object to further process and analyze the query.

For example, you may want to convert the EQL AST to an Elasticsearch Query DSL query, which can be executed against an Elasticsearch index:

main.py
from elasticsearch_dsl import Search

q = eql.ast_to_elastic(ast)
s = Search(using=client, index="myindex").query(q)
response = s.execute()
140 chars
6 lines

This code creates a Search object using the Elasticsearch DSL library, sets the query to the one generated from the EQL AST, executes the query against an Elasticsearch index, and returns the response object.

gistlibby LogSnag