Files
linux_system_tuning/system-analyzer.sh
mindesbunister 2edb4a73c3 Complete Linux system optimization suite with tmpfs, zram, and German locale support
- Fixed missing setup_tmpfs() function that was causing silent failures
- Added comprehensive system scanning for browsers, IDEs, gaming caches
- Implemented detailed optimization information display for transparency
- Added German locale compatibility for 'free' command (Speicher: vs Mem:)
- Fixed division by zero errors in RAM calculations
- Created tmpfs-info.sh helper script for detailed status reporting
- Enhanced scanning to work on already-optimized systems
- Added comprehensive optimization breakdowns with purpose explanations
2025-09-23 12:11:45 +02:00

251 lines
7.1 KiB
Bash
Executable File

#!/bin/bash
# System Analyzer - Hardware Detection and Usage Analysis
# Part of Linux System Tuning Suite
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MODULES_DIR="$SCRIPT_DIR/modules"
ANALYSIS_DIR="/tmp/system-analysis-$(date +%Y%m%d-%H%M%S)"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log() {
echo -e "${BLUE}[$(date '+%H:%M:%S')]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
}
success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
# Check if running as root
check_root() {
if [[ $EUID -ne 0 ]]; then
error "This script must be run as root for complete analysis"
echo "Usage: sudo $0"
exit 1
fi
}
# Create analysis directory
setup_analysis_dir() {
mkdir -p "$ANALYSIS_DIR"
log "Analysis directory created: $ANALYSIS_DIR"
}
# Load modules
load_modules() {
for module in "$MODULES_DIR"/*.sh; do
if [[ -f "$module" ]]; then
source "$module"
log "Loaded module: $(basename "$module")"
fi
done
}
# Main analysis function
run_analysis() {
log "Starting comprehensive system analysis..."
echo "=== SYSTEM ANALYSIS REPORT ===" > "$ANALYSIS_DIR/report.txt"
echo "Generated: $(date)" >> "$ANALYSIS_DIR/report.txt"
echo "Hostname: $(hostname)" >> "$ANALYSIS_DIR/report.txt"
echo "" >> "$ANALYSIS_DIR/report.txt"
# Hardware detection
log "Analyzing hardware configuration..."
detect_hardware > "$ANALYSIS_DIR/hardware.json"
# Memory analysis
log "Analyzing memory usage patterns..."
analyze_memory > "$ANALYSIS_DIR/memory.json"
# Storage analysis
log "Analyzing storage configuration..."
analyze_storage > "$ANALYSIS_DIR/storage.json"
# Usage pattern analysis
log "Analyzing file access patterns (this may take a moment)..."
analyze_usage_patterns > "$ANALYSIS_DIR/usage.json"
# Current optimizations
log "Checking existing optimizations..."
check_current_optimizations > "$ANALYSIS_DIR/current.json"
# Generate recommendations
log "Generating optimization recommendations..."
generate_recommendations > "$ANALYSIS_DIR/recommendations.json"
success "Analysis complete! Results saved to: $ANALYSIS_DIR"
}
# Display summary
show_summary() {
echo ""
echo "=== ANALYSIS SUMMARY ==="
local ram_gb=$(free -g | awk '/^Mem:/{print $2}')
local cpu_cores=$(nproc)
local disk_type=$(lsblk -d -o name,rota | awk '$2==0{print "SSD"} $2==1{print "HDD"}' | head -1)
echo "💻 Hardware Overview:"
echo " RAM: ${ram_gb}GB"
echo " CPU Cores: $cpu_cores"
echo " Primary Storage: ${disk_type:-Unknown}"
if [[ -f "$ANALYSIS_DIR/recommendations.json" ]]; then
echo ""
echo "🎯 Top Recommendations:"
jq -r '.recommendations[] | " • " + .description' "$ANALYSIS_DIR/recommendations.json" 2>/dev/null | head -5 || {
echo " Run ./tune-system.sh --analyze for detailed recommendations"
}
fi
echo ""
echo "📊 Next Steps:"
echo " 1. Review full report: cat $ANALYSIS_DIR/report.txt"
echo " 2. Apply optimizations: sudo ./tune-system.sh --auto"
echo " 3. Monitor performance: ./monitor.sh"
}
main() {
check_root
setup_analysis_dir
# Load modules if they exist
if [[ -d "$MODULES_DIR" ]]; then
load_modules
else
warn "Modules directory not found. Using built-in functions."
fi
run_analysis
show_summary
}
# Built-in analysis functions (if modules not available)
detect_hardware() {
cat << EOF
{
"ram": {
"total_gb": $(free -g | awk '/^Mem:/{print $2}'),
"total_bytes": $(free -b | awk '/^Mem:/{print $2}'),
"available_gb": $(free -g | awk '/^Mem:/{print $7}'),
"used_percent": $(free | awk '/^Mem:/{printf "%.1f", $3/$2*100}')
},
"cpu": {
"cores": $(nproc),
"model": "$(lscpu | grep 'Model name:' | sed 's/Model name:[[:space:]]*//' | tr -d '\n')",
"architecture": "$(uname -m)"
},
"storage": $(lsblk -J -o NAME,SIZE,TYPE,MOUNTPOINT,ROTA 2>/dev/null || echo '{"blockdevices":[]}')
}
EOF
}
analyze_memory() {
cat << EOF
{
"current_usage": $(free -j 2>/dev/null || free | awk '/^Mem:/{printf "{\"total\":%s,\"used\":%s,\"free\":%s,\"available\":%s}", $2, $3, $4, $7}'),
"swap": $(free | awk '/^Swap:/{printf "{\"total\":%s,\"used\":%s,\"free\":%s}", $2, $3, $4}'),
"tmpfs_mounts": $(df -t tmpfs --output=target,size,used,avail | tail -n +2 | jq -R -s 'split("\n") | map(select(length > 0)) | map(split(" ") | {target: .[0], size: .[1], used: .[2], available: .[3]})')
}
EOF
}
analyze_storage() {
cat << EOF
{
"filesystems": $(df -h --output=source,fstype,size,used,avail,pcent,target | tail -n +2 | jq -R -s 'split("\n") | map(select(length > 0)) | map(split(" ") | select(length >= 7) | {device: .[0], type: .[1], size: .[2], used: .[3], available: .[4], percent: .[5], mount: .[6]})'),
"block_devices": $(lsblk -J 2>/dev/null || echo '{"blockdevices":[]}')
}
EOF
}
analyze_usage_patterns() {
# Analyze frequently accessed directories/files
cat << EOF
{
"large_cache_dirs": [
$(find /home -name ".cache" -type d 2>/dev/null | while read dir; do
size=$(du -sh "$dir" 2>/dev/null | cut -f1 || echo "0")
echo " {\"path\": \"$dir\", \"size\": \"$size\"},"
done | sed '$s/,$//')
],
"browser_profiles": [
$(find /home -path "*/.mozilla/firefox/*/storage" -o -path "*/.config/google-chrome/*/storage*" -o -path "*/.config/chromium/*/storage*" 2>/dev/null | while read dir; do
size=$(du -sh "$(dirname "$dir")" 2>/dev/null | cut -f1 || echo "0")
echo " {\"path\": \"$(dirname "$dir")\", \"type\": \"browser\", \"size\": \"$size\"},"
done | sed '$s/,$//')
]
}
EOF
}
check_current_optimizations() {
cat << EOF
{
"zram": $(zramctl -J 2>/dev/null || echo '[]'),
"tmpfs_count": $(mount -t tmpfs | wc -l),
"kernel_params": {
"swappiness": $(cat /proc/sys/vm/swappiness),
"dirty_ratio": $(cat /proc/sys/vm/dirty_ratio),
"dirty_background_ratio": $(cat /proc/sys/vm/dirty_background_ratio),
"vfs_cache_pressure": $(cat /proc/sys/vm/vfs_cache_pressure)
}
}
EOF
}
generate_recommendations() {
local ram_gb=$(free -g | awk '/^Mem:/{print $2}')
local has_zram=$(zramctl 2>/dev/null | grep -q zram && echo "true" || echo "false")
cat << EOF
{
"recommendations": [
{
"priority": "high",
"category": "memory",
"description": "Configure zram compression (current: $has_zram)",
"command": "./tune-system.sh --enable-zram"
},
{
"priority": "high",
"category": "tmpfs",
"description": "Set up tmpfs for browser cache",
"command": "./tune-system.sh --enable-tmpfs-cache"
},
{
"priority": "medium",
"category": "kernel",
"description": "Optimize kernel parameters for desktop",
"command": "./tune-system.sh --tune-kernel"
}
],
"system_score": {
"current": 65,
"potential": 85,
"improvement": "+20 points"
}
}
EOF
}
# Run if called directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi