-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathes_precreate_indices.py
58 lines (46 loc) · 1.81 KB
/
es_precreate_indices.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
58
#!/usr/bin/env python
from elasticsearch import Elasticsearch
import datetime
import re
import time
import argparse
"""
Script for elastcisearch indices creation, based on existing indices.
"""
__author__ = "Adel Sachkov <[email protected]>"
__date__ = "1 May 2018"
__version__ = "$Revision: 1.0 $"
es_host = 'localhost'
es_port = 9200
search_pattern = r"(.*\-logs\-)\d\d\d\d\.\d\d\.\d\d"
today = datetime.date.today().strftime('%Y.%m.%d')
tomorrow = (datetime.date.today() + datetime.timedelta(days=1)).strftime('%Y.%m.%d')
delay = 5 # Delay between indices creation (sec.)
if __name__ == '__main__':
create = False
parser = argparse.ArgumentParser(description='Script for pre-creating indices for next day.')
parser.add_argument('--create', nargs='?', default='dry-run', help='Performs index creation (default: false, dry-run only)')
args = parser.parse_args()
if not args.create:
create = True
print "Dry-run mode disabled. Performing indexes creation..."
# Establishing a connection to Elasticsearch
es = None
try:
es = Elasticsearch(host=es_host, port=es_port, timeout=30)
except Exception as e:
print "Elasticsearch connection error.", e
exit(1)
indices = es.indices.get('*-logs-' + today)
for index in sorted(indices.keys()):
print "Index:", index
index_body = {'mappings': indices[index]['mappings']}
print "Settings:", index_body
match = re.search(search_pattern, index)
index_tomorrow = ''.join(match.groups()) + tomorrow
if create:
print "Creating index for tomorrow:", index_tomorrow
es.indices.create(index=index_tomorrow, ignore=400, body=index_body)
time.sleep(5)
else:
print "Dry-run. Index need to be created:", index_tomorrow