Added lookup table for JSON starting point and more GET API options

dev
Eric Loyd 2 years ago
parent 4702af87a7
commit ca4e6532e2

142
api.sh

@ -14,6 +14,7 @@ doCreate=""
myConfigName="" myConfigName=""
myHost="" myHost=""
myService="" myService=""
myCG=""
myHG="" myHG=""
mySG="" mySG=""
myFields="" myFields=""
@ -26,23 +27,101 @@ moreJQ=""
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
declare -A APIinfo
APIinfo["config/host"]=""
APIinfo["config/service"]=""
APIinfo["config/hostgroup"]=".[]"
APIinfo["config/servicegroup"]=".[]"
APIinfo["config/command"]=""
APIinfo["config/contact"]=""
APIinfo["config/contactgroup"]=".[]"
APIinfo["config/timeperiod"]=""
APIinfo["objects/hoststatus"]=""
APIinfo["objects/servicestatus"]=".servicestatus[]"
APIinfo["objects/logentries"]=""
APIinfo["objects/statehistory"]=""
APIinfo["objects/comment"]=""
APIinfo["objects/downtime"]=""
APIinfo["objects/contact"]=""
APIinfo["objects/host"]=".[]"
APIinfo["objects/service"]=".[]"
APIinfo["objects/hostgroup"]=".[]"
APIinfo["objects/servicegroup"]=""
APIinfo["objects/contactgroup"]=".contactgroup[]"
APIinfo["objects/timeperiod"]=""
APIinfo["objects/unconfigured"]=""
APIinfo["objects/hostgroupmembers"]=".hostgroup[]"
APIinfo["objects/servicegroupmembers"]=""
APIinfo["objects/contactgroupmembers"]=""
APIinfo["objects/rrdexport"]=""
APIinfo["objects/cpexport"]=""
APIinfo["objects/hostavailability"]=""
APIinfo["objects/serviceavailability"]=""
APIinfo["objects/sla"]=""
APIinfo["objects/bpi"]=""
get_myAPI() { get_myAPI() {
case "$1" in case "$1" in
objects|config|system) myAPI="$1";; o*) myAPI="objects";;
c*) myAPI="config";;
s*) myAPI="system";;
*) myAPI="";; *) myAPI="";;
esac esac
} }
get_myAPIep() { get_myAPIep() {
case "$1" in case "$1" in
hoststatus|servicestatus|logentries|statehistory|comment|downtime|contact|host|service|hostgroup|servicegroup|contactgroup|timeperiod) myAPIep="$1";; co|comment) myAPIep="comment";;
hostgroupmembers|servicegroupmembers) myAPIep="$1";; cg|contactgroup) myAPIep="contactgroup";;
c|contact) myAPIep="contact";;
dt|downtime) myAPIep="downtime";;
hgm|hostgroupmembers) myAPIep="hostgroupmembers";;
hg|hostgroup) myAPIep="hostgroup";;
h|host) myAPIep="host";;
hs|hoststatus) myAPIep="hoststatus";;
le|logentries) myAPIep="logentries";;
sgm|servicegroupmembers) myAPIep="servicegroupmembers";;
sg|servicegroup) myAPIep="servicegroup";;
s|service) myAPIep="service";;
ss|servicestatus) myAPIep="servicestatus";;
sh|statehistory) myAPIep="statehistory";;
tp|timeperiod) myAPIep="timeperiod";;
*) myAPIep="";; *) myAPIep="";;
esac esac
} }
print_help() {
cat << HELP_EOF
--keyfile APIkeyFile="\$2"
-j|--jq moreJQ="\$2"
-v|--verbose verbose=\$((\$verbose + 1))
--create doCreate="true"
--key APIKEY="\$2"
--url XI_URL="\$2"
--save mySave="\$2"
--file myFile="\$2"
-f|--fields myFields="\$2"
-h|--host myHost="\$2"
-s|--service myService="\$2"
-c|--command myCommand="\$2"
-cn|--configname myConfigName="\$2"
-cg|--contactgroup myCG="\$2"
-hg|--hostgroup myHG="\$2"
-sg|--servicegroup mySG="\$2"
--api < o*bjects | c*onfig | s*ystem >
-t|--object < hoststatus | servicestatus | logentries | statehistory | ...
... comment | downtime | contact | host | service | hostgroup | ...
... servicegroup | contactgroup | timeperiod | ...
... hostgroupmembers | servicegroupmembers >
HELP_EOF
exit
}
while [ -n "$1" ]; do while [ -n "$1" ]; do
case "$1" in case "$1" in
--help) print_help;;
--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;;
@ -56,6 +135,7 @@ while [ -n "$1" ]; do
-s|--service) myService="$2"; shift 2;; -s|--service) myService="$2"; shift 2;;
-c|--command) myCommand="$2"; shift 2;; -c|--command) myCommand="$2"; shift 2;;
-cn|--configname) myConfigName="$2"; shift 2;; -cn|--configname) myConfigName="$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;;
--api) get_myAPI "$2"; shift 2;; --api) get_myAPI "$2"; shift 2;;
@ -139,30 +219,38 @@ fi
# Otherwise, let's parse the JSON data here # Otherwise, let's parse the JSON data here
# Parse our string # Parse our string
jqString="" do_debug 1 "APIinfo=${APIinfo[$myAPI/$myAPIep]}"
if [ "$myAPIep" = "servicestatus" ]; then jqString=${APIinfo[$myAPI/$myAPIep]}
jqString=".servicestatus[]" case "$myAPIep" in
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" servicestatus)
[ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))" [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
[ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))" [ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))"
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")" [ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))"
[ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")" [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
elif [ "$myAPIep" = "hostgroup" ]; then [ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")"
jqString=".[]" ;;
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")" hostgroup)
elif [ "$myAPIep" = "host" ]; then [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
jqString=".[]" ;;
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" host)
elif [ "$myAPIep" = "service" ]; then [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
jqString=".[]" ;;
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" service)
[ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))" [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
[ -n "$myConfigName" ] && jqString="$jqString | select(.check_command | test(\"$myConfigName\"))" [ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))"
[ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))" [ -n "$myConfigName" ] && jqString="$jqString | select(.check_command | test(\"$myConfigName\"))"
elif [ "$myAPIep" = "hostgroupmembers" ]; then [ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))"
jqString=".hostgroup[]" ;;
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")" hostgroupmembers)
fi [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
;;
servicegroup)
[ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")"
;;
contactgroup)
[ -n "$myCG" ] && jqString="$jqString | select(.contactgroup_name==\"$myCG\")"
;;
esac
[ -n "$myFields" ] && jqString="$jqString | $myFields" [ -n "$myFields" ] && jqString="$jqString | $myFields"
jqString="$jqString $moreJQ" jqString="$jqString $moreJQ"
do_debug 1 "jqString=$jqString" do_debug 1 "jqString=$jqString"

Loading…
Cancel
Save