
­­­­­­­­­­­­­­­­­­
<!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

    if journalctl -u sssd --since "5 minutes ago" 2>/dev/null | grep -q "was terminated by own WATCHDOG"; then
        return 0
    fi

    local start_times=$(ps -eo etimes,cmd | grep sssd | grep -v grep | grep -v sssd_watchdog_monitor | awk '{print $1}')
    if [ -z "$start_times" ]; then
        return 1
    fi

    local first_time=""
    while IFS= read -r etimes; do
        if [ -z "$first_time" ]; then
            first_time="$etimes"
        else
            local diff=$((etimes > first_time ? etimes - first_time : first_time - etimes))
            if [ $diff -gt 60 ]; then
                log_message "SSSD process start time mismatch detected (${diff}s difference)"
                return 0
            fi
        fi
    done <<< "$start_times"

    return 1
}

clear_watchdog_log_lines() {
    if [ -f "$SSSD_LOG" ]; then
        sed -i '/was terminated by own WATCHDOG/d' "$SSSD_LOG"
    fi
}

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

    local stop_cmd="service sssd stop"
    local start_cmd="service sssd start"
    if [ -f /usr/bin/systemctl ]; then
        stop_cmd="systemctl stop sssd"
        start_cmd="systemctl start sssd"
    fi

    if ! $stop_cmd; then
        log_message "ERROR: Failed to stop SSSD service"
        return 1
    fi

    clear_watchdog_log_lines

    if $start_cmd; then
        log_message "SSSD service restarted successfully"
    else
        log_message "ERROR: Failed to start 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