Compare commits
5 Commits
cd69e35901
...
35d722541b
| Author | SHA1 | Date |
|---|---|---|
|
|
35d722541b | 2 months ago |
|
|
373fe6e812 | 4 months ago |
|
|
9885d886d5 | 4 months ago |
|
|
ecceee2572 | 7 months ago |
|
|
60080dd75a | 10 months ago |
@ -0,0 +1,178 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
declare -A myVar
|
||||||
|
myVar[Name]="servicedesc"
|
||||||
|
myVar[myDelim]="[ :]"
|
||||||
|
|
||||||
|
declare -A myOptions
|
||||||
|
|
||||||
|
do_help() {
|
||||||
|
cat << HELP_EOF
|
||||||
|
This utility is designed to be called by the ARGx parameters of a Nagios configuration as one of the following:
|
||||||
|
\$\$(\$USER1\$/env [-n <var>] [-T <var2>] [-t <default>] [-f|-F <field>] [-d <delim>] [-g|-s <val>] [-u|-l] [-v]
|
||||||
|
\$\$(\$USER1\$/env [-NT] [-NP] [-HT|-ST|-CT suffix]
|
||||||
|
-H|-S|-C will cause var to be of type NAGIOS__<HOST|SERVICE|CONTACT><var> and then continue with normal processing
|
||||||
|
Use --pre <prefix> and --post <postfix> to add prefix/postfix to the output string after all other processing has completed, but only if not blank.
|
||||||
|
|
||||||
|
Where:
|
||||||
|
var the name of a case-insensitive NAGIOS_variable (defaults to "servicedesc")
|
||||||
|
default a default value if \$NAGIOS_var\$ is blank (case sensitive)
|
||||||
|
field an awk-compatible field number (potentially to use with <delim>) (see below)
|
||||||
|
delim an awk-compatible field delimiter (defaults to space and colon)
|
||||||
|
-T if var returns blank, try vars as NAGIOS___HOSTvar2 before applying -t as a default
|
||||||
|
Example: svc="HTTP: 8080" and $0 -T HTTPPORT -t 80 -f -1 --num will return 8080 but "svc: HTTP" will return NAGIOS__HTTPPORT if exists, or 80 if not
|
||||||
|
-u Convert the output to uppercase
|
||||||
|
-l Convert the output to lowercase
|
||||||
|
-p Convert / to | (mainly for disk names in NCPA checks)
|
||||||
|
suffix Take var result and use the it to get NAGIOS__[HOST|SERVICE|CONTACT]<result><suffix>
|
||||||
|
-v Be verbose (only for debugging)
|
||||||
|
-g Returns "val" if "val" appears anywhere in the value of var (basically, a grep) and then continues processing as normal
|
||||||
|
-s The result must match this value or else it will return blank
|
||||||
|
--num The result will have all non-numbers stripped from it
|
||||||
|
-NP Returns NAGIOS__HOSTNCPAPORT (implied default of 5693)
|
||||||
|
-NT Look for NAGIOS__HOSTNCPA<suffix> (defaults to TOKEN)
|
||||||
|
Can do double lookup if -HT is specified directly: -HT TOKEN will find NAGIOS__HOSTNCPATOKEN=Level1 which then finds NAGIOS__HOSTLEVEL1TOKEN=Secret
|
||||||
|
Or -HT PASS will find NAGIOS__HOSTNCPAPASS=Level2 which will then find NAGIOS__HOSTLEVEL2PASS=SuperSecret
|
||||||
|
|
||||||
|
The value will always be trimmed of leading and/or trailing spaces/tabs.
|
||||||
|
|
||||||
|
If -f is used, then the result will be as if the request were as follows, unless the field starts with a negative, in which case it will read from the last word forward:
|
||||||
|
echo \$NAGIOS_var\$ | awk -F <delim> '{print $<field>}'
|
||||||
|
If -F is used, -d is forced to " " and the field number reflects is the first, second, or third of the last three fields
|
||||||
|
Ex: Disk check on / 80 90 With -F 1=/, -F 2=80, -F 3=90
|
||||||
|
|
||||||
|
Examples (service_description is assumed to be "MySQL Check: myDatabase performance"):
|
||||||
|
$0 Return: "MySQL Check: myDatabase performance"
|
||||||
|
$0 -n hostName Return: "localhost" (or whatever the name of the host is that ran the check)
|
||||||
|
$0 -n SERVICESTATE -t 0 Return: current state of the service (0, 1, 2) or 0 if the state is not defined
|
||||||
|
$0 -n servicedesc -f 2 Return: "Check" (watch out for defaults!)
|
||||||
|
$0 -n servicedesc -f 4 Return: "myDatabase" (watch out for defaults!)
|
||||||
|
$0 -n servicedesc -d : -f 2 Return: "myDatabase performance"
|
||||||
|
$0 -n servicedesc -d : -f -2 Return: "MySQL Check" (2nd field from right, split by :)*
|
||||||
|
$0 -n servicedesc -f -2 -HT pass Return: the value of \$NAGIOS__HOSTNAGIOSXIPASS\$
|
||||||
|
|
||||||
|
see https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/macrolist.html for possible macro names.
|
||||||
|
*0 for this field will print the entire line
|
||||||
|
HELP_EOF
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
|
||||||
|
while [ -n "$1" ]; do
|
||||||
|
case "$1" in
|
||||||
|
-h|--help) do_help;;
|
||||||
|
-n) myVar[Name]="$2"; shift 2;;
|
||||||
|
-T) myVar[HostVar]="$2"; shift 2;;
|
||||||
|
-t) myVar[Default]="$2"; shift 2;;
|
||||||
|
-d) myVar[myDelim]="$2"; shift 2;;
|
||||||
|
-f) myVar[myField]="$2"; shift 2;;
|
||||||
|
-F) myVar[myDelim]=" "; myVar[myField]=$(($2-4)); shift 2;;
|
||||||
|
-HT) myVar[myHT]="$2"; shift 2;;
|
||||||
|
-ST) myVar[myST]="$2"; shift 2;;
|
||||||
|
-CT) myVar[myCT]="$2"; shift 2;;
|
||||||
|
-g) myVar[myGrep]="$2"; shift 2;;
|
||||||
|
-s) myVar[myString]="$2"; shift 2;;
|
||||||
|
--pre) myVar[myPrefix]="$2"; shift 2;;
|
||||||
|
--post) myVar[myPostfix]="$2"; shift 2;;
|
||||||
|
--num) myOptions[doNumber]="true"; shift 1;;
|
||||||
|
-NT) myOptions[doNCPA]="true"; shift 1;;
|
||||||
|
-NP) myVar[Name]="NAGIOS__HOSTNCPAPORT"; myVar[Default]="5693"; shift 1;;
|
||||||
|
-H) myOptions[doHost]="true"; shift 1;;
|
||||||
|
-S) myOptions[doService]="true"; shift 1;;
|
||||||
|
-C) myOptions[doContact]="true"; shift 1;;
|
||||||
|
-p) myOptions[doPipe]="true"; shift 1;;
|
||||||
|
-u) myOptions[doUpper]="true"; shift 1;;
|
||||||
|
-l) myOptions[doLower]="true"; shift 1;;
|
||||||
|
-v) myOptions[doVerbose]="true"; shift 1;;
|
||||||
|
*) shift 1;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
do_debug() {
|
||||||
|
[ -n "${myOptions[doVerbose]}" ] && echo "### DEBUG: $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
do_lookup() {
|
||||||
|
var="$1"; def="$2"
|
||||||
|
val=${!var:-$def}
|
||||||
|
echo "$val"
|
||||||
|
}
|
||||||
|
|
||||||
|
do_output() {
|
||||||
|
val="$1"
|
||||||
|
[ -z "$val" ] && return
|
||||||
|
echo "${myVar[myPrefix]}${val}${myVar[myPostfix]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ -n "${myOptions[doNCPA]}" ]; then
|
||||||
|
do_debug "NCPA mode active. myVar[myHT]}=${myVar[myHT]}"
|
||||||
|
myVar[Name]=`echo "NAGIOS__HOSTNCPA${myVar[myHT]:-token}" | tr "a-z" "A-Z"`
|
||||||
|
else
|
||||||
|
do_debug "NCPA mode NOT active."
|
||||||
|
do_debug "myOptions[doHost]=${myOptions[doHost]} myOptions[doService]=${myOptions[doService]} myOptions[doContact]=${myOptions[doContact]}"
|
||||||
|
[ -n "${myOptions[doHost]}" ] && myVar[Name]="_HOST${myVar[Name]}"
|
||||||
|
[ -n "${myOptions[doService]}" ] && myVar[Name]="_SERVICE${myVar[Name]}"
|
||||||
|
[ -n "${myOptions[doContact]}" ] && myVar[Name]="_CONTACT${myVar[Name]}"
|
||||||
|
myVar[Name]=`echo "NAGIOS_${myVar[Name]}" | tr "a-z" "A-Z"`
|
||||||
|
fi
|
||||||
|
|
||||||
|
do_debug "1: Looking for ${myVar[Name]}..."
|
||||||
|
myVar[Value]=`do_lookup "${myVar[Name]}"`
|
||||||
|
do_debug "2: Got myVar[Value]=${myVar[Value]}"
|
||||||
|
|
||||||
|
# if -g was specified, then only return the portion of the result that matches
|
||||||
|
if [ -n "${myVar[myGrep]}" ]; then
|
||||||
|
do_debug "1: grepping ${myVar[Name]} for ${myVar[myGrep]}"
|
||||||
|
myVar[Value]=`echo "${myVar[Value]}" | egrep -o -- "${myVar[myGrep]}"`
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "${myVar[myField]}" ]; then
|
||||||
|
do_debug "myVar[myField]=${myVar[myField]}"
|
||||||
|
if [ "${myVar[myField]}" -lt 0 ]; then
|
||||||
|
myVar[Value]=`echo "${myVar[Value]}" | rev | awk -F "${myVar[myDelim]}" -v f="${myVar[myField]:1}" '{print $f}' | rev`
|
||||||
|
else
|
||||||
|
myVar[Value]=`echo "${myVar[Value]}" | awk -F "${myVar[myDelim]}" -v f="${myVar[myField]}" '{print $(f)}'`
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
myVar[Value]=`echo ${myVar[Value]} | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//"`
|
||||||
|
do_debug "3: After processing myVar[Value]=${myVar[Value]}"
|
||||||
|
|
||||||
|
[ -n "${myOptions[doUpper]}" ] && myVar[Value]=`echo "${myVar[Value]}" | tr "a-z" "A-Z"`
|
||||||
|
[ -n "${myOptions[doLower]}" ] && myVar[Value]=`echo "${myVar[Value]}" | tr "A-Z" "a-z"`
|
||||||
|
|
||||||
|
# Check for myHT
|
||||||
|
if [ -n "${myVar[myHT]}" ]; then
|
||||||
|
myVar[myHT]=`echo "NAGIOS__HOST${myVar[Value]}${myVar[myHT]}" | tr "a-z" "A-Z"`
|
||||||
|
do_debug "4: myVar[myHT]=${myVar[myHT]}"
|
||||||
|
myVar[Value]=`do_lookup ${myVar[myHT]}`
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check for myST
|
||||||
|
if [ -n "${myVar[myST]}" ]; then
|
||||||
|
myVar[myST]=`echo "NAGIOS__SERVICE${myVar[Value]}${myVar[myST]}" | tr "a-z" "A-Z"`
|
||||||
|
do_debug "4: myVar[myST]}=${myVar[myST]}"
|
||||||
|
myVar[Value]=`do_lookup ${myVar[myST]}`
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check for myCT
|
||||||
|
if [ -n "${myVar[myCT]}" ]; then
|
||||||
|
myVar[myCT]=`echo "NAGIOS__CONTACT${myVar[Value]}${myVar[myCT]}" | tr "a-z" "A-Z"`
|
||||||
|
do_debug "4: myVar[myCT]=${myVar[myCT]}"
|
||||||
|
myVar[Value]=`do_lookup "${myVar[myCT]}"`
|
||||||
|
fi
|
||||||
|
|
||||||
|
[ -n "${myVar[myString]}" -a "${myVar[myString]}" != "${myVar[Value]}" ] && myVar[Value]=""
|
||||||
|
[ -n "${myOptions[doNumber]}" ] && myVar[Value]=`echo ${myVar[Value]} | tr -d -c "0-9"`
|
||||||
|
[ -n "${myOptions[doPipe]}" ] && myVar[Value]=`echo ${myVar[Value]} | tr "/" "|"`
|
||||||
|
|
||||||
|
do_debug "About to check for -T options because myVar[Value]=${myVar[Value]}"
|
||||||
|
if [ -z "${myVar[Value]}" ]; then
|
||||||
|
do_debug "myVar[Value] is blank, checking for ${myVar[HostVar]}"
|
||||||
|
myVar[Value]=`do_lookup "NAGIOS__HOST${myVar[HostVar]}"`
|
||||||
|
do_debug "received ${myVar[Value]}"
|
||||||
|
[ -z "${myVar[Value]}" ] && myVar[Value]="${myVar[Default]}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Print the result
|
||||||
|
do_output "${myVar[Value]}"
|
||||||
|
|
||||||
|
exit 0
|
||||||
@ -0,0 +1,50 @@
|
|||||||
|
ENVtest1="MySQL Check: myDatabase performance"
|
||||||
|
ENVtest2="Disk Usage on / 80 90"
|
||||||
|
ENVtest3="One Two: Three - Four 5 6"
|
||||||
|
|
||||||
|
test: test-env
|
||||||
|
|
||||||
|
test-env: ENV
|
||||||
|
@echo "### This should give $(ENVtest1)"
|
||||||
|
@NAGIOS_TEST=$(ENVtest1) ./ENV -n test
|
||||||
|
@echo ""
|
||||||
|
@echo "### This should give / then 80 then 90 then |"
|
||||||
|
NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 1
|
||||||
|
NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 2
|
||||||
|
NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 3
|
||||||
|
NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 1 -p
|
||||||
|
@echo ""
|
||||||
|
@echo "### This should give Check, then myDatabase, then myDatabase performance, then MySQL Check"
|
||||||
|
NAGIOS_TEST=$(ENVtest1) ./ENV -n test -f 2
|
||||||
|
NAGIOS_TEST=$(ENVtest1) ./ENV -n test -f 4
|
||||||
|
NAGIOS_TEST=$(ENVtest1) ./ENV -n test -d : -f 2
|
||||||
|
NAGIOS_TEST=$(ENVtest1) ./ENV -n test -d : -f -2
|
||||||
|
@echo ""
|
||||||
|
@echo "### This should give Password"
|
||||||
|
NAGIOS_TEST=$(ENVtest1) NAGIOS__HOSTMYDATABASEPASS=Password ./ENV -n test -f -2 -HT pass
|
||||||
|
@echo ""
|
||||||
|
@echo "### Test string is: $(ENVtest3)"
|
||||||
|
@echo "### The first test should be the full test line, then blank line, then number 5, then FourToken twice"
|
||||||
|
NAGIOS_TEST=$(ENVtest3) ./ENV -n TeSt
|
||||||
|
NAGIOS_TEST=$(ENVtest3) ./ENV -n TEST -F 2 -S 5
|
||||||
|
NAGIOS_TEST=$(ENVtest3) ./ENV -n test -F 2 --num
|
||||||
|
NAGIOS_TEST=$(ENVtest3) NAGIOS__HOSTFOURTOKEN="FourToken" ./ENV -H -n fourtoken
|
||||||
|
NAGIOS_TEST=$(ENVtest3) NAGIOS__HOSTFOURTOKEN="FourToken" ./ENV -n test -F 1 -HT TOKEN
|
||||||
|
@echo ""
|
||||||
|
@echo "### This should return Level2"
|
||||||
|
NAGIOS__HOSTNCPAPASS="Level2" ./ENV set.txt -n asdf -T NCPAPASS
|
||||||
|
@echo ""
|
||||||
|
@echo "### This should return Balloons, 91, Red"
|
||||||
|
NAGIOS_TEST="HTTP -S 8099" ./ENV set.txt -n TEST -g "8.*" -d 0 -f 2 --pre "Balloons, " --post ", Red"
|
||||||
|
|
||||||
|
test-napi:
|
||||||
|
echo ""; echo "Testing --status"
|
||||||
|
./napi --ace 1 --status | csvtool readable - | head
|
||||||
|
echo ""; echo "Testing --bstatus"
|
||||||
|
./napi --ace 1 --bstatus | csvtool readable - | head
|
||||||
|
echo ""; echo "Testing --hstatus"
|
||||||
|
./napi --ace 1 --hstatus | csvtool readable - | head
|
||||||
|
echo ""; echo "Testing --bhstatus"
|
||||||
|
./napi --ace 1 --bhstatus | csvtool readable - | head
|
||||||
|
echo ""; echo "Testing localhost"
|
||||||
|
./napi -h localhost -s Active -q
|
||||||
@ -0,0 +1,273 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
APIkeyFile=".nagiosapikey"
|
||||||
|
[ ! -r "$APIkeyFile" ] && APIkeyFile="${HOME}/.nagiosapikey"
|
||||||
|
[ ! -r "$APIkeyFile" ] && APIkeyFile=""
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
# 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"
|
||||||
|
verbose="0"
|
||||||
|
tmpJSON=""
|
||||||
|
tmpQuick=""
|
||||||
|
tmpQuicker=""
|
||||||
|
|
||||||
|
# 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["objects/host"]=".host[]"
|
||||||
|
APIinfo["objects/hoststatus"]=".hoststatus[]"
|
||||||
|
APIinfo["objects/servicestatus"]=".servicestatus[]"
|
||||||
|
APIinfo["system/user"]=".users[]"
|
||||||
|
|
||||||
|
do_debug() {
|
||||||
|
[ "$verbose" -ge "$1" ] && echo "$2" >&2
|
||||||
|
}
|
||||||
|
|
||||||
|
get_myAPI() {
|
||||||
|
case "$1" in
|
||||||
|
o*) myOptions[API]="objects";;
|
||||||
|
*) myOptions[API]="";;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
get_myAPIep() {
|
||||||
|
case "$1" in
|
||||||
|
h|host) myOptions[APIep]="host";;
|
||||||
|
hs|hoststatus) myOptions[APIep]="hoststatus";;
|
||||||
|
s|service) myOptions[APIep]="service";;
|
||||||
|
ss|servicestatus) myOptions[APIep]="servicestatus";;
|
||||||
|
u|user) myOptions[APIep]="user";;
|
||||||
|
*) myOptions[APIep]="";;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
print_help() {
|
||||||
|
cat << HELP_EOF
|
||||||
|
--api < o*bjects | c*onfig | s*ystem >
|
||||||
|
-t|--object < hoststatus | servicestatus | logentries | statehistory | uo | ...
|
||||||
|
--url XI_URL=<value>
|
||||||
|
|
||||||
|
--ace active_checks_enabled=<0,1>
|
||||||
|
-c|--command check_command=<value>
|
||||||
|
-f|--fields JQ-valid list of fields to show=<value>
|
||||||
|
--file load JSON from=<value>
|
||||||
|
--help This text
|
||||||
|
-h|--host host_name=<value>
|
||||||
|
-j|--jq additional valid JQ=<value>
|
||||||
|
--key APIKEY=<value>
|
||||||
|
--keyfile APIkeyFile=<value>
|
||||||
|
--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>
|
||||||
|
-s|--service service_description=<value>
|
||||||
|
--users Lists XI users
|
||||||
|
--usersa Lists XI users (advanced)
|
||||||
|
--status Quick select for showing similar things that you would see on main servicestatus page in the GUI
|
||||||
|
--bstatus Same as --status but only show things that are not in an OK state
|
||||||
|
--hstatus Quick select for showing similar things that you would see on a main hoststatus page in the GUI
|
||||||
|
--bhstatus Same as --hstatus but only show things that are not in an OK state
|
||||||
|
--state 0, 1, or 2 (or other, I suppose)
|
||||||
|
--stype 0, 1 (SOFT or HARD)
|
||||||
|
-v|--verbose verbose=\$((\$verbose + 1))
|
||||||
|
HELP_EOF
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
|
||||||
|
# get_opts
|
||||||
|
while [ -n "$1" ]; do
|
||||||
|
case "$1" in
|
||||||
|
--help) print_help;;
|
||||||
|
--key) APIKEY="$2"; shift 2;;
|
||||||
|
--keyfile) APIkeyFile="$2"; shift 2;;
|
||||||
|
--url) XI_URL="$2"; shift 2;;
|
||||||
|
-v|--verbose) verbose=$(($verbose + 1)); shift 1;;
|
||||||
|
--api) get_myAPI "$2"; shift 2;;
|
||||||
|
-t|--object) get_myAPIep "$2"; 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;;
|
||||||
|
-D) myDir[$2]="$3"; shift 3;;
|
||||||
|
-h|--host) myDir[host_name]="$2"; shift 2;;
|
||||||
|
--output) myDir[output]="$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;;
|
||||||
|
--ctrace) myOptions[CommandTrace]="true"; shift 1;;
|
||||||
|
--delete) myOptions[Delete]="true"; 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;;
|
||||||
|
--save) myOptions[Save]="$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;;
|
||||||
|
*) 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"
|
||||||
|
|
||||||
|
# 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[Options]=""
|
||||||
|
myOptions[Quick]="true"
|
||||||
|
myOptions[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]"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
if [ -n "${myOptions[HStatus]}" ]; then
|
||||||
|
myOptions[API]="objects"
|
||||||
|
myOptions[APIep]="hoststatus"
|
||||||
|
myOptions[Options]=""
|
||||||
|
myOptions[Quick]="true"
|
||||||
|
myOptions[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]"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If we're listing users, then set things accordingly
|
||||||
|
if [ -n "${myOptions[Users]}" ]; then
|
||||||
|
myOptions[API]="system"
|
||||||
|
myOptions[APIep]="user"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get our API key
|
||||||
|
if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then
|
||||||
|
do_debug 1 "Looking for url=$url in $APIkeyFile"
|
||||||
|
while read url key; do
|
||||||
|
if [ -z "$XI_URL" ]; then
|
||||||
|
XI_URL="$url"
|
||||||
|
APIKEY="$key"
|
||||||
|
do_debug 1 "Found url=$url key=$key"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
do_debug 2 "-> Looking for url=$url XI_URL=$XI_URL"
|
||||||
|
if [[ "$url" =~ "$XI_URL" ]]; then
|
||||||
|
XI_URL="$url"
|
||||||
|
APIKEY="$key"
|
||||||
|
fi
|
||||||
|
done < "$APIkeyFile"
|
||||||
|
fi
|
||||||
|
do_debug 1 "Final URL=$XI_URL and key=$APIKEY"
|
||||||
|
[ -z "$XI_URL" -o -z "$APIKEY" ] && echo "Empty URL or Key." && exit
|
||||||
|
|
||||||
|
do_api() {
|
||||||
|
api_start="$1"
|
||||||
|
api_command="$2"
|
||||||
|
url="${XI_URL}/api/v1/${api_start}/${api_command}?pretty=0&apikey=${APIKEY}"
|
||||||
|
[ -n "${myOptions[OrderBy]}" ] && url+="&orderby=${myOptions[OrderBy]}"
|
||||||
|
[ "${myOptions[Users]}" == "1" ] && url+="&advanced=1"
|
||||||
|
do_debug 2 "start=$1 command=$2"
|
||||||
|
$curl -XGET -k "$url"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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 [ -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
|
||||||
|
|
||||||
|
# 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[${myOptions[API]}/${myOptions[APIep]}]}"
|
||||||
|
jqString=${APIinfo[${myOptions[API]}/${myOptions[APIep]}]}
|
||||||
|
do_debug 2 " Before: jqString=$jqString"
|
||||||
|
for thing in "${!myDir[@]}"; do
|
||||||
|
[ -n "${myDir[$thing]}" ] && jqString+="| select(.$thing $(jq_check_case ${myDir[$thing]}))"
|
||||||
|
do_debug 2 " During: jqString=$jqString"
|
||||||
|
done
|
||||||
|
do_debug 2 " After: jqString=$jqString"
|
||||||
|
|
||||||
|
# endpoint specific things
|
||||||
|
case "${myOptions[API]}/${myOptions[APIep]}" in
|
||||||
|
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";
|
||||||
|
tmpQuicker="Host,Address,State,Type,Last Check,Attempt,Normal,Retry,Max,Command";;
|
||||||
|
objects/servicestatus) tmpQuick=".service_description,.host_name,.current_state,.state_type,.last_check,.current_check_attempt,.normal_check_interval,.retry_check_interval,.max_check_attempts,.output";
|
||||||
|
tmpQuicker="Service,Host,State,Type,Last Check,Attempt,Normal,Retry,Max,Output";;
|
||||||
|
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]}"
|
||||||
|
[[ ${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]}"
|
||||||
|
do_debug 1 "jqString=$jqString"
|
||||||
|
|
||||||
|
if [ -n "${myOptions[Raw]}" ]; then
|
||||||
|
do_debug 1 "*** RAW specified *** Overriding all other options"
|
||||||
|
cat $tmpJSON
|
||||||
|
else
|
||||||
|
cat $tmpJSON | jq -r "$jqString"
|
||||||
|
fi
|
||||||
|
[ -z "${myOptions[File]}" ] && rm $tmpJSON
|
||||||
|
|
||||||
|
exit
|
||||||
Loading…
Reference in New Issue