--disable and --enable now modify service active_checks_enabled accordingly.

dev
Eric Loyd 10 months ago
parent c2fc91379e
commit f4b9d3e1b9

68
napi

@ -7,7 +7,6 @@ APIkeyFile=".nagiosapikey"
APIKEY="" APIKEY=""
XI_URL="" XI_URL=""
curl="curl -k -s" curl="curl -k -s"
# curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=<key>"
# myDir (for "Directives") are now too big for individual variables. We'll read them into an associative array instead # myDir (for "Directives") are now too big for individual variables. We'll read them into an associative array instead
declare -A myDir declare -A myDir
@ -16,10 +15,10 @@ declare -A myDir
declare -A myOptions declare -A myOptions
myOptions[API]="objects" myOptions[API]="objects"
myOptions[APIep]="servicestatus" myOptions[APIep]="servicestatus"
myOptions[TestMode]="" #myOptions[TestMode]=""
myOptions[Create]="" #myOptions[Create]=""
myOptions[Apply]="" #myOptions[Apply]=""
myOptions[Options]="" #myOptions[Options]=""
verbose="0" verbose="0"
tmpJSON="" tmpJSON=""
tmpQuick="" tmpQuick=""
@ -168,6 +167,8 @@ print_help() {
--create doCreate="true" --create doCreate="true"
--apply If we're creating something, then Apply Configuration --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) --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 <Nagios Config Directive>=<value> This will take anything listed as a Nagios configuration directive and search for it -D <Nagios Config Directive>=<value> This will take anything listed as a Nagios configuration directive and search for it
-f|--fields JQ-valid list of fields to show=<value> -f|--fields JQ-valid list of fields to show=<value>
--file load JSON from=<value> --file load JSON from=<value>
@ -271,6 +272,8 @@ while [ -n "$1" ]; do
--ctrace) myOptions[CommandTrace]="true"; shift 1;; --ctrace) myOptions[CommandTrace]="true"; shift 1;;
--create) myOptions[Create]="true"; shift 1;; --create) myOptions[Create]="true"; shift 1;;
--delete) myOptions[Delete]="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;; --addvar) myOptions[AddVar]="$2"; shift 1;;
-f|--fields) myOptions[Fields]="$2"; shift 2;; -f|--fields) myOptions[Fields]="$2"; shift 2;;
--file) myOptions[File]="$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]}" = "servicestatus" ] && myOptions[APIep]="service"
[ "${myOptions[API]}" = "config" -a "${myOptions[APIep]}" = "hoststatus" ] && myOptions[APIep]="host" [ "${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[Start]}" ] && myOptions[Start]=`convert_time "${myOptions[Start]}"`
[ -n "${myOptions[End]}" ] && myOptions[End]=`convert_time "${myOptions[End]}"` [ -n "${myOptions[End]}" ] && myOptions[End]=`convert_time "${myOptions[End]}"`
# If we're doing status mode, then override a bunch of other options # If we're doing status mode, then override a bunch of other options
if [ -n "${myOptions[Status]}" ]; then if [ -n "${myOptions[Status]}" ]; then
myOptions[API]="objects" myOptions[API]="objects"
myOptions[APIep]="servicestatus" myOptions[APIep]="servicestatus"
myOptions[Create]="" myOptions[Create]=""
myOptions[Disable]=""
myOptions[Apply]="" myOptions[Apply]=""
myOptions[Options]="" myOptions[Options]=""
myOptions[Quick]="true" myOptions[Quick]="true"
@ -318,9 +324,10 @@ do_debug 1 "Looking for url=$url in $APIkeyFile"
if [ -z "$XI_URL" ]; then if [ -z "$XI_URL" ]; then
XI_URL="$url" XI_URL="$url"
APIKEY="$key" APIKEY="$key"
continue do_debug 1 "Found url=$url key=$key"
break
fi 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 if [[ "$url" =~ "$XI_URL" ]]; then
XI_URL="$url" XI_URL="$url"
APIKEY="$key" APIKEY="$key"
@ -328,6 +335,7 @@ do_debug 2 "Looking for url=$url XI_URL=$XI_URL"
done < "$APIkeyFile" done < "$APIkeyFile"
fi fi
do_debug 1 "Final URL=$XI_URL and key=$APIKEY"
[ -z "$XI_URL" -o -z "$APIKEY" ] && echo "Empty URL or Key." && exit [ -z "$XI_URL" -o -z "$APIKEY" ] && echo "Empty URL or Key." && exit
do_api() { do_api() {
@ -349,7 +357,7 @@ do_api() {
[ -z "${myOptions[TestMode]}" ] && $curl -XGET -k "$url" [ -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://<host>/nagiosxi/api/v1/config/hostgroup?apikey=fsZZ4pXaKaVjSG7IYcjMRYhK8NqcqN2NGPck8gPhFoZMJGKj4YUjZCF8qSqsK7Ln&pretty=1" -d "hostgroup_name=testapihostgroup&alias=HostGroup&applyconfig=1"
do_api_post() { do_api_post() {
api_start="$1" api_start="$1"
api_command="$2" 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 # 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" # NEED: curl -XPUT "http://<host>/nagiosxi/api/v1/config/service/NAS/Physical+Disks?apikey=<token>&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" # GOT: curl -k -s -XPUT -k "http://<host>/nagiosxi/api/v1/config/service/NAS/Physical+Disks?apikey=<token>&pretty=0&host_name=NAS-H,NNA&applyconfig=0"
update_service() { update_service() {
cname=`echo "${myDir[config_name]}" | tr " " "+"` cname=`echo "${myDir[config_name]}" | tr " " "+"`
sname=`echo "${myDir[service_description]}" | tr " " "+"` sname=`echo "${myDir[service_description]}" | tr " " "+"`
@ -414,6 +422,28 @@ update_service() {
do_api_put config "service/$cname/$sname" "host_name=$hname" 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 # Delete a service, given hostname and service_description
do_delete() { do_delete() {
do_debug 1 "Trying to delete host and service, so let's verify first" 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 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 we used --addvar then we want to add a custom macro to a host. Other options available later
if [ -n "${myOptions[AddVar]}" ]; then if [ -n "${myOptions[AddVar]}" ]; then
if [ -z "${myDir[host_name]}" ]; 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 exit 1
fi fi
hname=`echo "${myDir[host_name]}" | tr " " "+"` hname=`echo "${myDir[host_name]}" | tr " " "+"`
@ -462,6 +492,20 @@ if [ -n "${myOptions[Create]}" ]; then
exit exit
fi 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 all we said was --apply, then we want to apply previous changes
if [ -n "${myOptions[Apply]}" ]; then if [ -n "${myOptions[Apply]}" ]; then
do_apply do_apply

Loading…
Cancel
Save