Compare commits

..

No commits in common. 'd178ceaa79598d9a1a25cbf8b4ee1b1968fd1eea' and '77c773b0dd65e22aa2a37e486516fcc688de12b4' have entirely different histories.

202
ENV

@ -1,58 +1,54 @@
#!/bin/bash #!/bin/bash
[ "$1" = "set.txt" ] && . set.txt 2> /dev/null varName="servicedesc"
varDefault=""
declare -A myVar myDelim="[ :-]"
myVar[Name]="servicedesc" myField=""
myVar[myDelim]="[ :]" myHost=""
myService=""
declare -A myOptions myContact=""
doUpper=""
doVerbose=""
doNCPA=""
do_help() { do_help() {
cat << HELP_EOF cat << HELP_EOF
This utility is designed to be called by the ARGx parameters of a Nagios configuration as one of the following: 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] \$\$(\$USER1\$/env [-n <var>] [-t <default>] [-f <field>] [-d <delim>] [-u] [-H|-S|-C <suffix>] [-v]
-H|-S|-C will cause var to be of type NAGIOS__<HOST|SERVICE|CONTACT><var> and then continue with normal processing \$\$(\$USER1\$/env -N [-H suffix]
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: Where:
var the name of a case-insensitive NAGIOS_variable (defaults to "servicedesc") var the name of a case-insensitive NAGIOS_variable (defaults to "servicedesc")
default a default value if \$NAGIOS_var\$ is blank (case sensitive) default a default value if \$NAGIOS_var\$ is blank (case sensitive)
field an awk-compatible field number (potentially to use with <delim>) (see below) field an awk-compatible field number (potentially to use with <delim>)
delim an awk-compatible field delimiter (defaults to space and colon) delim an awk-compatible field delimiter (defaults to space, dash, 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 -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> suffix Take var result and use the it to get NAGIOS__[HOST|SERVICE|CONTACT]<result><suffix>
-v Be verbose (only for debugging) -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 -N Look for NAGIOS__HOSTNCPA<suffix> (defaults to TOKEN)
-s The result must match this value or else it will return blank Can do double lookup if -H is specified directly: -H TOKEN will find NAGIOS__HOSTNCPATOKEN=Level1 which then finds NAGIOS__HOSTLEVEL1TOKEN=Secret
--num The result will have all non-numbers stripped from it Or -H PASS will find NAGIOS__HOSTNCPAPASS=Level2 which will then find NAGIOS__HOSTLEVEL2PASS=SuperSecret
-NP Returns NAGIOS__HOSTNCPAPORT (implied default of 5693)
-N DEPRECATED version of -NT
-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. 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: 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>}' 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"): 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) .../env Return: "MySQL Check: myDatabase performance"
$0 -n SERVICESTATE -t 0 Return: current state of the service (0, 1, 2) or 0 if the state is not defined .../env -n hostName Return: "localhost" (or whatever the name of the host is that ran the check)
$0 -n servicedesc -f 2 Return: "Check" (watch out for defaults!) .../env -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 4 Return: "myDatabase" (watch out for defaults!) .../env -n servicedesc -f 2 Return: "Check" (watch out for defaults!)
$0 -n servicedesc -d : -f 2 Return: "myDatabase performance" .../env -n servicedesc -f 4 Return: "myDatabase" (watch out for defaults!)
$0 -n servicedesc -d : -f -2 Return: "MySQL Check" (2nd field from right, split by :)* .../env -n servicedesc -d : -f 2 Return: "myDatabase performance"
$0 -n servicedesc -f -2 -HT pass Return: the value of \$NAGIOS__HOSTNAGIOSXIPASS\$ .../env -n servicedesc -d : -f -2 Return: "MySQL Check" (2nd field from right, split by :)*
.../env -n servicedesc -f -2 -H pass Return: the value of \$NAGIOS__HOSTNAGIOSXIPASS\$
see https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/macrolist.html for possible macro names. 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 *0 for this field will print the entire line
@ -63,119 +59,69 @@ HELP_EOF
while [ -n "$1" ]; do while [ -n "$1" ]; do
case "$1" in case "$1" in
-h|--help) do_help;; -h|--help) do_help;;
-n) myVar[Name]="$2"; shift 2;; -n) varName="$2"; shift 2;;
-T) myVar[HostVar]="$2"; shift 2;; -t) varDefault="$2"; shift 2;;
-t) myVar[Default]="$2"; shift 2;; -d) myDelim="$2"; shift 2;;
-d) myVar[myDelim]="$2"; shift 2;; -f) myField="$2"; shift 2;;
-f) myVar[myField]="$2"; shift 2;; -H) myHost="$2"; shift 2;;
-F) myVar[myDelim]=" "; myVar[myField]=$(($2-4)); shift 2;; -S) myService="$2"; shift 2;;
-HT) myVar[myHT]="$2"; shift 2;; -C) myContact="$2"; shift 2;;
-ST) myVar[myST]="$2"; shift 2;; -N) doNCPA="true"; shift 1;;
-CT) myVar[myCT]="$2"; shift 2;; -u) doUpper="true"; shift 1;;
-g) myVar[myGrep]="$2"; shift 2;; -v) doVerbose="true"; shift 1;;
-s) myVar[myString]="$2"; shift 2;;
--pre) myVar[myPrefix]="$2"; shift 2;;
--post) myVar[myPostfix]="$2"; shift 2;;
-N|-NT) myOptions[doNCPA]="true"; shift 1;;
--num) myOptions[doNumber]="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;; *) shift 1;;
esac esac
done done
do_debug() { do_debug() {
[ -n "${myOptions[doVerbose]}" ] && echo "### DEBUG: $1" [ -n "$doVerbose" ] && echo "DEBUG: $*"
}
do_lookup() {
var="$1"; def="$2"
val=${!var:-$def}
echo "$val"
} }
do_output() { if [ -n "$doNCPA" ]; then
val="$1" do_debug "NCPA mode active. myHost=$myHost"
[ -z "$val" ] && return varName=`echo "NAGIOS__HOSTNCPA${myHost:-token}" | tr "a-z" "A-Z"`
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 else
do_debug "NCPA mode NOT active." do_debug "NCPA mode NOT active."
do_debug "myOptions[doHost]=${myOptions[doHost]} myOptions[doService]=${myOptions[doService]} myOptions[doContact]=${myOptions[doContact]}" varName=`echo "NAGIOS_$varName" | tr "a-z" "A-Z"`
[ -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 fi
do_debug "Looking for $varName..."
if [ -n "${myVar[myField]}" ]; then varValue="${!varName}"
do_debug "myVar[myField]=${myVar[myField]}" do_debug "Got varValue=$varValue"
if [ "${myVar[myField]}" -lt 0 ]; then if [ -n "$myField" ]; then
myVar[Value]=`echo "${myVar[Value]}" | rev | awk -F "${myVar[myDelim]}" -v f="${myVar[myField]:1}" '{print $f}' | rev` do_debug "myField=$myField"
if [ "$myField" -lt 0 ]; then
varValue=`echo "$varValue" | rev | awk -F "$myDelim" -v f="${myField:1}" '{print $f}' | rev`
else else
myVar[Value]=`echo "${myVar[Value]}" | awk -F "${myVar[myDelim]}" -v f="${myVar[myField]}" '{print $(f)}'` varValue=`echo "$varValue" | awk -F "$myDelim" -v f="$myField" '{print $(f)}'`
fi fi
fi fi
myVar[Value]=`echo ${myVar[Value]} | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//"` varValue=`echo $varValue | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//"`
do_debug "3: After processing myVar[Value]=${myVar[Value]}" do_debug "After processing varValue=$varValue"
[ -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 [ -n "$doUpper" ] && varValue=`echo "$varValue" | tr "a-z" "A-Z"`
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 # Check for myHost
if [ -n "${myVar[myST]}" ]; then if [ -n "$myHost" ]; then
myVar[myST]=`echo "NAGIOS__SERVICE${myVar[Value]}${myVar[myST]}" | tr "a-z" "A-Z"` myHost=`echo "NAGIOS__HOST${varValue}${myHost}" | tr "a-z" "A-Z"`
do_debug "4: myVar[myST]}=${myVar[myST]}" do_debug "myHost=$myHost"
myVar[Value]=`do_lookup ${myVar[myST]}` varValue=${!myHost}
fi fi
# Check for myCT # Check for myService
if [ -n "${myVar[myCT]}" ]; then if [ -n "$myService" ]; then
myVar[myCT]=`echo "NAGIOS__CONTACT${myVar[Value]}${myVar[myCT]}" | tr "a-z" "A-Z"` myService=`echo "NAGIOS__SERVICE${varValue}${myService}" | tr "a-z" "A-Z"`
do_debug "4: myVar[myCT]=${myVar[myCT]}" do_debug "myService=$myService"
myVar[Value]=`do_lookup "${myVar[myCT]}"` varValue=${!myService}
fi fi
[ -n "${myVar[myString]}" -a "${myVar[myString]}" != "${myVar[Value]}" ] && myVar[Value]="" # Check for myContact
[ -n "${myOptions[doNumber]}" ] && myVar[Value]=`echo ${myVar[Value]} | tr -d -c "0-9"` if [ -n "$myContact" ]; then
[ -n "${myOptions[doPipe]}" ] && myVar[Value]=`echo ${myVar[Value]} | tr "/" "|"` myContact=`echo "NAGIOS__CONTACT${varValue}${myContact}" | tr "a-z" "A-Z"`
do_debug "myContact=$myContact"
do_debug "About to check for -T options because myVar[Value]=${myVar[Value]}" varValue=${!myContact}
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 fi
# Print the result [ -z "$varValue" ] && varValue="$varDefault"
do_output "${myVar[Value]}" echo "$varValue"
exit 0 exit 0

@ -1,32 +1,11 @@
binFiles=napi nlog get_profile.sh
gzFiles=napi.gz nlog.gz get_profile.sh.gz
libFiles=ENV
sync: bin lib gz
gz: $(gzFiles)
rsync -avu $^ ssd:eloyd/
bin: $(binFiles)
rsync -avu $^ root@192.168.1.19:/usr/local/bin/
rsync -avu $^ root@192.168.1.15:/usr/local/bin/
lib: $(libFiles)
rsync -avu $^ ssd:eloyd/
rsync -avu $(libFiles) root@192.168.1.19:/usr/local/nagios/libexec/
rsync -avu $(libFiles) root@192.168.1.15:/usr/local/nagios/libexec/
napi.gz: napi napi.gz: napi
gzip -f -9 --keep $< gzip -f -9 --keep $<
chmod 644 $@ chmod 644 $@
scp $@ ssd:eloyd/
nlog.gz: nlog nagios: napi
gzip -f -9 --keep $< gzip -f -9 --keep nlog
chmod 644 $@ rsync -avu nlog napi root@192.168.1.19:/usr/local/bin/
get_profile.sh.gz: get_profile.sh
gzip -f -9 --keep $<
chmod 644 $@
test: napi test: napi
@echo "### Checking HGM:" @echo "### Checking HGM:"
@ -43,39 +22,3 @@ test: napi
@echo "" @echo ""
@echo "Checking stats:" @echo "Checking stats:"
./napi --stats ./napi --stats
ENVtest1="MySQL Check: myDatabase performance"
ENVtest2="Disk Usage on / 80 90"
ENVtest3="One Two: Three - Four 5 6"
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"

@ -1,8 +0,0 @@
/* Blue gradient */
#header {
background: rgb(0,0,255);
background: -moz-linear-gradient(90deg, rgba(0,0,255,1) 0%, rgba(0,0,0,1) 50%);
background: -webkit-linear-gradient(90deg, rgba(0,0,255,1) 0%, rgba(0,0,0,1) 50%);
background: linear-gradient(90deg, rgba(0,0,255,1) 0%, rgba(0,0,0,1) 50%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#0000ff",endColorstr="#000000",GradientType=1);
}

@ -1,8 +0,0 @@
/* Green gradient */
#header {
background: rgb(0,255,0);
background: -moz-linear-gradient(90deg, rgba(0,255,0,1) 0%, rgba(0,0,0,1) 50%);
background: -webkit-linear-gradient(90deg, rgba(0,255,0,1) 0%, rgba(0,0,0,1) 50%);
background: linear-gradient(90deg, rgba(0,255,0,1) 0%, rgba(0,0,0,1) 50%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#00ff00",endColorstr="#000000",GradientType=1);
}

@ -1,8 +0,0 @@
/* Blue gradient */
#header {
background: rgb(255,0,0);
background: -moz-linear-gradient(90deg, rgba(255,0,0,1) 0%, rgba(0,0,0,1) 50%);
background: -webkit-linear-gradient(90deg, rgba(255,0,0,1) 0%, rgba(0,0,0,1) 50%);
background: linear-gradient(90deg, rgba(255,0,0,1) 0%, rgba(0,0,0,1) 50%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#ff0000",endColorstr="#000000",GradientType=1);
}

43
napi

@ -23,7 +23,6 @@ myOptions[Options]=""
verbose="0" verbose="0"
tmpJSON="" tmpJSON=""
tmpQuick="" 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 # 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 declare -A APIinfo
@ -45,7 +44,7 @@ APIinfo["objects/downtime"]=""
APIinfo["objects/contact"]=".contact[]" APIinfo["objects/contact"]=".contact[]"
APIinfo["objects/host"]=".host[]" APIinfo["objects/host"]=".host[]"
APIinfo["objects/service"]=".[]" APIinfo["objects/service"]=".[]"
APIinfo["objects/hostgroup"]=".hostgroup[]" APIinfo["objects/hostgroup"]=".[]"
APIinfo["objects/servicegroup"]="" APIinfo["objects/servicegroup"]=""
APIinfo["objects/contactgroup"]=".contactgroup[]" APIinfo["objects/contactgroup"]=".contactgroup[]"
APIinfo["objects/timeperiod"]="" APIinfo["objects/timeperiod"]=""
@ -109,7 +108,6 @@ print_helpopt() {
servicestatus|hoststatus: servicestatus|hoststatus:
c Show fields selected by -f as quoted CSV c Show fields selected by -f as quoted CSV
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
hostgroupmembers: hostgroupmembers:
h Only show hosts (not the complete JSON data) h Only show hosts (not the complete JSON data)
@ -126,12 +124,9 @@ HELPOPT_EOF
print_help() { print_help() {
cat << HELP_EOF cat << HELP_EOF
--api < o*bjects | c*onfig | s*ystem >
-t|--object < hoststatus | servicestatus | logentries | statehistory | ...
--url XI_URL=<value>
--ack problem_has_been_acknowledged=<0,1> --ack problem_has_been_acknowledged=<0,1>
--ace active_checks_enabled=<0,1> --ace active_checks_enabled=<0,1>
--api < o*bjects | c*onfig | s*ystem >
-c|--command check_command=<value> -c|--command check_command=<value>
-cca current_check_attempt=<value> -cca current_check_attempt=<value>
--col column name=<type>:<value> --col column name=<type>:<value>
@ -177,6 +172,8 @@ print_help() {
--state 0, 1, or 2 (or other, I suppose) --state 0, 1, or 2 (or other, I suppose)
--stype 0, 1 (SOFT or HARD) --stype 0, 1 (SOFT or HARD)
--test Don't call the API, just show what would happen --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)) -v|--verbose verbose=\$((\$verbose + 1))
[...] [...]=<...> NOTE: This will take anything listed as a Nagios configuration directive and search for it [...] [...]=<...> NOTE: This will take anything listed as a Nagios configuration directive and search for it
@ -219,9 +216,9 @@ while [ -n "$1" ]; do
case "$1" in case "$1" in
--helpopt) print_helpopt;; --helpopt) print_helpopt;;
--help) print_help;; --help) print_help;;
--key) APIKEY="$2"; shift 2;; --key) apikey="$2"; shift 2;;
--keyfile) APIkeyFile="$2"; shift 2;; --keyfile) apikeyfile="$2"; shift 2;;
--url) XI_URL="$2"; shift 2;; --url) xi_url="$2"; shift 2;;
-v|--verbose) verbose=$(($verbose + 1)); shift 1;; -v|--verbose) verbose=$(($verbose + 1)); shift 1;;
--api) get_myAPI "$2"; shift 2;; --api) get_myAPI "$2"; shift 2;;
-t|--object) get_myAPIep "$2"; shift 2;; -t|--object) get_myAPIep "$2"; shift 2;;
@ -268,10 +265,6 @@ while [ -n "$1" ]; do
esac esac
done 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"
[ -n "${myOptions[Start]}" ] && myOptions[Start]=`convert_time "${myOptions[Start]}"` [ -n "${myOptions[Start]}" ] && myOptions[Start]=`convert_time "${myOptions[Start]}"`
[ -n "${myOptions[End]}" ] && myOptions[End]=`convert_time "${myOptions[End]}"` [ -n "${myOptions[End]}" ] && myOptions[End]=`convert_time "${myOptions[End]}"`
# If we're doing status mode, then override a bunch of other options # If we're doing status mode, then override a bunch of other options
@ -282,7 +275,7 @@ if [ -n "${myOptions[Status]}" ]; then
myOptions[Apply]="" myOptions[Apply]=""
myOptions[Options]="" myOptions[Options]=""
myOptions[Quick]="true" myOptions[Quick]="true"
myOptions[Options]+="c,X," myOptions[Options]+="c,C,"
# If we did a bstatus, then we only want things that are bad # If we did a bstatus, then we only want things that are bad
if [ -n "${myOptions[BStatus]}" ]; then if [ -n "${myOptions[BStatus]}" ]; then
myDir[current_state]="[^0]" myDir[current_state]="[^0]"
@ -290,7 +283,6 @@ if [ -n "${myOptions[Status]}" ]; then
fi fi
if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then
do_debug 1 "Looking for url=$url in $APIkeyFile"
while read url key; do while read url key; do
if [ -z "$XI_URL" ]; then if [ -z "$XI_URL" ]; then
XI_URL="$url" XI_URL="$url"
@ -421,24 +413,22 @@ for thing in "${!myDir[@]}"; do
done done
# endpoint specific things # endpoint specific things
case "${myOptions[API]}/${myOptions[APIep]}" in case "${myOptions[APIep]}" in
objects/hostgroupmembers) 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"
;; ;;
objects/servicegroupmembers) servicegroupmembers)
[[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name" [[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
[[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .service_object_id" [[ ${myOptions[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .service_object_id"
[[ ${myOptions[Options]} =~ "s," ]] && jqString+="| .members[] | .[] | .service_description" [[ ${myOptions[Options]} =~ "s," ]] && jqString+="| .members[] | .[] | .service_description"
tmpQuick=".service_object_id,.host_name,.service_description" tmpQuick=".service_object_id,.host_name,.service_description"
;; ;;
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"; 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";; 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";;
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"; contact) tmpQuick=".contact_name,.email_address,.host_notifications_enabled,.service_notifications_enabled,.is_active";;
tmpQuicker="Service,Host,State,Type,Last Check,Attempt,Normal,Retry,Max,Output";; command) ;;
objects/contact) tmpQuick=".contact_name,.email_address,.host_notifications_enabled,.service_notifications_enabled,.is_active";; 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/host) tmpQuick=".host_name,.address,.check_command";;
esac esac
do_debug 1 "myOptions[Options]=${myOptions[Options]}" do_debug 1 "myOptions[Options]=${myOptions[Options]}"
@ -448,7 +438,6 @@ elif [ -n "${myOptions[Quick]}" ]; then
myOptions[Fields]="$tmpQuick" myOptions[Fields]="$tmpQuick"
fi fi
[[ ${myOptions[Options]} =~ "C," ]] && echo "${myOptions[Fields]}" [[ ${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]}" do_debug 1 "myOptions[Fields]=${myOptions[Fields]}"
[ -n "${myOptions[Fields]}" ] && jq_get_fields [ -n "${myOptions[Fields]}" ] && jq_get_fields
jqString+="${myOptions[MoreJQ]}" jqString+="${myOptions[MoreJQ]}"

110
nlog

@ -20,47 +20,36 @@ num=""
# These two need to be regexp wildcards to match everything when nothing is specified # These two need to be regexp wildcards to match everything when nothing is specified
aSource="(HOST|SERVICE)" aSource="(HOST|SERVICE)"
aType="(ALERT|NOTIFICATION|EVENT HANDLER)" aType="(ALERT|NOTIFICATION|EVENT HANDLER)"
verbose=""
do_debug() {
[ -n "$verbose" ] && echo "### DEBUG: $*"
}
print_help() { print_help() {
cat << HELP_EOF echo "Usage:"
Usage: echo " -d (debug mode)"
-d (debug mode) echo " -h|--host <host>"
-h|--host <host> echo " -s|--service <service>"
-s|--service <service> echo " --state <warning type (OK, WARNING, CRITICAL, UNKNOWN)> no default"
--current show CURRENT HOST|SERVICE STATE information echo " --ok|--warn|--warning|--crit|--critical only print things that match"
--state <warning type (OK, WARNING, CRITICAL, UNKNOWN)> no default echo " --hard only print hard stuff"
--ok|--warn|--warning|--crit|--critical only print things that match echo " --soft only print soft stuff"
--up|--down|--un only print things that match (un=unreachable) echo " --sev|--severity <type (HARD, SOFT)>, default=all"
--hard|--sort only print things that match echo " -Q does a quick version of HARD CRITICAL"
--soft only print soft stuff echo " -n <#> selects the alert number, no default"
--sev|--severity <type (HARD, SOFT)>, default=all echo " --type <alert type (ALERT, EVENT, NOTIFICATION)>, default=all"
-Q does a quick version of HARD CRITICAL echo " --event set alert type to EVENT HANDLER"
-n <#> selects the alert number (of max_retries), no default echo " --src|--source <alert source (HOST, SERVICE)>, default=all"
--type <alert type (ALERT, EVENT, NOTIFICATION)>, default=all echo " --from <from time>, default=today at midnight"
--event set alert type to EVENT HANDLER echo " --to <to time>, default=now"
--src|--source <alert source (HOST, SERVICE)>, default=all echo " --notime don't convert timestamp to human time"
--from <from time>, default=today at midnight echo " -c|--constant) the equivalent of a tail -f on the Nagios log file"
--to <to time>, default=now echo " --file <file[s]>) scan <file[s]> instead of $file; use \"...\" if wildcards"
--notime don't convert timestamp to human time echo ""
-c|--constant) the equivalent of a tail -f on the Nagios log file echo "All input is evaluated as a case-insensitive regexp surrounded by wildcards."
--file <file[s]>) scan <file[s]> instead of $files; use "..." if wildcards echo "Time values can be in the following formats:"
-v turn on verbose/debug mode echo " HH:MM[:SS] YYYY-MM-DD YYYY-MM-DD HH:MM[:SS]*"
echo "This program does not directly scan log files other than the current one. If you want"
All input is evaluated as a case-insensitive regexp surrounded by wildcards. echo "to scan older files, use the --file option. Note that this can be combined with the"
Time values can be in the following formats: echo "--from and --to options, but keep in mind that file names are named based on the date they"
HH:MM[:SS] YYYY-MM-DD YYYY-MM-DD HH:MM[:SS]* echo "were rotated, not the date of their contents."
This program does not directly scan log files other than the current one. If you want echo "*Due to the awk pattern matching, dates before 2020-09-09 will produce empty output."
to scan older files, use the --file option. Note that this can be combined with the
--from and --to options, but keep in mind that file names are named based on the date they
were rotated, not the date of their contents.
Some options set others, so options earlier on the command line can be overwritten by later ones
*Due to the awk pattern matching, dates before 2020-09-09 will produce empty output.
HELP_EOF
exit; exit;
} }
@ -72,10 +61,6 @@ while [ -n "$1" ]; do
-s|--service) service="$2"; shift 2;; -s|--service) service="$2"; shift 2;;
--state) state="$2"; shift 2;; --state) state="$2"; shift 2;;
--ok) state="OK"; shift 1;; --ok) state="OK"; shift 1;;
--up) state="UP"; shift ;;
--down) state="DOWN"; shift 1;;
--un) state="UNREACHABLE"; shift 1;;
--current) aSource="(HOST|SERVICE)"; aType="STATE"; shift 1;;
--warn|--warning) state="WARNING"; shift 1;; --warn|--warning) state="WARNING"; shift 1;;
--crit|--critical) state="CRITICAL"; shift 1;; --crit|--critical) state="CRITICAL"; shift 1;;
--hard) severity="HARD"; shift 1;; --hard) severity="HARD"; shift 1;;
@ -89,19 +74,17 @@ while [ -n "$1" ]; do
--from) fromTime="$2"; shift 2;; --from) fromTime="$2"; shift 2;;
--to) toTime="$2"; shift 2;; --to) toTime="$2"; shift 2;;
--notime) noTime="true"; shift 1;; --notime) noTime="true"; shift 1;;
-c|--constant) tailMode="--follow=name"; shift 1;; -c|--constant) tailMode="-f"; shift 1;;
-v) verbose="true"; shift 1;;
*) shift 1;; *) shift 1;;
esac esac
done done
# $1 $2 $3 $4 $5 $6 # $1 $2 $3 $4 $5 $6
# [fromTime] aSource aType: host;service;state;severity;num;text # [fromTime] aSource aType: host;service;state;severity;num;text
# $1 $2 $3 $4 $5 $NF # $1 $2 $3 $4 $5
# [1723708041] SERVICE ALERT: localhost; URL Status: frommyhive.com; CRITICAL; SOFT; 1; CRITICAL - Socket timeout # [1690749418] HOST ALERT: Security Cameras; DOWN; SOFT; 1; CRITICAL - 192.168.1.88: rta nan, lost 100%
# [1723722735] HOST ALERT: Security Cameras; DOWN; SOFT; 1; CRITICAL - 192.168.1.88: rta nan, lost 100% # [1690765779] SERVICE ALERT: DD-WRT; Port: vlan1 Bandwidth; CRITICAL; SOFT; 4; CRITICAL - Current BW in: 8.22Mbps Out: 1.58Mbps
# (NF-3)(NF-2)(NF-1) # (NF-3) (NF-2)(NF-1)
# [1723694400] CURRENT HOST STATE: localhost; UP;HARD;1;OK - 127.0.0.1 rta 0.067ms lost 0%
# (HOST|SERVICE) (DOWNTIME|FLAPPING)? (ALERT|NOTIFICATION) # (HOST|SERVICE) (DOWNTIME|FLAPPING)? (ALERT|NOTIFICATION)
timeSel="" timeSel=""
@ -116,34 +99,13 @@ fi
timeSub="" timeSub=""
[ -z "$noTime" ] && timeSub="&& sub (/^\[[0-9]{10}]/, strftime (\"%Y-%m-%d %H:%M:%S\", substr (\$1, 2, 10)), \$1)" [ -z "$noTime" ] && timeSub="&& sub (/^\[[0-9]{10}]/, strftime (\"%Y-%m-%d %H:%M:%S\", substr (\$1, 2, 10)), \$1)"
do_debug "host=$host"
do_debug "service=$service"
do_debug "awk=^\[[0-9]{10}] (GLOBAL |CURRENT )?$aSource (DOWNTIME |FLAPPING )?$aType: .*$host.*$service"
do_debug "state=$state"
do_debug "severity=$severity"
do_debug "num=$num"
# $(NF) = the text of the output
# $(NF-1) = check number (of max_retries)
# $(NF-2) = HARD/SOFT
# $(NF-3) = UP/DOWN/UNREACHABLE/OK/WARNING/CRITICAL/etc
tail -n +1 $tailMode $files | gawk -F\; "\ tail -n +1 $tailMode $files | gawk -F\; "\
BEGIN {IGNORECASE=1} \ BEGIN {IGNORECASE=1} \
/^\[[0-9]{10}] (GLOBAL |CURRENT )?$aSource (DOWNTIME |FLAPPING )?$aType: .*$host.*$service/ \ /^\[[0-9]{10}] (GLOBAL )?$aSource (DOWNTIME |FLAPPING )?$aType: .*$host.*;$service/ \
&& \$(NF-3)~/$state/ \ && \$(NF-3)~/$state/ \
&& \$(NF-2)~/$severity/ \ && \$(NF-2)~/$severity/ \
&& \$(NF-1)~/$num/ \ && \$(NF-1)~/$num/ \
$timeSel \ $timeSel \
$timeSub \ $timeSub \
{ {printf \"%s: %s;%s;%s;%s;%s\n\", \$1, \$2, \$3, \$4, \$5, \$6}" |
sub(/OK/, \"\x1b[32mOK\x1b[0m\", \$(NF-3)) sed -e "s/;OK;/;\x1b[32mOK\x1b[0m;/" -e "s/;CRITICAL;/;\x1b[31mCRITICAL\x1b[0m;/" -e "s/;WARNING;/;\x1b[33mWARNING\x1b[0m;/" -e "s/;HARD;/;\x1b[35mHARD\x1b[0m;/" -e "s/;SOFT;/;\x1b[36mSOFT\x1b[0m;/"
sub(/WARNING/, \"\x1b[31mWARNING\x1b[0m\", \$(NF-3))
sub(/CRITICAL/, \"\x1b[33mCRITICAL\x1b[0m\", \$(NF-3))
sub(/UP/, \"\x1b[32mUP\x1b[0m\", \$(NF-3))
sub(/DOWN/, \"\x1b[33mDOWN\x1b[0m\", \$(NF-3))
sub(/UNREACHABLE/, \"\x1b[31mUNREACHABLE\x1b[0m\", \$(NF-3))
sub(/HARD/, \"\x1b[35mHARD\x1b[0m\", \$(NF-2))
sub(/SOFT/, \"\x1b[36mSOFT\x1b[0m\", \$(NF-2))
printf \"%s: %s;%s;%s;%s;%s\n\", \$1, \$2, \$3, \$4, \$5, \$6
}"

@ -1,35 +0,0 @@
#!/bin/sh
do_stop() {
service nagios stop
service npcd stop
service ndo2db stop
service mysqld stop
service postgresql stop
service httpd stop
service crond stop
}
do_start() {
service crond start
service httpd start
service postgresql start
service mysqld start
service ndo2db start
service npcd start
service nagios start
}
do_restart() {
do_stop
do_start
}
while [ -n "$1" ]; do
case "$1" in
--stop) do_stop; exit;;
--start) do_start; exit;;
--restart) do_restart; exit;;
*) echo "Usage: $0 (--stop | --start | --restart)"; exit;;
esac
done
Loading…
Cancel
Save