MJN All Blog Cheatsheets Elasticsearch GCP JS LinuxBash Misc Notes Other ShortcutKeys / - Search

Home / LinuxBash / Script query-es - Elasticsearch Query Utility


#!/bin/bash
help_text="
NAME
   query_es - One line description.

USAGE
   query_es [options] <parameters>

OPTIONS
   -x
      Description...

   -h|--help
      Show help text.

DESCRIPTION
   Description description description description.

AUTHOR
  mjnurse.dev - 2020
"
help_line="tbc"
desc_line="tbc"
web_desc_line="Elasticsearch Query Utility"

tmpfile="/tmp/es.$RANDOM.$RANDOM.tmp"
filter_regex=""
debug_yn=n

if [[ $# == 0 ]]; then
   echo "Usage: query_es options"
   echo
   echo "   -a  api (mandatory)"
   echo "   -bp base path (default:\$QES_BASE_PATH=$QES_BASE_PATH)"
   echo "   -d  debug y/n (default:n)"
   echo "   -f  filter_regex"
   echo "   -h  host (default:\$QES_HOST=$QES_HOST)"
   echo "   -j  json"
   echo "   -o  opn (mandatory)"
   echo "   -p  port (default:\$QES_PORT=$QES_PORT)"
   echo "   -pa proxy authentication (default:\$QES_PROXY_AUTH=$QES_PROXY_AUTH)"
   echo "   -r  header rows (default:0)"
   echo "   -s  show settings"
   echo "   -up username:password (default:\$QES_USER_PASS=$QES_USER_PASS)" 
fi

curlcmd="curl -s"
# get curl localhost to work in WSL
#if [[ "$WSL_DISTRO_NAME" != "" ]]; then
#  curlcmd="cmd.exe /c curl"
#fi

# Default Values
debug_yn=y
show_settings_yn=n
host="$QES_HOST"
port="$QES_PORT"
if [[ "$QES_USER_PASS" != "" ]]; then
  userpass="-u $QES_USER_PASS"
fi
if [[ "$QES_PROXY_AUTH" != "" ]]; then
  proxyauth="Proxy-Authorization: $QES_PROXY_AUTH"
fi
basepath="$QES_BASE_PATH"
header_rows=0

while [[ $# > 0 ]]; do
   case $1 in
   -d) debug_yn=y ;;
   -a) shift; api="$1" ;;
   -bp) shift; basepath="$1" ;;
   -f) shift; filter_regex="$1" ;;
   -h) shift; host="$1" ;;
   -j) shift; json="$1" ;;
   -o) shift; opn="$1" ;;
   -p) shift; port="$1" ;;
   -pa) shift: proxyauth="Proxy-Authorization: $1" ;;
   -r) shift; header_rows="$1" ;;
   -s) show_settings_yn=y ;;
   -up) shift; userpass="-u $1" ;;
   *)  echo "ERROR: OTHER VALUE: $1"; exit ;;
   esac
   shift
done

if [[ "$host" == "" ]]; then
  echo Missing host - Consider setting environment variable QES_HOST
  exit
fi
if [[ "$port" == "" ]]; then
  echo Missing port - Consider setting environment variable QES_PORT
  exit
else
  port=":$port"
fi

url="$host$port/$basepath/$api"

# Replace any // with a / (this allows us to miss index names from some commands to run on all indexes)
if [[ "$QES_HTTPS_YN" == "Y" || "$QES_HTTPS_YN" == "y" ]]; then
  url="${url//\/\//\/}"
  url="https://${url//\/\//\/}"
else
  url="${url//\/\//\/}"
  url="http://${url//\/\//\/}"
fi

cmd="Command: $opn $url"
cmddiv=$(echo $cmd | sed "s/./-/g")

if [[ $show_settings_yn == y ]]; then
  echo 
  echo Settings:
  echo
  echo host="\"$host\""
  echo port="\"$port\""
  echo basepath="\"$basepath\""
  echo userpass="\"$userpass\""
  echo proxyauth="\"$proxyauth\""
  echo
  exit
fi

if [[ "$opn" == "DELETE" ]]; then
   read -p "Are you sure you want to delete index: "'"'$api'"'" [yN]: " yn
   if [[ "$yn" != "y" && "$yn" != "Y" ]]; then
      exit
   fi
fi

if [[ "$json" != "" ]]; then
   if [[ $debug_yn == y ]]; then
      echo ==================================================
      echo COMMAND
      echo ==================================================
      echo $curlcmd $userpass $proxyauth -X $opn "$url"  -H "'Content-Type: application/json'" -d "'$json'" | tr -d "\n" | sed "s/  */ /g"
      echo
      echo ==================================================
      echo RESULTS
      echo ==================================================
   fi
   $curlcmd -s -k $userpass -H "$proxyauth" -X $opn "$url"  -H 'Content-Type: application/json' -d "$json" 2>/dev/null | sed "$filter_regex" > $tmpfile 2>/dev/null
else
   if [[ $debug_yn == y ]]; then
      echo ==================================================
      echo COMMAND
      echo ==================================================
      echo $curlcmd $userpass $proxyauth -X $opn "'$url'" | tr -d "\n" | sed "s/  */ /g"
      echo
      echo ==================================================
      echo RESULTS
      echo ==================================================
   fi
   $curlcmd  -s -k $userpass -H "$proxyauth" -X $opn "$url" 2>/dev/null | sed "$filter_regex" > $tmpfile 2>/dev/null
fi

reshead="$(head -1 $tmpfile)"
if [[ "${reshead:2:5}" == "error" ]]; then
   echo
   echo ERROR:
   cat $tmpfile
   exit
fi

#sed -i "s/->/\n   ->/g" $tmpfile
cat $tmpfile

num_lines=$(cat $tmpfile | wc -l)
let num_lines=num_lines-header_rows

echo
echo "($num_lines lines)"


This page was generated by GitHub Pages. Page last modified: 24/07/11 15:34