#!/usr/bin/env bash

APIkeyFile=".nagiosapikey"
[ ! -r "$APIkeyFile" ] && APIkeyFile="${HOME}/.nagiosapikey"
[ ! -r "$APIkeyFile" ] && APIkeyFile=""

APIKEY=""
XI_URL=""
curl="curl -k -s"
# curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=<key>"

# myDir (for "Directives") are now too big for individual variables.  We'll read them into an associative array instead
declare -A myDir

# We need a separate thing for myOptions and it's easier to have a couple of them be straight variables
declare -A myOptions
myOptions[API]="objects"
myOptions[APIep]="servicestatus"
myOptions[TestMode]=""
myOptions[Create]=""
myOptions[Apply]=""
myOptions[Options]=""
verbose="0"
tmpJSON=""
tmpQuick=""
tmpQuicker=""

# Different API commands return different JSON datasets.  So let's make a lookup table that figures out where to start the data extracts
declare -A APIinfo
APIinfo["config/host"]=".[]"
APIinfo["config/service"]=""
APIinfo["config/hostgroup"]=".[]"
APIinfo["config/servicegroup"]=".[]"
APIinfo["config/command"]=".[]"
APIinfo["config/contact"]=".contact[]"
APIinfo["config/contactgroup"]=".[]"
APIinfo["config/timeperiod"]=".[]"

APIinfo["objects/hoststatus"]=".hoststatus[]"
APIinfo["objects/servicestatus"]=".servicestatus[]"
APIinfo["objects/logentries"]=""
APIinfo["objects/statehistory"]=".stateentry[]"
APIinfo["objects/comment"]=""
APIinfo["objects/downtime"]=""
APIinfo["objects/contact"]=".contact[]"
APIinfo["objects/host"]=".host[]"
APIinfo["objects/service"]=".[]"
APIinfo["objects/hostgroup"]=".hostgroup[]"
APIinfo["objects/servicegroup"]=""
APIinfo["objects/contactgroup"]=".contactgroup[]"
APIinfo["objects/timeperiod"]=""
APIinfo["objects/unconfigured"]=""
APIinfo["objects/hostgroupmembers"]=".hostgroup[]"
APIinfo["objects/servicegroupmembers"]=".servicegroup[]"
APIinfo["objects/contactgroupmembers"]=""
APIinfo["objects/rrdexport"]=""
APIinfo["objects/cpexport"]=""
APIinfo["objects/hostavailability"]=".hostavailability[]"
APIinfo["objects/serviceavailability"]=".serviceavailability[]"
APIinfo["objects/sla"]=""
APIinfo["objects/bpi"]=""

do_debug() {
  [ "$verbose" -ge "$1" ] && echo "$2" >&2
}

get_myAPI() {
  case "$1" in
    o*) myOptions[API]="objects";;
    c*) myOptions[API]="config";;
    s*) myOptions[API]="system";;
    *)  myOptions[API]="";;
  esac
}

get_myAPIep() {
  case "$1" in
    co|comment) myOptions[APIep]="comment";;
    com|command) myOptions[APIep]="command";;
    cg|contactgroup) myOptions[APIep]="contactgroup";;
    c|contact) myOptions[APIep]="contact";;
    dt|downtime) myOptions[APIep]="downtime";;
    ha|hostavailability) myOptions[API]="objects"; myOptions[APIep]="hostavailability";;
    hgm|hostgroupmembers) myOptions[API]="objects"; myOptions[APIep]="hostgroupmembers";;
    hg|hostgroup) myOptions[APIep]="hostgroup";;
    h|host) myOptions[APIep]="host";;
    hs|hoststatus) myOptions[APIep]="hoststatus";;
    le|logentries) myOptions[APIep]="logentries";;
    sgm|servicegroupmembers) myOptions[API]="objects"; myOptions[APIep]="servicegroupmembers";;
    sg|servicegroup) myOptions[APIep]="servicegroup";;
    sa|serviceavailability) myOptions[API]="objects"; myOptions[APIep]="serviceavailability";;
    s|service) myOptions[APIep]="service";;
    ss|servicestatus) myOptions[APIep]="servicestatus";;
    sh|statehistory) myOptions[API]="objects"; myOptions[APIep]="statehistory";;
    tp|timeperiod) myOptions[APIep]="timeperiod";;
    *) myOptions[APIep]="";;
  esac
}

print_helpopt() {
  cat << HELPOPT_EOF
    Use these options to add options to the command specified.  To add multiple options, specify -o|--opt multiple times (they will add together)

    Options for different APIs:

      all:
	i	Make all string comparisons case-insentitive

      servicestatus|hoststatus:
        c	Show fields selected by -f as quoted CSV
	C	Same as C but also include the fields
	X	Same as C but use a more human-friendly set of names for the fields when printed

      hostgroupmembers:
	h	Only show hosts (not the complete JSON data)
	o	Only show host_object_id (specifying both options will result in errors)

      servicegroupmembers:
	h	Only show hosts (not the complete JSON data)
        s	Only show service names (not the complete JSON data)
	o	Only show service_object_id (specifying both options will result in errors)

HELPOPT_EOF
  exit
}

print_help() {
  cat << HELP_EOF
    --api	< o*bjects | c*onfig | s*ystem >
    -t|--object	< hoststatus | servicestatus | logentries | statehistory | ...
    --url	XI_URL=<value>

    --ack	problem_has_been_acknowledged=<0,1>
    --ace	active_checks_enabled=<0,1>
    -c|--command	check_command=<value>
    -cca	current_check_attempt=<value>
    --col	column name=<type>:<value>
		Ex: name=lk:local - Displays any matching name with 'local' anywhere in the string.
		Ex: name=in:localhost,nagios,testhost - Displays any matching name with the comma-separated list.
		Note: Multiple --col selectors are not valid at this time
    -cnn	current_notification_number=<n>
    --cname	command_name=<...>
    --cline	command_line=<...>
    --ctrace	Take the --cname given, get its --cline, and replace arguments to show a valid command (whew!)
    -cg|--contactgroup	contact_groups=<value>
    -cn|--configname	config_name=<value>
		... comment | downtime | contact | host | service | hostgroup | ...
    --create	doCreate="true"
    --apply	If we're creating something, then Apply Configuration
    -D		<Nagios Config Directive>=<value> This will take anything listed as a Nagios configuration directive and search for it
    -f|--fields	JQ-valid list of fields to show=<value>
    --file	load JSON from=<value>
    --helpopt	Show help for command options
    --help	This text
    -hg|--hostgroup	hostgroup=<value>
    -hgm	hostgroup_members (mainly for creating hostgroups)
    -h|--host	host_name=<value>
		... hostgroupmembers | servicegroupmembers >
    -j|--jq	additional valid JQ=<value>
    --key	APIKEY=<value>
    --keyfile	APIkeyFile=<value>
    -o|--opt	cmdOptions=<value> (endpoint specific options.  See --helpopt)
    --order 	orderby	<column>:<a or d>
    --output		output text=<value>
    -Q|--quick	Sets -f to .host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged (assumes servicestatus)
    -q		Same as --quick but add -o c
    -qq		Same as --quick but add -o c but also print the first line of the CSV output (fields)
    --records	records	<amount>:<starting at>
    --save	save JSON to=<value>
    -sg|--servicegroup	servicegroup=<value>
    -sgm	servicegroup_members (mainly for creating servicegroups)
    -s|--service	service_description=<value>
    --start	For things that have date selectors, this is a "show me after" selector: YYYYMMDDHHMMSS
    --end	For things that have date selectors, this is a "show me before" selector: YYYYMMDDHHMMSS
    --status	Quick select for showing similar things that you would see on main servicestatus page in the GUI
    --bstatus	Same as --status but only show things that are not in an OK state
    --state	0, 1, or 2 (or other, I suppose)
    --stype	0, 1 (SOFT or HARD)
    --test	Don't call the API, just show what would happen
    -v|--verbose	verbose=\$((\$verbose + 1))
HELP_EOF
  exit
}

# We're going to do nagiostats instead
do_stats() {
  if [ ! -x "/usr/local/nagios/bin/nagiostats" ]; then
    echo "Cannot find nagiostats program."
    exit 1
  fi
  /usr/local/nagios/bin/nagiostats --mrtg --data=MINACTHSTLAT,MAXACTHSTLAT,AVGACTHSTLAT | xargs | while read min max avg; do
    echo "Active Host Check Latency (min/max/avg in ms): $min / $max / $avg"
  done
  /usr/local/nagios/bin/nagiostats --mrtg --data=MINACTSVCLAT,MAXACTSVCLAT,AVGACTSVCLAT | xargs | while read min max avg; do
    echo "Active Service Check Latency (min/max/avg in ms): $min / $max / $avg"
  done
  exit
}

# Convert YYYYMMDDHHSS to Unix Timestamp - for instance, date -d "20240701 08:05" +"%s"
convert_time() {
  do_debug 2 "CONVERT_TIME input is $1"
  theYear="${1:0:4}"; [ -z "$theYear" ] && echo "" && return		# We need a valid year, at least
  theMonth="${1:4:2}"; [ -z "$theMonth" ] && theMonth="01"
  theDay="${1:6:2}"; [ -z "$theDay" ] && theDay="01"
  theHour="${1:8:2}"; [ -z "$theHour" ] && theHour="00"
  theMinute="${1:10:2}"; [ -z "$theMinute" ] && theMinute="00"
  theSecond="${1:12:2}"; [ -z "$theSecond" ] && theSecond="00"
  theTime="$theYear-$theMonth-$theDay $theHour:$theMinute:$theSecond"
  do_debug 2 "CONVERT_TIME output is $theTime"
  do_debug 2 `date -d "$theTime" +"%s"`
  date -d "$theTime" +"%s"
}

# get_opts
while [ -n "$1" ]; do
  case "$1" in
    --helpopt) print_helpopt;;
    --help) print_help;;
    --key) APIKEY="$2"; shift 2;;
    --keyfile) APIkeyFile="$2"; shift 2;;
    --url) XI_URL="$2"; shift 2;;
    -v|--verbose) verbose=$(($verbose + 1)); shift 1;;
    --api) get_myAPI "$2"; shift 2;;
    -t|--object) get_myAPIep "$2"; shift 2;;
    --stats) do_stats; shift 2;;
    --ack) myDir[problem_has_been_acknowledged]="$2"; shift 2;;
    --ace) myDir[active_checks_enabled]="$2"; shift 2;;
    -c|--command) myDir[check_command]="$2"; shift 2;;
    -cca) myDir[current_check_attempt]="$2"; shift 2;;
    --cname) myDir[command_name]="$2"; shift 2;;
    --cline) myDir[command_line]="$2"; shift 2;;
    -cnn) myDir[current_notification_number]="$2"; shift 2;;
    -cg|--contactgroup) myDir[contact_groups]="$2"; shift 2;;
    -cn|--configname) myDir[config_name]="$2"; shift 2;;
    -D) myDir[$2]="$3"; shift 3;;
    -hg|--hostgroup) myDir[hostgroup_name]="$2"; shift 2;;
    -hgm) myDir[hostgroup_members]="$2"; shift 2;;
    -h|--host) myDir[host_name]="$2"; shift 2;;
    --output) myDir[output]="$2"; shift 2;;
    -sg|--servicegroup) myDir[servicegroup_name]="$2"; shift 2;;
    -sgm) myDir[servicegroup_members]="$2"; shift 2;;
    -s|--service) myDir[service_description]="$2"; shift 2;;
    --state) myDir[current_state]="$2"; shift 2;;
    --stype) myDir[state_type]="$2"; shift 2;;
    --apply) myOptions[Apply]="true"; shift 1;;
    --col) myOptions[Column]="$2"; shift 2;;
    --ctrace) myOptions[CommandTrace]="true"; shift 1;;
    --create) myOptions[Create]="true"; shift 1;;
    -f|--fields) myOptions[Fields]="$2"; shift 2;;
    --file) myOptions[File]="$2"; shift 2;;
    -j|--jq) myOptions[MoreJQ]="$2"; shift 2;;
    -o|--opt) myOptions[Options]+="$2,"; shift 2;;
    --order) myOptions[OrderBy]="$2"; shift 2;;
    -q) myOptions[Quick]="true"; myOptions[Options]+="c,"; shift 1;;
    -qq) myOptions[Quick]="true"; myOptions[Options]+="c,C,"; shift 1;;
    -Q|--quick) myOptions[Quick]="true"; shift 1;;
    --records) myOptions[Records]="$2"; shift 2;;
    --save) myOptions[Save]="$2"; shift 2;;
    --end) myOptions[End]="$2"; shift 2;;
    --start) myOptions[Start]="$2"; shift 2;;
    --status) myOptions[Status]="true"; shift 1;;
    --bstatus) myOptions[Status]="true"; myOptions[BStatus]="true"; shift 1;;
    --test) myOptions[TestMode]="true"; shift 1;;
    *) shift 1;;
  esac
done

# Fix things that are impossible to call
[ "${myOptions[API]}" = "config" -a "${myOptions[APIep]}" = "servicestatus" ] && myOptions[APIep]="service"
[ "${myOptions[API]}" = "config" -a "${myOptions[APIep]}" = "hoststatus" ] && myOptions[APIep]="host"

[ -n "${myOptions[Start]}" ] && myOptions[Start]=`convert_time "${myOptions[Start]}"`
[ -n "${myOptions[End]}" ] && myOptions[End]=`convert_time "${myOptions[End]}"`
# If we're doing status mode, then override a bunch of other options
if [ -n "${myOptions[Status]}" ]; then
  myOptions[API]="objects"
  myOptions[APIep]="servicestatus"
  myOptions[Create]=""
  myOptions[Apply]=""
  myOptions[Options]=""
  myOptions[Quick]="true"
  myOptions[Options]+="c,X,"
  # If we did a bstatus, then we only want things that are bad
  if [ -n "${myOptions[BStatus]}" ]; then
    myDir[current_state]="[^0]"
  fi
fi

if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then
do_debug 1 "Looking for url=$url in $APIkeyFile"
  while read url key; do
    if [ -z "$XI_URL" ]; then
      XI_URL="$url"
      APIKEY="$key"
      continue
    fi
    if [ "$url" = "$XI_URL" ]; then
      APIKEY="$key"
    fi
  done < "$APIkeyFile"
fi

do_api() {
  api_start="$1"
  api_command="$2"
  url="${XI_URL}/api/v1/${api_start}/${api_command}?pretty=0&apikey=${APIKEY}"
  [ -n "${myOptions[Start]}" ] && url+="&starttime=${myOptions[Start]}"
  [ -n "${myOptions[End]}" ] && url+="&endtime=${myOptions[End]}"
  [ -n "${myOptions[Records]}" ] && url+="&records=${myOptions[Records]}"
  [ -n "${myOptions[OrderBy]}" ] && url+="&orderby=${myOptions[OrderBy]}"
  if [ -n "${myOptions[Column]}" ]; then
    colName=`echo "${myOptions[Column]}" | cut -d= -f 1`
    colVal=`echo "${myOptions[Column]}" | cut -d= -f 2`
    do_debug 1 "colName=$colName colVal=$colVal"
    url+="&$colName=$colVal"
  fi
  do_debug 2 "start=$1 command=$2"
  do_debug 1 "Executing (tesmode=${myOptions[TestMode]}): $url"
  [ -z "${myOptions[TestMode]}" ] && $curl -XGET -k "$url"
}

# curl -XPOST "http://192.168.1.128/nagiosxi/api/v1/config/hostgroup?apikey=fsZZ4pXaKaVjSG7IYcjMRYhK8NqcqN2NGPck8gPhFoZMJGKj4YUjZCF8qSqsK7Ln&pretty=1" -d "hostgroup_name=testapihostgroup&alias=HostGroup&applyconfig=1"
do_api_post() {
  api_start="$1"
  api_command="$2"
  api_data=""
  [ -n "$3" ] && api_data="$3&applyconfig=0"
  url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0"
  do_debug 2 "start=$1 command=$2"
  do_debug 1 "Executing: $curl -XPOST -k \"$url\" -d \"$api_data\""
  [ -z "${myOptions[TestMode]}" ] && $curl -XPOST -k "$url" -d "$api_data"
}

# At this time, all we can create ia a hostgroup
create_hostgroup() {
  do_debug 1 "about to do an API post call"
  api_data="hostgroup_name=${myDir[hostgroup_name]}&alias=${myDir[hostgroup_name]}"
  [ -n "${myDir[host_name]}" ] && api_data+="&members=${myDir[host_name]}"
  [ -n "${myDir[hostgroup_members]}" ] && api_data+="&hostgroup_members=${myDir[hostgroup_members]}"
  do_api_post config hostgroup "$api_data"
}

create_servicegroup() {
  do_debug 1 "about to do an API post call"
  api_data="servicegroup_name=${myDir[servicegroup_name]}&alias=${myDir[servicegroup_name]}"
  [ -n "${myDir[service_description]}" ] && api_data+="&members=${myDir[service_description]}"
  [ -n "${myDir[servicegroup_members]}" ] && api_data+="&servicegroup_members=${myDir[servicegroup_members]}"
  do_api_post config servicegroup "$api_data"
}

do_create() {
  do_debug 1 "about to do a create_command"
  [ -n "${myDir[hostgroup_name]}" ] && create_hostgroup
  [ -n "${myDir[servicegroup_members]}" ] && create_servicegroup
}

do_apply() {
  do_debug 1 "About to Apply Configuration..."
  do_api_post system applyconfig
}

# If we said "--create" then we want to make something
if [ -n "${myOptions[Create]}" ]; then
  do_create
  [ -n "${myOptions[Apply]}" -a -z "${myOptions[TestMode]}" ] && do_apply
  exit
fi

# Grab a copy of the JSON data so we don't have to keep making calls over and over
# If we used an existing file, then just use that
# If we're in test mode, then skip this part
if [ -z "${myOptions[File]}" ]; then
  tmpJSON=`mktemp`
  do_debug 1 "tmp file is $tmpJSON"
  do_debug 2 "  myAPI is ${myOptions[API]} and myAPIep is ${myOptions[APIep]}"
  do_api "${myOptions[API]}" "${myOptions[APIep]}" > $tmpJSON
else
  do_debug 1 "myFile=${myOptions[File]}"
  tmpJSON="${myOptions[File]}"
  do_debug 1 "tmpJSON=$tmpJSON"
fi

# if mySave is not empty, then we're just saving it into the file called ${myOptions[Save]}
if [ -n "${myOptions[Save]}" ]; then
  mv $tmpJSON ${myOptions[Save]}
  do_debug 1 "JSON data saved to ${myOptions[Save]}"
  exit
fi

# Helper functions for creating our jqString
# First, we need to know if our tests should be case-sensitive or not
jq_check_case() {
  thing="$*"
  do_debug 1 "### JQ_CHECK_CASE looking for thing=$thing"
  if [ -n "$thing" ]; then
    do_debug 2 "### in JQ_CHECK_CASE cmdOptions=${myOptions[Options]}"
    val="| test(\"$thing\""
    [[ "${myOptions[Options]}" =~ "i," ]] && val+="; \"i\""
    echo "$val)"
  else
    echo ""
  fi
}
  
# Create the jQuery search string
jq_get_fields() {
  [[ "${myOptions[Options]}" =~ "c," ]] && jqString+="| [${myOptions[Fields]}] | @csv" || jqString+="| ${myOptions[Fields]}"
}

# Otherwise, let's parse the JSON data here
# Parse our string
do_debug 1 "APIinfo=${APIinfo[${myOptions[API]}/${myOptions[APIep]}]}"
jqString=${APIinfo[${myOptions[API]}/${myOptions[APIep]}]}
do_debug 2 "  Before: jqString=$jqString"
for thing in "${!myDir[@]}"; do
  [ -n "${myDir[$thing]}" ] && jqString+="| select(.$thing $(jq_check_case ${myDir[$thing]}))"
  do_debug 2 "  After: jqString=$jqString"
done

# endpoint specific things
case "${myOptions[API]}/${myOptions[APIep]}" in
  objects/hostgroupmembers)
    [[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
    [[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
    ;;
  objects/servicegroupmembers)
    [[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
    [[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .service_object_id"
    [[ ${myOptions[Options]} =~ "s," ]] && jqString+="| .members[] | .[] | .service_description"
    tmpQuick=".service_object_id,.host_name,.service_description"
    ;;
  objects/hoststatus) tmpQuick=".host_name,.address,.current_state,.state_type,.last_check,.current_check_attempt,.normal_check_interval,.retry_check_interval,.max_check_attempts,.check_command";
	tmpQuicker="Host,Address,State,Type,Last Check,Attempt,Normal,Retry,Max,Command";;
  objects/servicestatus) tmpQuick=".service_description,.host_name,.current_state,.state_type,.last_check,.current_check_attempt,.normal_check_interval,.retry_check_interval,.max_check_attempts,.output";
	tmpQuicker="Service,Host,State,Type,Last Check,Attempt,Normal,Retry,Max,Output";;
  objects/contact) tmpQuick=".contact_name,.email_address,.host_notifications_enabled,.service_notifications_enabled,.is_active";;
  objects/statehistory) tmpQuick=".host_name,.service_description,.state_time,.state_change,.state,.state_type,.current_check_attempt,.max_check_attempts,.last_state,.last_hard_state,.output";;
  config/host) tmpQuick=".host_name,.address,.check_command";;
esac

do_debug 1 "myOptions[Options]=${myOptions[Options]}"
if [ -n "${myOptions[Quick]}" -a -n "${myOptions[Fields]}" ]; then
  myOptions[Fields]="$tmpQuick,${myOptions[Fields]}"
elif [ -n "${myOptions[Quick]}" ]; then
  myOptions[Fields]="$tmpQuick"
fi
[[ ${myOptions[Options]} =~ "C," ]] && echo "${myOptions[Fields]}"
[[ ${myOptions[Options]} =~ "X," ]] && echo "$tmpQuicker" && printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
do_debug 1 "myOptions[Fields]=${myOptions[Fields]}"
[ -n "${myOptions[Fields]}" ] && jq_get_fields
jqString+="${myOptions[MoreJQ]}"
do_debug 1 "jqString=$jqString"

if [ -n "${myOptions[TestMode]}" ]; then
  echo "### TEST MODE - Here is what would have been done (testMode=${myOptions[TestMode]}) ###"
  echo "cat <tmpfile> | jq -r \"$jqString\""
else
  cat $tmpJSON | jq -r "$jqString"
fi
[ -z "${myOptions[File]}" ] && rm $tmpJSON
