Skip to content

Instantly share code, notes, and snippets.

@shamil
Forked from FreeTymeKiyan/reroute.py
Last active July 7, 2021 08:24
Show Gist options
  • Save shamil/303ce41198135a1961b193e62b3579d8 to your computer and use it in GitHub Desktop.
Save shamil/303ce41198135a1961b193e62b3579d8 to your computer and use it in GitHub Desktop.
An example python script to reroute unassigned shards to NODE_NAME node thus recovering from the red cluster status
#!/usr/bin/env python3
#
# An example python script to reroute unassigned shards to NODE_NAME node,
# thus recovering from the red cluster status
#
# pip install requests before using requests
import requests
import json
HOSTNAME="your.elasticsearch.host.com" # hostname
NODE_NAME="node001" # node to reroute to
PORT=9200 # port number
def reroute(index, shard):
payload = { "commands": [{ "allocate_empty_primary": { "index": index, "shard": shard, "node": NODE_NAME, "accept_data_loss": "true" } }] }
res = requests.post("http://" + HOSTNAME + ":" + str(PORT) + "/_cluster/reroute", data=json.dumps(payload), headers={'Content-Type': 'application/json'})
print(res.text)
pass
res = requests.post("http://" + HOSTNAME + ":" + str(PORT) + "/_flush/synced", headers={'Content-Type': 'application/json'})
j = res.json()
for field in j:
if j[field]["failed"] != 0 and field != "_shards":
for item in j[field]["failures"]:
reroute(field, item["shard"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment