#!/bin/bash ############################################################################### # server-forensics.sh - Post-mortem forensics for crashed/locked servers # # Collects system state, logs, crash dumps, resource usage history, and # network info into a timestamped report for root-cause analysis. # # Author: Phil Connor # Contact: contact@mylinux.work # License: MIT # Version 1.00 # # Usage: # ./server-forensics.sh # Full forensic collection # ./server-forensics.sh --quick # Quick summary only # ./server-forensics.sh --service nginx # Focus on a specific service # ./server-forensics.sh --since "1 hour ago" # Logs since a time # ./server-forensics.sh --output /tmp # Custom output directory ############################################################################### set -uo pipefail # NOTE: no -e — we want to keep collecting even if a command fails #------------------------------------------------------------------------------ # CONFIGURATION #------------------------------------------------------------------------------ REPORT_DIR="/var/log/forensics" SINCE="4 hours ago" TARGET_SERVICE="" QUICK_MODE=false RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' CYAN='\033[0;36m' NC='\033[0m' log() { echo -e "${GREEN}[forensics]${NC} $1"; } warn() { echo -e "${YELLOW}[forensics]${NC} $1"; } error() { echo -e "${RED}[forensics]${NC} $1" >&2; } header(){ echo -e "${CYAN}=== $1 ===${NC}"; } #------------------------------------------------------------------------------ # PARSE ARGUMENTS #------------------------------------------------------------------------------ for arg in "$@"; do case "$arg" in --quick) QUICK_MODE=true ;; --service) shift; TARGET_SERVICE="$1" ;; --since) shift; SINCE="$1" ;; --output) shift; REPORT_DIR="$1" ;; --help|-h) echo "Usage: $0 [OPTIONS]" echo "" echo " --quick Quick summary (skip deep collection)" echo " --service Focus forensics on a specific service" echo " --since