Added basic cgm to napi

dev
Eric Loyd 1 year ago
parent 1e8f9c1f0f
commit 1ed55ac09c

99
napi

@ -52,7 +52,7 @@ APIinfo["objects/timeperiod"]=""
APIinfo["objects/unconfigured"]="" APIinfo["objects/unconfigured"]=""
APIinfo["objects/hostgroupmembers"]=".hostgroup[]" APIinfo["objects/hostgroupmembers"]=".hostgroup[]"
APIinfo["objects/servicegroupmembers"]=".servicegroup[]" APIinfo["objects/servicegroupmembers"]=".servicegroup[]"
APIinfo["objects/contactgroupmembers"]="" APIinfo["objects/contactgroupmembers"]=".contactgroup[]"
APIinfo["objects/rrdexport"]="" APIinfo["objects/rrdexport"]=""
APIinfo["objects/cpexport"]="" APIinfo["objects/cpexport"]=""
APIinfo["objects/hostavailability"]=".hostavailability[]" APIinfo["objects/hostavailability"]=".hostavailability[]"
@ -78,6 +78,7 @@ get_myAPIep() {
co|comment) myOptions[APIep]="comment";; co|comment) myOptions[APIep]="comment";;
com|command) myOptions[APIep]="command";; com|command) myOptions[APIep]="command";;
cg|contactgroup) myOptions[APIep]="contactgroup";; cg|contactgroup) myOptions[APIep]="contactgroup";;
cgm|contactgroupmembers) myOptions[APIep]="contactgroupmembers";;
c|contact) myOptions[APIep]="contact";; c|contact) myOptions[APIep]="contact";;
dt|downtime) myOptions[APIep]="downtime";; dt|downtime) myOptions[APIep]="downtime";;
ha|hostavailability) myOptions[API]="objects"; myOptions[APIep]="hostavailability";; ha|hostavailability) myOptions[API]="objects"; myOptions[APIep]="hostavailability";;
@ -111,6 +112,10 @@ print_helpopt() {
C Same as C but also include the fields C Same as C but also include the fields
X Same as C but use a more human-friendly set of names for the fields when printed X Same as C but use a more human-friendly set of names for the fields when printed
contactgroupmembers:
m Only show contacts (not the complete JSON data)
o Only show host_object_id (specifying both options will result in errors)
hostgroupmembers: hostgroupmembers:
h Only show hosts (not the complete JSON data) h Only show hosts (not the complete JSON data)
o Only show host_object_id (specifying both options will result in errors) o Only show host_object_id (specifying both options will result in errors)
@ -124,6 +129,21 @@ HELPOPT_EOF
exit exit
} }
print_helpcreate() {
cat << HELPCREATE_EOF
If --create is specified, we can create/update various things. Note that NONE of this data is verified at this point so USE AT YOUR OWN RISK!!!
1) if --cname, -s, and -h are specified, then we can update <svc> in config_name <cname> so that the host list is <hosts>:
$0 --create -cn "NAS Checks" -s "Physical Disks" -h "NAS-1,NAS-2"
2) if -hg and -h are specified, then we can add/update a hostgroup:
$0 --create -hg Hostgroup -h Host,Host2
There are other combinations that are undocumented at this time.
HELPCREATE_EOF
exit
}
print_help() { print_help() {
cat << HELP_EOF cat << HELP_EOF
--api < o*bjects | c*onfig | s*ystem > --api < o*bjects | c*onfig | s*ystem >
@ -147,9 +167,11 @@ print_help() {
... comment | downtime | contact | host | service | hostgroup | ... ... comment | downtime | contact | host | service | hostgroup | ...
--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)
-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>
--helpcr* Show help for create options
--helpopt Show help for command options --helpopt Show help for command options
--help This text --help This text
-hg|--hostgroup hostgroup=<value> -hg|--hostgroup hostgroup=<value>
@ -215,6 +237,7 @@ convert_time() {
# get_opts # get_opts
while [ -n "$1" ]; do while [ -n "$1" ]; do
case "$1" in case "$1" in
--helpcr*) print_helpcreate;;
--helpopt) print_helpopt;; --helpopt) print_helpopt;;
--help) print_help;; --help) print_help;;
--key) APIKEY="$2"; shift 2;; --key) APIKEY="$2"; shift 2;;
@ -247,6 +270,7 @@ while [ -n "$1" ]; do
--col) myOptions[Column]="$2"; shift 2;; --col) myOptions[Column]="$2"; shift 2;;
--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;;
-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;;
-j|--jq) myOptions[MoreJQ]="$2"; shift 2;; -j|--jq) myOptions[MoreJQ]="$2"; shift 2;;
@ -314,8 +338,8 @@ do_api() {
[ -n "${myOptions[Records]}" ] && url+="&records=${myOptions[Records]}" [ -n "${myOptions[Records]}" ] && url+="&records=${myOptions[Records]}"
[ -n "${myOptions[OrderBy]}" ] && url+="&orderby=${myOptions[OrderBy]}" [ -n "${myOptions[OrderBy]}" ] && url+="&orderby=${myOptions[OrderBy]}"
if [ -n "${myOptions[Column]}" ]; then if [ -n "${myOptions[Column]}" ]; then
colName=`echo "${myOptions[Column]}" | cut -d= -f 1` colName=`echo "${myOptions[Column]}" | cut -d= -f 1 | tr " " "+"`
colVal=`echo "${myOptions[Column]}" | cut -d= -f 2` colVal=`echo "${myOptions[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
@ -330,13 +354,38 @@ do_api_post() {
api_command="$2" api_command="$2"
api_data="" api_data=""
[ -n "$3" ] && api_data="$3&applyconfig=0" [ -n "$3" ] && api_data="$3&applyconfig=0"
api_type="${4:-XPOST}"
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 type=$api_type"
do_debug 1 "Executing: $curl -XPOST -k \"$url\" -d \"$api_data\"" do_debug 1 "Executing: $curl -${api_type} -k \"$url\" -d \"$api_data\""
[ -z "${myOptions[TestMode]}" ] && $curl -XPOST -k "$url" -d "$api_data" [ -z "${myOptions[TestMode]}" ] && $curl -${api_type} -k "$url" -d "$api_data"
} }
# At this time, all we can create ia a hostgroup do_api_put() {
api_start="$1"
api_command="$2"
api_data=""
[ -n "$3" ] && api_data="$3&applyconfig=0"
api_type="${4:-XPUT}"
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"
}
do_api_delete() {
api_start="$1"
api_command="$2"
api_data=""
[ -n "$3" ] && api_data="$3&applyconfig=0"
api_type="${4:-XPUT}"
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"
}
# 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=${myDir[hostgroup_name]}&alias=${myDir[hostgroup_name]}"
@ -345,6 +394,7 @@ create_hostgroup() {
do_api_post config hostgroup "$api_data" do_api_post config hostgroup "$api_data"
} }
# 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=${myDir[servicegroup_name]}&alias=${myDir[servicegroup_name]}"
@ -353,8 +403,33 @@ create_servicegroup() {
do_api_post config servicegroup "$api_data" do_api_post config servicegroup "$api_data"
} }
# 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"
update_service() {
cname=`echo "${myDir[config_name]}" | tr " " "+"`
sname=`echo "${myDir[service_description]}" | tr " " "+"`
hname=`echo "${myDir[host_name]}" | tr " " "+"`
do_api_put config "service/$cname/$sname" "host_name=$hname"
}
# Delete a service, given hostname and service_description
do_delete() {
do_debug 1 "Trying to delete host and service, so let's verify first"
host="${myDir[host_name]}"
svc="${myDir[service_description]}"
[ -z "$host" -o -z "$svc" ] && echo "Cannot delete without a host and a service." && exit 1
host=`echo "$host" | tr " " "+"`
svc=`echo "$svc" | tr " " "+"`
do_debug 1 "DELETE: about to delete $host: $svc"
api_data="host_name=$host&service_description=$svc"
do_api_delete config service "$api_data" XDELETE
}
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 "${myDir[host_name]}" -a -n "${myDir[service_description]}" -a -n "${myDir[config_name]}" ] && update_service
[ -n "${myDir[hostgroup_name]}" ] && create_hostgroup [ -n "${myDir[hostgroup_name]}" ] && create_hostgroup
[ -n "${myDir[servicegroup_members]}" ] && create_servicegroup [ -n "${myDir[servicegroup_members]}" ] && create_servicegroup
} }
@ -424,6 +499,10 @@ done
# endpoint specific things # endpoint specific things
case "${myOptions[API]}/${myOptions[APIep]}" in case "${myOptions[API]}/${myOptions[APIep]}" in
objects/contactgroupmembers)
[[ ${myOptions[Options]} =~ "m," ]] && jqString+="| .members[] | .[] | .contact_name"
[[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .contact_object_id"
;;
objects/hostgroupmembers) objects/hostgroupmembers)
[[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name" [[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
[[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id" [[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
@ -440,6 +519,12 @@ case "${myOptions[API]}/${myOptions[APIep]}" in
tmpQuicker="Service,Host,State,Type,Last Check,Attempt,Normal,Retry,Max,Output";; tmpQuicker="Service,Host,State,Type,Last Check,Attempt,Normal,Retry,Max,Output";;
objects/contact) tmpQuick=".contact_name,.email_address,.host_notifications_enabled,.service_notifications_enabled,.is_active";; objects/contact) tmpQuick=".contact_name,.email_address,.host_notifications_enabled,.service_notifications_enabled,.is_active";;
objects/statehistory) tmpQuick=".host_name,.service_description,.state_time,.state_change,.state,.state_type,.current_check_attempt,.max_check_attempts,.last_state,.last_hard_state,.output";; objects/statehistory) tmpQuick=".host_name,.service_description,.state_time,.state_change,.state,.state_type,.current_check_attempt,.max_check_attempts,.last_state,.last_hard_state,.output";;
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]}))"
;;
config/host) tmpQuick=".host_name,.address,.check_command";; config/host) tmpQuick=".host_name,.address,.check_command";;
esac esac

Loading…
Cancel
Save