#!/bin/bash

#######
## Description: This script checks the job queue in ISPConfig.
## Author: Sebastian Klee
## E-Mail: s.klee@timmehosting.de
## Version: 1.1
## Date: 17.01.2024
#######

# Determination of the server id of the server and the associated number of the last executed job in the job queue for this server(multiple entries if it is a master/slave setup).
table_server=$(mysql --defaults-extra-file=/etc/mysql/debian.cnf dbispconfig -e "select server_id,updated from server" | grep -Ev 'server' | tr '\t' ":")

seconds_crit=$1
seconds_warn=$2

minutes_crit=$((seconds_crit/60))
minutes_warn=$((seconds_warn/60))

# Repeat the for loop for each row in the variable "table_server"
for i in $table_server; do
  # Write the first column of the output to the variable "table_col_server_id".
  table_col_server_id=$(echo "$i" | awk -F ':' '{print $1}')
  # Write the second columen of the output to the variable "table_col_updated".
  table_col_updated=$(echo "$i" | awk -F ':' '{print $2}')
  # Determine the timestamp of the first job that followed after the last successfully executed job.
  timestamp_jobqueue=$(mysql --defaults-extra-file=/etc/mysql/debian.cnf dbispconfig -e \
  "select datalog_id,server_id,tstamp from sys_datalog where datalog_id > \"$table_col_updated\" AND server_id = \"$table_col_server_id\" limit 1" \
  | grep -Ev 'server' | awk '{print $3}')

  # Determination of the variable size, if the value is bigger than 0, we know there are not yet executed jobs.
  size_variable=${#timestamp_jobqueue}
  if [[ $size_variable -ge 1 ]]; then
    # Increase the counter "warn" by one
    warn=$((warn + 1))
    # Actual timestamp
    actual_unix_timestamp=$(date +"%s")
    # Differnce of the variable "actual_unix_timestamp" and "timestamp_jobqueue".
    difference=$((actual_unix_timestamp - timestamp_jobqueue))

    if [[ $difference -gt $seconds_crit ]]; then
      # Increase the counter "difference_crit" by one
      difference_crit=$((difference_crit + 1))
    elif [[ $difference -gt $seconds_warn ]]; then
      # Increase the counter "difference_warn" by one
      difference_warn=$((difference_warn + 1))
    fi

  fi

  # We need this counter for a later check to detect whether it is a slave server
  counter_server=$((counter_server + 1))
done

# If the ID of the server is greater than 1 and the number of servers is equal to 1, the script ends with this if block.
if [[ $table_col_server_id -gt 1 && $counter_server -eq 1 ]]; then
  echo "Es handelt sich um einen Slave-Server, Warteschlange des Servers wird ueber den Master Server geprueft"
  exit 0
fi

# Check results
if [[ $warn -lt 1 ]]; then
  echo "Zustand der Jobwarteschlange ist in Ordnung."
  exit 0
elif [[ $difference_crit -ge "1" ]]; then
  echo "Mindestens ein Task ist bereits seit mehr als ${minutes_crit} Minuten in der Jobwarteschlange, die Jobwarteschlange sollte dringend ueberpueft werden."
  exit 2
elif [[ $difference_warn -ge "1" ]]; then
  echo "Mindestens ein Task ist bereits seit mehr als ${minutes_warn} Minuten in der Warteschlange, die Jobwarteschlange sollte überprueft werden."
  exit 1
else
  echo "Zustand der Jobwarteschlange ist in Ordnung."
  exit 0
fi
