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.
95 lines
2.6 KiB
Bash
95 lines
2.6 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
APIKEY="2q2jXBDQ9nq40KEZWoJAUMefV9JvkpRF9Se6JQW9eGceHPtTtsJIeWWp6FeJk5Es"
|
|
XI_URL="https://192.168.1.19/nagiosxi"
|
|
curl="curl -k -s"
|
|
# curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=<key>"
|
|
|
|
hostgroup=""
|
|
api_start=""
|
|
api_command=""
|
|
|
|
while [ -n "$1" ]; do
|
|
case "$1" in
|
|
-g|--group|--hostgroup) hostgroup="$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
|
|
|
|
if [ -z "$hostgroup" ]; then
|
|
echo "You did not specify a hostgroup to pull hosts and services for. Please try again."
|
|
exit
|
|
fi
|
|
|
|
# Verify that we were given a valid hostgroup and grab its hosts
|
|
# For extracting JSON data into JSON data, use @csv
|
|
#members=`do_api config hostgroup | jq -r ".[] | select (.hostgroup_name==\"${hostgroup}\") | .members | @csv"`
|
|
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
|
|
tmpJSON=`mktemp`
|
|
|
|
# Create the hosts file
|
|
create_host() {
|
|
host="$1"
|
|
echo "# Creating $host..."
|
|
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
|
|
create_service() {
|
|
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"
|
|
do_api config host > $tmpJSON
|
|
for member in $members; do
|
|
cat $tmpJSON | jq -r ".[] | select (.host_name==\"$member\") | keys | .[]" | create_host $member
|
|
done
|
|
echo "# Done with host configuration"
|
|
echo "#"
|
|
echo "#"
|
|
echo "# Creating services"
|
|
|
|
do_api object servicestatus > $tmpJSON
|
|
for member in $members; do
|
|
cat $tmpJSON | jq ".servicestatus[] | select (.host_name==\"$member\")" | create_service $member
|
|
done
|
|
echo "# Done with services"
|
|
echo "# Done"
|
|
|
|
rm $tmpJSON
|