ELK watcher setting for aggregation data

SamSam
7 min readJul 26, 2018

ELK watcher json setting agg aggregate aggregation data mail email elasticsearch logstash kibana 設定 發送 資料 build

這篇文章還是關於資料工程的一小部分,是關於ELK elasticsearch, logstash and kibana的聚合資料alert 發送設定。

那為什麼要寫這樣的文章呢,就如同我前幾篇一樣,主要是網路上資源整合,或是中文說明、範例實在不好找,太難的我不會,簡單的就讓小弟幫大家整理吧。

什麼是 ELK 自己看啊@@

實用魂,先介紹功能目的,再講求實踐過程

目的將依據特定欄位聚合的資料,使用 ELK 的 watcher 發送 email,信件內容是資料值

功能實踐
1. ELK x-pack ($$$)
2. ELK system (open免費)
3. mail server (八仙過海,各顯神通。這邊不講mail server 架設以及ELK mail 設定)

然後我會省略很大很多基礎工程,直接推薦網路上的大大 John Wu 的文章,人家寫的美美的,沒道理不看。ELK架設唯一推薦:ELK 教學 — 從無到有安裝 ELK (CentOS/Red Hat)

好了進入正題。總之設定的時候小弟就發生了障礙:
設定watcher 發 email 我會;
跑資料聚合值 data aggregation 我也會。

可是就是那個but,email 信件內文要顯示 data aggregate 後的值,我還真的不太會… 當然這難得倒我,難不倒 Google ,我餵了幾個關鍵字就出現這篇文章:https://discuss.elastic.co/t/send-all-the-aggregations-as-text-with-watcher/70722

好了文章結束。

好啦我開玩笑的。我會解釋一下啦。先貼個人工作 .json 範本,當然已經完全資料加密處理過了,安安。

{
“trigger”: {
“schedule”: {
“interval”: “5m”
}
},
“input”: {
“search”: {
“request”: {
“search_type”: “query_then_fetch”,
“indices”: [
“your-indices-*”
],
“types”: [],
“body”: {
“size”: 0,
“query”: {
“bool”: {
“must”: [
{
“range”: {
@timestamp”: {
“from”: “{{ctx.trigger.scheduled_time}}||-5m”,
“to”: “{{ctx.trigger.triggered_time}}”
}
}
},
{
“query_string”: {
“default_field”: “api_message”,
“query”: “Connection request time-out”
}
}
]
}
},
“aggs”: {
“group_by_state”: {
“terms”: {
“field”: “api.keyword”
}
}
}
}
}
}
},
“condition”: {
“compare”: {
“ctx.payload.hits.total”: {
“gte”: 1
}
}
},
“actions”: {
“send_email”: {
“email”: {
“profile”: “standard”,
“to”: [
sam.xiao@supersam.com
],
“subject”: “[ELK Notification][YourteamName][backend] Counts group by API”,
“body”: {
“text”: “Encountered: {{ctx.payload.hits.total}} \n API called counts: \n {{ctx.payload.aggregations.group_by_state.buckets.0.key}}: {{ctx.payload.aggregations.group_by_state.buckets.0.doc_count}} \n {{ctx.payload.aggregations.group_by_state.buckets.1.key}}: {{ctx.payload.aggregations.group_by_state.buckets.1.doc_count}} \n {{ctx.payload.aggregations.group_by_state.buckets.2.key}}: {{ctx.payload.aggregations.group_by_state.buckets.2.doc_count}} \n {{ctx.payload.aggregations.group_by_state.buckets.3.key}}: {{ctx.payload.aggregations.group_by_state.buckets.3.doc_count}} \n {{ctx.payload.aggregations.group_by_state.buckets.4.key}}: {{ctx.payload.aggregations.group_by_state.buckets.4.doc_count}} \n {{ctx.payload.aggregations.group_by_state.buckets.5.key}}: {{ctx.payload.aggregations.group_by_state.buckets.5.doc_count}} \n”
}
}
}
}
}

Aggregate 資料聚合的依據欄位。如圖,我們是使用api欄位的keyword當作group by 欄位

使用SQL概念寫起來就會是

select count(*), api.keyword from [ your-indices-* ] group by api.keyword

是不是很容易呢?再來是mail信件中的顯示,也是個小小技巧。

group_by_state. buckets. #. key = 顯示group by 欄位值
group_by_state.buckets. #.doc_count= 顯示group by 數量值

實際展示demo結果,你會收到這樣的信件

信件標題
[ELK Notification][YourteamName][backend] Counts group by API

信件內文
Encountered: 10973

API called counts:
myAPI.hihi_howareyou: 7606
myAPI.im_fine_thankyou: 1134
myAPI.may_ihaveyour_phonenumber: 704

打完收工。是不是很好用呢?

--

--