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*')

  

然後定時運行~~

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章