#!/bin/bash

#######
## Description: Clamav Check Plugin: The script checks the status of the clamav daemon, but only if the total ram is larger then 8G, otherwise it
## 		Checks the Stunnel TCP connections.
## Author: Sebastian Klee
## E-Mail: s.klee@timmehosting.de
## Version: 1.0
## Date: 29.05.2024
#######

# Variables for the hostnames/ip-addresses and port numbers of the TCP connections to be tested, gets its value from the monitoring
hostname_av1=$1
hostname_av2=$2
port_int_av1=$3
port_int_av2=$4
port_ext=$5
# Variables for the clamav check, gets its value from the monitoring
clamav_socket=$7
clamav_timeout=$8

# Conditon for "if statement one", gets its value from the monitoring
mem_condition=$6
# Determined server RAM size
mem_server=$(free -g | grep 'Mem' |  awk '{print $2}')

# Variable for the exit code
exit_code=0

# We need this for results of this script which would normally be leading to warnings in the monitoring, such a result should not generate a warning on the first check attempt.
# Only if the next check attempt through this script, also produces a warning, we want a warning in the monitoring. We want to give the monitored service some time to recover on its own,
# before we intervene.
# To do this, we create a file if it does not already exist. The value of the file should normally be 0, but if a situation occurs that would lead to a warning, the file receives the value 1.
# If the next check attempt also results in a warning, the script recognizes from the value in the file that this is not the first time and signals to the monitoring that there is a problem.
# If the next check attempt does not detect any errors, the file receives the value 0 again. We use the file in the if statements of the lines 69, 87, 100 and 108.
count_warning=/tmp/count_warning.txt
if [[ ! -f $count_warning ]]; then
echo "0" > /tmp/count_warning.txt
fi

# If statement one: The if statement checks the server's RAM, the script jumps to the then-block if the total RAM is less than the value in the variable mem_condition, otherwise it jumps to the else-block.
if [[ $mem_server -le $mem_condition  ]]; then
  # The script checks the TCP connections for Stunnel with the check_tcp plugin, it writes the results of the check and the corresponding status codes to the defined variables.
  ## Results of the TCP connections for av1.meinserver.io.
  check_result_av1_loc=$(/usr/lib/nagios/plugins/check_tcp -H 127.0.0.1 -p "$port_int_av1")
  av1_status_code_loc=$?
  check_result_av1_ext=$(/usr/lib/nagios/plugins/check_tcp -H "$hostname_av1" -p "$port_ext")
  av1_status_code_ext=$?
  ## Results of the TCP connections for av2.eminserver.io.
  check_result_av2_loc=$(/usr/lib/nagios/plugins/check_tcp -H 127.0.0.1 -p "$port_int_av2")
  av2_status_code_loc=$?
  check_result_av2_ext=$(/usr/lib/nagios/plugins/check_tcp -H "$hostname_av2" -p "$port_ext")
  av2_status_code_ext=$?

  # If statement two: The if statement checks the status code of the tested TCP connections and depending on the result the script decides which block to jump to.
  if [[ $av1_status_code_ext -gt 0 ]] && [[ $av2_status_code_ext -gt 0 ]]; then
    echo "Probleme mit Stunnel:"
    exit_code=2
  elif [[ $av1_status_code_ext -eq 0 ]] && [[ $av2_status_code_ext -gt 0 ]]; then
      if [[ $av1_status_code_loc -gt 0 ]] && [[ $av2_status_code_loc -gt 0 ]]; then 
        echo "Probleme mit Stunnel:"
        exit_code=2
      elif [[ $av1_status_code_loc -gt 0 ]] && [[ $av2_status_code_loc -eq 0 ]]; then
        echo "Probleme mit Stunnel:"
        exit_code=2
      else
        counter_check_attempts=$(tail /tmp/count_warning.txt)
        if [[ $counter_check_attempts -eq 0 ]]; then
          echo "Stunnel TCP Verbindungen teilweise nicht verfügbar(erste Ueberpruefung):"
          echo "1" > /tmp/count_warning.txt
          exit_code=0
        else
          echo "Stunnel TCP Verbindungen teilweise nicht verfügbar:"
          exit_code=1
        fi
      fi
  elif [[ $av1_status_code_ext -gt 0 ]] && [[ $av2_status_code_ext -eq 0 ]]; then
      if [[ $av1_status_code_loc -gt 0 ]] && [[ $av2_status_code_loc -gt 0 ]]; then
        echo "Probleme mit Stunnel:" 
        exit_code=2
      elif [[ $av1_status_code_loc -eq 0 ]] && [[ $av2_status_code_loc -gt 0 ]]; then
        echo "Probleme mit Stunnel:"
        exit_code=2
      else
        counter_check_attempts=$(tail /tmp/count_warning.txt)
        if [[ $counter_check_attempts -eq 0 ]]; then
          echo "Stunnel TCP Verbindungen teilweise nicht verfügbar(erste Ueberpruefung):"
          echo "1" > /tmp/count_warning.txt
          exit_code=0
        else
          echo "Stunnel TCP Verbindungen teilweise nicht verfügbar:"
          exit_code=1
        fi
      fi
  else
      if [[ $av1_status_code_loc -gt 0 ]] && [[ $av2_status_code_loc -gt 0 ]]; then 
        echo "Probleme mit Stunnel:"
        exit_code=2
      elif [[ $av1_status_code_loc -eq 0 ]] && [[ $av2_status_code_loc -eq 0 ]]; then
        echo "Keine Probleme mit den Stunnel TCP Verbindungen:"
        echo "0" > /tmp/count_warning.txt
        exit_code=0
      else
        counter_check_attempts=$(tail /tmp/count_warning.txt)
        if [[ $counter_check_attempts -eq 0 ]]; then
          echo "Stunnel TCP Verbindungen teilweise nicht verfügbar(erste Ueberpruefung):"
          echo "1" > /tmp/count_warning.txt
          exit_code=0
        else
          echo "Stunnel TCP Verbindungen teilweise nicht verfügbar:"
          exit_code=1
        fi
      fi
  fi

  # Skript output
  echo "TCP Connection Stunnel AV1 Local: ${check_result_av1_loc}"
  echo "TCP Connection Stunnel AV1 Extern: ${check_result_av1_ext}"
  echo "TCP Connection Stunnel AV2 Local: ${check_result_av2_loc}"
  echo "TCP Connection Stunnel AV2 Extern: ${check_result_av2_ext}"

  # The exit_code is determined by the previous if statement(If statement two)
  exit $exit_code

# Else-Block of "if statement one": Checks if the Clamav daemon is running
else
  /usr/lib/nagios/plugins/check_clamd -H "$clamav_socket" -M warn -r crit -t "$clamav_timeout"
  exit $?
fi
