溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python獲取ELK數(shù)據(jù)進行統(tǒng)計分析

發(fā)布時間:2020-05-24 15:30:58 來源:網(wǎng)絡(luò) 閱讀:789 作者:大牙啊 欄目:系統(tǒng)運維

首先,我們現(xiàn)在kiban上面繪制自己需要統(tǒng)計出來的數(shù)據(jù)

python獲取ELK數(shù)據(jù)進行統(tǒng)計分析

然后點擊右上角inspect,將request的json格式復(fù)制

python獲取ELK數(shù)據(jù)進行統(tǒng)計分析

粘貼到Dev Tools上面,這樣會將我們需要的數(shù)據(jù),已json的形式返回

python獲取ELK數(shù)據(jù)進行統(tǒng)計分析

將請求的json格式保存下來,開始寫py文件

from?elasticsearch?import?Elasticsearch
import?datetime

#初始化鏈接
es?=?Elasticsearch([{'host':'10.3.2.1','port':9200}])
#獲取當前時間和7天前的UTC格式時間戳
last_7day=datetime.datetime.utcnow()-datetime.timedelta(days=6,?hours=16)
now_time=datetime.datetime.utcnow()+datetime.timedelta(hours=8)
last_7day=last_7day.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
now_time=now_time.strftime('%Y-%m-%dT%H:%M:%S.%f%z')

#定義DSL請求體
query_json={
????"aggs":?{
????????"2":?{
????????????"terms":?{
????????????????"field":?"message.keyword",
????????????????"size":?30,
????????????????"order":?{
????????????????????"_count":?"desc"
????????????????}
????????????}
????????}
????},
????"size":?0,
????"_source":?{
????????"excludes":?[]
????},
????"stored_fields":?[
????????"*"
????],
????"script_fields":?{},
????"docvalue_fields":?[
????????{
????????????"field":?"@timestamp",
????????????"format":?"date_time"
????????},
????????{
????????????"field":?"timestamp",
????????????"format":?"date_time"
????????}
????],
????"query":?{
????????"bool":?{
????????????"must":?[
????????????????{
????????????????????"query_string":?{
????????????????????????"query":?"level:error",
????????????????????????"analyze_wildcard":?"true",
????????????????????????"default_field":?"*"
????????????????????}
????????????????},
????????????????{
????????????????????"range":?{
????????????????????????"@timestamp":?{
????????????????????????????"gte":?last_7day,
????????????????????????????"lte":?now_time,
????????????????????????}
????????????????????}
????????????????}
????????????],
????????????"filter":?[],
????????????"should":?[],
????????????"must_not":?[]
????????}
????}
}
res=es.search(index='index-*',?body=query_json)
res=res['aggregations']['2']['buckets']
for?i?in?res:
????print(i)


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI