More changes to api.sh

dev
Eric Loyd 2 years ago
parent 081d2026cd
commit 405aeddf46

103
api.sh

@ -5,13 +5,31 @@ XI_URL="https://192.168.1.19/nagiosxi"
curl="curl -k -s" 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>"
hostgroup="" myHost=""
myService=""
myHG=""
mySG=""
myFields=""
myCommand=""
mySave=""
myFile=""
tmpJSON=""
api_start="" api_start=""
api_command="" api_command=""
while [ -n "$1" ]; do while [ -n "$1" ]; do
case "$1" in case "$1" in
-g|--group|--hostgroup) hostgroup="$2"; shift 2;; --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;; --start) api_start="$2"; shift 2;;
--command) api_command="$2"; shift 2;; --command) api_command="$2"; shift 2;;
*) shift 1;; *) shift 1;;
@ -31,64 +49,35 @@ if [ -n "$api_start" -a -n "$api_command" ]; then
exit exit
fi fi
if [ -z "$hostgroup" ]; then # Parse our string
echo "You did not specify a hostgroup to pull hosts and services for. Please try again." jqString='.servicestatus[]'
exit [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))"
fi [ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))"
[ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))"
# Verify that we were given a valid hostgroup and grab its hosts [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")"
# For extracting JSON data into JSON data, use @csv [ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")"
#members=`do_api config hostgroup | jq -r ".[] | select (.hostgroup_name==\"${hostgroup}\") | .members | @csv"` [ -n "$myFields" ] && jqString="$jqString | $myFields"
members=`do_api config hostgroup | jq -rc ".[] | select (.hostgroup_name==\"${hostgroup}\") | .members | .[]"`
if [ -z "$members" ]; then
echo "Hostgroup: $hostgroup not found."
exit
fi
# Grab a copy of the hosts.json so we don't have to keep making calls over and over # Grab a copy of the hosts.json so we don't have to keep making calls over and over
tmpJSON=`mktemp` # If we used an existing file, then just use that
if [ -z "$myFile" ]; then
# Create the hosts file tmpJSON=`mktemp`
create_host() { else
host="$1" tmpJSON="$myFile"
echo "# Creating $host..." fi
echo "define host {"
while read key; do
val=`cat $tmpJSON | jq -r ".[] | select (.host_name==\"$host\") .${key} | if type==\"string\" then [.] else . end | .[]" | tr "\n" "," | sed -e "s/,$/\n/"`
echo " $key $val"
done
echo "}"
echo ""
}
# Create the services file # Let's pull everything that matches everything we were given
create_service() { do_api objects servicestatus > $tmpJSON
host="$1"
echo "# Creating $host services..."
echo "define host {"
while read key; do
val=`cat $tmpJSON | jq -r ".[] | select (.host_name==\"$host\") .${key} | if type==\"string\" then [.] else . end | .[]" | tr "\n" "," | sed -e "s/,$/\n/"`
echo " $key $val"
done
echo "}"
echo ""
}
echo "# Creating new host config file for hostgroup: $hostgroup" # if mySave is not empty, then we're just saving it into the file called $mySave
do_api config host > $tmpJSON if [ -n "$mySave" ]; then
for member in $members; do cp $tmpJSON $mySave
cat $tmpJSON | jq -r ".[] | select (.host_name==\"$member\") | keys | .[]" | create_host $member [ -z "$myFile" ] && rm $tmpJSON
done echo "JSON data saved to $mySave"
echo "# Done with host configuration" exit
echo "#" fi
echo "#"
echo "# Creating services"
do_api object servicestatus > $tmpJSON # Otherwise, let's parse the JSON data here
for member in $members; do cat $tmpJSON | jq -r "$jqString"
cat $tmpJSON | jq ".servicestatus[] | select (.host_name==\"$member\")" | create_service $member
done
echo "# Done with services"
echo "# Done"
rm $tmpJSON [ -z "$myFile" ] && rm $tmpJSON

Loading…
Cancel
Save