elastalert基本配置說明

elastalert 配置語法:html

簡單rule規則:nginx

  • es_host,es_port:查詢elasticsearch集羣
  • name: 規則的惟一名稱。若是相同,則elastalert不會啓動。
  • type: 數據驗證方式(規則類型)
  • index: 要查詢的索引名稱。默認logstash-*
  • filter: 至關於query查詢語法,將須要匹配的信息給匹配
  • alter: 每一個匹配項上運行的警報列表。

query部分

  • run_every: 定時向ES發請求
  • buffer_time: 用來設置請求裏時間字段的範圍,默認45分鐘
  • rules_folder: 用來加載下一階段的rule設置,默認是example_rules
  • timestamp_field: 設置buffer_time時針對哪一個字段,默認是@timestamp

type(規則類型):

任何

  • any: 匹配一切,查詢返回的每一個結果都會生成一個警報。

黑名單

  • blacklist: 黑名單規則將針對黑名單檢查某個字段,若是它在黑名單中則匹配
    • compare_key: 用於與黑名單進行比較的字段名稱。若是該字段爲null,則將忽略這些事件
    • blacklist: 列入黑名單的列表,and or
      適用於,已知全部報警的錯誤時。並將其列舉出來。
    compare_key: "request"
    blacklist:
      - /index.html        #request字段匹配有請求/index.html就報警
      - "!file /tmp/blacklist1.txt"
      - "!file /tmp/blacklist2.txt"

白名單

  • whitelist: 相似於blacklist,此規則會將某個字段與白名單進行比較,若是列表中不包含該字詞則匹配。(過濾器)
    • compare_key: 用於與白名單進行比較的字段名稱
    • ignore_null: 若是爲true,則沒有compare_key字段的事件不匹配
    • whitelist: 列入白名單值列表
    compare_key: "request"
    ignore_null: "true"
    whitelist:
      - /index.html        #request字段匹配過濾請求/index.html的請求
      - "!file /tmp/blacklist1.txt"
      - "!file /tmp/blacklist2.txt"

未成功測試,暫時放棄

change(值改變)

  • change: 此規則將監視某個字段並匹配該字段是否更改。該
    • compare_key: 要監視更改的字段名稱。因爲這是一個字符串列表,若是任何一個字段發生更改,將觸發報警
    • ignore_null: 沒有compare_key字段的事件將不計爲已更改
    • query_key
    • timeframe: 更改之間的最長時間,若是超過該時間將忘記舊值。再次發生改變時將不認爲change

頻率

  • frequency: 給定時間範圍內匹配必定數量的事件時。能夠基於query_key計數
    • num_events: 觸發警報事件數
    • timeframe: 必須在此時間範圍內觸發的num_events數量
type: frequency
index: n-nanjing-console
num_events: 5
timeframe:
    minutes: 1
filter:
- term:
   status: "404"

最近一分鐘內觸發五次404的請求才觸發報警後端

spike(尖峯)

用處比較流量突起,溫度計api

  • spike: 前兩個timeframe時間段內的比較
    • spike_height: 前兩個時間段內相差值
    • spike_type: up(後一個時間段比前一個時間段高,則觸發報警)/down/both
    • timeframe:
      可選參數:
    • threshold_ref: 前一個時間段內的下限,若是不達標,則不觸發報警
    • threshold_cur: 當前時間段內的下限,若是不達標,則不觸發報警

flatline(水平線)

水平線如下觸發報警app

  • flatline: 當threshold一段時間內事件總數低於給定時間時,此規則匹配
    • threshold: 不觸發報警的最小事件數
    • timeframe:
      可選參數:
    • use_count_query: 若是爲true,elastalert將使用count api輪詢elasticsearch,而不是下載全部匹配的文檔。若是隻關心數據而不關心實際數據。

新值(new_term)

字段的值與30天前的數據是不是新出現,如比較後是新值,則觸發報警elasticsearch

  • new_term: 此規則匹配新值出如今之前從未見過的字段中。當ElastAlert啓動時,它將使用聚合查詢來收集字段列表的全部已知術語。
    • fields: 要監視的字段
      其它選項請參考官方文檔

cardinality(基線)

基線上下的值,觸發報警測試

  • cardinality: 當一個時間範圍內某個字段的惟一值總數高於或低於閾值時,引規則匹配。
    • timeframe:
    • cardinality_field: 計數基數的字段
    • max_cardinality: 如查數據的基數大於此數字,則會觸發警報。每一個提高基數的新事件都會觸發警報
    • min_cardinality: 若是數據的基數低於此數據,將觸發警報

metric_aggregation(度量數據聚合)

  • metric_aggregation: 計算窗口中的度量值高於或低於閾值時,此規則匹配。
    • metric_agg_key: 計算度量值的字段。
    • metric_agg_type: 在metric_agg_key字段上執行聚合操做。聚合類型:min,max,avg,sum,cardinality,value_count
    • max_threshold: 若是計算度量標準值大於此數字,則會觸發報警
    • min_threshold: 如查計算試題標準值小於此數字,則會觸發報警
      可選的:
    • use_run_every_query_size: 默認狀況下,度量值是經過buffer_time大小的窗口計算的。若是此參數爲true,則規則將run_every用做計算窗口。

percentage_match(百分比)

  • percentage_match: 當計算窗口中匹配桶中的文檔百分比高於或低於閾值時,此規則匹配。默認狀況下,計算窗口爲buffer_time
    • match_bucket_filter: 定義桶的過濾器,該過濾器就匹配主查詢過濾器返回的文檔子集
    • min_percentage: 若是匹配文檔的百分比小於此數字,則會觸發警報
    • max_percentage: 若是匹配文檔的百分比大於此數字,則會觸發警報

注: 多個type能夠寫在一個規則配置文件中,按順序進行匹配url

過濾器

lucene語法規則code

咱們在使用的時候要預防報警風暴(在實際使用中咱們遇到過可能一分鐘成百上千的錯誤,要是都是發出來,就有問題了)。咱們利用下面的一些措施來控制報警風暴:
1 aggregation: 設置一個時長,則該時長內,全部的報警(同一個配置文件內的報警)最終合併在一塊兒發送一次:
2 realert: 設置一個時長,在該時間內,相同 query_key 的報警只發一個
3 exponential_realert: 設置一個時長,必須大於realert 設置,則在realert到exponential_realert之間,每次報警以後,realert 自動翻倍htm

alter警報

每條規則均可以附加任意數量的警報

email:

  • alert_subject: 郵件主題
  • alert_subject_args: 主題中能夠提供變量,變量值在此定義
  • alert_text: 正文
  • alert_text_args: 正方變量,可從匹配中獲取
  • alert_text_type:
    • alert_text_only :輸出自定義主體
    • exclude_fields: 簡單輸出查詢時間段內匹配到幾條數據
alert_subject: "Alter {0} occurred at {1} {2}"
alert_subject_args:
- _index
- "@timestamp"
- request
alert_text: "最近三分鐘有三次以上404請求"

注意:

格式化程序的參數將從與警報相關的匹配對象中提供。若是規則匹配索引中多個對象,則僅使用第一個匹配來填充格式化程序的參數。若是缺乏參數列表中提到的字段,則電子郵件使用alert_missing_value代替。

smtp配置:

smtp_host: smtp.qq.com
smtp_port: 25
smtp_auth_file: /opt/elastalert/rule_templates/smtp_auth_file.yaml   #賬號密碼配置在此
from_addr: "xxxx@qq.com"
alert:
- "email"
email:
- "xxxx@qq.com"

$ cat /opt/elastalert/rule_templates/smtp_auth_file.yaml
user: xxxx@qq.com
password: xxxxxxxxxx

command

命令輸出,容許執行任意命令並從匹配中傳遞參數或stdin

alert:
  - command
command: ["/bin/send_alert", "--username", "{match[username]}"]

其它更新配置信息,請參考官方文檔

示例一:

五分鐘內流量總和超過200M就發郵件

es_host: 192.168.20.6
es_port: 9200
run_every:
  minutes: 5

name: nanjing_flow
type: metric_aggregation
index: n-xxx-*
buffer_time:
  minutes: 5

metric_agg_key: body_bytes_sent
metric_agg_type: sum
max_threshold: 209715200
use_run_every_query_size: true

alert_text_type: alert_text_only
alert_subject: "Alter nanjing 最近五分鐘流量超200M,請注意!!!"
alert_text: |
  最近五分鐘總流量: {0} B
  kibana url: http://xxxxx

alert_text_args:
  - metric_body_bytes_sent_sum

smtp_host: smtp.qq.com
smtp_port: 25
smtp_auth_file: /opt/elastalert/rule_templates/smtp_auth_file.yaml
from_addr: "xxxx@qq.com"
alert:
- "email"
email:
- "xxxx@qq.com"

示例二

nginx例子,對後端請求超過3秒的發送郵件。須要對特定的接口,好比認證接口過濾(不計算在內)

es_host: 192.168.20.6
es_port: 9200
run_every:
  seconds: 30
name: xxx_reponse_time
index: n-xxx-*
type: whitelist
compare_key: "request"
ignore_null: true
whitelist:
  - /index.html
  - /siteapp/ecsAuthentication/hasAuthentication

type: frequency
num_events: 1
timeframe:
    seconds: 30
filter:
- query_string:
   query: "upstream_response_time: >3 "

alert_text_type: alert_text_only
alert_subject: "Alter {0} 接口後端處理超過3秒!!!"
alert_subject_args:
- _index

html_table_title: "<h2>This is a heading</h2>"
alert_text: |
  timestamp: {0}
  request_method: {1}
  request: {2}
  request_body: {3}
  request_time: {4} s
  upstream_response_time: {5} s
  body_bytes_sent: {6} B
  status: {7}
  remote_addr: {8}
  http_x_forwarded_for: {9}
  upstream_addr: {10}
  agent: {11}

alert_text_args:
  - timestamp
  - request_method
  - request
  - request_body
  - request_time
  - upstream_response_time
  - body_bytes_sent
  - status
  - remote_addr
  - http_x_forwarded_for
  - upstream_addr
  - agent

smtp_host: smtp.qq.com
smtp_port: 25
smtp_auth_file: /opt/elastalert/rule_templates/smtp_auth_file.yaml
from_addr: "xxx@qq.com"
alert:
- "email"
email:
- "xxxxx@qq.com"
相關文章
相關標籤/搜索