Elastalert filter を試す
使用する Docker image
filebeat 機能を含めたこちらのイメージを使って試していきます。
github.com
参考:イメージ作成時の記事
https://blueskyarea.hatenablog.com/entry/2020/05/13/235521
gmail 用 User と Password 設定
$ vim elastalert/pass/smtp_auth_user.yaml user: "{your_gmail_user}@gmail.com" password: "{user_gmail_password}"
docker-compose で各コンテナ開始
docker-compose up --build -d
Elastalert ルールの作成
http://localhost:5601 にアクセスして kibana UI を開きます。
ルール: message フィールドに "ERROR" が含まれているメッセージが1時間以内に1回検知されれば、アラートメールを送信する。
- すでに "filebeat-*" で index pattern が作成されているとします。
- filter を使って、message フィールドに "ERROR" が含まれているもの を対象に絞るようにしています。
- filter のシンタックスは、Apache Lucene に準拠しているようです。
[全体像]
es_host: doc-elastic101 es_port: 9200 name: Rule find error type: frequency index: filebeat-* is_enabled: true num_events: 1 timeframe: hours: 1 filter: - query: query_string: query: "message: *ERROR*" #subject to email alert_subject: "Found ERROR" alert: - "email" email: "{user_gmail}@gmail.com" smtp_host: "smtp.gmail.com" smtp_port: 465 smtp_ssl: true from_addr: "{user_gmail}@gmail.com" smtp_auth_file: "/opt/elastalert/pass/smtp_auth_user.yaml"
以下のように書くことで、1時間に1回、message フィールドに "ERROR" が含まれているものが検知されれば、このルールが適用される(今回ではメール送付)という意味になります。
num_events: 1 timeframe: hours: 1 filter: - query: query_string: query: "message: *ERROR*"
Elastalert ログの監視
これから filebeat を通してメッセージを elasticsearch に投入します。
それらのメッセージを elastalert 側で検知、またメール送信のイベントが発生しているかをログから確認します。
docker logs -f elastalert
[INFO] メッセージを elasticsearch に投入
echo "[INFO] this is info message1." >> filebeat/logs/sample.log
[elastalert ログ]
以下のように hit(設定したルールに該当する)したものはゼロ件です。
※今回のルールは "ERROR" が含まれているメッセージを検知するようにしているため
01:31:48.888Z ERROR elastalert-server: ProcessController: INFO:elastalert:Queried rule Rule file error from 2020-MM-dd 01:29 UTC to 2020-MM-dd 01:30 UTC: 0 / 0 hits 01:30:48.890Z ERROR elastalert-server: ProcessController: INFO:elastalert:Ran Rule file error from 2020-MM-dd 01:29 UTC to 2020-MM-dd 01:30 UTC: 0 query hits (0 already seen), 0 matches, 0 alerts sent
[ERROR] メッセージを elasticsearch に投入
echo "[INFO] this is error message1." >> filebeat/logs/sample.log
[elastalert ログ]
以下のように hit(設定したルールに該当する)したものは1件です。
さらに、Sent email to ['{your_gmail}@gmail.com'] とメール送信のイベントが発生していることが分かります。
01:31:58.939Z ERROR elastalert-server: ProcessController: INFO:elastalert:Queried rule Rule file error from 2020-MM-dd 01:30 UTC to 2020-MM-dd 01:31 UTC: 1 / 1 hits 01:32:02.879Z ERROR elastalert-server: ProcessController: INFO:elastalert:Sent email to ['{your_gmail}@gmail.com'] 01:32:02.937Z ERROR elastalert-server: ProcessController: INFO:elastalert:Ran Rule file error from 2020-MM-dd 01:30 UTC to 2020-MM-dd 01:31 UTC: 1 query hits (0 already seen), 1 matches, 1 alerts sent
[gmail]
実際に以下のようなメールが受信されていました。
"At least 1 events" というメッセージから、1 回の検知のみで送信されたことが分かります。
Rule find error At least 1 events occurred between 2020-MM-dd 00:31 UTC and 2020-MM-dd 01:31 UTC
Elastalert ルールの変更
ルール: message フィールドに "ERROR" が含まれているメッセージが1時間以内に3回検知されれば、アラートメールを送信する。
[ERROR] メッセージを3回 elasticsearch に投入
echo "[ERROR] this is error message2." >> filebeat/logs/sample.log echo "[ERROR] this is error message3." >> filebeat/logs/sample.log echo "[ERROR] this is error message4." >> filebeat/logs/sample.log
[elastalert ログ]
以下のように hit(設定したルールに該当する)したものは1件です。
さらに、Sent email to ['{your_gmail}@gmail.com'] とメール送信のイベントが発生していることが分かります。
01:38:54.443Z ERROR elastalert-server: ProcessController: INFO:elastalert:Queried rule Rule file error from 2020-MM-dd 01:37 UTC to 2020-MM-dd 01:38 UTC: 1 / 1 hits 01:38:57.527Z ERROR elastalert-server: ProcessController: INFO:elastalert:Sent email to ['{your_gmail}@gmail.com'] 01:38:57.592Z ERROR elastalert-server: ProcessController: INFO:elastalert:Ran Rule file error from 2020-MM-dd 01:37 UTC to 2020-MM-dd 01:38 UTC: 1 query hits (0 already seen), 1 matches, 1 alerts sent
[gmail]
実際に以下のようなメールが受信されていました。
"At least 3 events" というメッセージから、3 回の検知を以って送信されたことが分かります。
※ルール変更前に送信済みであった1回は、既にメール送信済みのイベントであるため、カウントには含まれません。ルール変更後に改めて3回 "ERROR" メッセージを投入する必要がありました。
Rule file error At least 3 events occurred between 2020-MM-dd 00:38 UTC and 2020-MM-dd 01:38 UTC
参考
https://elastalert.readthedocs.io/en/latest/ruletypes.html
https://dev.classmethod.jp/articles/elastalert/
https://qbox.io/blog/elasticsearch-alerting-at-scale-using-elastalert
https://elastalert.readthedocs.io/en/latest/recipes/writing_filters.html
https://lucene.apache.org/core/2_9_4/queryparsersyntax.html#Wildcard%20Searches