Elastic Search - Open and Close all Indices using python
If you are using Elastic Search and need to Close all indices this python script does it for you.
It also provides the option to open all indices as well !
#!/usr/bin/env python
import json
import simplejson
import urllib.request as request
import sys
import time
######################################################################
# Usage:
# Set the elasticsearch_addr in the file, (this can be externalized)
# - NGINX URL which point to ElasticSearch with Auth Header
# python <filename>.py close
# python <filename>.py open
#
######################################################################
elasticsearch_addr = "http://NGINX-POINTS-TO-ELASTICSEARCH:80"
def fetch_document_list_with_status() :
# URL to fetch all indices
request_url = elasticsearch_addr + '/' + '_cat/indices?format=json&pretty=true'
response = request.urlopen(request_url)
json_data = simplejson.load(response)
return json_data
def close_document_index(index_name):
# Close document index
post_close_url = elasticsearch_addr + '/' + index_name + '/_close'
req = request.Request(post_close_url, data=None, method='POST')
response = request.urlopen(req).read()
print (' Closed ' + index_name + ' response: ', response)
def open_document_index(index_name):
# Open document index
post_open_url = elasticsearch_addr + '/' + index_name + '/_open'
req = request.Request(post_open_url, data=None, method='POST')
response = request.urlopen(req).read()
print (' Opened ' + index_name + ' response: ', response)
def change_documents_status(json_data, status_to_change):
for d in json_data:
index_name = d['index']
# Slow down the process if necessary
# time.sleep(1)
# Don't close the .security index which will make your user inactive
if index_name != '.security' and status_to_change:
# if status is open and status to change is to close then Close index
if d['status'] == 'open' and status_to_change == 'close':
close_document_index(index_name)
# if status is close and status to change is to open then Open index
if d['status'] == 'close' and status_to_change == 'open':
open_document_index(index_name)
argument_length = len(sys.argv)
print (' Number of Arguments : ', argument_length, ' arguments.')
if (argument_length > 1):
argument = sys.argv[1]
print (' Argument List : ', argument)
json_data = fetch_document_list_with_status()
if argument == 'close' or argument == 'open':
change_documents_status(json_data, argument)
print(' ****** Script complete ******* ')
Comments
Post a Comment