Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mschuett
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mschuett/de223abb0ca87d21a067 to your computer and use it in GitHub Desktop.
Save mschuett/de223abb0ca87d21a067 to your computer and use it in GitHub Desktop.
Get Elasticsearch index name from alias
#! /usr/bin/python2
#
# aux script to read elasticsearch aliases info and return real index name for given alias
#
# written for use with https://github.com/mallocator/Elasticsearch-Exporter which
# does not seem to work with aliases
#
# 2015, DECK36 GmbH & Co. KG, <martin.schuette@deck36.de>
import json
import sys
if len(sys.argv) != 2:
print "Read Elasticsearch /_aliases/ output and return real index name for given alias"
print "Usage: give alias name as command-line parameter, pipe JSON to stdin"
print ""
print "Example: curl -s localhost:9200/_aliases/ | get_index.py myalias"
sys.exit(1)
alias = sys.argv[1]
a = json.loads(sys.stdin.read())
l = [x for x in a if a[x]["aliases"].has_key(alias)]
if len(l) == 1:
print l[0]
sys.exit(0)
# else: print alias name
if a.has_key(alias):
print alias
sys.exit(0)
# else:
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment