Slight change to command line params, added alternative locations for .nagiosapikey, related changes to jQ filters

dev
Eric Loyd 2 years ago
parent 5794d33489
commit 4702af87a7

@ -1,6 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
APIkeyFile="${HOME}/.nagiosapikey" APIkeyFile=".nagiosapikey"
[ ! -r "$APIkeyFile" ] && APIkeyFile="${HOME}/.nagiosapikey"
[ ! -r "$APIkeyFile" ] && APIkeyFile=""
APIKEY="" APIKEY=""
XI_URL="" XI_URL=""
@ -9,6 +11,7 @@ curl="curl -k -s"
verbose="0" verbose="0"
doCreate="" doCreate=""
myConfigName=""
myHost="" myHost=""
myService="" myService=""
myHG="" myHG=""
@ -21,7 +24,22 @@ tmpJSON=""
moreJQ="" moreJQ=""
myAPI="objects" myAPI="objects"
myAPIobject="servicestatus" myAPIep="servicestatus"
get_myAPI() {
case "$1" in
objects|config|system) myAPI="$1";;
*) myAPI="";;
esac
}
get_myAPIep() {
case "$1" in
hoststatus|servicestatus|logentries|statehistory|comment|downtime|contact|host|service|hostgroup|servicegroup|contactgroup|timeperiod) myAPIep="$1";;
hostgroupmembers|servicegroupmembers) myAPIep="$1";;
*) myAPIep="";;
esac
}
while [ -n "$1" ]; do while [ -n "$1" ]; do
case "$1" in case "$1" in
@ -37,15 +55,16 @@ while [ -n "$1" ]; do
-h|--host) myHost="$2"; shift 2;; -h|--host) myHost="$2"; shift 2;;
-s|--service) myService="$2"; shift 2;; -s|--service) myService="$2"; shift 2;;
-c|--command) myCommand="$2"; shift 2;; -c|--command) myCommand="$2"; shift 2;;
-cn|--configname) myConfigName="$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;;
--api) myAPI="$2"; shift 2;; --api) get_myAPI "$2"; shift 2;;
--object) myAPIobject="$2"; shift 2;; -t|--object) get_myAPIep "$2"; shift 2;;
*) shift 1;; *) shift 1;;
esac esac
done done
if [ -r "$APIkeyFile" ]; then if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then
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"
@ -65,7 +84,7 @@ do_debug() {
do_api() { do_api() {
api_start="$1" api_start="$1"
api_command="$2" api_command="$2"
url="${XI_URL}/nagiosxi/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0" url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0"
do_debug 2 "start=$1 command=$2" do_debug 2 "start=$1 command=$2"
do_debug 1 "Executing: $url" do_debug 1 "Executing: $url"
$curl -XGET -k "$url" $curl -XGET -k "$url"
@ -75,7 +94,7 @@ do_api() {
do_api_post() { do_api_post() {
api_start="$1" api_start="$1"
api_command="$2" api_command="$2"
url="${XI_URL}/nagiosxi/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0" url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0"
do_debug 2 "start=$1 command=$2" do_debug 2 "start=$1 command=$2"
do_debug 1 "Executing: $url" do_debug 1 "Executing: $url"
$curl -XPOST -k "$url" -d "hostgroup_name=$myHG&alias=$myHG&applyconfig=0" $curl -XPOST -k "$url" -d "hostgroup_name=$myHG&alias=$myHG&applyconfig=0"
@ -104,8 +123,8 @@ fi
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"
do_debug 2 "myAPI is $myAPI and myAPIobject is $myAPIobject" do_debug 2 "myAPI is $myAPI and myAPIep is $myAPIep"
do_api "$myAPI" "$myAPIobject" > $tmpJSON do_api "$myAPI" "$myAPIep" > $tmpJSON
else else
do_debug 1 "myFile=$myFile" do_debug 1 "myFile=$myFile"
tmpJSON="$myFile" tmpJSON="$myFile"
@ -114,29 +133,39 @@ fi
# if mySave is not empty, then we're just saving it into the file called $mySave # if mySave is not empty, then we're just saving it into the file called $mySave
if [ -n "$mySave" ]; then if [ -n "$mySave" ]; then
mv $tmpJSON $mySave mv $tmpJSON $mySave
echo "JSON data saved to $mySave" do_debug 1 "JSON data saved to $mySave"
exit exit
fi fi
# Otherwise, let's parse the JSON data here # Otherwise, let's parse the JSON data here
# Parse our string # Parse our string
jqString="" jqString=""
if [ "$myAPIobject" = "servicestatus" ]; then if [ "$myAPIep" = "servicestatus" ]; then
jqString=".servicestatus[]" jqString=".servicestatus[]"
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
[ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))" [ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))"
[ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))" [ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))"
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")" [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
[ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")" [ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")"
elif [ "$myAPIobject" = "hostgroup" ]; then elif [ "$myAPIep" = "hostgroup" ]; then
jqString=".[]" jqString=".[]"
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")" [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
elif [ "$myAPIobject" = "host" ]; then elif [ "$myAPIep" = "host" ]; then
jqString=".[]"
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
elif [ "$myAPIep" = "service" ]; then
jqString=".[]" jqString=".[]"
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
[ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))"
[ -n "$myConfigName" ] && jqString="$jqString | select(.check_command | test(\"$myConfigName\"))"
[ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))"
elif [ "$myAPIep" = "hostgroupmembers" ]; then
jqString=".hostgroup[]"
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
fi fi
[ -n "$myFields" ] && jqString="$jqString | $myFields" [ -n "$myFields" ] && jqString="$jqString | $myFields"
jqString="$jqString $moreJQ" jqString="$jqString $moreJQ"
do_debug 1 "jqString=$jqString"
cat $tmpJSON | jq -r "$jqString" cat $tmpJSON | jq -r "$jqString"
[ -z "$myFile" ] && rm $tmpJSON [ -z "$myFile" ] && rm $tmpJSON

Loading…
Cancel
Save