#!/bin/bash

function exit_usage() {
    local status=${1:-0}
    [[ $status != 0 ]] && exec >&2
    echo "Usage: ${0#*/} [-w [WATCH-SECONDS]]"
    exit "$status"
}

while (( $# > 0 )); do
    case "$1" in
        -h|--help) exit_usage 0 ;;
        -w|--watch) watch=${2:-5}; shift ;;
        *) exit_usage 1 ;;
    esac
    shift
done

if [[ -n $watch ]]; then
    [[ -z ${watch//[0-9]} ]] || exit_usage 1
    exec watch -n "$watch" "$0"
fi

echo "# es-curl _cat/health"
es-curl _cat/health
echo

echo "# es-curl _cat/nodes"
es-curl _cat/nodes?s=n
echo

echo "# es-curl _nodes/stats/fs (used)"
es-curl _nodes/stats/fs |
    jq --arg human 1 -r '
def numfmt2:
    . * 100 |
        round |
        tostring |
        sub("(?<x>..)$"; ".\(.x)")
    ;
def human_bytes:
    def hb(v; u):
        if (u |length) == 1 or v < 1000 then
            "\(v |numfmt2)\(u[0])B"
        else
            hb(v/1024; u[1:])
        end
        ;
    hb(.; ["","K","M","G","T","P"])
    ;
.nodes |to_entries |map(
    .value |
    . + { used_bytes: (.fs.total.total_in_bytes - .fs.total.available_in_bytes) } |
    . + { used_pct: (.used_bytes*100/.fs.total.total_in_bytes) } |
    if $human == "1" then
        .fs.total.total_in_bytes |= human_bytes |
        .fs.total.available_in_bytes |= human_bytes |
        .used_bytes |= human_bytes
    else . end |
    "\(.name)\t\(.ip|sub(":.*"; ""))\t\(.fs.total.total_in_bytes)\t\(.used_bytes)\t\(.used_pct |numfmt2)%"
) |sort[]' |
    column -t -o ' '
echo

echo "# es-curl _cluster/settings"
es-curl _cluster/settings |jq -cM .
echo

echo "# es-curl _cat/recovery (not done)"
es-curl '_cat/recovery?s=i,s&h=i,s,source_node,target_node,tn,st,fp,bp,top' |
        awk '$5=="done"{next}{for(i=3;i<=4;i++)$(i)=gensub(/\..*/,"",1,$(i));print;}' |
        column -t -o ' '
echo

echo "# es-curl _cat/shards (not STARTED)"
es-curl '_cat/shards?s=i&h=i,sh,pr,st' |fgrep -v STARTED
echo
