es数据采集脚本样例

采集es数据时并进行分析作用,示例脚本:

 

#coding:utf-8

from elasticsearch5 import Elasticsearch
import time


class GetElasticSearchClass(object):
    def __init__(self, url, time1):
        self.time1 = time1
        self.es = Elasticsearch([url,])

    def foreach(self, data):
        doc = data['hits']['hits']
        print(len(doc))
        if len(doc):
            for item in doc:
                print(item['_source'])

    def search(self, index_name):
        #res = es.search(index='route*', size=10, body = {
        s_time = int(round((self.time1 - 60) * 1000000))
        e_time = int(round(self.time1 * 1000000))
        print(s_time)
        print(e_time)
        res = self.es.search(index='route*', size=1000, body = {
                        "query": {
                            "bool":{
                                "must": [
                                  {
                                    "terms": {
                                        "日志等级": ["WARNING"]
                                    }
                                  },
                                  {
                                    "range": {
                                        "SERVER_TIMESTAMP": {
                                            "gte": s_time,
                                            "lte": e_time
                                      }
                                    }
                                  }
                                ]
                            }
                        },
                    })
        self.foreach(res)


if __name__== "__main__":
    t_now = int(time.time())
    tt = t_now % 60
    start_time = t_now - tt
    obj = GetElasticSearchClass('192.168.222.9:8400', start_time)
    obj.search('switch*')

  

然后定时运行~~

 

posted @ 2021-07-05 15:31  fengzao  阅读(239)  评论(0编辑  收藏  举报