-- Container crash events table for tracking HAProxy (and other container) crashes
-- Populated by /root/whp/scripts/haproxy-crash-monitor.sh daemon

CREATE TABLE IF NOT EXISTS whp.container_crash_events (
    id INT AUTO_INCREMENT PRIMARY KEY,
    container_name VARCHAR(100) NOT NULL DEFAULT 'haproxy-manager',
    event_type VARCHAR(20) NOT NULL COMMENT 'die, oom, kill, restart',
    exit_code INT DEFAULT NULL,
    oom_killed TINYINT(1) NOT NULL DEFAULT 0,
    cause VARCHAR(50) NOT NULL COMMENT 'OOM_DOCKER, OOM_KERNEL, SEGFAULT, SIGKILL_EXTERNAL, PROCESS_ERROR, CLEAN_SHUTDOWN, ABORT, UNKNOWN_EXIT_N',
    restart_count INT DEFAULT NULL,
    downtime_seconds INT DEFAULT NULL,
    memory_usage VARCHAR(100) DEFAULT NULL COMMENT 'Container memory usage at time of crash',
    host_memory_free VARCHAR(100) DEFAULT NULL COMMENT 'Host memory state at time of crash',
    dmesg_excerpt TEXT DEFAULT NULL COMMENT 'Relevant kernel log entries',
    container_logs TEXT DEFAULT NULL COMMENT 'Last 50 lines of container logs before crash',
    docker_inspect_json TEXT DEFAULT NULL COMMENT 'Full docker inspect State JSON',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_container_crash_name (container_name),
    INDEX idx_container_crash_cause (cause),
    INDEX idx_container_crash_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
