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.

338 lines
12 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>"
# myDir (for "Directives") are now too big for individual variables. We'll read them into an associative array instead
declare -A myDir
# 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[Options]=""
verbose="0"
tmpJSON=""
tmpQuick=""
# 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"]=".contact[]"
APIinfo["config/contactgroup"]=".[]"
APIinfo["config/timeperiod"]=""
APIinfo["objects/hoststatus"]=".hoststatus[]"
APIinfo["objects/servicestatus"]=".servicestatus[]"
APIinfo["objects/logentries"]=""
APIinfo["objects/statehistory"]=""
APIinfo["objects/comment"]=""
APIinfo["objects/downtime"]=""
APIinfo["objects/contact"]=".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"]=".servicegroup[]"
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)
servicegroupmembers:
h Only show hosts (not the complete JSON data)
s Only show service names (not the complete JSON data)
o Only show service_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=<value>
-cca current_check_attempt=<value>
-cnn current_notification_number=<n>
-cg|--contactgroup contact_groups=<value>
-cn|--configname config_name=<value>
... comment | downtime | contact | host | service | hostgroup | ...
--create doCreate="true"
-D <Nagios Config Directive>=<value>
-f|--fields JQ-valid list of fields to show=<value>
--file load JSON from=<value>
--helpopt Show help for command options
--help This text
-hg|--hostgroup hostgroup=<value>
-h|--host host_name=<value>
... hostgroupmembers | servicegroupmembers >
-j|--jq additional valid JQ=<value>
--key APIKEY=<value>
--keyfile APIkeyFile=<value>
-o|--opt cmdOptions=<value> (endpoint specific options. See --helpopt)
--output output text=<value>
-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
-qq Same as --quick but add -o c but also print the first line of the CSV output (fields)
--save save JSON to=<value>
-sg|--servicegroup servicegroup=<value>
-s|--service service_description=<value>
--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=<value>
-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) myDir[problem_has_been_acknowledged]="$2"; shift 2;;
--ace) myDir[active_checks_enabled]="$2"; shift 2;;
--api) get_myAPI "$2"; shift 2;;
-c|--command) myDir[check_command]="$2"; shift 2;;
-cca) myDir[current_check_attempt]="$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;;
--create) myOptions[Create]="true"; shift 1;;
-D) myDir[$2]="$3"; shift 3;;
-f|--fields) myOptions[Fields]="$2"; shift 2;;
--file) myOptions[File]="$2"; shift 2;;
--helpopt) print_helpopt;;
--help) print_help;;
-hg|--hostgroup) myDir[hostgroup_name]="$2"; shift 2;;
-h|--host) myDir[host_name]="$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) myDir[Output]="$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;;
--save) myOptions[Save]="$2"; shift 2;;
-sg|--servicegroup) myDir[servicegroup_name]="$2"; shift 2;;
-s|--service) myDir[Service]="$2"; shift 2;;
--state) myDir[State]="$2"; shift 2;;
--stype) myDir[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=${myDir[HG]}&alias=${myDir[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 "${myDir[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() {
[[ "${myOptions[Options]}" =~ "c," ]] && jqString+="| [${myOptions[Fields]}] | @csv" || jqString+="| ${myOptions[Fields]}"
}
# 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 "${!myDir[@]}"; do
[ -n "${myDir[$thing]}" ] && jqString+="| select(.$thing $(jq_check_case ${myDir[$thing]}))"
done
case "${myOptions[APIep]}" in
hostgroupmembers)
[[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
[[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
;;
servicegroupmembers)
[[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
[[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .service_object_id"
[[ ${myOptions[Options]} =~ "s," ]] && jqString+="| .members[] | .[] | .service_description"
tmpQuick=".service_object_id,.host_name,.service_description"
;;
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";;
servicestatus) tmpQuick=".service_description,.host_name,.current_state,.state_type,.last_check,.current_check_attempt,.normal_check_interval,.retry_check_interval,.max_check_attempts,.check_command";;
contact) tmpQuick=".contact_name,.email_address,.host_notifications_enabled,.service_notifications_enabled,.is_active";;
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"
fi
[[ ${myOptions[Options]} =~ "C," ]] && echo "${myOptions[Fields]}"
do_debug 1 "myOptions[Fields]=${myOptions[Fields]}"
[ -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