diff --git a/napi b/napi index 933e047..97fef53 100755 --- a/napi +++ b/napi @@ -7,7 +7,6 @@ APIkeyFile=".nagiosapikey" APIKEY="" XI_URL="" curl="curl -k -s" -# curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=" # myDir (for "Directives") are now too big for individual variables. We'll read them into an associative array instead declare -A myDir @@ -16,10 +15,10 @@ declare -A myDir declare -A myOptions myOptions[API]="objects" myOptions[APIep]="servicestatus" -myOptions[TestMode]="" -myOptions[Create]="" -myOptions[Apply]="" -myOptions[Options]="" +#myOptions[TestMode]="" +#myOptions[Create]="" +#myOptions[Apply]="" +#myOptions[Options]="" verbose="0" tmpJSON="" tmpQuick="" @@ -168,6 +167,8 @@ print_help() { --create doCreate="true" --apply If we're creating something, then Apply Configuration --delete If we're creating something, then delete it instead (host and service must be specified) + --disable Disable a service in a config (must use -cn and -s; sets the service to active_checks_enabled=0) + --enable Enable a service in a config (must use -cn and -s; sets the service to active_checks_enabled=1) -D = This will take anything listed as a Nagios configuration directive and search for it -f|--fields JQ-valid list of fields to show= --file load JSON from= @@ -271,6 +272,8 @@ while [ -n "$1" ]; do --ctrace) myOptions[CommandTrace]="true"; shift 1;; --create) myOptions[Create]="true"; shift 1;; --delete) myOptions[Delete]="true"; shift 1;; + --disable) myOptions[Disable]="true"; shift 1;; + --enable) myOptions[Enable]="true"; shift 1;; --addvar) myOptions[AddVar]="$2"; shift 1;; -f|--fields) myOptions[Fields]="$2"; shift 2;; --file) myOptions[File]="$2"; shift 2;; @@ -295,13 +298,16 @@ done [ "${myOptions[API]}" = "config" -a "${myOptions[APIep]}" = "servicestatus" ] && myOptions[APIep]="service" [ "${myOptions[API]}" = "config" -a "${myOptions[APIep]}" = "hoststatus" ] && myOptions[APIep]="host" +# Convert command line date formats to UNIX time [ -n "${myOptions[Start]}" ] && myOptions[Start]=`convert_time "${myOptions[Start]}"` [ -n "${myOptions[End]}" ] && myOptions[End]=`convert_time "${myOptions[End]}"` + # If we're doing status mode, then override a bunch of other options if [ -n "${myOptions[Status]}" ]; then myOptions[API]="objects" myOptions[APIep]="servicestatus" myOptions[Create]="" + myOptions[Disable]="" myOptions[Apply]="" myOptions[Options]="" myOptions[Quick]="true" @@ -318,9 +324,10 @@ do_debug 1 "Looking for url=$url in $APIkeyFile" if [ -z "$XI_URL" ]; then XI_URL="$url" APIKEY="$key" - continue +do_debug 1 "Found url=$url key=$key" + break fi -do_debug 2 "Looking for url=$url XI_URL=$XI_URL" +do_debug 2 "-> Looking for url=$url XI_URL=$XI_URL" if [[ "$url" =~ "$XI_URL" ]]; then XI_URL="$url" APIKEY="$key" @@ -328,6 +335,7 @@ do_debug 2 "Looking for url=$url XI_URL=$XI_URL" done < "$APIkeyFile" fi +do_debug 1 "Final URL=$XI_URL and key=$APIKEY" [ -z "$XI_URL" -o -z "$APIKEY" ] && echo "Empty URL or Key." && exit do_api() { @@ -349,7 +357,7 @@ do_api() { [ -z "${myOptions[TestMode]}" ] && $curl -XGET -k "$url" } -# curl -XPOST "http://192.168.1.128/nagiosxi/api/v1/config/hostgroup?apikey=fsZZ4pXaKaVjSG7IYcjMRYhK8NqcqN2NGPck8gPhFoZMJGKj4YUjZCF8qSqsK7Ln&pretty=1" -d "hostgroup_name=testapihostgroup&alias=HostGroup&applyconfig=1" +# curl -XPOST "http:///nagiosxi/api/v1/config/hostgroup?apikey=fsZZ4pXaKaVjSG7IYcjMRYhK8NqcqN2NGPck8gPhFoZMJGKj4YUjZCF8qSqsK7Ln&pretty=1" -d "hostgroup_name=testapihostgroup&alias=HostGroup&applyconfig=1" do_api_post() { api_start="$1" api_command="$2" @@ -405,8 +413,8 @@ create_servicegroup() { } # Given a config_name and service_description, set the host_name[s] to be the list given in -h -# NEED: curl -XPUT "http://192.168.1.15/nagiosxi/api/v1/config/service/NAS/Physical+Disks?apikey=9RTG9Aq2NTfefE3XeAiDUbacmNvOJPWfRV3Vbs5DS8ZCCAr6XrIUDLqZeBCceQUo&pretty=1&host_name=NAS-H,NAS-W" -# GOT: curl -k -s -XPUT -k "http://192.168.1.15/nagiosxi/api/v1/config/service/NAS/Physical+Disks?apikey=9RTG9Aq2NTfefE3XeAiDUbacmNvOJPWfRV3Vbs5DS8ZCCAr6XrIUDLqZeBCceQUo&pretty=0&host_name=NAS-H,NNA&applyconfig=0" +# NEED: curl -XPUT "http:///nagiosxi/api/v1/config/service/NAS/Physical+Disks?apikey=&pretty=1&host_name=NAS-H,NAS-W" +# GOT: curl -k -s -XPUT -k "http:///nagiosxi/api/v1/config/service/NAS/Physical+Disks?apikey=&pretty=0&host_name=NAS-H,NNA&applyconfig=0" update_service() { cname=`echo "${myDir[config_name]}" | tr " " "+"` sname=`echo "${myDir[service_description]}" | tr " " "+"` @@ -414,6 +422,28 @@ update_service() { do_api_put config "service/$cname/$sname" "host_name=$hname" } +do_enable() { + do_debug 1 "about to enable a service" + cname=`echo "${myDir[config_name]}" | tr " " "+"` + sname=`echo "${myDir[service_description]}" | tr " " "+"` + if [ -z "$cname" -o -z "$sname" ]; then + echo "When using --enable then you must specify -cn for the config name and -s for the service to enable" + exit 1 + fi + do_api_put config "service/$cname/$sname" "active_checks_enabled=1" +} + +do_disable() { + do_debug 1 "about to disable a service" + cname=`echo "${myDir[config_name]}" | tr " " "+"` + sname=`echo "${myDir[service_description]}" | tr " " "+"` + if [ -z "$cname" -o -z "$sname" ]; then + echo "When using --disable then you must specify -cn for the config name and -s for the service to disable" + exit 1 + fi + do_api_put config "service/$cname/$sname" "active_checks_enabled=0" +} + # Delete a service, given hostname and service_description do_delete() { do_debug 1 "Trying to delete host and service, so let's verify first" @@ -440,11 +470,11 @@ do_apply() { do_api_post system applyconfig } -### curl -XPUT "http://X.X.X.X/nagiosxi/api/v1/config/host/$x?apikey=KEY&pretty=1&old_host_name=testapihost&_foo=bartoo" +# curl -XPUT "http://X.X.X.X/nagiosxi/api/v1/config/host/$x?apikey=KEY&pretty=1&old_host_name=testapihost&_foo=bartoo" # If we used --addvar then we want to add a custom macro to a host. Other options available later if [ -n "${myOptions[AddVar]}" ]; then if [ -z "${myDir[host_name]}" ]; then - echo "If using --addvar then you must specify -h for the host to add to" + echo "When using --addvar then you must specify -h for the host to add to" exit 1 fi hname=`echo "${myDir[host_name]}" | tr " " "+"` @@ -462,6 +492,20 @@ if [ -n "${myOptions[Create]}" ]; then exit fi +# If we want to "--enable" [a service] then let's make that happen +if [ -n "${myOptions[Enable]}" ]; then + do_enable + [ -n "${myOptions[Apply]}" -a -z "${myOptions[TestMode]}" ] && do_apply + exit +fi + +# If we want to "--disable" [a service] then let's make that happen +if [ -n "${myOptions[Disable]}" ]; then + do_disable + [ -n "${myOptions[Apply]}" -a -z "${myOptions[TestMode]}" ] && do_apply + exit +fi + # If all we said was --apply, then we want to apply previous changes if [ -n "${myOptions[Apply]}" ]; then do_apply