#!/bin/bash

# Linux Server Performance Diagnostic Checklist
#
# Technical reference:
# Server optimization service:
# https://i...content-available-to-author-only...t.com/server-optimization-service/
#
# This script performs read-only checks.
# It does not modify system configuration.

echo "=============================================="
echo " Linux Server Performance Diagnostic"
echo "=============================================="

echo
echo "[1] System Uptime and Load"
echo "----------------------------------------------"
uptime

echo
echo "[2] CPU Information"
echo "----------------------------------------------"
nproc 2>/dev/null
lscpu 2>/dev/null | grep -E \
    '^(CPU\(s\)|Model name|CPU MHz|Architecture)' \
    | head -n 10

echo
echo "[3] Memory Usage"
echo "----------------------------------------------"
free -m

echo
echo "[4] Top CPU-Consuming Processes"
echo "----------------------------------------------"
ps aux --sort=-%cpu | head -n 15

echo
echo "[5] Top Memory-Consuming Processes"
echo "----------------------------------------------"
ps aux --sort=-%mem | head -n 15

echo
echo "[6] Filesystem Capacity"
echo "----------------------------------------------"
df -h

echo
echo "[7] Inode Usage"
echo "----------------------------------------------"
df -i

echo
echo "[8] Disk and Block Device Information"
echo "----------------------------------------------"
lsblk 2>/dev/null

echo
echo "[9] Disk I/O Statistics"
echo "----------------------------------------------"

if command -v iostat >/dev/null 2>&1; then
    iostat -xz 1 2
else
    echo "iostat is not installed."
    echo "Install the sysstat package on a test server if required."
fi

echo
echo "[10] Virtual Memory Statistics"
echo "----------------------------------------------"

if command -v vmstat >/dev/null 2>&1; then
    vmstat 1 3
fi

echo
echo "[11] Listening Network Services"
echo "----------------------------------------------"
ss -lntp 2>/dev/null

echo
echo "[12] Failed Systemd Services"
echo "----------------------------------------------"
systemctl --failed --no-pager 2>/dev/null

echo
echo "[13] Running Services"
echo "----------------------------------------------"
systemctl list-units \
    --type=service \
    --state=running \
    --no-pager 2>/dev/null | head -n 40

echo
echo "[14] Web Server Status"
echo "----------------------------------------------"

systemctl is-active nginx 2>/dev/null || true
systemctl is-active apache2 2>/dev/null || true
systemctl is-active httpd 2>/dev/null || true

echo
echo "[15] Database Service Status"
echo "----------------------------------------------"

systemctl is-active mysql 2>/dev/null || true
systemctl is-active mariadb 2>/dev/null || true

echo
echo "[16] Recent System Errors"
echo "----------------------------------------------"
journalctl -p err -n 30 --no-pager 2>/dev/null

echo
echo "[17] Recent Kernel Warnings and Errors"
echo "----------------------------------------------"
dmesg --level=err,warn 2>/dev/null | tail -n 30

echo
echo "[18] Largest Files in /var"
echo "----------------------------------------------"

if [ -d /var ]; then
    find /var -type f -printf '%s %p\n' 2>/dev/null \
        | sort -nr \
        | head -n 15
fi

echo
echo "[19] Performance Review Checklist"
echo "----------------------------------------------"
echo "[ ] CPU saturation investigated"
echo "[ ] Load average reviewed"
echo "[ ] Memory pressure checked"
echo "[ ] Swap activity reviewed"
echo "[ ] Disk I/O latency investigated"
echo "[ ] Filesystem capacity checked"
echo "[ ] Inode usage checked"
echo "[ ] High-resource processes identified"
echo "[ ] Failed services investigated"
echo "[ ] Web server performance reviewed"
echo "[ ] Database performance reviewed"
echo "[ ] Network listeners reviewed"
echo "[ ] System logs checked"
echo "[ ] Recent configuration changes reviewed"

echo
echo "=============================================="
echo " Diagnostic collection completed"
echo "=============================================="