Skip to content

Instantly share code, notes, and snippets.

@jceloria
Last active January 5, 2018 19:35
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 jceloria/6f9c0e401e5509f4063b860752de9e29 to your computer and use it in GitHub Desktop.
Save jceloria/6f9c0e401e5509f4063b860752de9e29 to your computer and use it in GitHub Desktop.
bash functions for ES
########################################################################################################################
# Common Elasticsearch functions
function es() {
local id=$1; shift; local cmd=$1; shift; local connect region json curl="$(type -P curl) -sL --netrc-file ~/.netrc-es"
local actions=(aliases allocation health indices nodes recovery shards settings stats clear-cache)
local regex="+(*$(IFS='|';echo "${actions[*]}"|sed 's/|/|*/g'))"
function _help() {
echo "Usage: es <id> ${regex##*+}" >&2
}
[[ ! ${cmd} ]] || [[ ! ${id} ]] && { _help; return 1; }
if [[ ! -e ~/.netrc-es ]]; then
local user pass
read -p 'Enter username: ' user
read -s -p 'Enter password: ' pass
echo "default login ${user} password ${pass}" > ~/.netrc-es
chmod 600 ~/.netrc-es && echo "Authentication file ~/.netrc-es, created"
fi
case ${cmd} in
${regex})
[[ ${id} -eq 7 ]] && region=eu-central-1 || region=us-east-1
connect="elasticsearch-${id}.prod.${region}.internal.${domain}:9200"
;;
*) { _help; return 99; } ;;
esac
case ${cmd} in
aliases)
cmd="${curl} http://${connect}/_aliases"
echo "Running: ${cmd}" && eval ${cmd} | jq '.'
;;
allocation)
cmd="${curl} http://${connect}/_cat/allocation?v"
echo "Running: ${cmd}" && eval ${cmd}
;;
health)
cmd="${curl} http://${connect}/_cluster/health"
echo "Running: ${cmd}" && eval ${cmd} | jq '.'
;;
indices)
cmd="${curl} http://${connect}/_cat/indices?v"
echo "Running: ${cmd}" && eval ${cmd}
;;
nodes)
cmd="${curl} http://${connect}/_cat/nodes?v"
echo "Running: ${cmd}" && eval ${cmd}
;;
recovery)
case $1 in
on) json='{"transient":{"cluster.routing.allocation.node_concurrent_recoveries":2}}' ;;
off) json='{"transient":{"cluster.routing.allocation.node_concurrent_recoveries":0}}' ;;
esac
if [[ ${json} ]]; then
cmd="${curl} -XPUT http://${connect}/_cluster/settings -d ${json}"
echo "Running: ${cmd}" && eval ${cmd} | jq '.'
else
cmd="${curl} http://${connect}/_cat/recovery?v"
echo "Running: ${cmd}" && eval ${cmd}
fi
;;
shards)
cmd="${curl} http://${connect}/_cat/shards?v&bytes=b"
echo "Running: ${cmd}" && eval ${cmd}
;;
settings)
cmd="${curl} http://${connect}/_cluster/settings"
echo "Running: ${cmd}" && eval ${cmd} | jq '.'
;;
stats)
cmd="${curl} http://${connect}/_cluster/stats"
echo "Running: ${cmd}" && eval ${cmd} | jq '.'
;;
clear-cache)
cmd="echo ${curl} -XPOST http://${connect}/_cache/clear"
echo "${cmd}"
;;
*)
echo "${cmd}"
;;
esac
unset _help
}
########################################################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment