#!/usr/bin/env bash# Diagnostic script that generates a quick report on system health, disk usage, and failed services.# Enforce strict error handlingset-euopipefail
# Safely resolve hostname for minimal environmentsifcommand-vhostname>/dev/null2>&1;thenCURRENT_HOSTNAME=$(hostname)elif[-r/etc/hostname];thenCURRENT_HOSTNAME=$(cat/etc/hostname)elseCURRENT_HOSTNAME="Unknown"fiecho"=================================================="echo" SYSTEM HEALTH REPORT "echo" Date: $(date"+%Y-%m-%d %H:%M:%S") "echo" Hostname: ${CURRENT_HOSTNAME} "echo"=================================================="echo""# 1. Check Uptime and CPU Load Averageecho"[+] Uptime & CPU Load Average:"ifcommand-vuptime>/dev/null2>&1;thenuptime
elif[-r/proc/uptime]&&[-r/proc/loadavg];then# Fallback for environments without the 'uptime' binaryup_seconds=$(cut-d.-f1/proc/uptime)up_days=$((up_seconds/86400))up_hours=$(((up_seconds%86400)/3600))up_mins=$(((up_seconds%3600)/60))load_avg=$(cat/proc/loadavg|awk'{print $1", "$2", "$3}')echo"up ${up_days} days, ${up_hours}:${up_mins}, load average: ${load_avg}"elseecho" ⚠️ 'uptime' command not found and /proc metrics are unavailable."fiecho""# 2. Check Memory & Swap Usageecho"[+] Memory Usage:"ifcommand-vfree>/dev/null2>&1;thenfree-h
elif[-r/proc/meminfo];then# Fallback for environments without the 'free' binary (missing procps)grep-E'MemTotal|MemFree|MemAvailable|SwapTotal|SwapFree'/proc/meminfo
elseecho" ⚠️ 'free' command not found and /proc/meminfo is unavailable."fiecho""# 3. Check Disk Spaceecho"[+] Real Disk Space Usage:"ifcommand-vdf>/dev/null2>&1;thendf-h-T-xtmpfs-xdevtmpfs-xsquashfs-xefivarfs||trueecho""# 4. Check for Disk Space Warnings (>85% capacity)echo"[+] Disk Capacity Warnings:"warning_found=false# Parse df output, grab usage percentage and mount point, skipping the header linewhileread-rusagemount;do# Remove the % sign for integer comparisonusage_val=${usage%\%}# Ensure the parsed value is a number before attempting an integer comparison.if[["$usage_val"=~^[0-9]+$]];thenif["$usage_val"-gt85];thenecho" ⚠️ WARNING: Partition '$mount' is at ${usage} capacity!"warning_found=truefifidone<<(df-h-T-xtmpfs-xdevtmpfs-xsquashfs-xefivarfs2>/dev/null|awk'NR>1 {print $6, $7}')if["$warning_found"=false];thenecho" ✅ No partitions are above 85% capacity."fielseecho" ⚠️ 'df' command not found. Skipping disk checks."fiecho""# 5. Check for Failed Services (Distro-Agnostic Init System Detection)echo"[+] Failed Services:"# Check for Systemd (Ubuntu, Debian, Fedora, Arch, openSUSE)ifcommand-vsystemctl>/dev/null2>&1&&[-d/run/systemd/system];thenfailed_services=$(systemctl--failed--no-legend--plain)if[-z"$failed_services"];thenecho" ✅ No failed systemd services. System is running cleanly."elseecho" ❌ WARNING: The following systemd services have failed:"echo"$failed_services"fi# Check for OpenRC (Alpine Linux, Gentoo, Artix)elifcommand-vrc-status>/dev/null2>&1;thenfailed_services=$(rc-status--crashed--nocolor|grep-v'crashed')if[-z"$failed_services"]||[["$failed_services"==*"(empty)"*]];thenecho" ✅ No crashed OpenRC services. System is running cleanly."elseecho" ❌ WARNING: The following OpenRC services have crashed:"rc-status--crashed--nocolor
fi# Check for Runit (Void Linux, Artix Runit variant)elifcommand-vsv>/dev/null2>&1&&[-d/var/service];thenecho" ℹ️ System uses Runit. Showing current service states (check for unexpected 'down' states):"svstatus/var/service/*
# Fallback for SysVinit or environments with no init daemon (e.g., Docker containers)elseecho" ⚠️ Init system not recognized as Systemd, OpenRC, or Runit."echo" ℹ️ Skipping automated failed service detection."fiecho""echo"=================================================="echo" REPORT COMPLETE "echo"=================================================="