发表于 2019-1-28 10:27:19

python 实现elk接口获取数据

  # 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(' '))
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
obtain(number)



页: [1]
查看完整版本: python 实现elk接口获取数据