Last active
August 29, 2015 14:20
-
-
Save mschuett/de223abb0ca87d21a067 to your computer and use it in GitHub Desktop.
Get Elasticsearch index name from alias
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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