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.

333 lines
11 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>"
# myThings are now too big for individual variables. We'll read them into an associative array instead
declare -A myThing
# We need a separate thing for myOptions
declare -A myOptions
myOptions[API]="objects"
myOptions[APIep]="servicestatus"
myOptions[TestMode]=""
myOptions[Create]=""
myOptions[Options]=""
verbose="0"
tmpJSON=""
# 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*) ${myOptions[API]}="objects";;
c*) ${myOptions[API]}="config";;
s*) ${myOptions[API]}="system";;
*) ${myOptions[API]}="";;
esac
}
get_myAPIep() {
case "$1" in
co|comment) myOptions[APIep]="comment";;
cg|contactgroup) myOptions[APIep]="contactgroup";;
c|contact) myOptions[APIep]="contact";;
dt|downtime) myOptions[APIep]="downtime";;
hgm|hostgroupmembers) myOptions[API]="objects"; myOptions[APIep]="hostgroupmembers";;
hg|hostgroup) myOptions[APIep]="hostgroup";;
h|host) myOptions[APIep]="host";;
hs|hoststatus) myOptions[APIep]="hoststatus";;
le|logentries) myOptions[APIep]="logentries";;
sgm|servicegroupmembers) myOptions[API]="objects"; myOptions[APIep]="servicegroupmembers";;
sg|servicegroup) myOptions[APIep]="servicegroup";;
s|service) myOptions[APIep]="service";;
ss|servicestatus) myOptions[APIep]="servicestatus";;
sh|statehistory) myOptions[APIep]="statehistory";;
tp|timeperiod) myOptions[APIep]="timeperiod";;
*) myOptions[APIep]="";;
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:
all:
i Make all string comparisons case-insentitive
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
--ack problem_has_been_acknowledged=<0,1>
--ace active_checks_enabled=<0,1>
--api < o*bjects | c*onfig | s*ystem >
-c|--command check_command="\$2"
-cca current_check_attempt="\$2"
-cnn current_notification_number=<n>
-cg|--contactgroup contact_groups="\$2"
-cn|--configname config_name="\$2"
... comment | downtime | contact | host | service | hostgroup | ...
--create doCreate="true"
-f|--fields JQ-valid list of fields to show="\$2"
--file load JSON from="\$2"
--helpopt Show help for command options
--help This text
-hg|--hostgroup hostgroup="\$2"
-h|--host host_name="\$2"
... hostgroupmembers | servicegroupmembers >
-j|--jq additional valid JQ="\$2"
--key APIKEY="\$2"
--keyfile APIkeyFile="\$2"
-o|--opt cmdOptions="\$2" (endpoint specific options. See --helpopt)
--output output text="\$2"
-q|--quick Sets -f to .host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged (assumes servicestatus)
-Q Same as --quick but add -o c
--save save JSON to="\$2"
... servicegroup | contactgroup | timeperiod | ...
-sg|--servicegroup servicegroup="\$2"
-s|--service service_description="\$2"
--state 0, 1, or 2 (or other, I suppose)
--stype 0, 1 (SOFT or HARD)
--test Don't call the API, just show what would happen
-t|--object < hoststatus | servicestatus | logentries | statehistory | ...
--url XI_URL="\$2"
-v|--verbose verbose=\$((\$verbose + 1))
[...] [...]=<...> NOTE: This will take anything listed as a Nagios configuration directive and search for it
HELP_EOF
exit
}
while [ -n "$1" ]; do
case "$1" in
--ack) myThing[problem_has_been_acknowledged]="$2"; shift 2;;
--ace) myThing[active_checks_enabled]="$2"; shift 2;;
--api) get_myAPI "$2"; shift 2;;
-c|--command) myThing[check_command]="$2"; shift 2;;
-cca) myThing[current_check_attempt]="$2"; shift 2;;
-cnn) myThing[current_notification_number]="$2"; shift 2;;
-cg|--contactgroup) myThing[contact_groups]="$2"; shift 2;;
-cn|--configname) myThing[config_name]="$2"; shift 2;;
--create) myOptions[Create]="true"; shift 1;;
-f|--fields) myOptions[Fields]="$2"; shift 2;;
--file) myOptions[File]="$2"; shift 2;;
--helpopt) print_helpopt;;
--help) print_help;;
-hg|--hostgroup) myThing[hostgroup_name]="$2"; shift 2;;
-h|--host) myThing[Host]="$2"; shift 2;;
-j|--jq) myOptions[MoreJQ]="$2"; shift 2;;
--key) APIKEY="$2"; shift 2;;
--keyfile) APIkeyFile="$2"; shift 2;;
-o|--opt) myOptions[Options]+="$2,"; shift 2;;
--output) myThing[Output]="$2"; shift 2;;
-Q) myOptions[Quick]="true"; myOptions[Options]+="c,"; shift 1;;
-q|--quick) myOptions[Quick]="true"; shift 1;;
--save) myOptions[Save]="$2"; shift 2;;
-sg|--servicegroup) myThing[SG]="$2"; shift 2;;
-s|--service) myThing[Service]="$2"; shift 2;;
--state) myThing[State]="$2"; shift 2;;
--stype) myThing[StateType]="$2"; shift 2;;
--test) myOptions[TestMode]="true"; shift 1;;
-t|--object) get_myAPIep "$2"; shift 2;;
--url) XI_URL="$2"; shift 2;;
-v|--verbose) verbose=$(($verbose + 1)); shift 1;;
*) 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=${myThing[HG]}&alias=${myThing[HG]}&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 "${myThing[HG]}" ] && create_hostgroup
exit
}
# If we said "--create" then we want to make something
if [ -n "${myOptions[Create]}" ]; 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 "${myOptions[TestMode]}" ]; then
if [ -z "${myOptions[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
else
do_debug 1 "myFile=${myOptions[File]}"
tmpJSON="${myOptions[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]}"
exit
fi
else
echo "### TEST MODE - Skipping API call (testMode=${myOptions[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="$*"
do_debug 1 "### JQ_CHECK_CASE looking for thing=$thing"
if [ -n "$thing" ]; then
do_debug 2 "### in JQ_CHECK_CASE cmdOptions=${myOptions[Options]}"
val="| test(\"$thing\""
[[ "${myOptions[Options]}" =~ "i," ]] && val+="; \"i\""
echo "$val)"
else
echo ""
fi
}
# Create the jQuery search string
jq_get_fields() {
if [[ "$myOptions[Options]" =~ "c," ]]; then
jqString+="| [${myOptions[Fields]}] | @csv"
else
jqString+="| ${myOptions[Fields]}"
fi
}
# Otherwise, let's parse the JSON data here
# Parse our string
do_debug 1 "APIinfo=${APIinfo[${myOptiosn[API]}/${myOptions[APIep]}]}"
jqString=${APIinfo[${myOptions[API]}/${myOptions[APIep]}]}
for thing in "${!myThing[@]}"; do
[ -n "${myThing[$thing]}" ] && jqString+="| select(.$thing $(jq_check_case ${myThing[$thing]}))"
done
case "${myOptions[APIep]}" in
servicestatus)
;;
hostgroup)
;;
host)
;;
service)
;;
hostgroupmembers)
[[ $myOptions[Options] =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
[[ $myOptions[Options] =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
;;
servicegroup)
;;
contactgroup)
;;
esac
if [ -n "${myOptions[Quick]}" -a -n "${myOptions[Fields]}" ]; then
myOptions[Fields]=".host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged,${myOptions[Fields]}"
elif [ -n "${myOptions[Quick]}" ]; then
myOptions[Fields]=".host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged"
fi
[ -n "${myOptions[Fields]}" ] && jq_get_fields
jqString+="${myOptions[MoreJQ]}"
do_debug 1 "jqString=$jqString"
if [ -n "${myOptions[TestMode]}" ]; then
echo "### TEST MODE - Here is what would have been done (testMode=${myOptions[TestMode]}) ###"
echo "cat <tmpfile> | jq -r \"$jqString\""
else
cat $tmpJSON | jq -r "$jqString"
[ -z "${myOptions[File]}" ] && rm $tmpJSON
fi