edumgui

nagiosxi_api_check

May 24th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.04 KB | None | 0 0
  1. #!/bin/bash
  2. #Execution example: nagiosxi_api_check.sh hg_environment_service
  3.  
  4. apikey="YOUR_NAGIOS_XI_APIKEY"
  5. hostgroup_name=$1
  6. environment=$(echo "$hostgroup_name" | awk -F'_' '{ printf $2 }')
  7.  
  8. hostgroup=$(curl --silent "https://nagiosxi.hotelbeds.com/nagiosxi/api/v1/objects/hostgroupmembers?hostgroup_name=$hostgroup_name&apikey=$apikey&pretty=1")
  9. hostgroup_members=$(echo $hostgroup | jq -c '.hostgrouplist.hostgroup.members.host' | jq -r '.[] .host_name')
  10. if [ "$hostgroup_members" = "null" ]
  11. then
  12.   hostgroup_members=$(echo $hostgroup | jq -c '.hostgrouplist.hostgroup.members.host' | jq -r '.host_name')
  13. fi
  14. echo "$hostgroup_members" | while IFS= read -r host
  15. do
  16.   echo "$host"
  17.   echo
  18.   host_services=$(curl --silent "https://nagiosxi.hotelbeds.com/nagiosxi/api/v1/objects/servicestatus?host_name=$host&apikey=$apikey&pretty=1" | jq -c '.servicestatuslist.servicestatus[]')
  19.   echo "$host_services" | while IFS= read -r service
  20.   do
  21.     service_name=$(echo "$service" | jq -r '.name')
  22.     service_status=$(echo "$service" | jq -r '.current_state')
  23.     service_last_hard=$(echo "$service" | jq -r '.last_hard_state_change')
  24.     echo "$service_status" | while IFS= read -r status
  25.     do
  26.     case $status in
  27.       0)
  28.         echo "$service_name is OK"
  29.         ;;
  30.       1)
  31.         echo "$service_name is WARNING"
  32.         ;;
  33.       2)
  34.         echo "$service_name is CRITICAL"
  35.         last_hard=$(date -d "$service_last_hard" +%s)
  36.         datetime=$(date +%Y-%m-%d\ %H:%M:%S --date='5 minutes ago')
  37.         five_minutes_ago=$(date -d "$datetime" +%s)
  38.         if [ $five_minutes_ago -gt $last_hard ]
  39.         then
  40.           echo
  41.           echo "HOST $host $service_name has been in critical state for more than 5 minutes. Calling RESTART..."
  42.           echo
  43.           ### Call another script to operate ###
  44.           service_name=$(echo "$service_name" | awk '{printf $1}')
  45.           bash operate.sh "$environment" "$service_name" "$host" restarted
  46.         fi
  47.         ;;
  48.       *)
  49.         echo "Not FOUND"
  50.         ;;
  51.     esac
  52.     done
  53.   done
  54. done
Advertisement
Add Comment
Please, Sign In to add comment