Skip to content

Commit

Permalink
Merge pull request #70 from drakaru/alt
Browse files Browse the repository at this point in the history
allow loading jinja templates from filesystem
  • Loading branch information
jertel authored Mar 22, 2021
2 parents b5278b0 + f15b8aa commit c8949c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/source/elastalert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ The default value is ``.raw`` for Elasticsearch 2 and ``.keyword`` for Elasticse

``jinja_root_name``: When using a Jinja template, specify the name of the root field name in the template. The default is ``_data``.

``jinja_template_path``: When using a Jinja template, specify filesystem path to template, this overrides the default behaviour of using alert_text as the template.

Logging
-------

Expand Down
10 changes: 9 additions & 1 deletion elastalert/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import yaml
import yaml.scanner
from jinja2 import Template
from jinja2 import Environment
from jinja2 import FileSystemLoader
from staticconf.loader import yaml_loader

from . import alerts
Expand Down Expand Up @@ -95,6 +97,8 @@ class RulesLoader(object):

base_config = {}

jinja_environment = Environment(loader=FileSystemLoader(""))

def __init__(self, conf):
# schema for rule yaml
self.rule_schema = jsonschema.Draft7Validator(
Expand Down Expand Up @@ -401,7 +405,11 @@ def _dt_to_ts_with_format(dt):

# Compile Jinja Template
if rule.get('alert_text_type') == 'alert_text_jinja':
rule["jinja_template"] = Template(str(rule.get('alert_text', '')))
jinja_template_path = rule.get('jinja_template_path')
if jinja_template_path:
rule["jinja_template"] = self.jinja_environment.get_or_select_template(jinja_template_path)
else:
rule["jinja_template"] = Template(str(rule.get('alert_text', '')))

def load_modules(self, rule, args=None):
""" Loads things that could be modules. Enhancements, alerts and rule type. """
Expand Down

0 comments on commit c8949c1

Please sign in to comment.