#!/bin/bash
LOG_FILE="/var/log/system_inspection.log"
log() {echo "$(date +"%Y-%m-%d %H:%M:%S") : $*" | tee -a "$LOG_FILE"
}
os_check() {if [ -e /etc/redhat-release ]; thenREDHAT=$(awk '{print $1}' /etc/redhat-release)elseDEBIAN=$(awk '{print $1}' /etc/issue)fiif [[ "$REDHAT" == "CentOS" || "$REDHAT" == "Red" ]]; thenP_M="yum" elif [[ "$DEBIAN" == "Ubuntu" || "$DEBIAN" == "ubuntu" ]]; thenP_M="apt-get" elselog "Operating system does not support."exit 1fi
}
if [ "$LOGNAME" != "root" ]; thenlog "Please use the root account operation."exit 1
fi
install_tool() {local tool_name="$1"local package_name="$2"if ! which "$tool_name" &>/dev/null; thenlog "$tool_name not found, installing..."os_check $P_M install "$package_name" -y | tee -a "$LOG_FILE" fi
}
install_tool "vmstat" "procps"
install_tool "iostat" "sysstat"
cpu_load() {log "Checking CPU load..."for i in {1..3}; dolog "Sample $i:" vmstat | awk 'NR==3 { print "Util:", 100-$15"%", "User:", $13"%", "System:", $14"%", "I/O wait:", $16"%" }' | tee -a "$LOG_FILE"sleep 1 done
}
disk_load() {log "Checking Disk I/O load..."for i in {1..3}; dolog "Sample $i:" iostat -x -k | awk '/^[v|s]/{OFS=": "; print $1, "Util:", $NF"%", "Read/s:", $6"KB", "Write/s:", $7"KB"}' | tee -a "$LOG_FILE"sleep 1 done
}
disk_use() {log "Checking Disk usage..."df -h | awk '/^\/dev/ && int(substr($5, 1, length($5)-1)) > 90 { print $6 " = " $5 }' | tee -a "$LOG_FILE"
}
disk_inode() {log "Checking Disk inode usage..."df -i | awk '/^\/dev/ && $5+0 > 90 { print $6 " = " $5 }' | tee -a "$LOG_FILE"
}
mem_use() {log "Checking Memory usage..."free -m | awk '/^Mem:/ { print "Total:", $2"MB", "Used:", $3"MB", "Free:", $4"MB" }' | tee -a "$LOG_FILE"
}
tcp_status() {log "Checking TCP connection status..."ss -ant | awk '!/State/ { status[$1]++ } END { for (i in status) print i, status[i] }' | tee -a "$LOG_FILE"
}
cpu_top10() {log "Checking top 10 CPU consuming processes..."ps aux --sort=-%cpu | awk 'NR<=11 { print $2, $3"%", $11 }' | tee -a "$LOG_FILE"
}
mem_top10() {log "Checking top 10 Memory consuming processes..."ps aux --sort=-%mem | awk 'NR<=11 { print $2, $4"%", $11 }' | tee -a "$LOG_FILE"
}
traffic() {read -p "Enter network card name (e.g., eth0): " ethif ifconfig "$eth" &>/dev/null; thenlog "Checking Network traffic on $eth..."for i in {1..3}; dotraffic_info=$(ifconfig "$eth")OLD_IN=$(echo "$traffic_info" | awk '/RX bytes/ {print $5}' | cut -d: -f2)OLD_OUT=$(echo "$traffic_info" | awk '/TX bytes/ {print $5}' | cut -d: -f2)sleep 1traffic_info=$(ifconfig "$eth")NEW_IN=$(echo "$traffic_info" | awk '/RX bytes/ {print $5}' | cut -d: -f2)NEW_OUT=$(echo "$traffic_info" | awk '/TX bytes/ {print $5}' | cut -d: -f2)RX_DIFF=$((NEW_IN - OLD_IN))TX_DIFF=$((NEW_OUT - OLD_OUT))log "In: $RX_DIFF Bytes, Out: $TX_DIFF Bytes"doneelselog "Invalid network card name."fi
}
while true; doselect input in cpu_load disk_load disk_use disk_inode mem_use tcp_status cpu_top10 mem_top10 traffic quit all_check; docase $input incpu_load) cpu_load ;;disk_load) disk_load ;;disk_use) disk_use ;;disk_inode) disk_inode ;;mem_use) mem_use ;;tcp_status) tcp_status ;;cpu_top10) cpu_top10 ;;mem_top10) mem_top10 ;;traffic) traffic ;;quit)log "Exiting."exit 0;;all_check)log "Starting Full System Check..."cpu_loaddisk_loaddisk_usedisk_inodemem_usetcp_statuscpu_top10mem_top10trafficlog "Full System Check Completed."break;;*)log "Invalid option.";;esacdone
done