Added cmdOptions to be able to do things differently for different API endpoints

dev
Eric Loyd 1 year ago
parent ca4e6532e2
commit c7d53a00fd

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

Loading…
Cancel
Save