Skip to content

Instantly share code, notes, and snippets.

@JamieCressey
Created February 19, 2017 20:43
Show Gist options
  • Save JamieCressey/2c2e3f6e63f3119ddf1ad6aab02a3e14 to your computer and use it in GitHub Desktop.
Save JamieCressey/2c2e3f6e63f3119ddf1ad6aab02a3e14 to your computer and use it in GitHub Desktop.
Reallocate unassigned ElasticSearch Shards
#!/usr/bin/env python
import requests
import random
from time import sleep
def get_cluster_nodes():
nodes = []
resp = requests.get('http://localhost:9200/_nodes').json()
for foo in resp['nodes']:
for k, v in resp['nodes'][foo].iteritems():
if k == 'name':
nodes.append(v)
return nodes
def random_node(nodes=None):
if not nodes:
nodes = get_cluster_nodes()
return random.choice(nodes)
def main():
nodes = get_cluster_nodes()
resp = requests.get('http://localhost:9200/_cat/shards').text
for line in resp.splitlines():
split = line.split()
if split[3] != "UNASSIGNED":
continue
if split[2] != "p":
continue
rqst = {
"commands": [{
"allocate": {
"index": split[0],
"shard": split[1],
"node": random_node(nodes),
"allow_primary": True
}
}]
}
requests.post('http://localhost:9200/_cluster/reroute', json=rqst)
sleep(10)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment