You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
380 lines
10 KiB
Bash
380 lines
10 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
APIkeyFile=".nagiosapikey"
|
|
[ ! -r "$APIkeyFile" ] && APIkeyFile="${HOME}/.nagiosapikey"
|
|
[ ! -r "$APIkeyFile" ] && APIkeyFile=""
|
|
|
|
APIKEY=""
|
|
XI_URL=""
|
|
curl="curl -k -s"
|
|
# curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=<key>"
|
|
|
|
verbose="0"
|
|
testMode=""
|
|
doCreate=""
|
|
|
|
myAck=""
|
|
myCG=""
|
|
myCommand=""
|
|
myConfigName=""
|
|
myFields=""
|
|
myFile=""
|
|
myHG=""
|
|
myHost=""
|
|
myOutput=""
|
|
mySave=""
|
|
myService=""
|
|
mySG=""
|
|
myState=""
|
|
myStateType=""
|
|
myQuick=""
|
|
|
|
tmpJSON=""
|
|
moreJQ=""
|
|
cmdOptions=""
|
|
myAPI="objects"
|
|
myAPIep="servicestatus"
|
|
|
|
# Different API commands return different JSON datasets. So let's make a lookup table that figures out where to start the data extracts
|
|
declare -A APIinfo
|
|
APIinfo["config/host"]=".[]"
|
|
APIinfo["config/service"]=""
|
|
APIinfo["config/hostgroup"]=".[]"
|
|
APIinfo["config/servicegroup"]=".[]"
|
|
APIinfo["config/command"]=""
|
|
APIinfo["config/contact"]=""
|
|
APIinfo["config/contactgroup"]=".[]"
|
|
APIinfo["config/timeperiod"]=""
|
|
|
|
APIinfo["objects/hoststatus"]=""
|
|
APIinfo["objects/servicestatus"]=".servicestatus[]"
|
|
APIinfo["objects/logentries"]=""
|
|
APIinfo["objects/statehistory"]=""
|
|
APIinfo["objects/comment"]=""
|
|
APIinfo["objects/downtime"]=""
|
|
APIinfo["objects/contact"]=""
|
|
APIinfo["objects/host"]=".host[]"
|
|
APIinfo["objects/service"]=".[]"
|
|
APIinfo["objects/hostgroup"]=".[]"
|
|
APIinfo["objects/servicegroup"]=""
|
|
APIinfo["objects/contactgroup"]=".contactgroup[]"
|
|
APIinfo["objects/timeperiod"]=""
|
|
APIinfo["objects/unconfigured"]=""
|
|
APIinfo["objects/hostgroupmembers"]=".hostgroup[]"
|
|
APIinfo["objects/servicegroupmembers"]=""
|
|
APIinfo["objects/contactgroupmembers"]=""
|
|
APIinfo["objects/rrdexport"]=""
|
|
APIinfo["objects/cpexport"]=""
|
|
APIinfo["objects/hostavailability"]=""
|
|
APIinfo["objects/serviceavailability"]=""
|
|
APIinfo["objects/sla"]=""
|
|
APIinfo["objects/bpi"]=""
|
|
|
|
get_myAPI() {
|
|
case "$1" in
|
|
o*) myAPI="objects";;
|
|
c*) myAPI="config";;
|
|
s*) myAPI="system";;
|
|
*) myAPI="";;
|
|
esac
|
|
}
|
|
|
|
get_myAPIep() {
|
|
case "$1" in
|
|
co|comment) myAPIep="comment";;
|
|
cg|contactgroup) myAPIep="contactgroup";;
|
|
c|contact) myAPIep="contact";;
|
|
dt|downtime) myAPIep="downtime";;
|
|
hgm|hostgroupmembers) myAPI="objects"; myAPIep="hostgroupmembers";;
|
|
hg|hostgroup) myAPIep="hostgroup";;
|
|
h|host) myAPIep="host";;
|
|
hs|hoststatus) myAPIep="hoststatus";;
|
|
le|logentries) myAPIep="logentries";;
|
|
sgm|servicegroupmembers) myAPI="objects"; myAPIep="servicegroupmembers";;
|
|
sg|servicegroup) myAPIep="servicegroup";;
|
|
s|service) myAPIep="service";;
|
|
ss|servicestatus) myAPIep="servicestatus";;
|
|
sh|statehistory) myAPIep="statehistory";;
|
|
tp|timeperiod) myAPIep="timeperiod";;
|
|
*) myAPIep="";;
|
|
esac
|
|
}
|
|
|
|
print_helpopt() {
|
|
cat << HELPOPT_EOF
|
|
Use these options to add options to the command specified. To add multiple options, specify -o|--opt multiple times (they will add together)
|
|
|
|
Options for different APIs:
|
|
|
|
servicestatus:
|
|
c Show fields selected by -f as quoted CSV
|
|
|
|
hostgroupmembers:
|
|
h Only show hosts (not the complete JSON data)
|
|
o Only show host_object_id (specifying both options will result in errors)
|
|
|
|
HELPOPT_EOF
|
|
exit
|
|
}
|
|
|
|
print_help() {
|
|
cat << HELP_EOF
|
|
--help This text
|
|
--helpopt Show help for command options
|
|
--keyfile APIkeyFile="\$2"
|
|
-q|--quick Appends the following to -f: .host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged (assumes servicestatus)
|
|
-Q Same as --quick but add -o c
|
|
-j|--jq moreJQ="\$2"
|
|
-v|--verbose verbose=\$((\$verbose + 1))
|
|
--create doCreate="true"
|
|
--key APIKEY="\$2"
|
|
--url XI_URL="\$2"
|
|
--save mySave="\$2"
|
|
--file myFile="\$2"
|
|
-f|--fields myFields="\$2"
|
|
-h|--host myHost="\$2"
|
|
-s|--service myService="\$2"
|
|
-c|--command myCommand="\$2"
|
|
-cn|--configname myConfigName="\$2"
|
|
-cg|--contactgroup myCG="\$2"
|
|
-hg|--hostgroup myHG="\$2"
|
|
-sg|--servicegroup mySG="\$2"
|
|
--state 0, 1, or 2 (or other, I suppose)
|
|
--stype 0, 1 (SOFT or HARD)
|
|
--output myOutput="\$2"
|
|
--ack 0, 1
|
|
-o|--opt cmdOptions="\$2" (endpoint specific options. See --helpopt)
|
|
--api < o*bjects | c*onfig | s*ystem >
|
|
-t|--object < hoststatus | servicestatus | logentries | statehistory | ...
|
|
... comment | downtime | contact | host | service | hostgroup | ...
|
|
... servicegroup | contactgroup | timeperiod | ...
|
|
... hostgroupmembers | servicegroupmembers >
|
|
--test Don't call the API, just show what would happen
|
|
|
|
HELP_EOF
|
|
exit
|
|
}
|
|
|
|
while [ -n "$1" ]; do
|
|
case "$1" in
|
|
--help) print_help;;
|
|
--helpopt) print_helpopt;;
|
|
--keyfile) APIkeyFile="$2"; shift 2;;
|
|
--test) testMode="true"; shift 1;;
|
|
-q|--quick) myQuick="true"; shift 1;;
|
|
-Q) myQuick="true"; cmdOptions+="c,"; shift 1;;
|
|
-j|--jq) moreJQ="$2"; shift 2;;
|
|
-v|--verbose) verbose=$(($verbose + 1)); shift 1;;
|
|
--create) doCreate="true"; shift 1;;
|
|
--key) APIKEY="$2"; shift 2;;
|
|
--url) XI_URL="$2"; shift 2;;
|
|
--save) mySave="$2"; shift 2;;
|
|
--file) myFile="$2"; shift 2;;
|
|
-f|--fields) myFields="$2"; shift 2;;
|
|
-h|--host) myHost="$2"; shift 2;;
|
|
-s|--service) myService="$2"; shift 2;;
|
|
-c|--command) myCommand="$2"; shift 2;;
|
|
-cn|--configname) myConfigName="$2"; shift 2;;
|
|
-cg|--contactgroup) myCG="$2"; shift 2;;
|
|
-hg|--hostgroup) myHG="$2"; shift 2;;
|
|
-sg|--servicegroup) mySG="$2"; shift 2;;
|
|
--state) myState="$2"; shift 2;;
|
|
--stype) myStateType="$2"; shift 2;;
|
|
--output) myOutput="$2"; shift 2;;
|
|
--ack) myAck="$2"; shift 2;;
|
|
-o|--opt) cmdOptions+="$2,"; shift 2;;
|
|
--api) get_myAPI "$2"; shift 2;;
|
|
-t|--object) get_myAPIep "$2"; shift 2;;
|
|
*) shift 1;;
|
|
esac
|
|
done
|
|
|
|
if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then
|
|
while read url key; do
|
|
if [ -z "$XI_URL" ]; then
|
|
XI_URL="$url"
|
|
APIKEY="$key"
|
|
continue
|
|
fi
|
|
if [ "$url" = "$XI_URL" ]; then
|
|
APIKEY="$key"
|
|
fi
|
|
done < "$APIkeyFile"
|
|
fi
|
|
|
|
do_debug() {
|
|
[ "$verbose" -ge "$1" ] && echo "$2" >&2
|
|
}
|
|
|
|
do_api() {
|
|
api_start="$1"
|
|
api_command="$2"
|
|
url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0"
|
|
do_debug 2 "start=$1 command=$2"
|
|
do_debug 1 "Executing: $url"
|
|
$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"
|
|
do_api_post() {
|
|
api_start="$1"
|
|
api_command="$2"
|
|
url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0"
|
|
do_debug 2 "start=$1 command=$2"
|
|
do_debug 1 "Executing: $url"
|
|
$curl -XPOST -k "$url" -d "hostgroup_name=$myHG&alias=$myHG&applyconfig=0"
|
|
}
|
|
|
|
# At this time, all we can create ia a hostgroup
|
|
create_hostgroup() {
|
|
do_debug 1 "about to do an API post call"
|
|
do_api_post config hostgroup
|
|
}
|
|
|
|
do_create() {
|
|
do_debug 1 "about to do a create_command"
|
|
[ -n "$myHG" ] && create_hostgroup
|
|
exit
|
|
}
|
|
|
|
# If we said "--create" then we want to make something
|
|
if [ -n "$doCreate" ]; then
|
|
do_create
|
|
exit
|
|
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 "$testMode" ]; then
|
|
if [ -z "$myFile" ]; then
|
|
tmpJSON=`mktemp`
|
|
do_debug 1 "tmp file is $tmpJSON"
|
|
do_debug 2 "myAPI is $myAPI and myAPIep is $myAPIep"
|
|
do_api "$myAPI" "$myAPIep" > $tmpJSON
|
|
else
|
|
do_debug 1 "myFile=$myFile"
|
|
tmpJSON="$myFile"
|
|
fi
|
|
|
|
# if mySave is not empty, then we're just saving it into the file called $mySave
|
|
if [ -n "$mySave" ]; then
|
|
mv $tmpJSON $mySave
|
|
do_debug 1 "JSON data saved to $mySave"
|
|
exit
|
|
fi
|
|
else
|
|
echo "### TEST MODE - Skipping API call (testMode=${testMode}) ###"
|
|
fi
|
|
|
|
# Helper functions for creating our jqString
|
|
# First, we need to know if our tests should be case-sensitive or not
|
|
jq_check_case() {
|
|
thing="$*"
|
|
if [ -n "$thing" ]; then
|
|
val="| test(\"$thing\""
|
|
[[ "$cmdOptions" =~ "i," ]] && val+="; \"i\""
|
|
echo "$val)"
|
|
else
|
|
echo ""
|
|
fi
|
|
}
|
|
|
|
# Do we need to add host limits to the query?
|
|
jq_add_host() {
|
|
[ -n "$myHost" ] && jqString+="| select(.host_name $(jq_check_case $myHost))"
|
|
}
|
|
jq_add_service() {
|
|
[ -n "$myService" ] && jqString+="| select(.service_description $(jq_check_case $myService))"
|
|
}
|
|
jq_add_hostgroup() {
|
|
[ -n "$myHG" ] && jqString+="| select(.hostgroup_name==\"$myHG\")"
|
|
}
|
|
jq_add_servicegroup() {
|
|
[ -n "$mySG" ] && jqString+="| select(.servicegroup_name==\"$mySG\")"
|
|
}
|
|
jq_add_command() {
|
|
[ -n "$myCommand" ] && jqString+="| select(.check_command $(jq_check_case $myCommand))"
|
|
}
|
|
jq_add_output() {
|
|
[ -n "$myOutput" ] && jqString+="| select(.output $(jq_check_case $myOutput))"
|
|
}
|
|
jq_add_configname() {
|
|
[ -n "$myConfigName" ] && jqString+="| select(.check_command $(jq_check_case $myConfigName))"
|
|
}
|
|
jq_add_contactgroup() {
|
|
[ -n "$myCG" ] && jqString+="| select(.contactgroup_name $(jq_check_case $myCG))"
|
|
}
|
|
jq_add_state() {
|
|
[ -n "$myState" ] && jqString+="| select(.current_state | test(\"$myState\"))"
|
|
}
|
|
jq_add_statetype() {
|
|
[ -n "$myStateType" ] && jqString+="| select(.state_type | test(\"$myStateType\"))"
|
|
}
|
|
jq_add_ack() {
|
|
[ -n "$myAck" ] && jqString+="| select(.problem_has_been_acknowledged | test(\"$myAck\"))"
|
|
}
|
|
jq_get_fields() {
|
|
if [[ "$cmdOptions" =~ "c," ]]; then
|
|
jqString+="| [$myFields] | @csv"
|
|
else
|
|
jqString+="| $myFields"
|
|
fi
|
|
}
|
|
|
|
# Otherwise, let's parse the JSON data here
|
|
# Parse our string
|
|
do_debug 1 "APIinfo=${APIinfo[$myAPI/$myAPIep]}"
|
|
jqString=${APIinfo[$myAPI/$myAPIep]}
|
|
case "$myAPIep" in
|
|
servicestatus)
|
|
jq_add_host
|
|
jq_add_service
|
|
jq_add_hostgroup
|
|
jq_add_servicegroup
|
|
jq_add_command
|
|
jq_add_output
|
|
jq_add_state
|
|
jq_add_statetype
|
|
jq_add_ack
|
|
;;
|
|
hostgroup)
|
|
jq_add_host
|
|
jq_add_hostgroup
|
|
;;
|
|
host)
|
|
jq_add_host
|
|
;;
|
|
service)
|
|
jq_add_host
|
|
jq_add_service
|
|
jq_add_configname
|
|
jq_add_command
|
|
;;
|
|
hostgroupmembers)
|
|
jq_add_host
|
|
jq_add_hostgroup
|
|
[[ $cmdOptions =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
|
|
[[ $cmdOptions =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
|
|
;;
|
|
servicegroup)
|
|
jq_add_host
|
|
jq_add_servicegroup
|
|
;;
|
|
contactgroup)
|
|
jq_add_contactgroup
|
|
;;
|
|
esac
|
|
[ -n "$myQuick" ] && myFields+=".host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged"
|
|
[ -n "$myFields" ] && jq_get_fields
|
|
jqString+="$moreJQ"
|
|
do_debug 1 "jqString=$jqString"
|
|
|
|
if [ -n "$testMode" ]; then
|
|
echo "### TEST MODE - Here is what would have been done (testMode=${testMode}) ###"
|
|
echo "cat <tmpfile> | jq -r \"$jqString\""
|
|
else
|
|
cat $tmpJSON | jq -r "$jqString"
|
|
[ -z "$myFile" ] && rm $tmpJSON
|
|
fi
|