You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.2 KiB
Bash

#!/usr/bin/env bash
APIKEY=""
XI_URL=""
curl="curl -k -s"
# curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=<key>"
myHost=""
myService=""
myHG=""
mySG=""
myFields=""
myCommand=""
mySave=""
myFile=""
tmpJSON=""
api_start=""
api_command=""
while [ -n "$1" ]; do
case "$1" in
--key) APIKEY="$2"; shift 2;;
--url) XI_URL="$2"; shift 2;;
--save) mySave="$2"; shift 2;;
--file) myFile="$2"; shift 2;;
-f|--fields) myFields="$2"; shift 2;;
-h|--host) myHost="$2"; shift 2;;
-s|--service) myService="$2"; shift 2;;
-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;;
*) shift 1;;
esac
done
do_api() {
api_start="$1"
api_command="$2"
url="${XI_URL}/nagiosxi/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0"
$curl -XGET -k "$url"
}
# 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
# 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 [ -z "$myFile" ]; then
tmpJSON=`mktemp`
do_api objects servicestatus > $tmpJSON
else
tmpJSON="$myFile"
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"
exit
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\")"
[ -n "$myFields" ] && jqString="$jqString | $myFields"
cat $tmpJSON | jq -r "$jqString"
[ -z "$myFile" ] && rm $tmpJSON