api.sh fleshing out further
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
APIkeyFile="${HOME}/.nagiosapikey"
|
||||
|
||||
APIKEY=""
|
||||
XI_URL=""
|
||||
curl="curl -k -s"
|
||||
# curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=<key>"
|
||||
|
||||
verbose="0"
|
||||
doCreate=""
|
||||
myHost=""
|
||||
myService=""
|
||||
@@ -15,12 +18,16 @@ myCommand=""
|
||||
mySave=""
|
||||
myFile=""
|
||||
tmpJSON=""
|
||||
moreJQ=""
|
||||
|
||||
api_start=""
|
||||
api_command=""
|
||||
myAPI="objects"
|
||||
myAPIobject="servicestatus"
|
||||
|
||||
while [ -n "$1" ]; do
|
||||
case "$1" in
|
||||
--keyfile) APIkeyFile="$2"; shift 2;;
|
||||
-j|--jq) moreJQ="$2"; shift 2;;
|
||||
-v|--verbose) verbose=$(($verbose + 1)); shift 1;;
|
||||
--create) doCreate="true"; shift 1;;
|
||||
--key) APIKEY="$2"; shift 2;;
|
||||
--url) XI_URL="$2"; shift 2;;
|
||||
@@ -32,16 +39,35 @@ while [ -n "$1" ]; do
|
||||
-c|--command) myCommand="$2"; shift 2;;
|
||||
-hg|--hostgroup) myHG="$2"; shift 2;;
|
||||
-sg|--servicegroup) mySG="$2"; shift 2;;
|
||||
--start) api_start="$2"; shift 2;;
|
||||
--command) api_command="$2"; shift 2;;
|
||||
--api) myAPI="$2"; shift 2;;
|
||||
--object) myAPIobject="$2"; shift 2;;
|
||||
*) shift 1;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ -r "$APIkeyFile" ]; then
|
||||
while read url key; do
|
||||
if [ -z "$XI_URL" ]; then
|
||||
XI_URL="$url"
|
||||
APIKEY="$key"
|
||||
continue
|
||||
fi
|
||||
if [ "$url" = "$XI_URL" ]; then
|
||||
APIKEY="$key"
|
||||
fi
|
||||
done < "$APIkeyFile"
|
||||
fi
|
||||
|
||||
do_debug() {
|
||||
[ "$verbose" -ge "$1" ] && echo "$2" >&2
|
||||
}
|
||||
|
||||
do_api() {
|
||||
api_start="$1"
|
||||
api_command="$2"
|
||||
url="${XI_URL}/nagiosxi/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"
|
||||
}
|
||||
|
||||
@@ -50,21 +76,19 @@ do_api_post() {
|
||||
api_start="$1"
|
||||
api_command="$2"
|
||||
url="${XI_URL}/nagiosxi/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"
|
||||
}
|
||||
|
||||
# If we were given specific API start/commands, then let's do that and exit
|
||||
if [ -n "$api_start" -a -n "$api_command" ]; then
|
||||
do_api ${api_start} ${api_command}
|
||||
exit
|
||||
fi
|
||||
|
||||
# At this time, all we can create ia a hostgroup
|
||||
create_hostgroup() {
|
||||
do_debug 1 "about to do an API post call"
|
||||
do_api_post config hostgroup
|
||||
}
|
||||
|
||||
do_create() {
|
||||
do_debug 1 "about to do a create_command"
|
||||
[ -n "$myHG" ] && create_hostgroup
|
||||
exit
|
||||
}
|
||||
@@ -79,8 +103,11 @@ fi
|
||||
# If we used an existing file, then just use that
|
||||
if [ -z "$myFile" ]; then
|
||||
tmpJSON=`mktemp`
|
||||
do_api objects servicestatus > $tmpJSON
|
||||
do_debug 1 "tmp file is $tmpJSON"
|
||||
do_debug 2 "myAPI is $myAPI and myAPIobject is $myAPIobject"
|
||||
do_api "$myAPI" "$myAPIobject" > $tmpJSON
|
||||
else
|
||||
do_debug 1 "myFile=$myFile"
|
||||
tmpJSON="$myFile"
|
||||
fi
|
||||
|
||||
@@ -93,13 +120,23 @@ fi
|
||||
|
||||
# Otherwise, let's parse the JSON data here
|
||||
# Parse our string
|
||||
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\")"
|
||||
jqString=""
|
||||
if [ "$myAPIobject" = "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
|
||||
jqString=".[]"
|
||||
[ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
|
||||
elif [ "$myAPIobject" = "host" ]; then
|
||||
jqString=".[]"
|
||||
[ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
|
||||
fi
|
||||
[ -n "$myFields" ] && jqString="$jqString | $myFields"
|
||||
jqString="$jqString $moreJQ"
|
||||
|
||||
cat $tmpJSON | jq -r "$jqString"
|
||||
[ -z "$myFile" ] && rm $tmpJSON
|
||||
|
||||
Reference in New Issue
Block a user