Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python acquires ELK data for statistical analysis

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

First of all, we now draw the data we need to count on kiban.

Then click inspect in the upper right corner to copy the json format of request

Paste it on Dev Tools, which will return the data we need in json form.

Save the requested json format and start writing the py file

From elasticsearch import Elasticsearchimport datetime# initialization link es = Elasticsearch ([{'host':'10.3.2.1','port':9200}]) # get the UTC format timestamp last_7day=datetime.datetime.utcnow ()-datetime.timedelta (days=6) of the current time and 7 days ago Hours=16) now_time=datetime.datetime.utcnow () + datetime.timedelta (hours=8) last_7day=last_7day.strftime ('% Ymuri% MMI% DT% HV)% MVV% S.F% Z') now_time=now_time.strftime ('% YMY% MMI% DV% MV% S% f% Z') # defines the DSL request body 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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report