#!/bin/bash
# Script to control and correct the zone of the monitored server if necessary
# Author: Sebastian Klee
# Version: 1.8
# Date: 14.10.2025

# Hostname of the monitored Server
servername=$1
# Hostname of the first master server
master=$2
# Hostname of the icingaweb2 server with its director
icinga2_director=$3

# The variable "state" receives the state of the "current load" service.
state=$(curl -s -k --netrc-file /etc/icinga2/scripts/api_u/api_zone_check.cnf -X GET -H 'Accept: application/json' https://"${master}":5665/v1/objects/services?pretty=1 -d \
'{ "type": "Host", "filter": "host.name==\"'"$servername"'\" && service.name==\"Current Load\"", "attrs" : ["state"] }' | tr -d ' "' | awk -v m="state" -F '[:.]' '$1 ~ m {print$2}')

# The condition of the "if" is true when the state of the "Current Load" service is 3(unknown)
if [[ $state -eq 3 ]]; then

    # We determine the zone of the monitored server through a list on the satellites(/var/log/icinga2/list_server_zone.log).
    # This list contains servers from our monitoring with there associated zones. We generate this list once every night using Ansible(tf1.meinserver.xyz) via a cron job.
    zone=$( < /var/log/icinga2/list_server_zone.log awk -v var_ser="$servername" '$0 ~ var_ser {print$2}'  )

    # The value of the variable "zone" should always match the regex, if that is not the case, we don't want to execute the curl requests to correct the apparently wrong zone.
    # If the regular expression does not match, we can assume that the checked server is missing from the previously generated list, the check should in this case only inform about the need for a manual check.
    regex="^.*timmehosting.*$"
    if [[ $zone =~ $regex ]]; then

      # We get the zone of the monitored server from the director with this curl request.
      actual_zone_dire=$(curl -s -k --netrc-file /etc/icinga2/scripts/api_u/api_zone_check.cnf -X GET -H 'Accept: application/json' https://"${icinga2_director}"/icingaweb2/director/host?name="${servername}&properties=zone" | tr -d '" ' | awk -F ':' '/zone/ {print$2}')
      # We only want to change the zone of the monitored server through api calls if the values ​​of the variables "zone_out_cut" and "$actual_zone_dire" are not the same. If they are the same the actual zone shouldn't be wrong.
      if [[ ! $zone == "$actual_zone_dire"  ]]; then
        # This curl-query changes the zone of the monitored server
        curl -s -k --netrc-file /etc/icinga2/scripts/api_u/api_zone_check.cnf -H 'Accept: application/json' -X POST https://"${icinga2_director}"/icingaweb2/director/host?name="${servername}" -d '{ "zone" : "'"$zone"'" }' > /dev/null 2>&1
        echo "Server war in der falschen Zone, Server wurde wieder der korrekte Zone zugeordnet!"
        exit 1
      fi

      echo "Server befindet sich in der korrekten Zone"
      exit 0

    else
      echo "Die Zone des Servers konnte nicht korrigiert werden, bitte pruefen Sie ob diese tatsächlich falsch ist und fuehren Sie in diesem Fall eine manuelle Korrektur durch(siehe auch \"Besonderheiten, Known Issues\" unter https://kb.timmehosting.de/books/icinga-2/page/zone-control-check)"
      exit 1
    fi

fi

echo "Server befindet sich in der korrekten Zone"
exit 0
