From a69c3b3b966a9f13af499f0622ecc0cacf21bfff Mon Sep 17 00:00:00 2001 From: Eric Loyd Date: Thu, 18 Jul 2024 13:53:23 -0400 Subject: [PATCH] Almost done with full nagios config directives --- api.sh | 51 ++++++++++----------------------------------------- 1 file changed, 10 insertions(+), 41 deletions(-) diff --git a/api.sh b/api.sh index ad88e7c..8c85d30 100755 --- a/api.sh +++ b/api.sh @@ -17,10 +17,9 @@ declare -A myOptions myOptions[API]="objects" myOptions[APIep]="servicestatus" myOptions[TestMode]="" -myOptions[Verbose]="0" myOptions[Create]="" myOptions[Options]="" - +verbose="0" tmpJSON="" # Different API commands return different JSON datasets. So let's make a lookup table that figures out where to start the data extracts @@ -143,7 +142,7 @@ print_help() { --test Don't call the API, just show what would happen -t|--object < hoststatus | servicestatus | logentries | statehistory | ... --url XI_URL="\$2" - -v|--verbose verbose=\$((\${myOptions[Verbose]} + 1)) + -v|--verbose verbose=\$((\$verbose + 1)) [...] [...]=<...> NOTE: This will take anything listed as a Nagios configuration directive and search for it HELP_EOF @@ -165,7 +164,7 @@ while [ -n "$1" ]; do --file) myOptions[File]="$2"; shift 2;; --helpopt) print_helpopt;; --help) print_help;; - -hg|--hostgroup) myThing[HG]="$2"; shift 2;; + -hg|--hostgroup) myThing[hostgroup_name]="$2"; shift 2;; -h|--host) myThing[Host]="$2"; shift 2;; -j|--jq) myOptions[MoreJQ]="$2"; shift 2;; --key) APIKEY="$2"; shift 2;; @@ -182,7 +181,7 @@ while [ -n "$1" ]; do --test) myOptions[TestMode]="true"; shift 1;; -t|--object) get_myAPIep "$2"; shift 2;; --url) XI_URL="$2"; shift 2;; - -v|--verbose) verbose=$((${myOptions[Verbose]} + 1)); shift 1;; + -v|--verbose) verbose=$(($verbose + 1)); shift 1;; *) shift 1;; esac done @@ -201,7 +200,7 @@ if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then fi do_debug() { - [ "${myOptions[Verbose]}" -ge "$1" ] && echo "$2" >&2 + [ "$verbose" -ge "$1" ] && echo "$2" >&2 } do_api() { @@ -270,10 +269,11 @@ fi # First, we need to know if our tests should be case-sensitive or not jq_check_case() { thing="$*" + do_debug 1 "### JQ_CHECK_CASE looking for thing=$thing" if [ -n "$thing" ]; then do_debug 2 "### in JQ_CHECK_CASE cmdOptions=${myOptions[Options]}" val="| test(\"$thing\"" - [[ "$myOptions[Options]" =~ "i," ]] && val+="; \"i\"" + [[ "${myOptions[Options]}" =~ "i," ]] && val+="; \"i\"" echo "$val)" else echo "" @@ -281,16 +281,6 @@ jq_check_case() { } # Create the jQuery search string -jq_add_myThing() { - thing="$1" - if [ -z "$1" ]; then - return - else - [ -n "${myThing[$thing]}" ] && jqString+="| select(.$thing | test(\"${myThing[$thing]}\"))" - fi -} - - jq_get_fields() { if [[ "$myOptions[Options]" =~ "c," ]]; then jqString+="| [${myOptions[Fields]}] | @csv" @@ -303,46 +293,25 @@ jq_get_fields() { # 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]}))" +done case "${myOptions[APIep]}" in servicestatus) - jq_add_myThing host_name - jq_add_myThing service_description - jq_add_myThing hostgroup_name - jq_add_myThing servicegroup_name - jq_add_myThing output - jq_add_myThing current_state - jq_add_myThing current_state_type - jq_add_myThing check_command - jq_add_myThing current_check_attempt - jq_add_myThing current_notification_number - jq_add_myThing active_checks_enabled - jq_add_myThing problem_has_been_acknowledged ;; hostgroup) - jq_add_myThing host_name - jq_add_myThing hostgroup_name ;; host) - jq_add_myThing host_name ;; service) - jq_add_myThing host_name - jq_add_myThing service_description - jq_add_myThing config_name - jq_add_myThing check_command ;; hostgroupmembers) - jq_add_myThing host_name - jq_add_myThing hostgroup_name [[ $myOptions[Options] =~ "h," ]] && jqString+="| .members[] | .[] | .host_name" [[ $myOptions[Options] =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id" ;; servicegroup) - jq_add_myThing host_name - jq_add_myThing servicegroup_name ;; contactgroup) - jq_add_myThing contact_groups ;; esac if [ -n "${myOptions[Quick]}" -a -n "${myOptions[Fields]}" ]; then