LoginSignup
3
7

More than 5 years have passed since last update.

AWS ElasticSearch ServiceのログをElastAlertで監視する

Posted at

ElastAlertをAWSのElasticSearchで使う具体的な方法がネットに転がってなかったのでメモ。

環境は
Ubuntu: 14.04
ElasticSearch: 5.3
ElastAlert: 0.1.16

EC2のIAM Roleに権限付与する

下記のようなポリシーを、ElastAlertを乗せるEC2のIAM Roleに付与する。

Policy
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "es:*",
            "Resource": [
                "ElasticSearch ServiceのARN"
            ]
        }
    ]
}

install

pipでインストールする。
python2でないと動作しない。

command
sudo pip2 install elastalert

ElastAlert用のインデックスを生成する

command
elasticsearch_host=hoge.com
aws_region=ap-northeast-1

elastalert-create-index \
  --host ${elasticsearch_host} \
  --port 443 \
  --aws-region ${aws_region} \
  --url-prefix '' \
  --no-auth \
  --ssl \
  --verify-certs \
  --index elastalert_status \
  --old-index elastalert_status
response
New index elastalert_status created
Done!

共通設定作成

これから通知のためのルールを作っていくが、それらに共通する設定を記載する。

command
# elastalert用のディレクトリ作成(どこでもよい)
mkdir -p /etc/elastalert
cd /etc/elastalert

vim config.yml
config.yml
es_host: [ESのendpoint]
es_port: 443
use_ssl: True
aws_region: ap-northeast-1
rules_folder: /etc/elastalert/rules
buffer_time:
  hours: 1
run_every:
  minutes: 1
writeback_index: elastalert_status
# Slackを利用する場合(下記のRuleにも記載可)
slack_username_override: "ElastAlert"
slack_webhook_url: "[Slackのwebhook url]"

個別の通知ルール作成

通知をするためのルールを作成する。
ルールの記載方法については下記参照。
http://elastalert.readthedocs.io/en/latest/ruletypes.html
http://elastalert.readthedocs.io/en/latest/recipes/writing_filters.html

command
mkdir -p /etc/elastalert/rules

vim rules/hoge_rules.conf
rules/hoge_rules.conf
name: status200-moniroting
type: frequency
index: access_log*
num_events: 10       # 発生回数
timestamp_field: tu  # Indexでのtime fieldを設定しないとエラーになる
timeframe:
    minutes: 1       # 1分間計測する
filter:
  - term:
      status: 200    # http statusが200なら
alert:
  - slack

実行

command
elastalert --config /etc/elastalert/config.yml
3
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7