Almost done with full nagios config directives

dev
Eric Loyd 1 year ago
parent 828f259647
commit a69c3b3b96

@ -17,10 +17,9 @@ declare -A myOptions
myOptions[API]="objects" myOptions[API]="objects"
myOptions[APIep]="servicestatus" myOptions[APIep]="servicestatus"
myOptions[TestMode]="" myOptions[TestMode]=""
myOptions[Verbose]="0"
myOptions[Create]="" myOptions[Create]=""
myOptions[Options]="" myOptions[Options]=""
verbose="0"
tmpJSON="" tmpJSON=""
# 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
@ -143,7 +142,7 @@ print_help() {
--test Don't call the API, just show what would happen --test Don't call the API, just show what would happen
-t|--object < hoststatus | servicestatus | logentries | statehistory | ... -t|--object < hoststatus | servicestatus | logentries | statehistory | ...
--url XI_URL="\$2" --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 [...] [...]=<...> NOTE: This will take anything listed as a Nagios configuration directive and search for it
HELP_EOF HELP_EOF
@ -165,7 +164,7 @@ while [ -n "$1" ]; do
--file) myOptions[File]="$2"; shift 2;; --file) myOptions[File]="$2"; shift 2;;
--helpopt) print_helpopt;; --helpopt) print_helpopt;;
--help) print_help;; --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;; -h|--host) myThing[Host]="$2"; shift 2;;
-j|--jq) myOptions[MoreJQ]="$2"; shift 2;; -j|--jq) myOptions[MoreJQ]="$2"; shift 2;;
--key) APIKEY="$2"; shift 2;; --key) APIKEY="$2"; shift 2;;
@ -182,7 +181,7 @@ while [ -n "$1" ]; do
--test) myOptions[TestMode]="true"; shift 1;; --test) myOptions[TestMode]="true"; shift 1;;
-t|--object) get_myAPIep "$2"; shift 2;; -t|--object) get_myAPIep "$2"; shift 2;;
--url) XI_URL="$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;; *) shift 1;;
esac esac
done done
@ -201,7 +200,7 @@ if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then
fi fi
do_debug() { do_debug() {
[ "${myOptions[Verbose]}" -ge "$1" ] && echo "$2" >&2 [ "$verbose" -ge "$1" ] && echo "$2" >&2
} }
do_api() { do_api() {
@ -270,10 +269,11 @@ fi
# First, we need to know if our tests should be case-sensitive or not # First, we need to know if our tests should be case-sensitive or not
jq_check_case() { jq_check_case() {
thing="$*" thing="$*"
do_debug 1 "### JQ_CHECK_CASE looking for thing=$thing"
if [ -n "$thing" ]; then if [ -n "$thing" ]; then
do_debug 2 "### in JQ_CHECK_CASE cmdOptions=${myOptions[Options]}" do_debug 2 "### in JQ_CHECK_CASE cmdOptions=${myOptions[Options]}"
val="| test(\"$thing\"" val="| test(\"$thing\""
[[ "$myOptions[Options]" =~ "i," ]] && val+="; \"i\"" [[ "${myOptions[Options]}" =~ "i," ]] && val+="; \"i\""
echo "$val)" echo "$val)"
else else
echo "" echo ""
@ -281,16 +281,6 @@ jq_check_case() {
} }
# Create the jQuery search string # 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() { jq_get_fields() {
if [[ "$myOptions[Options]" =~ "c," ]]; then if [[ "$myOptions[Options]" =~ "c," ]]; then
jqString+="| [${myOptions[Fields]}] | @csv" jqString+="| [${myOptions[Fields]}] | @csv"
@ -303,46 +293,25 @@ jq_get_fields() {
# Parse our string # Parse our string
do_debug 1 "APIinfo=${APIinfo[${myOptiosn[API]}/${myOptions[APIep]}]}" do_debug 1 "APIinfo=${APIinfo[${myOptiosn[API]}/${myOptions[APIep]}]}"
jqString=${APIinfo[${myOptions[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 case "${myOptions[APIep]}" in
servicestatus) 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) hostgroup)
jq_add_myThing host_name
jq_add_myThing hostgroup_name
;; ;;
host) host)
jq_add_myThing host_name
;; ;;
service) service)
jq_add_myThing host_name
jq_add_myThing service_description
jq_add_myThing config_name
jq_add_myThing check_command
;; ;;
hostgroupmembers) hostgroupmembers)
jq_add_myThing host_name
jq_add_myThing hostgroup_name
[[ $myOptions[Options] =~ "h," ]] && jqString+="| .members[] | .[] | .host_name" [[ $myOptions[Options] =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
[[ $myOptions[Options] =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id" [[ $myOptions[Options] =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
;; ;;
servicegroup) servicegroup)
jq_add_myThing host_name
jq_add_myThing servicegroup_name
;; ;;
contactgroup) contactgroup)
jq_add_myThing contact_groups
;; ;;
esac esac
if [ -n "${myOptions[Quick]}" -a -n "${myOptions[Fields]}" ]; then if [ -n "${myOptions[Quick]}" -a -n "${myOptions[Fields]}" ]; then

Loading…
Cancel
Save