Elastalert 구축

이준섭·2023년 4월 3일

Elastic

목록 보기
2/3
post-thumbnail

1. Python3 설치 및 Elastalert 설치

# 필수 패키지 설치
[root@localhost]# apt-get install python3

# pip upgrade
[root@localhost]# pip3 install --upgrade pip

# 설치 진행
[root@localhost]# pip3 install "setuptools>=11.3" python setup.py install
[root@localhost]# pip3 install elastalert

2. Config.yaml 파일 설정

# 디렉토리 생성
[root@localhost]# mkdir /etc/elastalert
# config.yaml 생성 
[root@localhost]# vim config.yaml

# 밑에 있는 내용 복붙
# 룰이 있는 디렉토리를 지정하고 elastalert가 실행되면 룰 디렉토리 아래에 있는 *.yaml 룰들이 모두 실행됨
rules_folder: /etc/elastalert/rules
# 룰에 대한 체크는 주기로 30초 마다 체크
# days, seconds, microseconds, milliseconds, minutes, hours, weeks 등을 사용 할 수 있음.
run_every:
  seconds: 30
# 룰 체크에 대한 결과를 한번에 완료 할 수 없는 경우 버퍼에 담아 놓게 되는데 이때 버퍼 유지 시간
buffer_time:
  minutes: 15
# elasticsearch 정보
es_host: localhost
es_port: 9200
# ELK에서 설정한 Elastic 계정 정보
# 계정 설정을 하지 않았으면 주석 처리
es_username: <elastic username>
es_password: <elastic password>
writeback_index: elastalert_status
writeback_alias: elastalert_alerts
# 알람 실패 시 재 시도할 주기
alert_time_limit:
  days: 2

3. Rules 생성

# rules 디렉토리 생성
[root@localhost]# mkdir /etc/elastalert/rules

# Rule yaml 생성
[root@localhost]# vim /etc/elastalert/rules/error.yaml
# Alert when the rate of events exceeds a threshold

# (Optional)
# Elasticsearch host
# es_host: elasticsearch.example.com

# (Optional)
# Elasticsearch port
# es_port: 14900

# (OptionaL) Connect with SSL to Elasticsearch
#use_ssl: True

# (Optional) basic-auth username and password for Elasticsearch
#es_username: someusername
#es_password: somepassword

# (Required)
# Rule name, must be unique
name: Error Log

# (Required)
# Type of alert.
# the frequency rule type alerts when num_events events occur with timeframe time
type: frequency

# (Required)
# Index to search, wildcard supported
index: <사용한 인덱스 패턴>

# (Required, frequency specific)
# Alert when this many documents matching the query occur within a timeframe
num_events: 10

# (Required, frequency specific)
# num_events must occur within this amount of time to trigger an alert
timeframe:
   minutes: 5

# (Required)
# A list of Elasticsearch filters used for find events
# These filters are joined with AND and nested in a filtered query
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html
filter:
- query:
    query_string:
      query: 'level : "ERROR"'

# (Required)
# The alert is use when a match is found
alert:
# - "email"
- "slack"
# (required, email specific)
# a list of email addresses to send alerts to
# email:
# - "miiingo@naver.com"

slack:
slack_webhook_url: "<slack url>"
slack_username_override: "<slack 에서 사용할 이름>"
slack_channel_override: "<slack channel name>"
slack_emoji_override: ":robot_face:"
slack_msg_color: "danger"
# 알람 문구 설정
alert_text: "개발서버에서 보내는 에러 알림 입니다.\n\n App Name : {0} \nLogger Name : {1} \nEvent Message : {2} \nEvent On Time : {3} \nTrace ID : {4}"
alert_text_type: "alert_text_only"
alert_text_args: ["springAppName", "logger_name", "message", "log_timestamp", "traceId"]

4. 룰 테스트

[root@localhost elastalert]# elastalert-test-rule --config config.yaml rules/error.yaml

5. Index 생성

[root@localhost]# elastalert-create-index

6. 실행

[root@localhost]# elastalert --verbose --start NOW --config config.yaml

cannot import name 'PROTOCOLTLS' from 'urllib3.util.ssl' (/usr/local/lib/python3.8/dist-packages/urllib3/util/ssl_.py)
위 에러가 날 경우
pip install awscli --ignore-installed six
위 awscli 설치 하면 실행 가능

7. 백그라운드 실행

[root@localhost]# elastalert --verbose --start NOW --config config.yaml &

ubuntu에서는 command 뒤에 &를 붙이면 백그라운드로 프로세스 실행한다.

0개의 댓글