python 实现elk接口获取数据

发布时间:2019-09-08 09:11:38编辑:auto阅读(2423)

    [root@ctum2A0703016 ~]# cat jiaoyihao.py
    #!/usr/bin/python2.7

    -- coding:utf-8 --

    from datetime import datetime
    from elasticsearch import Elasticsearch
    import time
    import datetime
    import sys
    import json
    import urllib
    import urllib2
    import re
    import time
    import math
    import chardet

    def obtain(number):
    es = Elasticsearch(
    ['10.24.0.13', '10.24.0.13'],
    http_auth=('elastic', 'elastic'),
    )

    str_search= number + " AND " + "code"
    res = es.search(index="logstash-*", body={
        "query": {
            "bool": {
                "must": [{"query_string": { "query": str_search }},
            {"match_phrase": { "fields.filename": {"query": "app-wanda-credit-ds.log"}}}                    
            ]
            }
        }
    }
    )
    print ("Got %d Hits:" % res['hits']['total'])
    for hit in res['hits']['hits']:
     s = json.loads(hit["_source"]["logmessage"].split(' ')[2])
    code_value = s["code"]
    msg_value = s["msg"] 
    print "code : %s , msg : %s"%(code_value,msg_value)

    print s_zidian

    key = s_zidian.iterkeys()

    CODE = key.next()

    MSG = key.next()

    print CODE

    print MSG

    result = dict(zip(CODE,MSG))

    print result

    if name == 'main':
    number=sys.argv[1]
    obtain(number)

关键字