elastalert0.2.1配置查詢當天或當月分片數據

看到網絡上很多人將elastalert配置,都是這樣配置得

name: API錯誤響應(status >= 400)
type: frequency
index: nginx-access-*

這樣配置索引有個弊端就是當你分片特別多的時候,它回去查詢所有分片,那麼性能上就非常差了。那麼怎麼辦呢?

翻閱了很多資料之後,從elastalert GITHUB上可以看到這樣一段話

I have lots of documents and it’s really slow, how can I speed it up?

There are several ways to potentially speed up queries. If you are using index: logstash-*, Elasticsearch will query all shards, even if they do not possibly contain data with the correct timestamp. Instead, you can use Python time format strings and set use_strftime_index
翻譯:

我有很多文檔並且查詢很慢,該如何提速度?

有好幾種方式可以提高查詢速度,如果你使用logstash-*這樣得索引方式,es將會查詢所有分片,即使它不包含正確得時間節點得數據。我們可以使用Python得時間格式,並設置use_strftime_index

查詢當月得分片
index: logstash-%Y.%m.*
use_strftime_index: true
查詢當天得分片
index: logstash-%Y.%m.%d
use_strftime_index: true

或者你得分片格式是 2020-01-12
你可以寫成這樣

index: logstash-%Y-%m-%d
use_strftime_index: true

關於Python format格式 請參考
Python strftime-strptime-behavior

驗證:

按以上配置後,可以抓包驗證我使用wireshark
安裝抓包工具

yum -y install wireshark

執行抓包

tshark -i lo port 9200 -R 'http.host and http.request.uri'

我們可以看到打印出來得uri是這樣得

按天分
GET /nginx-access-2020-01-19/_search?
按月分
GET /log-service-pay-2020-01-*/_search?

說明這樣配置是能夠有效提升查詢效率得,只查詢當前日期分片。

發佈了119 篇原創文章 · 獲贊 28 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章