api.sh Updated to do a lot more with the -q options

dev
Eric Loyd 1 year ago
parent a69c3b3b96
commit 293b51ddcf

139
api.sh

@ -9,10 +9,10 @@ XI_URL=""
curl="curl -k -s"
# curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=<key>"
# myThings are now too big for individual variables. We'll read them into an associative array instead
declare -A myThing
# 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
# 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"
@ -21,6 +21,7 @@ myOptions[Create]=""
myOptions[Options]=""
verbose="0"
tmpJSON=""
tmpQuick=""
# 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
@ -29,17 +30,17 @@ APIinfo["config/service"]=""
APIinfo["config/hostgroup"]=".[]"
APIinfo["config/servicegroup"]=".[]"
APIinfo["config/command"]=""
APIinfo["config/contact"]=""
APIinfo["config/contact"]=".contact[]"
APIinfo["config/contactgroup"]=".[]"
APIinfo["config/timeperiod"]=""
APIinfo["objects/hoststatus"]=""
APIinfo["objects/hoststatus"]=".hoststatus[]"
APIinfo["objects/servicestatus"]=".servicestatus[]"
APIinfo["objects/logentries"]=""
APIinfo["objects/statehistory"]=""
APIinfo["objects/comment"]=""
APIinfo["objects/downtime"]=""
APIinfo["objects/contact"]=""
APIinfo["objects/contact"]=".contact[]"
APIinfo["objects/host"]=".host[]"
APIinfo["objects/service"]=".[]"
APIinfo["objects/hostgroup"]=".[]"
@ -48,7 +49,7 @@ APIinfo["objects/contactgroup"]=".contactgroup[]"
APIinfo["objects/timeperiod"]=""
APIinfo["objects/unconfigured"]=""
APIinfo["objects/hostgroupmembers"]=".hostgroup[]"
APIinfo["objects/servicegroupmembers"]=""
APIinfo["objects/servicegroupmembers"]=".servicegroup[]"
APIinfo["objects/contactgroupmembers"]=""
APIinfo["objects/rrdexport"]=""
APIinfo["objects/cpexport"]=""
@ -103,6 +104,11 @@ print_helpopt() {
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
}
@ -112,36 +118,37 @@ print_help() {
--ack problem_has_been_acknowledged=<0,1>
--ace active_checks_enabled=<0,1>
--api < o*bjects | c*onfig | s*ystem >
-c|--command check_command="\$2"
-cca current_check_attempt="\$2"
-c|--command check_command=<value>
-cca current_check_attempt=<value>
-cnn current_notification_number=<n>
-cg|--contactgroup contact_groups="\$2"
-cn|--configname config_name="\$2"
-cg|--contactgroup contact_groups=<value>
-cn|--configname config_name=<value>
... comment | downtime | contact | host | service | hostgroup | ...
--create doCreate="true"
-f|--fields JQ-valid list of fields to show="\$2"
--file load JSON from="\$2"
-D <Nagios Config Directive>=<value>
-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="\$2"
-h|--host host_name="\$2"
-hg|--hostgroup hostgroup=<value>
-h|--host host_name=<value>
... hostgroupmembers | servicegroupmembers >
-j|--jq additional valid JQ="\$2"
--key APIKEY="\$2"
--keyfile APIkeyFile="\$2"
-o|--opt cmdOptions="\$2" (endpoint specific options. See --helpopt)
--output output text="\$2"
-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
--save save JSON to="\$2"
... servicegroup | contactgroup | timeperiod | ...
-sg|--servicegroup servicegroup="\$2"
-s|--service service_description="\$2"
-j|--jq additional valid JQ=<value>
--key APIKEY=<value>
--keyfile APIkeyFile=<value>
-o|--opt cmdOptions=<value> (endpoint specific options. See --helpopt)
--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)
--save save JSON to=<value>
-sg|--servicegroup servicegroup=<value>
-s|--service service_description=<value>
--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
-t|--object < hoststatus | servicestatus | logentries | statehistory | ...
--url XI_URL="\$2"
--url XI_URL=<value>
-v|--verbose verbose=\$((\$verbose + 1))
[...] [...]=<...> NOTE: This will take anything listed as a Nagios configuration directive and search for it
@ -151,33 +158,35 @@ HELP_EOF
while [ -n "$1" ]; do
case "$1" in
--ack) myThing[problem_has_been_acknowledged]="$2"; shift 2;;
--ace) myThing[active_checks_enabled]="$2"; shift 2;;
--ack) myDir[problem_has_been_acknowledged]="$2"; shift 2;;
--ace) myDir[active_checks_enabled]="$2"; shift 2;;
--api) get_myAPI "$2"; shift 2;;
-c|--command) myThing[check_command]="$2"; shift 2;;
-cca) myThing[current_check_attempt]="$2"; shift 2;;
-cnn) myThing[current_notification_number]="$2"; shift 2;;
-cg|--contactgroup) myThing[contact_groups]="$2"; shift 2;;
-cn|--configname) myThing[config_name]="$2"; shift 2;;
-c|--command) myDir[check_command]="$2"; shift 2;;
-cca) myDir[current_check_attempt]="$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;;
--create) myOptions[Create]="true"; shift 1;;
-D) myDir[$2]="$3"; shift 3;;
-f|--fields) myOptions[Fields]="$2"; shift 2;;
--file) myOptions[File]="$2"; shift 2;;
--helpopt) print_helpopt;;
--help) print_help;;
-hg|--hostgroup) myThing[hostgroup_name]="$2"; shift 2;;
-h|--host) myThing[Host]="$2"; shift 2;;
-hg|--hostgroup) myDir[hostgroup_name]="$2"; shift 2;;
-h|--host) myDir[host_name]="$2"; shift 2;;
-j|--jq) myOptions[MoreJQ]="$2"; shift 2;;
--key) APIKEY="$2"; shift 2;;
--keyfile) APIkeyFile="$2"; shift 2;;
-o|--opt) myOptions[Options]+="$2,"; shift 2;;
--output) myThing[Output]="$2"; shift 2;;
-Q) myOptions[Quick]="true"; myOptions[Options]+="c,"; shift 1;;
-q|--quick) myOptions[Quick]="true"; shift 1;;
--output) myDir[Output]="$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;;
--save) myOptions[Save]="$2"; shift 2;;
-sg|--servicegroup) myThing[SG]="$2"; shift 2;;
-s|--service) myThing[Service]="$2"; shift 2;;
--state) myThing[State]="$2"; shift 2;;
--stype) myThing[StateType]="$2"; shift 2;;
-sg|--servicegroup) myDir[servicegroup_name]="$2"; shift 2;;
-s|--service) myDir[Service]="$2"; shift 2;;
--state) myDir[State]="$2"; shift 2;;
--stype) myDir[StateType]="$2"; shift 2;;
--test) myOptions[TestMode]="true"; shift 1;;
-t|--object) get_myAPIep "$2"; shift 2;;
--url) XI_URL="$2"; shift 2;;
@ -219,7 +228,7 @@ do_api_post() {
url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0"
do_debug 2 "start=$1 command=$2"
do_debug 1 "Executing: $url"
$curl -XPOST -k "$url" -d "hostgroup_name=${myThing[HG]}&alias=${myThing[HG]}&applyconfig=0"
$curl -XPOST -k "$url" -d "hostgroup_name=${myDir[HG]}&alias=${myDir[HG]}&applyconfig=0"
}
# At this time, all we can create ia a hostgroup
@ -230,7 +239,7 @@ create_hostgroup() {
do_create() {
do_debug 1 "about to do a create_command"
[ -n "${myThing[HG]}" ] && create_hostgroup
[ -n "${myDir[HG]}" ] && create_hostgroup
exit
}
@ -282,43 +291,39 @@ jq_check_case() {
# Create the jQuery search string
jq_get_fields() {
if [[ "$myOptions[Options]" =~ "c," ]]; then
jqString+="| [${myOptions[Fields]}] | @csv"
else
jqString+="| ${myOptions[Fields]}"
fi
[[ "${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[${myOptiosn[API]}/${myOptions[APIep]}]}"
jqString=${APIinfo[${myOptions[API]}/${myOptions[APIep]}]}
for thing in "${!myThing[@]}"; do
[ -n "${myThing[$thing]}" ] && jqString+="| select(.$thing $(jq_check_case ${myThing[$thing]}))"
for thing in "${!myDir[@]}"; do
[ -n "${myDir[$thing]}" ] && jqString+="| select(.$thing $(jq_check_case ${myDir[$thing]}))"
done
case "${myOptions[APIep]}" in
servicestatus)
;;
hostgroup)
;;
host)
;;
service)
;;
hostgroupmembers)
[[ $myOptions[Options] =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
[[ $myOptions[Options] =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
;;
servicegroup)
[[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
[[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
;;
contactgroup)
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"
;;
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";;
servicestatus) tmpQuick=".service_description,.host_name,.current_state,.state_type,.last_check,.current_check_attempt,.normal_check_interval,.retry_check_interval,.max_check_attempts,.check_command";;
contact) tmpQuick=".contact_name,.email_address,.host_notifications_enabled,.service_notifications_enabled,.is_active";;
esac
do_debug 1 "myOptions[Options]=${myOptions[Options]}"
if [ -n "${myOptions[Quick]}" -a -n "${myOptions[Fields]}" ]; then
myOptions[Fields]=".host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged,${myOptions[Fields]}"
myOptions[Fields]="$tmpQuick,${myOptions[Fields]}"
elif [ -n "${myOptions[Quick]}" ]; then
myOptions[Fields]=".host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged"
myOptions[Fields]="$tmpQuick"
fi
[[ ${myOptions[Options]} =~ "C," ]] && echo "${myOptions[Fields]}"
do_debug 1 "myOptions[Fields]=${myOptions[Fields]}"
[ -n "${myOptions[Fields]}" ] && jq_get_fields
jqString+="${myOptions[MoreJQ]}"
do_debug 1 "jqString=$jqString"

Loading…
Cancel
Save