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