-
Notifications
You must be signed in to change notification settings - Fork 1
/
escli.py
57 lines (42 loc) · 1.8 KB
/
escli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from datetime import datetime, timedelta
from elasticsearch import Elasticsearch
import sys, getopt
def usage():
print ("\nThis is the usage function\n")
print ('Usage: '+sys.argv[0]+' --index <nom_index> --fieldname <timestamp|date> --period <hour|day>')
def es_search(index_name, field_name, search_date):
es = Elasticsearch()
request = []
print('Search Date : ', search_date.strftime("%Y-%m-%dT%H:%M:%S"))
req_head = {'index': index_name}
req_body = {'query':{'range':{field_name:{'gte': search_date.strftime("%Y-%m-%dT%H:%M:%S") }}}}
request.extend([req_head, req_body])
resp = es.msearch(body = request)
print(resp)
if __name__ == '__main__':
try:
options, remainder = getopt.gnu_getopt(sys.argv[1:], 'i:f:p', ['index=','fieldname=', 'period='])
if len(sys.argv) <= 1:
print(usage())
sys.exit(2)
print('OPTIONS :', options)
for opt, arg in options:
if opt in ('-o', '--index'):
index_name = arg
elif opt in ('-f', '--fieldname'):
fieldname = arg
elif opt in ('-p', '--period'):
period_eval = arg
if period_eval == 'hour':
search_date = datetime.now() - timedelta(hours=1)
elif period_eval == 'day':
search_date = datetime.now() - timedelta(days=1)
print('INDEX :', index_name)
print('PERIOD :', period_eval)
print('FIELDNAME :', fieldname)
print('Search Date : ', search_date)
es_search(index_name, fieldname, search_date)
except getopt.GetoptError as e:
print(e)
usage()
sys.exit(2)