Lots of updates to napi

This commit is contained in:
2026-03-17 09:48:57 -04:00
parent 35d722541b
commit 3e3330e3da
+202 -202
View File
@@ -8,17 +8,17 @@ APIKEY=""
XI_URL="" XI_URL=""
curl="curl -k -s" curl="curl -k -s"
# myDir (for "Directives") are now too big for individual variables. We'll read them into an associative array instead # myCmd (for "Commands") are now too big for individual variables. We'll read them into an associative array instead
declare -A myDir declare -A myCmd
# We need a separate thing for myOptions and it's easier to have a couple of them be straight variables # We need a separate thing for myOpts and it's easier to have a couple of them be straight variables
declare -A myOptions declare -A myOpts
myOptions[API]="objects" myOpts[API]="objects"
myOptions[APIep]="servicestatus" myOpts[APIep]="servicestatus"
#myOptions[TestMode]="" #myOpts[TestMode]=""
#myOptions[Create]="" #myOpts[Create]=""
#myOptions[Apply]="" #myOpts[Apply]=""
#myOptions[Options]="" #myOpts[Options]=""
verbose="0" verbose="0"
tmpJSON="" tmpJSON=""
tmpQuick="" tmpQuick=""
@@ -31,7 +31,7 @@ APIinfo["config/contact"]=".[]"
APIinfo["config/contactgroup"]=".[]" APIinfo["config/contactgroup"]=".[]"
APIinfo["config/host"]=".[]" APIinfo["config/host"]=".[]"
APIinfo["config/hostgroup"]=".[]" APIinfo["config/hostgroup"]=".[]"
APIinfo["config/service"]="" APIinfo["config/service"]=".[]"
APIinfo["config/servicegroup"]=".[]" APIinfo["config/servicegroup"]=".[]"
APIinfo["config/timeperiod"]=".[]" APIinfo["config/timeperiod"]=".[]"
@@ -49,9 +49,9 @@ APIinfo["objects/host"]=".host[]"
APIinfo["objects/hoststatus"]=".hoststatus[]" APIinfo["objects/hoststatus"]=".hoststatus[]"
APIinfo["objects/logentries"]="" APIinfo["objects/logentries"]=""
APIinfo["objects/rrdexport"]="" APIinfo["objects/rrdexport"]=""
APIinfo["objects/service"]=".[]" APIinfo["objects/service"]=".service[]"
APIinfo["objects/serviceavailability"]=".serviceavailability[]" APIinfo["objects/serviceavailability"]=".serviceavailability[]"
APIinfo["objects/servicegroup"]="" APIinfo["objects/servicegroup"]=".servicegroup[]"
APIinfo["objects/servicegroupmembers"]=".servicegroup[]" APIinfo["objects/servicegroupmembers"]=".servicegroup[]"
APIinfo["objects/servicestatus"]=".servicestatus[]" APIinfo["objects/servicestatus"]=".servicestatus[]"
APIinfo["objects/sla"]="" APIinfo["objects/sla"]=""
@@ -67,37 +67,37 @@ do_debug() {
get_myAPI() { get_myAPI() {
case "$1" in case "$1" in
o*) myOptions[API]="objects";; o*) myOpts[API]="objects";;
c*) myOptions[API]="config";; c*) myOpts[API]="config";;
s*) myOptions[API]="system";; s*) myOpts[API]="system";;
*) myOptions[API]="";; *) myOpts[API]="";;
esac esac
} }
get_myAPIep() { get_myAPIep() {
case "$1" in case "$1" in
c|contact) myOptions[APIep]="contact";; c|contact) myOpts[APIep]="contact";;
cg|contactgroup) myOptions[APIep]="contactgroup";; cg|contactgroup) myOpts[APIep]="contactgroup";;
cgm|contactgroupmembers) myOptions[APIep]="contactgroupmembers";; cgm|contactgroupmembers) myOpts[APIep]="contactgroupmembers";;
co|comment) myOptions[APIep]="comment";; co|comment) myOpts[APIep]="comment";;
com|command) myOptions[APIep]="command";; com|command) myOpts[APIep]="command";;
dt|downtime) myOptions[APIep]="downtime";; dt|downtime) myOpts[APIep]="downtime";;
ha|hostavailability) myOptions[API]="objects"; myOptions[APIep]="hostavailability";; ha|hostavailability) myOpts[API]="objects"; myOpts[APIep]="hostavailability";;
hg|hostgroup) myOptions[APIep]="hostgroup";; hg|hostgroup) myOpts[APIep]="hostgroup";;
hgm|hostgroupmembers) myOptions[API]="objects"; myOptions[APIep]="hostgroupmembers";; hgm|hostgroupmembers) myOpts[API]="objects"; myOpts[APIep]="hostgroupmembers";;
h|host) myOptions[APIep]="host";; h|host) myOpts[APIep]="host";;
hs|hoststatus) myOptions[APIep]="hoststatus";; hs|hoststatus) myOpts[APIep]="hoststatus";;
le|logentries) myOptions[APIep]="logentries";; le|logentries) myOpts[APIep]="logentries";;
sa|serviceavailability) myOptions[API]="objects"; myOptions[APIep]="serviceavailability";; sa|serviceavailability) myOpts[API]="objects"; myOpts[APIep]="serviceavailability";;
sgm|servicegroupmembers) myOptions[API]="objects"; myOptions[APIep]="servicegroupmembers";; sgm|servicegroupmembers) myOpts[API]="objects"; myOpts[APIep]="servicegroupmembers";;
sg|servicegroup) myOptions[APIep]="servicegroup";; sg|servicegroup) myOpts[APIep]="servicegroup";;
sh|statehistory) myOptions[API]="objects"; myOptions[APIep]="statehistory";; sh|statehistory) myOpts[API]="objects"; myOpts[APIep]="statehistory";;
s|service) myOptions[APIep]="service";; s|service) myOpts[APIep]="service";;
ss|servicestatus) myOptions[APIep]="servicestatus";; ss|servicestatus) myOpts[APIep]="servicestatus";;
tp|timeperiod) myOptions[APIep]="timeperiod";; tp|timeperiod) myOpts[APIep]="timeperiod";;
u|user) myOptions[APIep]="user";; u|user) myOpts[APIep]="user";;
uo|unconfigured) myOptiosn[API]="objects"; myOptions[APIep]="unconfigured";; uo|unconfigured) myOptiosn[API]="objects"; myOpts[APIep]="unconfigured";;
*) myOptions[APIep]="";; *) myOpts[APIep]="";;
esac esac
} }
@@ -258,101 +258,101 @@ while [ -n "$1" ]; do
--api) get_myAPI "$2"; shift 2;; --api) get_myAPI "$2"; shift 2;;
-t|--object) get_myAPIep "$2"; shift 2;; -t|--object) get_myAPIep "$2"; shift 2;;
--stats) do_stats; shift 2;; --stats) do_stats; shift 2;;
--ack) myDir[problem_has_been_acknowledged]="$2"; shift 2;; --ack) myCmd[problem_has_been_acknowledged]="$2"; shift 2;;
--ace) myDir[active_checks_enabled]="$2"; shift 2;; --ace) myCmd[active_checks_enabled]="$2"; shift 2;;
-c|--command) myDir[check_command]="$2"; shift 2;; -c|--command) myCmd[check_command]="$2"; shift 2;;
-cca) myDir[current_check_attempt]="$2"; shift 2;; -cca) myCmd[current_check_attempt]="$2"; shift 2;;
--cname) myDir[command_name]="$2"; shift 2;; --cname) myCmd[command_name]="$2"; shift 2;;
--cline) myDir[command_line]="$2"; shift 2;; --cline) myCmd[command_line]="$2"; shift 2;;
-cnn) myDir[current_notification_number]="$2"; shift 2;; -cnn) myCmd[current_notification_number]="$2"; shift 2;;
-cg|--contactgroup) myDir[contact_groups]="$2"; shift 2;; -cg|--contactgroup) myCmd[contact_groups]="$2"; shift 2;;
-cn|--configname) myDir[config_name]="$2"; shift 2;; -cn|--configname) myCmd[config_name]="$2"; shift 2;;
-D) myDir[$2]="$3"; shift 3;; -D) myCmd[$2]="$3"; shift 3;;
-hg|--hostgroup) myDir[hostgroup_name]="$2"; shift 2;; -hg|--hostgroup) myCmd[hostgroup_name]="$2"; shift 2;;
-hgm) myDir[hostgroup_members]="$2"; shift 2;; -hgm) myCmd[hostgroup_members]="$2"; shift 2;;
-h|--host) myDir[host_name]="$2"; shift 2;; -h|--host) myCmd[host_name]="$2"; shift 2;;
--output) myDir[output]="$2"; shift 2;; --output) myCmd[output]="$2"; shift 2;;
-sg|--servicegroup) myDir[servicegroup_name]="$2"; shift 2;; -sg|--servicegroup) myCmd[servicegroup_name]="$2"; shift 2;;
-sgm) myDir[servicegroup_members]="$2"; shift 2;; -sgm) myCmd[servicegroup_members]="$2"; shift 2;;
-s|--service) myDir[service_description]="$2"; shift 2;; -s|--service) myCmd[service_description]="$2"; shift 2;;
--state) myDir[current_state]="$2"; shift 2;; --state) myCmd[current_state]="$2"; shift 2;;
--stype) myDir[state_type]="$2"; shift 2;; --stype) myCmd[state_type]="$2"; shift 2;;
--apply) myOptions[Apply]="true"; shift 1;; --apply) myOpts[Apply]="true"; shift 1;;
--col) myOptions[Column]="$2"; shift 2;; --col) myOpts[Column]="$2"; shift 2;;
--ctrace) myOptions[CommandTrace]="true"; shift 1;; --ctrace) myOpts[CommandTrace]="true"; shift 1;;
--create) myOptions[Create]="true"; shift 1;; --create) myOpts[Create]="true"; shift 1;;
--delete) myOptions[Delete]="true"; shift 1;; --delete) myOpts[Delete]="true"; shift 1;;
--disable) myOptions[Disable]="true"; shift 1;; --disable) myOpts[Disable]="true"; shift 1;;
--enable) myOptions[Enable]="true"; shift 1;; --enable) myOpts[Enable]="true"; shift 1;;
--addvar) myOptions[AddVar]="$2"; shift 1;; --addvar) myOpts[AddVar]="$2"; shift 1;;
--users) myOptions[Users]="0"; shift 1;; --users) myOpts[Users]="0"; shift 1;;
--usersa) myOptions[Users]="1"; shift 1;; --usersa) myOpts[Users]="1"; shift 1;;
--useradd) myOptions[UserAdd]="$2"; shift 2;; --useradd) myOpts[UserAdd]="$2"; shift 2;;
--userdel) myOptions[UserDel]="$2"; shift 2;; --userdel) myOpts[UserDel]="$2"; shift 2;;
-f|--fields) myOptions[Fields]="$2"; shift 2;; -f|--fields) myOpts[Fields]="$2"; shift 2;;
--file) myOptions[File]="$2"; shift 2;; --file) myOpts[File]="$2"; shift 2;;
-j|--jq) myOptions[MoreJQ]="$2"; shift 2;; -j|--jq) myOpts[MoreJQ]="$2"; shift 2;;
-o|--opt) myOptions[Options]+="$2,"; shift 2;; -o|--opt) myOpts[Options]+="$2,"; shift 2;;
--order) myOptions[OrderBy]="$2"; shift 2;; --order) myOpts[OrderBy]="$2"; shift 2;;
-q) myOptions[Quick]="true"; myOptions[Options]+="c,"; shift 1;; -q) myOpts[Quick]="true"; myOpts[Options]+="c,"; shift 1;;
-qq) myOptions[Quick]="true"; myOptions[Options]+="c,C,"; shift 1;; -qq) myOpts[Quick]="true"; myOpts[Options]+="c,C,"; shift 1;;
-Q|--quick) myOptions[Quick]="true"; shift 1;; -Q|--quick) myOpts[Quick]="true"; shift 1;;
--records) myOptions[Records]="$2"; shift 2;; --records) myOpts[Records]="$2"; shift 2;;
--save) myOptions[Save]="$2"; shift 2;; --save) myOpts[Save]="$2"; shift 2;;
--end) myOptions[End]="$2"; shift 2;; --end) myOpts[End]="$2"; shift 2;;
--start) myOptions[Start]="$2"; shift 2;; --start) myOpts[Start]="$2"; shift 2;;
--status) myOptions[Status]="true"; shift 1;; --status) myOpts[Status]="true"; shift 1;;
--hstatus) myOptions[HStatus]="true"; shift 1;; --hstatus) myOpts[HStatus]="true"; shift 1;;
--bstatus) myOptions[Status]="true"; myOptions[BStatus]="true"; shift 1;; --bstatus) myOpts[Status]="true"; myOpts[BStatus]="true"; shift 1;;
--bhstatus) myOptions[HStatus]="true"; myOptions[BHStatus]="true"; shift 1;; --bhstatus) myOpts[HStatus]="true"; myOpts[BHStatus]="true"; shift 1;;
--raw) myOptions[Raw]="true"; shift 1;; --raw) myOpts[Raw]="true"; shift 1;;
--test) myOptions[TestMode]="true"; shift 1;; --test) myOpts[TestMode]="true"; shift 1;;
*) shift 1;; *) shift 1;;
esac esac
done done
# Fix things that are impossible to call # Fix things that are impossible to call
[ "${myOptions[API]}" = "config" -a "${myOptions[APIep]}" = "servicestatus" ] && myOptions[APIep]="service" [ "${myOpts[API]}" = "config" -a "${myOpts[APIep]}" = "servicestatus" ] && myOpts[APIep]="service"
[ "${myOptions[API]}" = "config" -a "${myOptions[APIep]}" = "hoststatus" ] && myOptions[APIep]="host" [ "${myOpts[API]}" = "config" -a "${myOpts[APIep]}" = "hoststatus" ] && myOpts[APIep]="host"
# Convert command line date formats to UNIX time # Convert command line date formats to UNIX time
[ -n "${myOptions[Start]}" ] && myOptions[Start]=`convert_time "${myOptions[Start]}"` [ -n "${myOpts[Start]}" ] && myOpts[Start]=`convert_time "${myOpts[Start]}"`
[ -n "${myOptions[End]}" ] && myOptions[End]=`convert_time "${myOptions[End]}"` [ -n "${myOpts[End]}" ] && myOpts[End]=`convert_time "${myOpts[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 "${myOpts[Status]}" ]; then
myOptions[API]="objects" myOpts[API]="objects"
myOptions[APIep]="servicestatus" myOpts[APIep]="servicestatus"
myOptions[Create]="" myOpts[Create]=""
myOptions[Disable]="" myOpts[Disable]=""
myOptions[Apply]="" myOpts[Apply]=""
myOptions[Options]="" myOpts[Options]=""
myOptions[Quick]="true" myOpts[Quick]="true"
myOptions[Options]+="c,X," myOpts[Options]+="c,X,"
# If we did a bstatus, then we only want things that are bad # If we did a bstatus, then we only want things that are bad
if [ -n "${myOptions[BStatus]}" ]; then if [ -n "${myOpts[BStatus]}" ]; then
myDir[current_state]="[^0]" myCmd[current_state]="[^0]"
fi fi
fi fi
if [ -n "${myOptions[HStatus]}" ]; then if [ -n "${myOpts[HStatus]}" ]; then
myOptions[API]="objects" myOpts[API]="objects"
myOptions[APIep]="hoststatus" myOpts[APIep]="hoststatus"
myOptions[Create]="" myOpts[Create]=""
myOptions[Disable]="" myOpts[Disable]=""
myOptions[Apply]="" myOpts[Apply]=""
myOptions[Options]="" myOpts[Options]=""
myOptions[Quick]="true" myOpts[Quick]="true"
myOptions[Options]+="c,X," myOpts[Options]+="c,X,"
# If we did a bstatus, then we only want things that are bad # If we did a bstatus, then we only want things that are bad
if [ -n "${myOptions[BHStatus]}" ]; then if [ -n "${myOpts[BHStatus]}" ]; then
myDir[current_state]="[^0]" myCmd[current_state]="[^0]"
fi fi
fi fi
# If we're listing users, then set things accordingly # If we're listing users, then set things accordingly
if [ -n "${myOptions[Users]}" ]; then if [ -n "${myOpts[Users]}" ]; then
myOptions[API]="system" myOpts[API]="system"
myOptions[APIep]="user" myOpts[APIep]="user"
fi fi
# Get our API key # Get our API key
@@ -379,20 +379,20 @@ do_api() {
api_start="$1" api_start="$1"
api_command="$2" api_command="$2"
url="${XI_URL}/api/v1/${api_start}/${api_command}?pretty=0&apikey=${APIKEY}" url="${XI_URL}/api/v1/${api_start}/${api_command}?pretty=0&apikey=${APIKEY}"
[ -n "${myOptions[Start]}" ] && url+="&starttime=${myOptions[Start]}" [ -n "${myOpts[Start]}" ] && url+="&starttime=${myOpts[Start]}"
[ -n "${myOptions[End]}" ] && url+="&endtime=${myOptions[End]}" [ -n "${myOpts[End]}" ] && url+="&endtime=${myOpts[End]}"
[ -n "${myOptions[Records]}" ] && url+="&records=${myOptions[Records]}" [ -n "${myOpts[Records]}" ] && url+="&records=${myOpts[Records]}"
[ -n "${myOptions[OrderBy]}" ] && url+="&orderby=${myOptions[OrderBy]}" [ -n "${myOpts[OrderBy]}" ] && url+="&orderby=${myOpts[OrderBy]}"
[ "${myOptions[Users]}" == "1" ] && url+="&advanced=1" [ "${myOpts[Users]}" == "1" ] && url+="&advanced=1"
if [ -n "${myOptions[Column]}" ]; then if [ -n "${myOpts[Column]}" ]; then
colName=`echo "${myOptions[Column]}" | cut -d= -f 1 | tr " " "+"` colName=`echo "${myOpts[Column]}" | cut -d= -f 1 | tr " " "+"`
colVal=`echo "${myOptions[Column]}" | cut -d= -f 2 | tr " " "+"` colVal=`echo "${myOpts[Column]}" | cut -d= -f 2 | tr " " "+"`
do_debug 1 "colName=$colName colVal=$colVal" do_debug 1 "colName=$colName colVal=$colVal"
url+="&$colName=$colVal" url+="&$colName=$colVal"
fi fi
do_debug 2 "start=$1 command=$2" do_debug 2 "start=$1 command=$2"
do_debug 1 "Executing (testmode=${myOptions[TestMode]}): $url" do_debug 1 "Executing (testmode=${myOpts[TestMode]}): $url"
[ -z "${myOptions[TestMode]}" ] && $curl -XGET -k "$url" [ -z "${myOpts[TestMode]}" ] && $curl -XGET -k "$url"
} }
# curl -XPOST "http://<host>/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"
@@ -405,7 +405,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 type=$api_type" do_debug 2 "start=$1 command=$2 type=$api_type"
do_debug 1 "Executing: $curl -${api_type} -k \"$url\" -d \"$api_data\"" do_debug 1 "Executing: $curl -${api_type} -k \"$url\" -d \"$api_data\""
[ -z "${myOptions[TestMode]}" ] && $curl -${api_type} -k "$url" -d "$api_data" [ -z "${myOpts[TestMode]}" ] && $curl -${api_type} -k "$url" -d "$api_data"
} }
do_api_put() { do_api_put() {
@@ -417,7 +417,7 @@ do_api_put() {
url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0&$api_data" url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0&$api_data"
do_debug 2 "start=$1 command=$2 type=$api_type" do_debug 2 "start=$1 command=$2 type=$api_type"
do_debug 1 "Executing: $curl -${api_type} -k \"$url\"" do_debug 1 "Executing: $curl -${api_type} -k \"$url\""
[ -z "${myOptions[TestMode]}" ] && $curl -${api_type} -k "$url" [ -z "${myOpts[TestMode]}" ] && $curl -${api_type} -k "$url"
} }
do_api_delete() { do_api_delete() {
@@ -429,24 +429,24 @@ do_api_delete() {
url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0&${api_data}" url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0&${api_data}"
do_debug 2 "start=$1 command=$2 type=$api_type" do_debug 2 "start=$1 command=$2 type=$api_type"
do_debug 1 "Executing: $curl -${api_type} -k \"$url\" -d \"$api_data\"" do_debug 1 "Executing: $curl -${api_type} -k \"$url\" -d \"$api_data\""
[ -z "${myOptions[TestMode]}" ] && $curl -${api_type} -k "$url" #-d "$api_data" [ -z "${myOpts[TestMode]}" ] && $curl -${api_type} -k "$url" #-d "$api_data"
} }
# Time to make a hostgroup # Time to make a hostgroup
create_hostgroup() { create_hostgroup() {
do_debug 1 "about to do an API post call" do_debug 1 "about to do an API post call"
api_data="hostgroup_name=${myDir[hostgroup_name]}&alias=${myDir[hostgroup_name]}" api_data="hostgroup_name=${myCmd[hostgroup_name]}&alias=${myCmd[hostgroup_name]}"
[ -n "${myDir[host_name]}" ] && api_data+="&members=${myDir[host_name]}" [ -n "${myCmd[host_name]}" ] && api_data+="&members=${myCmd[host_name]}"
[ -n "${myDir[hostgroup_members]}" ] && api_data+="&hostgroup_members=${myDir[hostgroup_members]}" [ -n "${myCmd[hostgroup_members]}" ] && api_data+="&hostgroup_members=${myCmd[hostgroup_members]}"
do_api_post config hostgroup "$api_data" do_api_post config hostgroup "$api_data"
} }
# Time to make a servicegroup # Time to make a servicegroup
create_servicegroup() { create_servicegroup() {
do_debug 1 "about to do an API post call" do_debug 1 "about to do an API post call"
api_data="servicegroup_name=${myDir[servicegroup_name]}&alias=${myDir[servicegroup_name]}" api_data="servicegroup_name=${myCmd[servicegroup_name]}&alias=${myCmd[servicegroup_name]}"
[ -n "${myDir[service_description]}" ] && api_data+="&members=${myDir[service_description]}" [ -n "${myCmd[service_description]}" ] && api_data+="&members=${myCmd[service_description]}"
[ -n "${myDir[servicegroup_members]}" ] && api_data+="&servicegroup_members=${myDir[servicegroup_members]}" [ -n "${myCmd[servicegroup_members]}" ] && api_data+="&servicegroup_members=${myCmd[servicegroup_members]}"
do_api_post config servicegroup "$api_data" do_api_post config servicegroup "$api_data"
} }
@@ -454,16 +454,16 @@ create_servicegroup() {
# NEED: curl -XPUT "http://<host>/nagiosxi/api/v1/config/service/NAS/Physical+Disks?apikey=<token>&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://<host>/nagiosxi/api/v1/config/service/NAS/Physical+Disks?apikey=<token>&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 "${myCmd[config_name]}" | tr " " "+"`
sname=`echo "${myDir[service_description]}" | tr " " "+"` sname=`echo "${myCmd[service_description]}" | tr " " "+"`
hname=`echo "${myDir[host_name]}" | tr " " "+"` hname=`echo "${myCmd[host_name]}" | tr " " "+"`
do_api_put config "service/$cname/$sname" "host_name=$hname" do_api_put config "service/$cname/$sname" "host_name=$hname"
} }
do_enable() { do_enable() {
do_debug 1 "about to enable a service" do_debug 1 "about to enable a service"
cname=`echo "${myDir[config_name]}" | tr " " "+"` cname=`echo "${myCmd[config_name]}" | tr " " "+"`
sname=`echo "${myDir[service_description]}" | tr " " "+"` sname=`echo "${myCmd[service_description]}" | tr " " "+"`
if [ -z "$cname" -o -z "$sname" ]; then 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" echo "When using --enable then you must specify -cn for the config name and -s for the service to enable"
exit 1 exit 1
@@ -473,8 +473,8 @@ do_enable() {
do_disable() { do_disable() {
do_debug 1 "about to disable a service" do_debug 1 "about to disable a service"
cname=`echo "${myDir[config_name]}" | tr " " "+"` cname=`echo "${myCmd[config_name]}" | tr " " "+"`
sname=`echo "${myDir[service_description]}" | tr " " "+"` sname=`echo "${myCmd[service_description]}" | tr " " "+"`
if [ -z "$cname" -o -z "$sname" ]; then 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" echo "When using --disable then you must specify -cn for the config name and -s for the service to disable"
exit 1 exit 1
@@ -485,8 +485,8 @@ do_disable() {
# 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 something..." do_debug 1 "Trying to delete something..."
host="${myDir[host_name]}" host="${myCmd[host_name]}"
svc="${myDir[service_description]}" svc="${myCmd[service_description]}"
if [ -n "$host" -a -n "$svc" ]; then if [ -n "$host" -a -n "$svc" ]; then
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"
host=`echo "$host" | tr " " "+"` host=`echo "$host" | tr " " "+"`
@@ -495,7 +495,7 @@ do_delete() {
api_data="host_name=$host&service_description=$svc" api_data="host_name=$host&service_description=$svc"
do_api_delete config service "$api_data" XDELETE do_api_delete config service "$api_data" XDELETE
else else
user="${myOptions[UserDel]}" user="${myOpts[UserDel]}"
if [ -n "$user" ]; then if [ -n "$user" ]; then
do_debug 1 "Trying to delete user, so let's verify first" do_debug 1 "Trying to delete user, so let's verify first"
userId=`echo "$user" | egrep "^[0-9]+$"` userId=`echo "$user" | egrep "^[0-9]+$"`
@@ -507,10 +507,10 @@ do_delete() {
do_create() { do_create() {
do_debug 1 "about to do a create_command" do_debug 1 "about to do a create_command"
[ -n "${myOptions[Delete]}" ] && do_delete [ -n "${myOpts[Delete]}" ] && do_delete
[ -n "${myDir[host_name]}" -a -n "${myDir[service_description]}" -a -n "${myDir[config_name]}" ] && update_service [ -n "${myCmd[host_name]}" -a -n "${myCmd[service_description]}" -a -n "${myCmd[config_name]}" ] && update_service
[ -n "${myDir[hostgroup_name]}" ] && create_hostgroup [ -n "${myCmd[hostgroup_name]}" ] && create_hostgroup
[ -n "${myDir[servicegroup_members]}" ] && create_servicegroup [ -n "${myCmd[servicegroup_members]}" ] && create_servicegroup
} }
do_apply() { do_apply() {
@@ -520,42 +520,42 @@ do_apply() {
# 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 "${myOpts[AddVar]}" ]; then
if [ -z "${myDir[host_name]}" ]; then if [ -z "${myCmd[host_name]}" ]; then
echo "When 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 "${myCmd[host_name]}" | tr " " "+"`
varName=`echo "${myOptions[AddVar]}" | cut -d= -f 1 | tr " " "+" | sed -e "s/^_//"` varName=`echo "${myOpts[AddVar]}" | cut -d= -f 1 | tr " " "+" | sed -e "s/^_//"`
varVal=`echo "${myOptions[AddVar]}" | cut -d= -f 2 | tr " " "+"` varVal=`echo "${myOpts[AddVar]}" | cut -d= -f 2 | tr " " "+"`
do_debug 1 "varName=$varName varVal=$varVal" do_debug 1 "varName=$varName varVal=$varVal"
do_api_put config "host/$hname" "_${varName}=${varVal}" do_api_put config "host/$hname" "_${varName}=${varVal}"
exit exit
fi fi
# If we said "--create" then we want to make something # If we said "--create" then we want to make something
if [ -n "${myOptions[Create]}" ]; then if [ -n "${myOpts[Create]}" ]; then
do_create do_create
[ -n "${myOptions[Apply]}" -a -z "${myOptions[TestMode]}" ] && do_apply [ -n "${myOpts[Apply]}" -a -z "${myOpts[TestMode]}" ] && do_apply
exit exit
fi fi
# If we want to "--enable" [a service] then let's make that happen # If we want to "--enable" [a service] then let's make that happen
if [ -n "${myOptions[Enable]}" ]; then if [ -n "${myOpts[Enable]}" ]; then
do_enable do_enable
[ -n "${myOptions[Apply]}" -a -z "${myOptions[TestMode]}" ] && do_apply [ -n "${myOpts[Apply]}" -a -z "${myOpts[TestMode]}" ] && do_apply
exit exit
fi fi
# If we want to "--disable" [a service] then let's make that happen # If we want to "--disable" [a service] then let's make that happen
if [ -n "${myOptions[Disable]}" ]; then if [ -n "${myOpts[Disable]}" ]; then
do_disable do_disable
[ -n "${myOptions[Apply]}" -a -z "${myOptions[TestMode]}" ] && do_apply [ -n "${myOpts[Apply]}" -a -z "${myOpts[TestMode]}" ] && do_apply
exit exit
fi 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 "${myOpts[Apply]}" ]; then
do_apply do_apply
exit exit
fi fi
@@ -563,21 +563,21 @@ 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 "${myOptions[File]}" ]; then if [ -z "${myOpts[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 ${myOptions[API]} and myAPIep is ${myOptions[APIep]}" do_debug 2 " myAPI is ${myOpts[API]} and myAPIep is ${myOpts[APIep]}"
do_api "${myOptions[API]}" "${myOptions[APIep]}" > $tmpJSON do_api "${myOpts[API]}" "${myOpts[APIep]}" > $tmpJSON
else else
do_debug 1 "myFile=${myOptions[File]}" do_debug 1 "myFile=${myOpts[File]}"
tmpJSON="${myOptions[File]}" tmpJSON="${myOpts[File]}"
do_debug 1 "tmpJSON=$tmpJSON" do_debug 1 "tmpJSON=$tmpJSON"
fi fi
# if mySave is not empty, then we're just saving it into the file called ${myOptions[Save]} # if mySave is not empty, then we're just saving it into the file called ${myOpts[Save]}
if [ -n "${myOptions[Save]}" ]; then if [ -n "${myOpts[Save]}" ]; then
mv $tmpJSON ${myOptions[Save]} mv $tmpJSON ${myOpts[Save]}
do_debug 1 "JSON data saved to ${myOptions[Save]}" do_debug 1 "JSON data saved to ${myOpts[Save]}"
exit exit
fi fi
@@ -587,9 +587,9 @@ jq_check_case() {
thing="$*" thing="$*"
do_debug 1 "### JQ_CHECK_CASE looking for 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=${myOpts[Options]}"
val="| test(\"$thing\"" val="| test(\"$thing\""
[[ "${myOptions[Options]}" =~ "i," ]] && val+="; \"i\"" [[ "${myOpts[Options]}" =~ "i," ]] && val+="; \"i\""
echo "$val)" echo "$val)"
else else
echo "" echo ""
@@ -598,34 +598,34 @@ jq_check_case() {
# Create the jQuery search string # Create the jQuery search string
jq_get_fields() { jq_get_fields() {
[[ "${myOptions[Options]}" =~ "c," ]] && jqString+="| [${myOptions[Fields]}] | @csv" || jqString+="| ${myOptions[Fields]}" [[ "${myOpts[Options]}" =~ "c," ]] && jqString+="| [${myOpts[Fields]}] | @csv" || jqString+="| ${myOpts[Fields]}"
} }
# 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[${myOptions[API]}/${myOptions[APIep]}]}" do_debug 1 "APIinfo=${APIinfo[${myOpts[API]}/${myOpts[APIep]}]}"
jqString=${APIinfo[${myOptions[API]}/${myOptions[APIep]}]} jqString=${APIinfo[${myOpts[API]}/${myOpts[APIep]}]}
do_debug 2 " Before: jqString=$jqString" do_debug 2 " Before: jqString=$jqString"
for thing in "${!myDir[@]}"; do for thing in "${!myCmd[@]}"; do
[ -n "${myDir[$thing]}" ] && jqString+="| select(.$thing $(jq_check_case ${myDir[$thing]}))" [ -n "${myCmd[$thing]}" ] && jqString+="| select(.$thing $(jq_check_case ${myCmd[$thing]}))"
do_debug 2 " During: jqString=$jqString" do_debug 2 " During: jqString=$jqString"
done done
do_debug 2 " After: jqString=$jqString" do_debug 2 " After: jqString=$jqString"
# endpoint specific things # endpoint specific things
case "${myOptions[API]}/${myOptions[APIep]}" in case "${myOpts[API]}/${myOpts[APIep]}" in
objects/contactgroupmembers) objects/contactgroupmembers)
[[ ${myOptions[Options]} =~ "m," ]] && jqString+="| .members[] | .[] | .contact_name" [[ ${myOpts[Options]} =~ "m," ]] && jqString+="| .members[] | .[] | .contact_name"
[[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .contact_object_id" [[ ${myOpts[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .contact_object_id"
;; ;;
objects/hostgroupmembers) objects/hostgroupmembers)
[[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name" [[ ${myOpts[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
[[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id" [[ ${myOpts[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
;; ;;
objects/servicegroupmembers) objects/servicegroupmembers)
[[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name" [[ ${myOpts[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
[[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .service_object_id" [[ ${myOpts[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .service_object_id"
[[ ${myOptions[Options]} =~ "s," ]] && jqString+="| .members[] | .[] | .service_description" [[ ${myOpts[Options]} =~ "s," ]] && jqString+="| .members[] | .[] | .service_description"
tmpQuick=".service_object_id,.host_name,.service_description" tmpQuick=".service_object_id,.host_name,.service_description"
;; ;;
objects/hoststatus) tmpQuick=".host_name,.address,.current_state,.state_type,.last_check,.current_check_attempt,.normal_check_interval,.retry_check_interval,.max_check_attempts,.check_command"; objects/hoststatus) tmpQuick=".host_name,.address,.current_state,.state_type,.last_check,.current_check_attempt,.normal_check_interval,.retry_check_interval,.max_check_attempts,.check_command";
@@ -637,36 +637,36 @@ case "${myOptions[API]}/${myOptions[APIep]}" in
config/service) config/service)
# cat service.json| jq '.[] | select (.host_name | index("NAS-W"))' # cat service.json| jq '.[] | select (.host_name | index("NAS-W"))'
jqString=".[]" jqString=".[]"
[ -n "${myDir[host_name]}" ] && jqString+=" | select (.host_name | index(\"${myDir[host_name]}\"))" [ -n "${myCmd[host_name]}" ] && jqString+=" | select (.host_name | index(\"${myCmd[host_name]}\"))"
[ -n "${myDir[service_description]}" ] && jqString+="| select(.service_description $(jq_check_case ${myDir[service_description]}))" [ -n "${myCmd[service_description]}" ] && jqString+="| select(.service_description $(jq_check_case ${myCmd[service_description]}))"
;; ;;
config/host) tmpQuick=".host_name,.address,.check_command";; config/host) tmpQuick=".host_name,.address,.check_command";;
config/contact) tmpQuick=".contact_name,.email";; config/contact) tmpQuick=".contact_name,.email";;
system/user) tmpQuick=".user_id,.username,.name,.email,.enabled";; system/user) tmpQuick=".user_id,.username,.name,.email,.enabled";;
esac esac
do_debug 1 "myOptions[Options]=${myOptions[Options]}" do_debug 1 "myOpts[Options]=${myOpts[Options]}"
if [ -n "${myOptions[Quick]}" -a -n "${myOptions[Fields]}" ]; then if [ -n "${myOpts[Quick]}" -a -n "${myOpts[Fields]}" ]; then
myOptions[Fields]="$tmpQuick,${myOptions[Fields]}" myOpts[Fields]="$tmpQuick,${myOpts[Fields]}"
elif [ -n "${myOptions[Quick]}" ]; then elif [ -n "${myOpts[Quick]}" ]; then
myOptions[Fields]="$tmpQuick" myOpts[Fields]="$tmpQuick"
fi fi
[[ ${myOptions[Options]} =~ "C," ]] && echo "${myOptions[Fields]}" [[ ${myOpts[Options]} =~ "C," ]] && echo "${myOpts[Fields]}"
[[ ${myOptions[Options]} =~ "X," ]] && echo "$tmpQuicker" && printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' - [[ ${myOpts[Options]} =~ "X," ]] && echo "$tmpQuicker" && printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
do_debug 1 "myOptions[Fields]=${myOptions[Fields]}" do_debug 1 "myOpts[Fields]=${myOpts[Fields]}"
[ -n "${myOptions[Fields]}" ] && jq_get_fields [ -n "${myOpts[Fields]}" ] && jq_get_fields
jqString+="${myOptions[MoreJQ]}" jqString+="${myOpts[MoreJQ]}"
do_debug 1 "jqString=$jqString" do_debug 1 "jqString=$jqString"
if [ -n "${myOptions[TestMode]}" ]; then if [ -n "${myOpts[TestMode]}" ]; then
echo "### TEST MODE - Here is what would have been done (testMode=${myOptions[TestMode]}) ###" echo "### TEST MODE - Here is what would have been done (testMode=${myOpts[TestMode]}) ###"
echo "cat <tmpfile> | jq -r \"$jqString\"" echo "cat <tmpfile> | jq -r \"$jqString\""
else else
if [ -n "${myOptions[Raw]}" ]; then if [ -n "${myOpts[Raw]}" ]; then
do_debug 1 "*** RAW specified *** Overriding all other options" do_debug 1 "*** RAW specified *** Overriding all other options"
cat $tmpJSON cat $tmpJSON
else else
cat $tmpJSON | jq -r "$jqString" cat $tmpJSON | jq -r "$jqString"
fi fi
fi fi
[ -z "${myOptions[File]}" ] && rm $tmpJSON [ -z "${myOpts[File]}" ] && rm $tmpJSON