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

dev
Eric Loyd 1 year ago
parent 4702af87a7
commit ca4e6532e2

142
api.sh

@ -14,6 +14,7 @@ doCreate=""
myConfigName=""
myHost=""
myService=""
myCG=""
myHG=""
mySG=""
myFields=""
@ -26,23 +27,101 @@ moreJQ=""
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/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() {
case "$1" in
objects|config|system) myAPI="$1";;
o*) myAPI="objects";;
c*) myAPI="config";;
s*) myAPI="system";;
*) myAPI="";;
esac
}
get_myAPIep() {
case "$1" in
hoststatus|servicestatus|logentries|statehistory|comment|downtime|contact|host|service|hostgroup|servicegroup|contactgroup|timeperiod) myAPIep="$1";;
hostgroupmembers|servicegroupmembers) myAPIep="$1";;
co|comment) myAPIep="comment";;
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="";;
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
case "$1" in
--help) print_help;;
--keyfile) APIkeyFile="$2"; shift 2;;
-j|--jq) moreJQ="$2"; shift 2;;
-v|--verbose) verbose=$(($verbose + 1)); shift 1;;
@ -56,6 +135,7 @@ while [ -n "$1" ]; do
-s|--service) myService="$2"; shift 2;;
-c|--command) myCommand="$2"; shift 2;;
-cn|--configname) myConfigName="$2"; shift 2;;
-cg|--contactgroup) myCG="$2"; shift 2;;
-hg|--hostgroup) myHG="$2"; shift 2;;
-sg|--servicegroup) mySG="$2"; shift 2;;
--api) get_myAPI "$2"; shift 2;;
@ -139,30 +219,38 @@ fi
# Otherwise, let's parse the JSON data here
# Parse our string
jqString=""
if [ "$myAPIep" = "servicestatus" ]; then
jqString=".servicestatus[]"
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
[ -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\")"
elif [ "$myAPIep" = "hostgroup" ]; then
jqString=".[]"
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
elif [ "$myAPIep" = "host" ]; then
jqString=".[]"
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
elif [ "$myAPIep" = "service" ]; then
jqString=".[]"
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
[ -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\"))"
elif [ "$myAPIep" = "hostgroupmembers" ]; then
jqString=".hostgroup[]"
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
fi
do_debug 1 "APIinfo=${APIinfo[$myAPI/$myAPIep]}"
jqString=${APIinfo[$myAPI/$myAPIep]}
case "$myAPIep" in
servicestatus)
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
[ -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)
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
;;
host)
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
;;
service)
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
[ -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\")"
;;
servicegroup)
[ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")"
;;
contactgroup)
[ -n "$myCG" ] && jqString="$jqString | select(.contactgroup_name==\"$myCG\")"
;;
esac
[ -n "$myFields" ] && jqString="$jqString | $myFields"
jqString="$jqString $moreJQ"
do_debug 1 "jqString=$jqString"

Loading…
Cancel
Save