Skip to content

Instantly share code, notes, and snippets.

@gehel
Created September 16, 2016 16:31
Show Gist options
  • Save gehel/f57f1c89e0a0ea612bd37f21ee82fc22 to your computer and use it in GitHub Desktop.
Save gehel/f57f1c89e0a0ea612bd37f21ee82fc22 to your computer and use it in GitHub Desktop.
Check reason for unassigned shards in elasticsearch
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import
import sys
import logging
from tabulate import tabulate
from elasticsearch import Elasticsearch
__author__ = "Guillaume Lederrey"
__copyright__ = "Guillaume Lederrey"
__license__ = "Apache"
_logger = logging.getLogger(__name__)
def main():
index = 'unassigned_test'
shard_id = 2
es = Elasticsearch()
nodes_info = es.nodes.info()
analysis = []
for node_id, node in nodes_info['nodes'].iteritems():
report = allocate_shard_on_node(es, index, shard_id, node['name'])
row = [node['name']]
if not analysis:
header = ['node']
for decider in sorted(report.keys()):
header.append(decider)
analysis.append(header)
for decider in sorted(report.keys()):
row.append(report[decider])
analysis.append(row)
print(tabulate(analysis, headers='firstrow'))
def allocate_shard_on_node(es, index, shard_id, node):
body = {'commands': [{'allocate': {'index': index, 'shard': shard_id, 'node': node}}]}
reroute = es.cluster.reroute(body=body, dry_run=True, explain=True)
decisions = reroute['explanations'][0]['decisions']
report = {}
for d in decisions:
decider = d['decider']
decision = d['decision']
explanation = d['explanation']
report[decider] = decision
_logger.info(decider, decision, explanation)
return report
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment