|
|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
APIkeyFile="${HOME}/.nagiosapikey"
|
|
|
|
|
APIkeyFile=".nagiosapikey"
|
|
|
|
|
[ ! -r "$APIkeyFile" ] && APIkeyFile="${HOME}/.nagiosapikey"
|
|
|
|
|
[ ! -r "$APIkeyFile" ] && APIkeyFile=""
|
|
|
|
|
|
|
|
|
|
APIKEY=""
|
|
|
|
|
XI_URL=""
|
|
|
|
|
@ -9,6 +11,7 @@ curl="curl -k -s"
|
|
|
|
|
|
|
|
|
|
verbose="0"
|
|
|
|
|
doCreate=""
|
|
|
|
|
myConfigName=""
|
|
|
|
|
myHost=""
|
|
|
|
|
myService=""
|
|
|
|
|
myHG=""
|
|
|
|
|
@ -21,7 +24,22 @@ tmpJSON=""
|
|
|
|
|
moreJQ=""
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
case "$1" in
|
|
|
|
|
@ -37,15 +55,16 @@ while [ -n "$1" ]; do
|
|
|
|
|
-h|--host) myHost="$2"; shift 2;;
|
|
|
|
|
-s|--service) myService="$2"; shift 2;;
|
|
|
|
|
-c|--command) myCommand="$2"; shift 2;;
|
|
|
|
|
-cn|--configname) myConfigName="$2"; shift 2;;
|
|
|
|
|
-hg|--hostgroup) myHG="$2"; shift 2;;
|
|
|
|
|
-sg|--servicegroup) mySG="$2"; shift 2;;
|
|
|
|
|
--api) myAPI="$2"; shift 2;;
|
|
|
|
|
--object) myAPIobject="$2"; shift 2;;
|
|
|
|
|
--api) get_myAPI "$2"; shift 2;;
|
|
|
|
|
-t|--object) get_myAPIep "$2"; shift 2;;
|
|
|
|
|
*) shift 1;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [ -r "$APIkeyFile" ]; then
|
|
|
|
|
if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then
|
|
|
|
|
while read url key; do
|
|
|
|
|
if [ -z "$XI_URL" ]; then
|
|
|
|
|
XI_URL="$url"
|
|
|
|
|
@ -65,7 +84,7 @@ do_debug() {
|
|
|
|
|
do_api() {
|
|
|
|
|
api_start="$1"
|
|
|
|
|
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 1 "Executing: $url"
|
|
|
|
|
$curl -XGET -k "$url"
|
|
|
|
|
@ -75,7 +94,7 @@ do_api() {
|
|
|
|
|
do_api_post() {
|
|
|
|
|
api_start="$1"
|
|
|
|
|
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 1 "Executing: $url"
|
|
|
|
|
$curl -XPOST -k "$url" -d "hostgroup_name=$myHG&alias=$myHG&applyconfig=0"
|
|
|
|
|
@ -104,8 +123,8 @@ fi
|
|
|
|
|
if [ -z "$myFile" ]; then
|
|
|
|
|
tmpJSON=`mktemp`
|
|
|
|
|
do_debug 1 "tmp file is $tmpJSON"
|
|
|
|
|
do_debug 2 "myAPI is $myAPI and myAPIobject is $myAPIobject"
|
|
|
|
|
do_api "$myAPI" "$myAPIobject" > $tmpJSON
|
|
|
|
|
do_debug 2 "myAPI is $myAPI and myAPIep is $myAPIep"
|
|
|
|
|
do_api "$myAPI" "$myAPIep" > $tmpJSON
|
|
|
|
|
else
|
|
|
|
|
do_debug 1 "myFile=$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 [ -n "$mySave" ]; then
|
|
|
|
|
mv $tmpJSON $mySave
|
|
|
|
|
echo "JSON data saved to $mySave"
|
|
|
|
|
do_debug 1 "JSON data saved to $mySave"
|
|
|
|
|
exit
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Otherwise, let's parse the JSON data here
|
|
|
|
|
# Parse our string
|
|
|
|
|
jqString=""
|
|
|
|
|
if [ "$myAPIobject" = "servicestatus" ]; then
|
|
|
|
|
if [ "$myAPIep" = "servicestatus" ]; then
|
|
|
|
|
jqString=".servicestatus[]"
|
|
|
|
|
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
|
|
|
|
|
[ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))"
|
|
|
|
|
[ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))"
|
|
|
|
|
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
|
|
|
|
|
[ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")"
|
|
|
|
|
elif [ "$myAPIobject" = "hostgroup" ]; then
|
|
|
|
|
elif [ "$myAPIep" = "hostgroup" ]; then
|
|
|
|
|
jqString=".[]"
|
|
|
|
|
[ -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=".[]"
|
|
|
|
|
[ -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
|
|
|
|
|
[ -n "$myFields" ] && jqString="$jqString | $myFields"
|
|
|
|
|
jqString="$jqString $moreJQ"
|
|
|
|
|
do_debug 1 "jqString=$jqString"
|
|
|
|
|
|
|
|
|
|
cat $tmpJSON | jq -r "$jqString"
|
|
|
|
|
[ -z "$myFile" ] && rm $tmpJSON
|
|
|
|
|
|