본문 바로가기

Back-end

ElastAlert

반응형

ElastAlert란?

  • Elasticsearch의 데이터에서 작성한 규칙에 따라서 알림을 전송하는 프레임워크.
  • Elasticsearch에 실시간으로 데이터를 기록하고 있고 해당 데이터가 특정 패턴(규칙)과 일치할 때 알림을 받고 싶을 때 사용할 수 있음.

ElastAlert 작동 방식

데이터 소스를 주기적으로 쿼리

→ 정의한 규칙 유형에 일치하는 지를 검사

→ 일치한다면 정의한 방법으로 alert 발

ElastAlert 규칙 구성

name : 규칙의 고유한 이름. 동일한 이름이 2개 이상일 경우 ElastAlert가 시작되지 않는다.
index : 쿼리할 데이터 소스의 위치. 와일드 카드, 여러개 사용 가능

index: topbeat-*,packetbeat-*

type : 규칙의 유형. type에 따라 다양한 방법으로 alert를 발생시킬 수 있습니다.

realert : 해당 규칙으로 인한 alert가 다시 보내질때까지의 최소 시간

filter : 데이터에서 결과를 필터링 하는데 사용하는 기준.

alert : alert 유형 (Slack, AWS SES, AWS SNS, Discord, Email, GoogleChat, HTTP POST 등등)

alert_text : alert 메세지 구성

alert_text_type : 메세지의 타입

alert_text_args : 메세지에서 사용할 매개변수

  1. Jinja Template (alert_text_jinja)
alert_text_type: alert_text_jinja

alert_text: 
  Alert triggered! *({{num_hits}} Matches!)*
  Something happened with {{username}} ({{email}})
  {{description|truncate}}
  1. 파이선 Formatting 문법 (alert_text_only)
alert_text: "Something happened with {0} at {1}"
alert_text_type: alert_text_only
alert_text_args: ["username", "@timestamp"]

Filter 유형

query_string

filter:
- query:
    query_string:
      query: "field: value OR otherfield: otherval

term

filter:
- term:
    name_field: "bob"

terms

- terms:
    fieldX: ["value1", "value2"]

wildcard

filter:
- query:
    wildcard:
      field: "foo*bar"

range

filter:
- range:
    status_code:
      from: 500
      to: 599

Rule Type

any

필에 일치하는 모든 데이터에 대해서 alert 발생

blacklist

모니터링 필드를 확인하고 데이터가 블랙리스트에 있으면 alert 발생

  • compare_key : 모니터링 필드, 해당 필드가 null이면 데이터가 무시된다.
  • blacklist : 블랙리스트 값 or 블랙리스트 값을 포함하는 파일의 경로
# (Required)
type: **blacklist**
# (Required)
****compare_key: filed1
# (Required)
blacklist:
    - value1
    - value2
    - "!file /tmp/blacklist1.txt"
    - "!file /tmp/blacklist2.txt"

whitelist

모니터링 필드를 확인하고 데이터가 화이트리스트에 없으면 alert 발생

  • compare_key : 모니터링 필드
  • ignore_null : true일 때 해당 필드가 null이면 데이터가 무시된다.
  • whitelist : 화이트리스트 값 or 화이트리스트 값을 포함하는 파일의 경로
# (Required)
type: **whitelist** 

ignore_null: true
# (Required)
compare_key: filed1
# (Required)
whitelist:
    - value1
    - value2
    - "!file /tmp/whitelist1.txt"
    - "!file /tmp/whitelist2.txt"

change

모니터링 필드에 대해서 값이 달라지면 alert 발생

  • compare_key : 모니터링 필드, 필드를 여러개 정의 가능
  • ignore_null : 필드가 없으면 데이터 무시
  • query_key : 해당 필드에 값이 같은 경우에 compare_key를 비교
  • timeframe : query_key에 대한 마지막 결과를 얼마나 유지시킬지. (Optional)
# (Required)
name: New country login

# (Required)
type: change

# (Required)
index: logstash-*

# (Required)
compare_key: country_name

# (Required)
ignore_null: true

# (Required)
query_key: username

timeframe:
  days: 1

# (Required)
filter:
- query:
    query_string:
      query: "document_type: login"

frequency

필터에 일치하는 이벤트의 수가 일정 수를 넘기면 alert 발생

  • num_events : alert를 발생시킬 이벤트의 수
  • timeframe : num_events의 수를 측정하는 시간
# (Required)
name: Example frequency rule

# (Required)
type: frequency

# (Required)
index: logstash-*

# (Required, frequency specific)
num_events: 50

# (Required, frequency specific)
timeframe:
  hours: 4

# (Required)
filter:
- term:
    some_field: "some_value"

spike

특정 기간의 이벤트 수가, 특정 배수만큼 증가하거나 줄어들었을 때 alert 발생

  • spike_height : alert를 발생시킬 이벤트 배수
  • spike_type : 'up' or 'down' or 'both'
    up 은 증가했을 때
    down은 감소했을 때
    both는 증가, 감소 했을 때
  • timeframe : 비교할 기간 정의
  • threshold_ref : reference에 해당하는 최소 이벤트 수
  • threshold_cur : current에 해당하는 최소 이벤트 수
  • field_value : 이벤트의 수가 아닌 해당 필드의 값을 기준으로 alert를 발생 -> 온도를 모니터링할 때 유용
# (Required)
name: Event spike

# (Required)
type: spike

# (Required)
index: logstash-*

# (Required one of _cur or _ref, spike specific)
threshold_cur: 15
#threshold_ref: 5

# (Required, spike specific)
timeframe:
  hours: 2

# (Required, spike specific)
spike_height: 4

# (Required, spike specific)
spike_type: "up"

# (Required)
filter:
- query:
    query_string:
      query: "field: value"

# 10:00 ~ 11:00 : 5 events (ref: 0, cur: 5)  - Alert X
# 11:00 ~ 12:00 : 5 events (ref: 0, cur: 10) - Alert X
# 12:00 ~ 13:00 : 10 events (ref: 5, cur: 15) - Alert X
# 13:00 ~ 14:00 : 35 events (ref: 10, cur: 45) - Alert O

flatline

이벤트의 총 수가 일정 기간 동안 지정된 임계값 아래에 있을 때 alert 발생

  • threshold : alert가 발생되지 않을 최소 이벤트 수
  • timeframe : 모니터링 기간
# (Required)
name: Event flatline

# (Required)
type: flatline 

# (Required)
index: logstash-*

# (Required)
threshold: 15

# (Required)
timeframe:
  hours: 2

# (Required)
filter:
- query:
    query_string:
      query: "field: value"

new_term

새로운 값이 들어오면 alert 발생

  • fileds : 모니터링 filed, 없으면 query_key 사용
# (Required)
name: Event newterm 

# (Required)
type: new_term 

# (Required)
index: logstash-*

fileds: ["field1"]

# (Required)
filter:
- query:
    query_string:
      query: "field: value"

cardinality

특정 필드의 유니크한 값의 수가 임계값보다 높거나 낮을 때 alert 발생

# Alert when the rate of events exceeds a threshold
# (Required)
# Index to search, wildcard supported
index: logstash-*

# (Required)
# Rule name, must be unique
name: Example cardinality rule

# (Required)
type: cardinality

# (Required, cardinality specific)
# Count the number of unique values for this field
cardinality_field: "Hostname"

# (Required, frequency specific)
# Alert when there less than 15 unique hostnames
min_cardinality: 15
max_cardinality: 15

# (Required, frequency specific)
# The cardinality is defined as the number of unique values for the most recent 4 hours
timeframe:
  hours: 4

# (Required)
filter:
- term:
    status: "active"

metric_aggregation

특정 필드들의 합, 평균, 최소, 최대 등을 계산하여 기준에 충족하면 alert 발생

# (Required)
name: Metricbeat CPU Spike Rule
# (Required)
type: metric_aggregation
# (Required)
index: metricbeat-*

buffer_time:
  hours: 1

# (Required)
metric_agg_key: system.cpu.user.pct (계산할 수 있는 값이여야함.)
# (Required)
metric_agg_type: avg
query_key: beat.hostname

bucket_interval:
  minutes: 5

# (Required)
min_threshold: 0.1
# (Required)
max_threshold: 0.8
# (Required)
filter:
- term:
    metricset.name: cpu

# (Required)
# The alert is use when a match is found
alert:
- "debug"

spike_aggregation

특정 필드들의 합, 평균, 최소, 최대 등을 계산한 값이 spike_height배 만큼 증가하거나 줄어들었을 때 alert 발생

percentage_match

쿼리 or 필드의 값이 설정한 기간동안의 비율이 설정한 비율보다 작거나, 클 때 alert 발생

# (Required)
name: Example Percentage Match
# (Required)
type: percentage_match
# (95% 작으면 alert)
# (Required)
index: logstash-http-request-*

# (Required)
filter:
- term:
   _type: http_request

buffer_time:
  minutes: 5

query_key: Hostname.keyword

# (Required)
match_bucket_filter:
- terms:
    ResponseStatus: [200]

# (Required)
min_percentage: 95
#max_percentage: 60

#bucket_interval:
#  minutes: 1

# (Required)
# The alert is use when a match is found
alert:
- "debug"

참고 사이트

ElastAlert2 공식문서: https://elastalert2.readthedocs.io/en/latest/elastalert.html

반응형