elastalert安裝使用


【1】安裝使用

(1.1)官網

https://github.com/Yelp/elastalert

https://elastalert.readthedocs.io/en/latest/running_elastalert.html

(1.2)安裝

https://github.com/Yelp/elastalert  我直接下載的zip 包

yum -y install wget openssl openssl-devel gcc gcc-c++
yum -y install python3  git python3-devel
pip3 install --upgrade pip
yum -y install python-pip python-devel

unzip elastalert-master.zip
cd elastalert-master
pip3 install "setuptools">=11.3
pip3 install -r requirements.txt
pip3 install setuptools-rust
#針對Es版本修改
pip3 install "elasticsearch>=5.0.0"
python3 setup.py install

 

虛擬環境安裝參考(並未執行)

yum -y install python3  git python3-devel
cd /usr/local/
git clone https://github.com/Yelp/elastalert.git
pip3 install virtualenv
mkdir -p  /usr/local/elastalert/venv_py3.6_elastalert-0.2.1
python3 -m venv /usr/local/elastalert/venv_py3.6_elastalert-0.2.1/
cd venv_py3.6_elastalert-0.2.1/bin/
. activate
pip install -r requirements.txt
pip3 install setuptools-rust

python setup.py install

(1.3)核驗

[root@DB6 /data/software/elastalert-master]$ ll /usr/local/bin/elastalert*
-rwxr-xr-x. 1 root root 973 10月 12 17:25 /usr/local/bin/elastalert
-rwxr-xr-x. 1 root root 999 10月 12 17:25 /usr/local/bin/elastalert-create-index
-rwxr-xr-x. 1 root root 1007 10月 12 17:25 /usr/local/bin/elastalert-rule-from-kibana
-rwxr-xr-x. 1 root root 993 10月 12 17:25 /usr/local/bin/elastalert-test-rule
[root@DB6 /data/software/elastalert-master]$

有環境變量就成了;

elastalert-create-index:ElastAlert會把執行記錄存放到一個ES 索引中,該命令就是用來 創建這個索引的,默認情況下,索引名叫elastalert_status。

            其中有4個 _type,都有 自己的@timestamp字段,所以同樣也可以用kibana,來查看這個索引的日志記錄情況。

elastalert-rule-from-kibana:從Kibana3已保存的儀表盤中讀取Filtering設置,幫助生成config.yaml里的配置。不過注意,它只會讀取filtering,不包括queries。

elastalert-test-rule:測試自定義配置中的rule設置

elastalert:實際運行命令

我們再檢索一下:

    

【2】修改配置

(2.1)修改總體配置 config.yaml

  

 

 默認是有個參考案例文件的;

我們復制一個

#當前已經在 elastalert 目錄下
cp config.yaml.example config.yaml

vim config.yaml

rules_folder: rules    # 規則目錄位置
run_every:    # 多久去查詢一下根據定義的規則去es查詢是否有符合規則的字段
  minutes: 1
buffer_time:    # 當查詢開始一直到結束,最大的緩存時間
  minutes: 15
es_host: 192.168.175.132    # es地址
es_port: 9200            # es端口
#es_username: elastic    # es賬戶
#es_password: 111qqq        # es密碼
writeback_index: elastalert_status    # es里的索引
writeback_alias: elastalert_alerts
alert_time_limit:     # 如果alert當時沒有發出去重試多久之后放棄發送
  days: 2

還有一些大多是加密、證書、等參數,有興趣可以自己看

(2.2)規則告警文件 rules/system.yaml

# 當前已經在 elastalert-master 目錄下;
# 構造規則目錄,就是 config.yaml 中的  rules_folder: rules    # 規則目錄位置
mkdir rules

vim rules/system.yaml

# ---------------- es配置 ----------------------
es_host: 192.168.175.132 # es的IP地址
es_port: 9200 # es的端口
#es_username: elastic  # es的用戶名
#es_password: 111qqq # es的密碼

# ----------------

# --------------- 報警基礎信息配置 ------------------
name: sys_errorlog   # 報警郵件的標題
type: frequency # 類型:頻率
index: system-* # 監控的索引,多個使用逗號隔開
num_events: 1 # 時間內觸發的次數
timeframe:
 minutes: 1   # 時間,和上邊的參數關聯,1分鍾內有1次會報警

# ---------------------


# ---------------- 報警過濾條件 -------------------
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html
filter: - query: query_string: query: "message: a" alert_text: "system日志異常測試" # 會在報警內容中顯示 smtp_host: smtp.qq.com # smtp的地址 smtp_port: 25 # 端口 smtp_auth_file: /data/software/elastalert-master/rules/smtp_auth_file.yaml # 用戶密碼的文件 email_reply_to: 815202984@qq.com # 發送郵件的郵箱 from_addr: 815202984@qq.com alert: - "email" # 報警類型 email: # 收件人地址 - "guochaoqun@xx.com" # -------------- 重復告警 --------------- realert: minutes: 1 # 1分鍾內相同告警不會重復發送 #exponential_realert: # minutes: 10 #指數型增長最大值 # 指數級擴大 realert 時間,中間如果有報警, # 則按照5->10->20->40->60不斷增大報警時間直到最大時間, # 如果之后報警減少,則會慢慢恢復原始realert時間 # -------------------------------------------- # ------------ 聚合相同告警 ------------------ aggregation_key: name #聚合報警的內容,只展示 name 與 message summary_table_fields: - name - _index - message # -------------------------- # ------------------------告警內容格式化 # 注意這些格式化的信息,都是從es中拿過來的 alert_subject: "Error {0}@{1}" alert_subject_args: - name # 該name 為本代碼上面定義的name - host.ip[0] alert_text_type: alert_text_only alert_text: | > datetime: {0} > Message: {1} > Host: {2} > index: {3} > ip_info: {4} > os_info: {5},{6} > input_type: {7} > log_file: {8} > num_hits: {9} > num_matches: {10} alert_text_args: - "@timestamp" - message - agent.hostname - _index - host.ip[0] - host.os.name - host.os.version - input.type - log.file.path - num_hits - num_matches # alert_text: "system日志異常測試" # 會在報警內容中顯示,非格式化內容 # ----------------------

vim  rules/smtp_auth_file.yaml 

user: "815202984@qq.com"
password: 'xxxxxx' # qq 郵箱授權碼

(2.3)測試規則告警文件

# 當前目錄在 elastalert-master目錄下
[root@DB5 /data/software/elastalert-master]$ elastalert-test-rule --config config.yaml rules/system.yaml
INFO:elastalert:Note: In debug mode, alerts will be logged to console but NOT actually sent.
            To send them but remain verbose, use --verbose instead.
/usr/local/lib/python3.6/site-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security.
  warnings.warn(message, category=ElasticsearchWarning)
Error running your filter:
TypeError("search() got multiple values for argument 'body'",)
INFO:elastalert:Note: In debug mode, alerts will be logged to console but NOT actually sent.
                To send them but remain verbose, use --verbose instead.
1 rules loaded
INFO:apscheduler.scheduler:Adding job tentatively -- it will be properly scheduled when the scheduler starts
INFO:elastalert:Queried rule sys_errorlog from 2021-10-13 10:11 CST to 2021-10-13 10:12 CST: 0 / 0 hits

Would have written the following documents to writeback index (default is elastalert_status):

elastalert_status - {'rule_name': 'sys_errorlog', 'endtime': datetime.datetime(2021, 10, 13, 2, 12, 41, 392253, tzinfo=tzutc()),
 'starttime': datetime.datetime(2021, 10, 13, 2, 11, 40, 792253, tzinfo=tzutc()), 'matches': 0, 'hits': 0, 
'@timestamp': datetime.datetime(2021, 10, 13, 2, 12, 41, 458879, tzinfo=tzutc()), 'time_taken': 0.026232481002807617}

 如上列代碼,基本有標紅那塊 就表示成功了

但很明顯期間也有錯誤  ,如上面標紫那塊,說我們的 query 里面的查詢參數不太對!

(2.4)初始化創建信息狀態存放索引

# 創建索引,ElastAlert會把執行記錄存放到這個索引中
# 索引名叫elastalert_status
elastalert-create-index

[root@DB5 /data/software/elastalert-master]$ elastalert-create-index
/usr/local/lib/python3.6/site-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security.
warnings.warn(message, category=ElasticsearchWarning)
Elastic Version: 7.14.1
Reading Elastic 6 index mappings:
Reading index mapping 'es_mappings/6/silence.json'
Reading index mapping 'es_mappings/6/elastalert_status.json'
Reading index mapping 'es_mappings/6/elastalert.json'
Reading index mapping 'es_mappings/6/past_elastalert.json'
Reading index mapping 'es_mappings/6/elastalert_error.json'
Index elastalert_status already exists. Skipping index creation.

會根據預設模板、json 等信息創建,創建默認的  elastalert_status  (這里因為我之前創建過了 索引提示信息說早已存在)

我們查看一下ES中,是否有這些索引了;如下圖,是有的

  

 

【3】啟動、使用 elastalert

(3.1)啟動 elastalert

# 當前目錄在 elastalert-master 目錄下
nohup python3 -m elastalert.elastalert --verbose --config config.yaml --rule rules/system.yaml &

#相關多rule文件參考,在總配置文件指定規則目錄: rules_folder: rules ,還有如果郵件的賬戶密碼文件,則需要配置到其他目錄去,不能放到該目錄下
nohup python3 -m elastalert.elastalert --verbose --config config.yaml &

(3.2)查看日志

[root@DB5 /data/software/elastalert-master]$ less nohup.out 
/usr/local/lib/python3.6/site-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security.
  warnings.warn(message, category=ElasticsearchWarning)
INFO:elastalert:Starting up
INFO:elastalert:Disabled rules are: []
INFO:elastalert:Sleeping for 59.999911 seconds
INFO:elastalert:Queried rule sys_errorlog from 2021-10-13 10:05 CST to 2021-10-13 10:20 CST: 0 / 0 hits
/usr/local/lib/python3.6/site-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: Camel case format name dateOptionalTime is deprecated and will be removed in a future version. Use snake case name date_optional_time instead.
  warnings.warn(message, category=ElasticsearchWarning)
INFO:elastalert:Ran sys_errorlog from 2021-10-13 10:05 CST to 2021-10-13 10:20 CST: 0 query hits (0 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Background configuration change check run at 2021-10-13 10:21 CST
INFO:elastalert:Background alerts thread 0 pending alerts sent at 2021-10-13 10:21 CST
INFO:elastalert:Disabled rules are: []
INFO:elastalert:Sleeping for 59.999893 seconds
INFO:elastalert:Queried rule sys_errorlog from 2021-10-13 10:06 CST to 2021-10-13 10:21 CST: 0 / 0 hits
INFO:elastalert:Ran sys_errorlog from 2021-10-13 10:06 CST to 2021-10-13 10:21 CST: 0 query hits (0 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Background configuration change check run at 2021-10-13 10:22 CST
INFO:elastalert:Background alerts thread 0 pending alerts sent at 2021-10-13 10:22 CST
INFO:elastalert:Disabled rules are: []
INFO:elastalert:Sleeping for 59.99981 seconds
INFO:elastalert:Queried rule sys_errorlog from 2021-10-13 10:07 CST to 2021-10-13 10:22 CST: 0 / 0 hits
INFO:elastalert:Ran sys_errorlog from 2021-10-13 10:07 CST to 2021-10-13 10:22 CST: 0 query hits (0 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Background configuration change check run at 2021-10-13 10:23 CST
INFO:elastalert:Background alerts thread 0 pending alerts sent at 2021-10-13 10:23 CST
INFO:elastalert:Disabled rules are: []
INFO:elastalert:Sleeping for 59.999838 seconds

(3.3)查看告警信息

(1)先查看一下 elastalert 的運行日志

INFO:elastalert:Queried rule sys_errorlog from 2021-10-15 15:02 CST to 2021-10-15 15:04 CST: 1 / 1 hits
INFO:elastalert:Sent email to ['guochaoqun@bianfeng.com']
INFO:elastalert:Ran sys_errorlog from 2021-10-15 15:02 CST to 2021-10-15 15:04 CST: 1 query hits (0 already seen), 1 matches, 1 alerts sent
INFO:elastalert:Background configuration change check run at 2021-10-15 15:05 CST
INFO:elastalert:Disabled rules are: []
INFO:elastalert:Sleeping for 59.999812 seconds
INFO:elastalert:Background alerts thread 0 pending alerts sent at 2021-10-15 15:05 CST
INFO:elastalert:Queried rule sys_errorlog from 2021-10-15 15:02 CST to 2021-10-15 15:05 CST: 4 / 4 hits
INFO:elastalert:Sent email to ['guochaoqun@bianfeng.com']
INFO:elastalert:Ignoring match for silenced rule sys_errorlog
INFO:elastalert:Ignoring match for silenced rule sys_errorlog
INFO:elastalert:Ran sys_errorlog from 2021-10-15 15:02 CST to 2021-10-15 15:05 CST: 4 query hits (1 already seen), 3 matches, 1 alerts sent

(2)查看告警郵件

   

有一個問題,3個命中,3個信息才對,結果只顯示了其中一個;

【4】深入告警配置文件

(4.1)報警方式

elastalert的報警方式有很多種,像郵件、微信、釘釘、post等等,我們主要介紹以下幾種常用的

郵件報警

alert:
- "email"
email:
 - "test@qq.com"
smtp_host: smtp.163.com
smtp_port: 25
smtp_auth_file: /opt/elastalert/smtp_auth_file.yaml
from_addr: test01@163.com
email_reply_to: teast02@163.com

修改/data/software/elastalert-master/rules/smtp_auth_file.yaml信息

企業微信機器人報警

微信報警模板

git clone https://github.com/anjia0532/elastalert-wechat-plugin.git
cp elastalert-wechat-plugin/elastalert_modules/* elastalert_modules/

添加報警方式

alert:
 - "elastalert_modules.wechat_qiye_alert.WeChatAlerter"
#后台登陸后【設置】->【權限管理】->【普通管理組】->【創建並設置通訊錄和應用權限】->【CorpID,Secret】
# 設置微信企業號的appid
corp_id: xxx
# 設置微信企業號的Secret
secret: xxx
# 后台登陸后【應用中心】->【選擇應用】->【應用id】
# 設置微信企業號應用id
agent_id: xx
# 部門id
party_id: xx
# 用戶微信號
user_id: xx
# 標簽id,多個用 | 分隔

釘釘報警方式

釘釘報警模板

git clone https://github.com/xuyaoqiang/elastalert-dingtalk-plugin.git
cp elastalert-dingtalk-plugin/elastalert_modules/dingtalk_alert.py elastalert_modules/

添加報警方式

alert:
- "elastalert_modules.dingtalk_alert.DingTalkAlerter"
dingtalk_webhook: "https://oapi.dingtalk.com/robot/send?access_token=fb6500f4c85b8cfe66fa9586870f3ce16c848eab1e1cb23110388d6d443f1e"
dingtalk_msgtype: text

(4.2)報警頻率

#限定時間內,發生事件次數
num_events: 3
#與上面參數結合使用,表示在2分鍾內發生3次就報警
timeframe:
  minutes: 2

(4.3)避免重復告警

避免一定時間段中重復告警,可以配置realertexponential_realert這兩個選項:

# 5分鍾內相同的報警不會重復發送
realert:
  minutes: 5
# 指數級擴大 realert 時間,中間如果有報警,
# 則按照5->10->20->40->60不斷增大報警時間到制定的最大時間,
# 如果之后報警減少,則會慢慢恢復原始realert時間
exponential_realert:
  hours: 1

 

(4.4)聚合相同告警

# 根據報警的內容將相同的報警按照 name 來聚合
aggregation_key: name
# 聚合報警的內容,只展示 name 與 message
summary_table_fields:
  - name
  - message

(4.5)告警內容格式化

可以自定義告警內容,內部是使用Pythonformat來實現的。

alert_subject: "Error {1} @{2}"
alert_subject_args:
  - name
  - "@timestamp"
alert_text_type: alert_text_only
alert_text: |
  > Name: {1}
  > Message: {2}
  > Host: {3} ({4})
alert_text_args:
  - name
  - message
  - hostname
  - host

(4.6)elastalert最佳實踐整理配置文件

# ---------------- es配置 ----------------------
es_host: 192.168.175.132 # es的IP地址
es_port: 9200 # es的端口
#es_username: elastic  # es的用戶名
#es_password: 111qqq # es的密碼

# ----------------
# --------------- 報警基礎信息配置 ------------------
name: sys_errorlog   # 報警郵件的標題
type: frequency # 類型:頻率
index: system-* # 監控的索引,多個使用逗號隔開
num_events: 1 # 時間內觸發的次數
timeframe:
 minutes: 1   # 時間,和上邊的參數關聯,1分鍾內有1次會報警
# ---------------------
# ---------------- 報警過濾條件 -------------------
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html filter: - query: query_string: query: "message: a" alert_text: "system日志異常測試" # 會在報警內容中顯示 smtp_host: smtp.qq.com # smtp的地址 smtp_port: 25 # 端口 smtp_auth_file: /data/software/elastalert-master/rules/smtp_auth_file.yaml # 用戶密碼的文件 email_reply_to: 815202984@qq.com # 發送郵件的郵箱 from_addr: 815202984@qq.com alert: - "email" # 報警類型 email: # 收件人地址 - "guochaoqun@bianfeng.com" # -------------- 重復告警 --------------- realert: minutes: 1 # 1分鍾內相同告警不會重復發送 #exponential_realert: # minutes: 10 #指數型增長最大值 # 指數級擴大 realert 時間,中間如果有報警, # 則按照5->10->20->40->60不斷增大報警時間直到最大時間, # 如果之后報警減少,則會慢慢恢復原始realert時間 # -------------------------------------------- # ------------ 聚合相同告警 ------------------ aggregation_key: name #聚合報警的內容,只展示 name 與 message summary_table_fields: - name - _index - message # -------------------------- # ------------------------告警內容格式化
# 內容是通過 es中的行提取出來的 alert_subject: "Error {0}@{1}" alert_subject_args: - name # 該name 為本代碼上面定義的name - host.ip[0] alert_text_type: alert_text_only alert_text: | > datetime: {0} > Message: {1} > Host: {2} > index: {3} > ip_info: {4} > os_info: {5},{6} > input_type: {7} > log_file: {8} > num_hits: {9} > num_matches: {10} alert_text_args: - "@timestamp" - message - agent.hostname - _index - host.ip[0] - host.os.name - host.os.version - input.type - log.file.path - num_hits - num_matches # alert_text: "system日志異常測試" # 會在報警內容中顯示,非格式化內容 # ----------------------

(4.7)config.yaml 中的 type規則

  • any:只要有匹配就報警;
  • blacklistcompare_key字段的內容匹配上blacklist數組里任意內容;
  • whitelistcompare_key字段的內容一個都沒能匹配上whitelist數組里內容;
  • change:在相同query_key條件下,compare_key字段的內容,在 timeframe范圍內 發送變化;
  • frequency:在相同query_key條件下,timeframe 范圍內有num_events個被過濾出 來的異常;
  • spike:在相同query_key條件下,前后兩個timeframe范圍內數據量相差比例超過spike_height。其中可以通過spike_type設置具體漲跌方向是- updownboth。還可以通過threshold_ref設置要求上一個周期數據量的下限,threshold_cur設置要求當前周期數據量的下限,如果數據量不到下限,也不觸發;
  • flatlinetimeframe范圍內,數據量小於threshold閾值;
  • new_term:fields字段新出現之前terms_window_size(默認30天)范圍內最多的terms_size(默認50)個結果以外的數據;
  • cardinality:在相同 query_key條件下,timeframe范圍內cardinality_field的值超過 max_cardinality或者低於min_cardinality

(4.8)rules下的 filter寫法

官網:https://elastalert.readthedocs.io/en/latest/recipes/writing_filters.html

【5】深入聚合(解決hit只顯示一行message的痛點)

轉自官網:https://elastalert.readthedocs.io/en/latest/ruletypes.html?highlight=aggregation

(5.1)聚合的概念

聚合

aggregation:此選項允許您將多個匹配項聚合到一個警報中。每次找到匹配項時,ElastAlert 將等待該時間段,並將在該時間段內發生的所有匹配項一起發送給特定規則。aggregation

例如:

aggregation: hours: 2 

意味着如果一場比賽發生在 12:00,另一場發生在 1:00,第三場發生在 2:30,則將在 2:00 發送一個警報,包含前兩場比賽,另一個在 4:30,包含第三場比賽加上在 4:30 之前發生的任何其他比賽。如果您期望有大量匹配並且只想要定期報告,這將非常有用。(可選,時間,默認無)

如果您希望匯總所有警報並定期發送它們,您可以使用該schedule字段執行此操作

例如,如果您希望每周一和周五收到提醒:

aggregation: schedule: '2 4 * * mon,fri' 

這使用 Cron 語法,您可以在此處閱讀更多信息確保包含計划字段或標准日期時間字段(例如hoursminutesdays),而不是同時包含兩者。

默認情況下,在一個過程中發生的所有事件 聚合窗口組合在一起。但是,如果您的規則aggregation_key設置了該字段,則共享公共鍵值的每個事件將被分組在一起。將為每個新遇到的鍵值創建一個單獨的聚合窗口。

(5.2)根據 Key 聚合分組

 

例如,如果您希望接收按觸發事件的用戶分組的警報,您可以設置:

aggregation_key: 'my_data.username'

然后,假設一個 聚合 10 分鍾的窗口,如果您收到以下數據點:

{'my_data': {'username': 'alice', 'event_type': 'login'}, '@timestamp': '2016-09-20T00:00:00'}
{'my_data': {'username': 'bob', 'event_type': 'something'}, '@timestamp': '2016-09-20T00:05:00'} {'my_data': {'username': 'alice', 'event_type': 'something else'}, '@timestamp': '2016-09-20T00:06:00'}

這應該會產生 2 個警報:一個包含 alice 的兩個事件,發送於 2016-09-20T00:10:00,另一個包含 bob 的一個事件發送於2016-09-20T00:16:00

為了 聚合s,有時會在查看媒體(電子郵件、jira 票等)中存在大量文檔。如果您設置了該summary_table_fields字段,Elastalert 將提供所有結果中指定字段的摘要。

例如,如果您希望匯總出現在文檔中的 usernames 和 event_types,以便快速瀏覽最相關的字段,您可以設置:

summary_table_fields:
    - my_data.username - my_data.event_type

然后,對於上面列出的 alice 和 bob 事件的相同示例數據,Elastalert 將在警報介質中提供以下匯總表:

  

注意

  默認情況下, 聚合時間是相對於當前系統時間,而不是比賽時間。

  這意味着在過去的事件上運行 elastalert 將導致與在這些事件發生時運行 elastalert 的警報不同。這種行為可以通過設置來改變aggregate_by_match_time

aggregate_by_match_time

  將此設置為 true 將導致 聚合s 相對於第一個事件的時間戳創建,而不是當前時間。這對於查詢歷史數據很有用,或者如果使用非常大的 buffer_time 並且您希望從單個查詢中發生多個聚合。

(5.3)聚合列 aggregation_key

 

(5.3.1)聚合列案例

 

深入研究了一下,這個列,會把多個告警之間根據 后面的值列來分組聚合;

aggregation:
minutes: 1
aggregation_key: [host.ip[0]] summary_table_fields: - _index - host.ip[0] - message

如下圖:我們發現ip一樣的在同一封郵件中;   

  

另外的案例:

aggregation:
 minutes: 1 # schedule: '* * * * *' # 聚合報警的內容,只展示 name 與 message aggregation_key: ['host.ip[0]' ,'_index'] summary_table_fields: - _index - host.ip[0] - message

 

我們可以發現一下子來了2封郵件:

  

 分別是:根據ip/index分組

  

   

 

 這就很坑爹啊,正經人肯定想要都在一起啊

(5.3.2)聚合key總結

(1)這個聚合其實就是把相同值放到一個告警里去,但是 如果聚合列有不同值,那么不同值會分開成多個告警

(2)如(5.3.1)中的第二個案例,就是上圖;所以略顯雞肋;

(3)如果我們要的是針對這整個索引的命中聚合,那么就去掉

aggregation_key: ['host.ip[0]' ,'_index']

 

 這個參數

 

(5.4)全體聚合(最佳實踐) 

aggregation:
 minutes: 1
 # schedule: '* * * * *'

#aggregation_key: ['host.ip[0]' ,'_index']   #去掉根據查詢結果列分組
summary_table_fields:
  - _index
  - host.ip[0]
  - message

得到的結果:

  

如果得不到這種結果;

可能是下面參數的問題,去掉下面這2個重復告警參數即可

realert:
 minutes: 1 # 1分鍾內相同告警不會重復發

相關參考:

  

 

 

【最佳實踐】rules文件

# ---------------- es配置 ----------------------
es_host: 192.168.175.132 # es的IP地址
es_port: 9200 # es的端口
#es_username: elastic  # es的用戶名
#es_password: 111qqq # es的密碼

# ----------------

# --------------- 報警基礎信息配置 ------------------
name: sys_errorlog   # 報警郵件的標題
type: frequency # 類型:頻率
index: system-* # 監控的索引,多個使用逗號隔開
num_events: 1 # 時間內觸發的次數
timeframe:
 minutes: 1   # 時間,和上邊的參數關聯,1分鍾內有1次會報警

# ---------------------


# ---------------- 報警過濾條件 -------------------
filter:
- query:
    query_string:
      query: "message: a"

alert_text: "system日志異常測試" # 會在報警內容中顯示
smtp_host: smtp.qq.com # smtp的地址
smtp_port: 25 # 端口
smtp_auth_file: /data/software/elastalert-master/rules/smtp_auth_file.yaml       # 用戶密碼的文件
email_reply_to: 815202984@qq.com            # 發送郵件的郵箱
from_addr: 815202984@qq.com
alert:
- "email" # 報警類型
email:    # 收件人地址
- "815202984@qq.com"

# -------------- 重復告警 ---------------
#realert:
#  minutes: 2  # 1分鍾內相同告警不會重復發送

  #exponential_realert:
  #  minutes: 10  #指數型增長最大值


# 指數級擴大 realert 時間,中間如果有報警,
# 則按照5->10->20->40->60不斷增大報警時間直到最大時間,
# 如果之后報警減少,則會慢慢恢復原始realert時間
# --------------------------------------------


# ------------ 聚合相同告警 ------------------
aggregation:
 minutes: 1
 # schedule: '* * * * *'

#aggregation_key: ['host.ip[0]' ,'_index']
summary_table_fields:
  - _index
  - host.ip[0]
  - log.file.path
  - message

# --------------------------

# ------------------------告警內容格式化

alert_subject: "Error {0}@{1}"
alert_subject_args:
  - name
  - host.ip[0]
alert_text_type: alert_text_only
alert_text: |
  > datetime: {0}
  > Message: {1}
  > Host: {2} 
  > index: {3}
  > ip_info: {4}
  > os_info: {5},{6}
  > input_type: {7}
  > log_file: {8}
  > num_hits: {9}
  > num_matches: {10}
alert_text_args:
  - "@timestamp"
  - message
  - agent.hostname
  - _index
  - host.ip[0]
  - host.os.name
  - host.os.version
  - input.type
  - log.file.path
  - num_hits
  - num_matches
  


# alert_text: "system日志異常測試" # 會在報警內容中顯示,非格式化內容
# ----------------------

 

【故障處理】

(1)ERROR:root:Error while running alert email: Error connecting to SMTP host: Connection unexpectedly closed

可能啟用了ssl加密傳輸的,所以郵件服務也需要加以下配置,在auth_smtp.yaml 文件中

 smtp_ssl: true

【參考文檔】

https://www.cnblogs.com/fatzi/p/13020824.html

相關系列及碰到的問題:https://www.cnblogs.com/sanduzxcvbnm/category/1842765.html

修改因pyhton3 覆蓋了 python 命令導致無法 yum

filter寫法官網:https://elastalert.readthedocs.io/en/latest/recipes/writing_filters.html

聚集通知,顯示所有hit信息:https://www.jianshu.com/p/47c77defd18b


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM