diff --git a/api.sh b/api.sh index 95830de..2351319 100755 --- a/api.sh +++ b/api.sh @@ -23,13 +23,13 @@ mySave="" myFile="" tmpJSON="" moreJQ="" - +cmdOptions="" myAPI="objects" myAPIep="servicestatus" # 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/host"]=".[]" APIinfo["config/service"]="" APIinfo["config/hostgroup"]=".[]" APIinfo["config/servicegroup"]=".[]" @@ -45,7 +45,7 @@ APIinfo["objects/statehistory"]="" APIinfo["objects/comment"]="" APIinfo["objects/downtime"]="" APIinfo["objects/contact"]="" -APIinfo["objects/host"]=".[]" +APIinfo["objects/host"]=".host[]" APIinfo["objects/service"]=".[]" APIinfo["objects/hostgroup"]=".[]" APIinfo["objects/servicegroup"]="" @@ -77,12 +77,12 @@ get_myAPIep() { cg|contactgroup) myAPIep="contactgroup";; c|contact) myAPIep="contact";; dt|downtime) myAPIep="downtime";; - hgm|hostgroupmembers) myAPIep="hostgroupmembers";; + hgm|hostgroupmembers) myAPI="objects"; myAPIep="hostgroupmembers";; hg|hostgroup) myAPIep="hostgroup";; h|host) myAPIep="host";; hs|hoststatus) myAPIep="hoststatus";; le|logentries) myAPIep="logentries";; - sgm|servicegroupmembers) myAPIep="servicegroupmembers";; + sgm|servicegroupmembers) myAPI="objects"; myAPIep="servicegroupmembers";; sg|servicegroup) myAPIep="servicegroup";; s|service) myAPIep="service";; ss|servicestatus) myAPIep="servicestatus";; @@ -92,8 +92,22 @@ get_myAPIep() { 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: + hostgroupmembers + h Only show hosts (not the complete JSON data) + o Only show host_object_id (specifying both options will result in errors) +HELPOPT_EOF + exit +} + print_help() { cat << HELP_EOF + --help This text + --helpopt Show help for command options --keyfile APIkeyFile="\$2" -j|--jq moreJQ="\$2" -v|--verbose verbose=\$((\$verbose + 1)) @@ -110,6 +124,7 @@ print_help() { -cg|--contactgroup myCG="\$2" -hg|--hostgroup myHG="\$2" -sg|--servicegroup mySG="\$2" + -o|--opt cmdOptions="\$2" (endpoint specific options. See --helpopt) --api < o*bjects | c*onfig | s*ystem > -t|--object < hoststatus | servicestatus | logentries | statehistory | ... ... comment | downtime | contact | host | service | hostgroup | ... @@ -122,6 +137,7 @@ HELP_EOF while [ -n "$1" ]; do case "$1" in --help) print_help;; + --helpopt) print_helpopt;; --keyfile) APIkeyFile="$2"; shift 2;; -j|--jq) moreJQ="$2"; shift 2;; -v|--verbose) verbose=$(($verbose + 1)); shift 1;; @@ -138,6 +154,7 @@ while [ -n "$1" ]; do -cg|--contactgroup) myCG="$2"; shift 2;; -hg|--hostgroup) myHG="$2"; shift 2;; -sg|--servicegroup) mySG="$2"; shift 2;; + -o|--opt) cmdOptions="$2,${cmdOptions}"; shift 2;; --api) get_myAPI "$2"; shift 2;; -t|--object) get_myAPIep "$2"; shift 2;; *) shift 1;; @@ -217,34 +234,43 @@ if [ -n "$mySave" ]; then exit fi +jq_add_host() { + [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" +} + # Otherwise, let's parse the JSON data here # Parse our string do_debug 1 "APIinfo=${APIinfo[$myAPI/$myAPIep]}" jqString=${APIinfo[$myAPI/$myAPIep]} case "$myAPIep" in servicestatus) - [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" + jq_add_host [ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))" [ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))" [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")" [ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")" ;; hostgroup) + jq_add_host [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")" ;; host) - [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" + jq_add_host ;; service) - [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" + jq_add_host [ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))" [ -n "$myConfigName" ] && jqString="$jqString | select(.check_command | test(\"$myConfigName\"))" [ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))" ;; hostgroupmembers) - [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")" + jq_add_host + [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name | test(\"$myHG\"))" + [[ $cmdOptions =~ "h," ]] && jqString="$jqString | .members[] | .[] | .host_name" + [[ $cmdOptions =~ "o," ]] && jqString="$jqString | .members[] | .[] | .host_object_id" ;; servicegroup) + jq_add_host [ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")" ;; contactgroup)