python 实现elk接口获取数据
[root@ctum2A0703016 ~]# cat jiaoyihao.py
#!/usr/bin/python2.7
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)
if name == 'main':
number=sys.argv[1]
obtain(number)
相关文章