
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
#!/bin/bash

SSSD_LOG="/var/log/sssd/sssd.log"
LOG_FILE="/var/log/sssd_watchdog_monitor.log"
LOCK_FILE="/var/run/sssd_watchdog_monitor.lock"
MIN_RESTART_INTERVAL=60

log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}

check_recent_restart() {
    if [ ! -f "$LOG_FILE" ]; then
        return 1
    fi

    local last_restart=$(tail -100 "$LOG_FILE" | grep "SSSD service restarted successfully" | tail -1 | awk '{print $1, $2}')

    if [ -z "$last_restart" ]; then
        return 1
    fi

    local last_restart_epoch=$(date -d "$last_restart" +%s 2>/dev/null || date -j -f "%Y-%m-%d %H:%M:%S" "$last_restart" +%s 2>/dev/null)

    if [ -z "$last_restart_epoch" ]; then
        return 1
    fi

    local current_epoch=$(date +%s)
    local time_diff=$((current_epoch - last_restart_epoch))

    if [ $time_diff -lt $MIN_RESTART_INTERVAL ]; then
        log_message "Skipping restart. Service was restarted $time_diff seconds ago (minimum: $MIN_RESTART_INTERVAL)"
        return 0
    fi

    return 1
}

check_watchdog_error() {
    if [ ! -f "$SSSD_LOG" ]; then
        log_message "ERROR: SSSD log file not found at $SSSD_LOG"
        return 1
    fi

    if tail -n 500 "$SSSD_LOG" | grep -q "was terminated by own WATCHDOG"; then
        return 0
    fi
    return 1
}

restart_sssd() {
    log_message "WATCHDOG termination detected. Restarting SSSD service..."

    local restart_cmd="service sssd restart"
    if [ -f /usr/bin/systemctl ]; then
        restart_cmd="systemctl restart sssd"
    fi

    if $restart_cmd; then
        log_message "SSSD service restarted successfully"
        sed -i '/was terminated by own WATCHDOG/d' "$SSSD_LOG"
    else
        log_message "ERROR: Failed to restart SSSD service"
        return 1
    fi
}

if [ -f "$LOCK_FILE" ]; then
    PID=$(cat "$LOCK_FILE")
    if ps -p "$PID" > /dev/null 2>&1; then
        log_message "Another instance is already running (PID: $PID). Exiting."
        exit 0
    else
        rm -f "$LOCK_FILE"
    fi
fi

echo $$ > "$LOCK_FILE"
trap "rm -f $LOCK_FILE; exit" INT TERM EXIT

if check_watchdog_error; then
    if ! check_recent_restart; then
        restart_sssd
    fi
fi