-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathelasticsearch2csv.py
More file actions
175 lines (156 loc) · 5.6 KB
/
elasticsearch2csv.py
File metadata and controls
175 lines (156 loc) · 5.6 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/python3
from os import kill
import elasticsearch
import argparse
import csv
from elasticsearch import helpers
import json
'''
Tool for exporting elasticsearch query to CSV file
assumption: the response document is not multidimensional(nested) document.
it will execute elasticsearch query_string,
for example query_string: this AND that OR thus
'''
parser=argparse.ArgumentParser()
parser.add_argument("-i", "--index", dest="index",
help="required: index name", required=True)
parser.add_argument("-t", "--type", dest="type", default='_doc',
help="required: doc type name")
parser.add_argument("--host", dest="host",
help="host url default=localhost", default='localhost')
parser.add_argument("--output", dest="output",
help="output file default: output", default="output")
parser.add_argument("-d", "--delimiter", dest="delimiter",
help="csv delimiter - default - , ", default=',')
parser.add_argument("--query", dest="json_query",
help="json customized query if query exists, query_string will be ignored, will also parse query from Kibana inspec", default=None)
parser.add_argument("--parse_query", dest="parse_query",
help="parse query from Kibana inspect", default=None)
parser.add_argument("--parse_query_file", dest="parse_query_file",
help="parse query from file, Kibana inspect", default=None)
parser.add_argument("--query_string", dest="query_string",
help="query string \'this AND that OR thus\'", default="*")
parser.add_argument("--fields", dest="fields",
help="comma separated fields - default all", default='all')
parser.add_argument("--size", dest="size",
help="size of the respons, default 1000", default='1000')
parser.add_argument("-u","--user", dest="user",
help="<user:password> Server user and password", default=None)
parser.add_argument("-f","--format", dest="format",
help="<csv|json> output file format", default="json")
args = vars(parser.parse_args())
host = args['host']
index = args['index']
doc_type = args['type']
output_files = args['output']
query_string = args['query_string']
delimiter = args['delimiter']
fields= args['fields']
size=int(args['size'])
json_query = args['json_query']
parse_query = args['parse_query']
parse_query_file = args['parse_query_file']
auth = args['user']
format = args['format']
'''
Create elasticsearch instance
'''
if auth:
if ":" in auth:
user,password = auth.split(":",1)
else:
user = auth
password = input("Enter host password for user '{}': ".format(user))
es = elasticsearch.Elasticsearch(host,http_auth=(user, password))
else:
es = elasticsearch.Elasticsearch(host)
'''
Get query from input
'''
if parse_query_file:
with open(parse_query_file) as f:
parse_query = f.read()
if json_query:
query = json.loads(json_query)
if "query" in query:
query = dict(query =query['query'] )
elif parse_query:
query = json.loads(parse_query)
query = dict(query =query['query'] )
else:
query = dict(
query = dict(
query_string=dict(
query=query_string
)
)
)
query['size'] = size
'''
Get real index name in case index is alias
'''
aliases = es.indices.get_alias()
for i in aliases:
if index in aliases[i]['aliases']:
index = i
'''
Fetch the mapping in order to create the header
'''
if index.endswith("*"):
i = es.indices.get(index)
for x in i:
temp_index = x
break
else:
temp_index=index
mapping=es.indices.get_mapping(index=temp_index)[temp_index]['mappings']['properties'].keys()
'''
Set handler to elasticsearch
'''
scanResp= helpers.scan(client=es, query=query, scroll="10m", index=index,size=size, doc_type=doc_type, clear_scroll=True, request_timeout=300)
if format == "csv":
if not output_files.endswith(".csv"):
output_files += ".csv"
with open(output_files, 'w') as f:
counter = 0
if fields == "all":
w = csv.DictWriter(f, mapping, delimiter=delimiter,extrasaction='ignore',quoting=csv.QUOTE_MINIMAL)
w.writeheader()
else:
fields = fields.split(",")
w = csv.DictWriter(f, [i for i in mapping if i in fields], delimiter=delimiter, extrasaction='ignore',quoting=csv.QUOTE_MINIMAL )
w.fieldnames = fields
w.writeheader()
try:
for row in scanResp:
if counter >= size:
break;
_ = w.writerow(row['_source'])
counter +=1
except elasticsearch.exceptions.NotFoundError:
pass
except elasticsearch.exceptions.RequestError:
pass
if format == "json":
if not output_files.endswith(".json"):
output_files += ".json"
with open(output_files, 'w') as f:
counter = 0
try:
for row in scanResp:
if counter >= size:
break;
_ = f.write(json.dumps(row['_source'])+"\n")
counter +=1
except elasticsearch.exceptions.NotFoundError:
pass
except elasticsearch.exceptions.RequestError:
pass
if counter > 0:
print('%s lines was exportred to file: %s'%(counter,output_files))
else:
if len(host.split(':')) == 1:
host +=':9200'
print('no data to export from source:\n'
'%s\nGET /%s/%s/_search'%(host,index,doc_type))
print(json.dumps(query,indent=4, sort_keys=True))