Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Execution example: nagiosxi_api_check.sh hg_environment_service
- apikey="YOUR_NAGIOS_XI_APIKEY"
- hostgroup_name=$1
- environment=$(echo "$hostgroup_name" | awk -F'_' '{ printf $2 }')
- hostgroup=$(curl --silent "https://nagiosxi.hotelbeds.com/nagiosxi/api/v1/objects/hostgroupmembers?hostgroup_name=$hostgroup_name&apikey=$apikey&pretty=1")
- hostgroup_members=$(echo $hostgroup | jq -c '.hostgrouplist.hostgroup.members.host' | jq -r '.[] .host_name')
- if [ "$hostgroup_members" = "null" ]
- then
- hostgroup_members=$(echo $hostgroup | jq -c '.hostgrouplist.hostgroup.members.host' | jq -r '.host_name')
- fi
- echo "$hostgroup_members" | while IFS= read -r host
- do
- echo "$host"
- echo
- host_services=$(curl --silent "https://nagiosxi.hotelbeds.com/nagiosxi/api/v1/objects/servicestatus?host_name=$host&apikey=$apikey&pretty=1" | jq -c '.servicestatuslist.servicestatus[]')
- echo "$host_services" | while IFS= read -r service
- do
- service_name=$(echo "$service" | jq -r '.name')
- service_status=$(echo "$service" | jq -r '.current_state')
- service_last_hard=$(echo "$service" | jq -r '.last_hard_state_change')
- echo "$service_status" | while IFS= read -r status
- do
- case $status in
- 0)
- echo "$service_name is OK"
- ;;
- 1)
- echo "$service_name is WARNING"
- ;;
- 2)
- echo "$service_name is CRITICAL"
- last_hard=$(date -d "$service_last_hard" +%s)
- datetime=$(date +%Y-%m-%d\ %H:%M:%S --date='5 minutes ago')
- five_minutes_ago=$(date -d "$datetime" +%s)
- if [ $five_minutes_ago -gt $last_hard ]
- then
- echo
- echo "HOST $host $service_name has been in critical state for more than 5 minutes. Calling RESTART..."
- echo
- ### Call another script to operate ###
- service_name=$(echo "$service_name" | awk '{printf $1}')
- bash operate.sh "$environment" "$service_name" "$host" restarted
- fi
- ;;
- *)
- echo "Not FOUND"
- ;;
- esac
- done
- done
- done
Advertisement
Add Comment
Please, Sign In to add comment