Slowly converting to using arrays instead of variables

dev
Eric Loyd 1 year ago
parent 8fa76d3b89
commit 828f259647

338
api.sh

@ -9,31 +9,19 @@ XI_URL=""
curl="curl -k -s" curl="curl -k -s"
# curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=<key>" # curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=<key>"
verbose="0" # myThings are now too big for individual variables. We'll read them into an associative array instead
testMode="" declare -A myThing
doCreate=""
# We need a separate thing for myOptions
myAck="" declare -A myOptions
myCG="" myOptions[API]="objects"
myCommand="" myOptions[APIep]="servicestatus"
myConfigName="" myOptions[TestMode]=""
myFields="" myOptions[Verbose]="0"
myFile="" myOptions[Create]=""
myHG="" myOptions[Options]=""
myHost=""
myOutput=""
mySave=""
myService=""
mySG=""
myState=""
myStateType=""
myQuick=""
tmpJSON="" tmpJSON=""
moreJQ=""
cmdOptions=""
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 # 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 declare -A APIinfo
@ -72,31 +60,31 @@ APIinfo["objects/bpi"]=""
get_myAPI() { get_myAPI() {
case "$1" in case "$1" in
o*) myAPI="objects";; o*) ${myOptions[API]}="objects";;
c*) myAPI="config";; c*) ${myOptions[API]}="config";;
s*) myAPI="system";; s*) ${myOptions[API]}="system";;
*) myAPI="";; *) ${myOptions[API]}="";;
esac esac
} }
get_myAPIep() { get_myAPIep() {
case "$1" in case "$1" in
co|comment) myAPIep="comment";; co|comment) myOptions[APIep]="comment";;
cg|contactgroup) myAPIep="contactgroup";; cg|contactgroup) myOptions[APIep]="contactgroup";;
c|contact) myAPIep="contact";; c|contact) myOptions[APIep]="contact";;
dt|downtime) myAPIep="downtime";; dt|downtime) myOptions[APIep]="downtime";;
hgm|hostgroupmembers) myAPI="objects"; myAPIep="hostgroupmembers";; hgm|hostgroupmembers) myOptions[API]="objects"; myOptions[APIep]="hostgroupmembers";;
hg|hostgroup) myAPIep="hostgroup";; hg|hostgroup) myOptions[APIep]="hostgroup";;
h|host) myAPIep="host";; h|host) myOptions[APIep]="host";;
hs|hoststatus) myAPIep="hoststatus";; hs|hoststatus) myOptions[APIep]="hoststatus";;
le|logentries) myAPIep="logentries";; le|logentries) myOptions[APIep]="logentries";;
sgm|servicegroupmembers) myAPI="objects"; myAPIep="servicegroupmembers";; sgm|servicegroupmembers) myOptions[API]="objects"; myOptions[APIep]="servicegroupmembers";;
sg|servicegroup) myAPIep="servicegroup";; sg|servicegroup) myOptions[APIep]="servicegroup";;
s|service) myAPIep="service";; s|service) myOptions[APIep]="service";;
ss|servicestatus) myAPIep="servicestatus";; ss|servicestatus) myOptions[APIep]="servicestatus";;
sh|statehistory) myAPIep="statehistory";; sh|statehistory) myOptions[APIep]="statehistory";;
tp|timeperiod) myAPIep="timeperiod";; tp|timeperiod) myOptions[APIep]="timeperiod";;
*) myAPIep="";; *) myOptions[APIep]="";;
esac esac
} }
@ -106,6 +94,9 @@ print_helpopt() {
Options for different APIs: Options for different APIs:
all:
i Make all string comparisons case-insentitive
servicestatus: servicestatus:
c Show fields selected by -f as quoted CSV c Show fields selected by -f as quoted CSV
@ -119,37 +110,41 @@ HELPOPT_EOF
print_help() { print_help() {
cat << HELP_EOF cat << HELP_EOF
--help This text --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"
-cnn current_notification_number=<n>
-cg|--contactgroup contact_groups="\$2"
-cn|--configname config_name="\$2"
... comment | downtime | contact | host | service | hostgroup | ...
--create doCreate="true"
-f|--fields JQ-valid list of fields to show="\$2"
--file load JSON from="\$2"
--helpopt Show help for command options --helpopt Show help for command options
--help This text
-hg|--hostgroup hostgroup="\$2"
-h|--host host_name="\$2"
... hostgroupmembers | servicegroupmembers >
-j|--jq additional valid JQ="\$2"
--key APIKEY="\$2"
--keyfile APIkeyFile="\$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|--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 -Q Same as --quick but add -o c
-j|--jq moreJQ="\$2" --save save JSON to="\$2"
-v|--verbose verbose=\$((\$verbose + 1)) ... servicegroup | contactgroup | timeperiod | ...
--create doCreate="true" -sg|--servicegroup servicegroup="\$2"
--key APIKEY="\$2" -s|--service service_description="\$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"
--state 0, 1, or 2 (or other, I suppose) --state 0, 1, or 2 (or other, I suppose)
--stype 0, 1 (SOFT or HARD) --stype 0, 1 (SOFT or HARD)
--output myOutput="\$2"
--ack 0, 1
-o|--opt cmdOptions="\$2" (endpoint specific options. See --helpopt)
--api < o*bjects | c*onfig | s*ystem >
-t|--object < hoststatus | servicestatus | logentries | statehistory | ...
... comment | downtime | contact | host | service | hostgroup | ...
... servicegroup | contactgroup | timeperiod | ...
... hostgroupmembers | servicegroupmembers >
--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 | ...
--url XI_URL="\$2"
-v|--verbose verbose=\$((\${myOptions[Verbose]} + 1))
[...] [...]=<...> NOTE: This will take anything listed as a Nagios configuration directive and search for it
HELP_EOF HELP_EOF
exit exit
@ -157,34 +152,37 @@ HELP_EOF
while [ -n "$1" ]; do while [ -n "$1" ]; do
case "$1" in case "$1" in
--help) print_help;; --ack) myThing[problem_has_been_acknowledged]="$2"; shift 2;;
--ace) myThing[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;;
--create) myOptions[Create]="true"; shift 1;;
-f|--fields) myOptions[Fields]="$2"; shift 2;;
--file) myOptions[File]="$2"; shift 2;;
--helpopt) print_helpopt;; --helpopt) print_helpopt;;
--keyfile) APIkeyFile="$2"; shift 2;; --help) print_help;;
--test) testMode="true"; shift 1;; -hg|--hostgroup) myThing[HG]="$2"; shift 2;;
-q|--quick) myQuick="true"; shift 1;; -h|--host) myThing[Host]="$2"; shift 2;;
-Q) myQuick="true"; cmdOptions+="c,"; shift 1;; -j|--jq) myOptions[MoreJQ]="$2"; shift 2;;
-j|--jq) moreJQ="$2"; shift 2;;
-v|--verbose) verbose=$(($verbose + 1)); shift 1;;
--create) doCreate="true"; shift 1;;
--key) APIKEY="$2"; shift 2;; --key) APIKEY="$2"; shift 2;;
--url) XI_URL="$2"; shift 2;; --keyfile) APIkeyFile="$2"; shift 2;;
--save) mySave="$2"; shift 2;; -o|--opt) myOptions[Options]+="$2,"; shift 2;;
--file) myFile="$2"; shift 2;; --output) myThing[Output]="$2"; shift 2;;
-f|--fields) myFields="$2"; shift 2;; -Q) myOptions[Quick]="true"; myOptions[Options]+="c,"; shift 1;;
-h|--host) myHost="$2"; shift 2;; -q|--quick) myOptions[Quick]="true"; shift 1;;
-s|--service) myService="$2"; shift 2;; --save) myOptions[Save]="$2"; shift 2;;
-c|--command) myCommand="$2"; shift 2;; -sg|--servicegroup) myThing[SG]="$2"; shift 2;;
-cn|--configname) myConfigName="$2"; shift 2;; -s|--service) myThing[Service]="$2"; shift 2;;
-cg|--contactgroup) myCG="$2"; shift 2;; --state) myThing[State]="$2"; shift 2;;
-hg|--hostgroup) myHG="$2"; shift 2;; --stype) myThing[StateType]="$2"; shift 2;;
-sg|--servicegroup) mySG="$2"; shift 2;; --test) myOptions[TestMode]="true"; shift 1;;
--state) myState="$2"; shift 2;;
--stype) myStateType="$2"; shift 2;;
--output) myOutput="$2"; shift 2;;
--ack) myAck="$2"; shift 2;;
-o|--opt) cmdOptions+="$2,"; shift 2;;
--api) get_myAPI "$2"; shift 2;;
-t|--object) get_myAPIep "$2"; shift 2;; -t|--object) get_myAPIep "$2"; shift 2;;
--url) XI_URL="$2"; shift 2;;
-v|--verbose) verbose=$((${myOptions[Verbose]} + 1)); shift 1;;
*) shift 1;; *) shift 1;;
esac esac
done done
@ -203,7 +201,7 @@ if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then
fi fi
do_debug() { do_debug() {
[ "$verbose" -ge "$1" ] && echo "$2" >&2 [ "${myOptions[Verbose]}" -ge "$1" ] && echo "$2" >&2
} }
do_api() { do_api() {
@ -222,7 +220,7 @@ do_api_post() {
url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0" url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0"
do_debug 2 "start=$1 command=$2" do_debug 2 "start=$1 command=$2"
do_debug 1 "Executing: $url" do_debug 1 "Executing: $url"
$curl -XPOST -k "$url" -d "hostgroup_name=$myHG&alias=$myHG&applyconfig=0" $curl -XPOST -k "$url" -d "hostgroup_name=${myThing[HG]}&alias=${myThing[HG]}&applyconfig=0"
} }
# At this time, all we can create ia a hostgroup # At this time, all we can create ia a hostgroup
@ -233,12 +231,12 @@ create_hostgroup() {
do_create() { do_create() {
do_debug 1 "about to do a create_command" do_debug 1 "about to do a create_command"
[ -n "$myHG" ] && create_hostgroup [ -n "${myThing[HG]}" ] && create_hostgroup
exit exit
} }
# If we said "--create" then we want to make something # If we said "--create" then we want to make something
if [ -n "$doCreate" ]; then if [ -n "${myOptions[Create]}" ]; then
do_create do_create
exit exit
fi fi
@ -246,25 +244,26 @@ fi
# Grab a copy of the JSON data so we don't have to keep making calls over and over # Grab a copy of the JSON data so we don't have to keep making calls over and over
# If we used an existing file, then just use that # If we used an existing file, then just use that
# If we're in test mode, then skip this part # If we're in test mode, then skip this part
if [ -z "$testMode" ]; then if [ -z "${myOptions[TestMode]}" ]; then
if [ -z "$myFile" ]; then if [ -z "${myOptions[File]}" ]; then
tmpJSON=`mktemp` tmpJSON=`mktemp`
do_debug 1 "tmp file is $tmpJSON" do_debug 1 "tmp file is $tmpJSON"
do_debug 2 "myAPI is $myAPI and myAPIep is $myAPIep" do_debug 2 "myAPI is ${myOptions[API]} and myAPIep is ${myOptions[APIep]}"
do_api "$myAPI" "$myAPIep" > $tmpJSON do_api "${myOptions[API]}" "${myOptions[APIep]}" > $tmpJSON
else else
do_debug 1 "myFile=$myFile" do_debug 1 "myFile=${myOptions[File]}"
tmpJSON="$myFile" tmpJSON="${myOptions[File]}"
do_debug 1 "tmpJSON=$tmpJSON"
fi fi
# if mySave is not empty, then we're just saving it into the file called $mySave # if mySave is not empty, then we're just saving it into the file called ${myOptions[Save]}
if [ -n "$mySave" ]; then if [ -n "${myOptions[Save]}" ]; then
mv $tmpJSON $mySave mv $tmpJSON ${myOptions[Save]}
do_debug 1 "JSON data saved to $mySave" do_debug 1 "JSON data saved to ${myOptions[Save]}"
exit exit
fi fi
else else
echo "### TEST MODE - Skipping API call (testMode=${testMode}) ###" echo "### TEST MODE - Skipping API call (testMode=${myOptions[TestMode]}) ###"
fi fi
# Helper functions for creating our jqString # Helper functions for creating our jqString
@ -272,112 +271,93 @@ fi
jq_check_case() { jq_check_case() {
thing="$*" thing="$*"
if [ -n "$thing" ]; then if [ -n "$thing" ]; then
do_debug 2 "### in JQ_CHECK_CASE cmdOptions=${myOptions[Options]}"
val="| test(\"$thing\"" val="| test(\"$thing\""
[[ "$cmdOptions" =~ "i," ]] && val+="; \"i\"" [[ "$myOptions[Options]" =~ "i," ]] && val+="; \"i\""
echo "$val)" echo "$val)"
else else
echo "" echo ""
fi fi
} }
# Do we need to add host limits to the query? # Create the jQuery search string
jq_add_host() { jq_add_myThing() {
[ -n "$myHost" ] && jqString+="| select(.host_name $(jq_check_case $myHost))" thing="$1"
} if [ -z "$1" ]; then
jq_add_service() { return
[ -n "$myService" ] && jqString+="| select(.service_description $(jq_check_case $myService))" else
} [ -n "${myThing[$thing]}" ] && jqString+="| select(.$thing | test(\"${myThing[$thing]}\"))"
jq_add_hostgroup() { fi
[ -n "$myHG" ] && jqString+="| select(.hostgroup_name==\"$myHG\")"
}
jq_add_servicegroup() {
[ -n "$mySG" ] && jqString+="| select(.servicegroup_name==\"$mySG\")"
}
jq_add_command() {
[ -n "$myCommand" ] && jqString+="| select(.check_command $(jq_check_case $myCommand))"
}
jq_add_output() {
[ -n "$myOutput" ] && jqString+="| select(.output $(jq_check_case $myOutput))"
}
jq_add_configname() {
[ -n "$myConfigName" ] && jqString+="| select(.check_command $(jq_check_case $myConfigName))"
}
jq_add_contactgroup() {
[ -n "$myCG" ] && jqString+="| select(.contactgroup_name $(jq_check_case $myCG))"
}
jq_add_state() {
[ -n "$myState" ] && jqString+="| select(.current_state | test(\"$myState\"))"
}
jq_add_statetype() {
[ -n "$myStateType" ] && jqString+="| select(.state_type | test(\"$myStateType\"))"
}
jq_add_ack() {
[ -n "$myAck" ] && jqString+="| select(.problem_has_been_acknowledged | test(\"$myAck\"))"
} }
jq_get_fields() { jq_get_fields() {
if [[ "$cmdOptions" =~ "c," ]]; then if [[ "$myOptions[Options]" =~ "c," ]]; then
jqString+="| [$myFields] | @csv" jqString+="| [${myOptions[Fields]}] | @csv"
else else
jqString+="| $myFields" jqString+="| ${myOptions[Fields]}"
fi fi
} }
# Otherwise, let's parse the JSON data here # Otherwise, let's parse the JSON data here
# Parse our string # Parse our string
do_debug 1 "APIinfo=${APIinfo[$myAPI/$myAPIep]}" do_debug 1 "APIinfo=${APIinfo[${myOptiosn[API]}/${myOptions[APIep]}]}"
jqString=${APIinfo[$myAPI/$myAPIep]} jqString=${APIinfo[${myOptions[API]}/${myOptions[APIep]}]}
case "$myAPIep" in case "${myOptions[APIep]}" in
servicestatus) servicestatus)
jq_add_host jq_add_myThing host_name
jq_add_service jq_add_myThing service_description
jq_add_hostgroup jq_add_myThing hostgroup_name
jq_add_servicegroup jq_add_myThing servicegroup_name
jq_add_command jq_add_myThing output
jq_add_output jq_add_myThing current_state
jq_add_state jq_add_myThing current_state_type
jq_add_statetype jq_add_myThing check_command
jq_add_ack 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_host jq_add_myThing host_name
jq_add_hostgroup jq_add_myThing hostgroup_name
;; ;;
host) host)
jq_add_host jq_add_myThing host_name
;; ;;
service) service)
jq_add_host jq_add_myThing host_name
jq_add_service jq_add_myThing service_description
jq_add_configname jq_add_myThing config_name
jq_add_command jq_add_myThing check_command
;; ;;
hostgroupmembers) hostgroupmembers)
jq_add_host jq_add_myThing host_name
jq_add_hostgroup jq_add_myThing hostgroup_name
[[ $cmdOptions =~ "h," ]] && jqString+="| .members[] | .[] | .host_name" [[ $myOptions[Options] =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
[[ $cmdOptions =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id" [[ $myOptions[Options] =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
;; ;;
servicegroup) servicegroup)
jq_add_host jq_add_myThing host_name
jq_add_servicegroup jq_add_myThing servicegroup_name
;; ;;
contactgroup) contactgroup)
jq_add_contactgroup jq_add_myThing contact_groups
;; ;;
esac esac
if [ -n "$myQuick" -a -n "$myFields" ]; then if [ -n "${myOptions[Quick]}" -a -n "${myOptions[Fields]}" ]; then
myFields=".host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged,${myFields}" myOptions[Fields]=".host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged,${myOptions[Fields]}"
elif [ -n "$myQuick" ]; then elif [ -n "${myOptions[Quick]}" ]; then
myFields=".host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged" myOptions[Fields]=".host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged"
fi fi
[ -n "$myFields" ] && jq_get_fields [ -n "${myOptions[Fields]}" ] && jq_get_fields
jqString+="$moreJQ" jqString+="${myOptions[MoreJQ]}"
do_debug 1 "jqString=$jqString" do_debug 1 "jqString=$jqString"
if [ -n "$testMode" ]; then if [ -n "${myOptions[TestMode]}" ]; then
echo "### TEST MODE - Here is what would have been done (testMode=${testMode}) ###" echo "### TEST MODE - Here is what would have been done (testMode=${myOptions[TestMode]}) ###"
echo "cat <tmpfile> | jq -r \"$jqString\"" echo "cat <tmpfile> | jq -r \"$jqString\""
else else
cat $tmpJSON | jq -r "$jqString" cat $tmpJSON | jq -r "$jqString"
[ -z "$myFile" ] && rm $tmpJSON [ -z "${myOptions[File]}" ] && rm $tmpJSON
fi fi

Loading…
Cancel
Save