Lots of new options and a QUICK option, too!
This commit is contained in:
@@ -10,18 +10,25 @@ curl="curl -k -s"
|
|||||||
# curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=<key>"
|
# curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=<key>"
|
||||||
|
|
||||||
verbose="0"
|
verbose="0"
|
||||||
|
testMode=""
|
||||||
doCreate=""
|
doCreate=""
|
||||||
myConfigName=""
|
|
||||||
myHost=""
|
myAck=""
|
||||||
myService=""
|
|
||||||
myCG=""
|
myCG=""
|
||||||
myHG=""
|
|
||||||
mySG=""
|
|
||||||
myFields=""
|
|
||||||
myCommand=""
|
myCommand=""
|
||||||
mySave=""
|
myConfigName=""
|
||||||
|
myFields=""
|
||||||
myFile=""
|
myFile=""
|
||||||
|
myHG=""
|
||||||
|
myHost=""
|
||||||
myOutput=""
|
myOutput=""
|
||||||
|
mySave=""
|
||||||
|
myService=""
|
||||||
|
mySG=""
|
||||||
|
myState=""
|
||||||
|
myStateType=""
|
||||||
|
myQuick=""
|
||||||
|
|
||||||
tmpJSON=""
|
tmpJSON=""
|
||||||
moreJQ=""
|
moreJQ=""
|
||||||
cmdOptions=""
|
cmdOptions=""
|
||||||
@@ -97,10 +104,15 @@ print_helpopt() {
|
|||||||
cat << HELPOPT_EOF
|
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)
|
Use these options to add options to the command specified. To add multiple options, specify -o|--opt multiple times (they will add together)
|
||||||
|
|
||||||
Options:
|
Options for different APIs:
|
||||||
hostgroupmembers
|
|
||||||
|
servicestatus:
|
||||||
|
c Show fields selected by -f as quoted CSV
|
||||||
|
|
||||||
|
hostgroupmembers:
|
||||||
h Only show hosts (not the complete JSON data)
|
h Only show hosts (not the complete JSON data)
|
||||||
o Only show host_object_id (specifying both options will result in errors)
|
o Only show host_object_id (specifying both options will result in errors)
|
||||||
|
|
||||||
HELPOPT_EOF
|
HELPOPT_EOF
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
@@ -110,6 +122,8 @@ print_help() {
|
|||||||
--help This text
|
--help This text
|
||||||
--helpopt Show help for command options
|
--helpopt Show help for command options
|
||||||
--keyfile APIkeyFile="\$2"
|
--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"
|
-j|--jq moreJQ="\$2"
|
||||||
-v|--verbose verbose=\$((\$verbose + 1))
|
-v|--verbose verbose=\$((\$verbose + 1))
|
||||||
--create doCreate="true"
|
--create doCreate="true"
|
||||||
@@ -125,13 +139,18 @@ print_help() {
|
|||||||
-cg|--contactgroup myCG="\$2"
|
-cg|--contactgroup myCG="\$2"
|
||||||
-hg|--hostgroup myHG="\$2"
|
-hg|--hostgroup myHG="\$2"
|
||||||
-sg|--servicegroup mySG="\$2"
|
-sg|--servicegroup mySG="\$2"
|
||||||
|
--state 0, 1, or 2 (or other, I suppose)
|
||||||
|
--stype 0, 1 (SOFT or HARD)
|
||||||
--output myOutput="\$2"
|
--output myOutput="\$2"
|
||||||
|
--ack 0, 1
|
||||||
-o|--opt cmdOptions="\$2" (endpoint specific options. See --helpopt)
|
-o|--opt cmdOptions="\$2" (endpoint specific options. See --helpopt)
|
||||||
--api < o*bjects | c*onfig | s*ystem >
|
--api < o*bjects | c*onfig | s*ystem >
|
||||||
-t|--object < hoststatus | servicestatus | logentries | statehistory | ...
|
-t|--object < hoststatus | servicestatus | logentries | statehistory | ...
|
||||||
... comment | downtime | contact | host | service | hostgroup | ...
|
... comment | downtime | contact | host | service | hostgroup | ...
|
||||||
... servicegroup | contactgroup | timeperiod | ...
|
... servicegroup | contactgroup | timeperiod | ...
|
||||||
... hostgroupmembers | servicegroupmembers >
|
... hostgroupmembers | servicegroupmembers >
|
||||||
|
--test Don't call the API, just show what would happen
|
||||||
|
|
||||||
HELP_EOF
|
HELP_EOF
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
@@ -141,6 +160,9 @@ while [ -n "$1" ]; do
|
|||||||
--help) print_help;;
|
--help) print_help;;
|
||||||
--helpopt) print_helpopt;;
|
--helpopt) print_helpopt;;
|
||||||
--keyfile) APIkeyFile="$2"; shift 2;;
|
--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;;
|
-j|--jq) moreJQ="$2"; shift 2;;
|
||||||
-v|--verbose) verbose=$(($verbose + 1)); shift 1;;
|
-v|--verbose) verbose=$(($verbose + 1)); shift 1;;
|
||||||
--create) doCreate="true"; shift 1;;
|
--create) doCreate="true"; shift 1;;
|
||||||
@@ -156,8 +178,11 @@ while [ -n "$1" ]; do
|
|||||||
-cg|--contactgroup) myCG="$2"; shift 2;;
|
-cg|--contactgroup) myCG="$2"; shift 2;;
|
||||||
-hg|--hostgroup) myHG="$2"; shift 2;;
|
-hg|--hostgroup) myHG="$2"; shift 2;;
|
||||||
-sg|--servicegroup) mySG="$2"; shift 2;;
|
-sg|--servicegroup) mySG="$2"; shift 2;;
|
||||||
|
--state) myState="$2"; shift 2;;
|
||||||
|
--stype) myStateType="$2"; shift 2;;
|
||||||
--output) myOutput="$2"; shift 2;;
|
--output) myOutput="$2"; shift 2;;
|
||||||
-o|--opt) cmdOptions="$2,${cmdOptions}"; shift 2;;
|
--ack) myAck="$2"; shift 2;;
|
||||||
|
-o|--opt) cmdOptions+="$2,"; shift 2;;
|
||||||
--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;;
|
||||||
*) shift 1;;
|
*) shift 1;;
|
||||||
@@ -220,6 +245,8 @@ fi
|
|||||||
|
|
||||||
# Grab a copy of the JSON data so we don't have to keep making calls over and over
|
# 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 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
|
if [ -z "$myFile" ]; then
|
||||||
tmpJSON=`mktemp`
|
tmpJSON=`mktemp`
|
||||||
do_debug 1 "tmp file is $tmpJSON"
|
do_debug 1 "tmp file is $tmpJSON"
|
||||||
@@ -236,6 +263,9 @@ if [ -n "$mySave" ]; then
|
|||||||
do_debug 1 "JSON data saved to $mySave"
|
do_debug 1 "JSON data saved to $mySave"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
echo "### TEST MODE - Skipping API call (testMode=${testMode}) ###"
|
||||||
|
fi
|
||||||
|
|
||||||
# Helper functions for creating our jqString
|
# Helper functions for creating our jqString
|
||||||
# First, we need to know if our tests should be case-sensitive or not
|
# First, we need to know if our tests should be case-sensitive or not
|
||||||
@@ -252,7 +282,44 @@ jq_check_case() {
|
|||||||
|
|
||||||
# Do we need to add host limits to the query?
|
# Do we need to add host limits to the query?
|
||||||
jq_add_host() {
|
jq_add_host() {
|
||||||
[ -n "$myHost" ] && jqString="$jqString | select(.host_name $(jq_check_case $myHost))"
|
[ -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
|
# Otherwise, let's parse the JSON data here
|
||||||
@@ -262,42 +329,51 @@ jqString=${APIinfo[$myAPI/$myAPIep]}
|
|||||||
case "$myAPIep" in
|
case "$myAPIep" in
|
||||||
servicestatus)
|
servicestatus)
|
||||||
jq_add_host
|
jq_add_host
|
||||||
[ -n "$myService" ] && jqString="$jqString | select(.service_description $(jq_check_case $myService))"
|
jq_add_service
|
||||||
[ -n "$myCommand" ] && jqString="$jqString | select(.check_command $(jq_check_case $myCommand))"
|
jq_add_hostgroup
|
||||||
[ -n "$myOutput" ] && jqString="$jqString | select(.output $(jq_check_case $myOutput))"
|
jq_add_servicegroup
|
||||||
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
|
jq_add_command
|
||||||
[ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")"
|
jq_add_output
|
||||||
|
jq_add_state
|
||||||
|
jq_add_statetype
|
||||||
|
jq_add_ack
|
||||||
;;
|
;;
|
||||||
hostgroup)
|
hostgroup)
|
||||||
jq_add_host
|
jq_add_host
|
||||||
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
|
jq_add_hostgroup
|
||||||
;;
|
;;
|
||||||
host)
|
host)
|
||||||
jq_add_host
|
jq_add_host
|
||||||
;;
|
;;
|
||||||
service)
|
service)
|
||||||
jq_add_host
|
jq_add_host
|
||||||
[ -n "$myService" ] && jqString="$jqString | select(.service_description $(jq_check_case $myService))"
|
jq_add_service
|
||||||
[ -n "$myConfigName" ] && jqString="$jqString | select(.check_command $(jq_check_case $myConfigName))"
|
jq_add_configname
|
||||||
[ -n "$myCommand" ] && jqString="$jqString | select(.check_command $(jq_check_case $myCommand))"
|
jq_add_command
|
||||||
;;
|
;;
|
||||||
hostgroupmembers)
|
hostgroupmembers)
|
||||||
jq_add_host
|
jq_add_host
|
||||||
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name $(jq_check_case $myHG))"
|
jq_add_hostgroup
|
||||||
[[ $cmdOptions =~ "h," ]] && jqString="$jqString | .members[] | .[] | .host_name"
|
[[ $cmdOptions =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
|
||||||
[[ $cmdOptions =~ "o," ]] && jqString="$jqString | .members[] | .[] | .host_object_id"
|
[[ $cmdOptions =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
|
||||||
;;
|
;;
|
||||||
servicegroup)
|
servicegroup)
|
||||||
jq_add_host
|
jq_add_host
|
||||||
[ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name $(jq_check_case $mySG))"
|
jq_add_servicegroup
|
||||||
;;
|
;;
|
||||||
contactgroup)
|
contactgroup)
|
||||||
[ -n "$myCG" ] && jqString="$jqString | select(.contactgroup_name $(jq_check_case $myCG))"
|
jq_add_contactgroup
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
[ -n "$myFields" ] && jqString="$jqString | $myFields"
|
[ -n "$myQuick" ] && myFields+=".host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged"
|
||||||
jqString="$jqString $moreJQ"
|
[ -n "$myFields" ] && jq_get_fields
|
||||||
|
jqString+="$moreJQ"
|
||||||
do_debug 1 "jqString=$jqString"
|
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"
|
cat $tmpJSON | jq -r "$jqString"
|
||||||
[ -z "$myFile" ] && rm $tmpJSON
|
[ -z "$myFile" ] && rm $tmpJSON
|
||||||
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user