Files
Linux-Server-Management-Too…/lib/system-detect.sh
T
cschantz a51d968185 Initial commit: Server Management Toolkit v2.0
- Complete security menu restructure (3-mode: Analysis/Actions/Live)
- Intelligent cPHulk enablement with CSF whitelist import
- Live network security monitoring dashboard
- Multi-source threat detection and classification
- 50+ organized security tools across 4-level menu hierarchy
- System health diagnostics with cPanel/WHM integration
- Reference database for cross-module intelligence sharing
2025-11-03 18:21:40 -05:00

438 lines
14 KiB
Bash
Executable File

#!/bin/bash
#############################################################################
# System Detection Library
# Runtime detection of control panels, OS, paths, and system resources
# No persistent caching - detects fresh every time
#############################################################################
# Source common functions if not already loaded
if [ -z "$TOOLKIT_BASE_DIR" ]; then
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/common-functions.sh"
fi
# Global variables (session-only)
export SYS_CONTROL_PANEL=""
export SYS_CONTROL_PANEL_VERSION=""
export SYS_OS_TYPE=""
export SYS_OS_VERSION=""
export SYS_WEB_SERVER=""
export SYS_WEB_SERVER_VERSION=""
export SYS_DB_TYPE=""
export SYS_DB_VERSION=""
export SYS_LOG_DIR=""
export SYS_USER_HOME_BASE=""
export SYS_PHP_VERSIONS=()
export SYS_CLOUDFLARE_ACTIVE=""
#############################################################################
# CONTROL PANEL DETECTION
#############################################################################
detect_control_panel() {
print_info "Detecting control panel..."
# cPanel
if [ -f "/usr/local/cpanel/version" ]; then
SYS_CONTROL_PANEL="cpanel"
SYS_CONTROL_PANEL_VERSION=$(cat /usr/local/cpanel/version)
SYS_LOG_DIR="/var/log/apache2/domlogs"
SYS_USER_HOME_BASE="/home"
# Alternative log locations for cPanel
[ ! -d "$SYS_LOG_DIR" ] && SYS_LOG_DIR="/usr/local/apache/domlogs"
print_success "Detected cPanel v${SYS_CONTROL_PANEL_VERSION}"
return 0
fi
# Plesk
if [ -f "/usr/local/psa/version" ]; then
SYS_CONTROL_PANEL="plesk"
SYS_CONTROL_PANEL_VERSION=$(cat /usr/local/psa/version | head -1)
SYS_LOG_DIR="/var/www/vhosts/system"
SYS_USER_HOME_BASE="/var/www/vhosts"
print_success "Detected Plesk v${SYS_CONTROL_PANEL_VERSION}"
return 0
fi
# InterWorx
if [ -d "/usr/local/interworx" ] || [ -f "/etc/interworx/iworx.ini" ]; then
SYS_CONTROL_PANEL="interworx"
if [ -f "/usr/local/interworx/iworx/version.php" ]; then
SYS_CONTROL_PANEL_VERSION=$(grep -oP "VERSION = '\K[^']+" /usr/local/interworx/iworx/version.php 2>/dev/null || echo "Unknown")
fi
SYS_LOG_DIR="/home"
SYS_USER_HOME_BASE="/home"
print_success "Detected InterWorx v${SYS_CONTROL_PANEL_VERSION}"
return 0
fi
# No control panel detected
SYS_CONTROL_PANEL="none"
SYS_LOG_DIR="/var/log/httpd"
[ ! -d "$SYS_LOG_DIR" ] && SYS_LOG_DIR="/var/log/apache2"
SYS_USER_HOME_BASE="/home"
print_warning "No control panel detected (standalone server)"
return 1
}
#############################################################################
# OPERATING SYSTEM DETECTION
#############################################################################
detect_os() {
print_info "Detecting operating system..."
if [ -f /etc/os-release ]; then
source /etc/os-release
SYS_OS_TYPE="$ID"
SYS_OS_VERSION="$VERSION_ID"
# Normalize OS names
case "$SYS_OS_TYPE" in
rhel|centos|almalinux|rocky|cloudlinux)
SYS_OS_TYPE=$(echo "$NAME" | awk '{print tolower($1)}')
;;
esac
print_success "Detected $NAME $VERSION_ID"
elif [ -f /etc/redhat-release ]; then
local release_info=$(cat /etc/redhat-release)
SYS_OS_TYPE="centos"
SYS_OS_VERSION=$(echo "$release_info" | grep -oP '\d+\.\d+' | head -1)
print_success "Detected $release_info"
else
SYS_OS_TYPE="unknown"
SYS_OS_VERSION="unknown"
print_warning "Could not detect OS"
fi
# Check for CloudLinux
if [ -f /etc/sysconfig/cloudlinux ] || [ -f /usr/bin/cloudlinux-config ]; then
print_info "CloudLinux detected"
export SYS_CLOUDLINUX="yes"
fi
}
#############################################################################
# WEB SERVER DETECTION
#############################################################################
detect_web_server() {
print_info "Detecting web server..."
# Apache
if command_exists httpd; then
SYS_WEB_SERVER="apache"
SYS_WEB_SERVER_VERSION=$(httpd -v 2>/dev/null | grep -oP 'Apache/\K[\d.]+' | head -1)
print_success "Detected Apache ${SYS_WEB_SERVER_VERSION}"
return 0
elif command_exists apache2; then
SYS_WEB_SERVER="apache"
SYS_WEB_SERVER_VERSION=$(apache2 -v 2>/dev/null | grep -oP 'Apache/\K[\d.]+' | head -1)
print_success "Detected Apache ${SYS_WEB_SERVER_VERSION}"
return 0
fi
# Nginx
if command_exists nginx; then
SYS_WEB_SERVER="nginx"
SYS_WEB_SERVER_VERSION=$(nginx -v 2>&1 | grep -oP 'nginx/\K[\d.]+')
print_success "Detected Nginx ${SYS_WEB_SERVER_VERSION}"
return 0
fi
# LiteSpeed
if [ -f /usr/local/lsws/bin/lswsctrl ]; then
SYS_WEB_SERVER="litespeed"
SYS_WEB_SERVER_VERSION=$(/usr/local/lsws/bin/lswsctrl version 2>/dev/null | grep -oP 'LiteSpeed/\K[\d.]+' | head -1)
print_success "Detected LiteSpeed ${SYS_WEB_SERVER_VERSION}"
return 0
fi
# OpenLiteSpeed
if command_exists /usr/local/lsws/bin/openlitespeed; then
SYS_WEB_SERVER="openlitespeed"
SYS_WEB_SERVER_VERSION=$(cat /usr/local/lsws/VERSION 2>/dev/null)
print_success "Detected OpenLiteSpeed ${SYS_WEB_SERVER_VERSION}"
return 0
fi
SYS_WEB_SERVER="unknown"
print_warning "Could not detect web server"
return 1
}
#############################################################################
# DATABASE DETECTION
#############################################################################
detect_database() {
print_info "Detecting database server..."
if command_exists mysql; then
local version_output=$(mysql --version 2>/dev/null)
if echo "$version_output" | grep -qi "mariadb"; then
SYS_DB_TYPE="mariadb"
SYS_DB_VERSION=$(echo "$version_output" | grep -oP '\d+\.\d+\.\d+' | head -1)
print_success "Detected MariaDB ${SYS_DB_VERSION}"
else
SYS_DB_TYPE="mysql"
SYS_DB_VERSION=$(echo "$version_output" | grep -oP '\d+\.\d+\.\d+' | head -1)
print_success "Detected MySQL ${SYS_DB_VERSION}"
fi
return 0
fi
SYS_DB_TYPE="none"
print_warning "No MySQL/MariaDB detected"
return 1
}
#############################################################################
# PHP VERSION DETECTION
#############################################################################
detect_php_versions() {
print_info "Detecting PHP versions..."
SYS_PHP_VERSIONS=()
# Check default PHP
if command_exists php; then
local default_version=$(php -v 2>/dev/null | grep -oP '^PHP \K[\d.]+' | head -1)
[ -n "$default_version" ] && SYS_PHP_VERSIONS+=("$default_version")
fi
# Check EA-PHP versions (cPanel)
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
for php_bin in /opt/cpanel/ea-php*/root/usr/bin/php; do
if [ -x "$php_bin" ]; then
local version=$($php_bin -v 2>/dev/null | grep -oP '^PHP \K[\d.]+' | head -1)
[ -n "$version" ] && SYS_PHP_VERSIONS+=("$version")
fi
done
fi
# Check alt-php versions (CloudLinux)
for php_bin in /opt/alt/php*/usr/bin/php; do
if [ -x "$php_bin" ]; then
local version=$($php_bin -v 2>/dev/null | grep -oP '^PHP \K[\d.]+' | head -1)
[ -n "$version" ] && SYS_PHP_VERSIONS+=("$version")
fi
done
# Remove duplicates
SYS_PHP_VERSIONS=($(echo "${SYS_PHP_VERSIONS[@]}" | tr ' ' '\n' | sort -u))
if [ ${#SYS_PHP_VERSIONS[@]} -gt 0 ]; then
print_success "Detected PHP versions: ${SYS_PHP_VERSIONS[*]}"
else
print_warning "No PHP installations detected"
fi
}
#############################################################################
# CLOUDFLARE DETECTION
#############################################################################
detect_cloudflare() {
SYS_CLOUDFLARE_ACTIVE="no"
# Check for mod_cloudflare
if [ "$SYS_WEB_SERVER" = "apache" ]; then
if httpd -M 2>/dev/null | grep -q cloudflare; then
SYS_CLOUDFLARE_ACTIVE="yes"
print_info "Cloudflare module detected"
fi
fi
# Check for railgun
if systemctl is-active --quiet railgun 2>/dev/null || service railgun status 2>/dev/null | grep -q running; then
SYS_CLOUDFLARE_ACTIVE="yes"
print_info "Cloudflare Railgun detected"
fi
}
#############################################################################
# SYSTEM RESOURCES (Comprehensive - like user's example)
#############################################################################
get_system_resources() {
local resources_file="${TEMP_SESSION_DIR}/system_resources.tmp"
# CPU Information
local cores=$(nproc)
local load=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | sed 's/,//')
local cpu_line=$(top -bn1 | grep "Cpu(s)")
local cpu_idle=$(echo "$cpu_line" | awk '{print $8}' | sed 's/%id,//')
local cpu_iowait=$(echo "$cpu_line" | awk '{print $10}' | sed 's/%wa,//')
local cpu_used=$(awk "BEGIN {printf \"%.1f\", 100-$cpu_idle}")
local load_percent=$(awk "BEGIN {printf \"%.0f\", ($load/$cores)*100}")
# Memory Information
local mem_total=$(free -h | awk '/^Mem:/ {print $2}')
local mem_used=$(free -h | awk '/^Mem:/ {print $3}')
local mem_percent=$(free | awk '/^Mem:/ {printf "%.0f", $3/$2*100}')
local mem_available=$(free -h | awk '/^Mem:/ {print $7}')
# Swap Information
local swap_total=$(free -h | awk '/^Swap:/ {print $2}')
local swap_used=$(free -h | awk '/^Swap:/ {print $3}')
local swap_percent=0
if [ "$swap_total" != "0B" ] && [ -n "$swap_total" ]; then
swap_percent=$(free | awk '/^Swap:/ {if($2>0) printf "%.0f", $3/$2*100; else print "0"}')
fi
# Disk Information
local disk_root_total=$(df -h / | awk 'NR==2 {print $2}')
local disk_root_used=$(df -h / | awk 'NR==2 {print $3}')
local disk_root_percent=$(df -h / | awk 'NR==2 {print $5}')
# Uptime
local uptime_str=$(uptime -p)
# Save to session file
cat > "$resources_file" << EOF
CPU_CORES=$cores
CPU_USED=$cpu_used
CPU_IOWAIT=$cpu_iowait
LOAD_AVERAGE=$load
LOAD_PERCENT=$load_percent
MEM_TOTAL=$mem_total
MEM_USED=$mem_used
MEM_PERCENT=$mem_percent
MEM_AVAILABLE=$mem_available
SWAP_TOTAL=$swap_total
SWAP_USED=$swap_used
SWAP_PERCENT=$swap_percent
DISK_ROOT_TOTAL=$disk_root_total
DISK_ROOT_USED=$disk_root_used
DISK_ROOT_PERCENT=$disk_root_percent
UPTIME=$uptime_str
EOF
# Also export for immediate use
export CPU_CORES=$cores
export CPU_USED=$cpu_used
export CPU_IOWAIT=$cpu_iowait
export LOAD_AVERAGE=$load
export LOAD_PERCENT=$load_percent
export MEM_TOTAL=$mem_total
export MEM_USED=$mem_used
export MEM_PERCENT=$mem_percent
}
#############################################################################
# DISPLAY FUNCTIONS
#############################################################################
show_system_info() {
echo ""
print_banner "System Information"
# Control Panel
echo -e "${BOLD}Control Panel:${NC}"
if [ "$SYS_CONTROL_PANEL" != "none" ]; then
echo " Type: $SYS_CONTROL_PANEL"
echo " Version: $SYS_CONTROL_PANEL_VERSION"
else
echo " Type: Standalone (no control panel)"
fi
echo ""
# Operating System
echo -e "${BOLD}Operating System:${NC}"
echo " OS: $SYS_OS_TYPE $SYS_OS_VERSION"
echo " Kernel: $(uname -r)"
[ "${SYS_CLOUDLINUX:-}" = "yes" ] && echo " CloudLinux: Enabled"
echo ""
# Web Server
echo -e "${BOLD}Web Server:${NC}"
echo " Type: $SYS_WEB_SERVER"
echo " Version: $SYS_WEB_SERVER_VERSION"
[ "$SYS_CLOUDFLARE_ACTIVE" = "yes" ] && echo " Cloudflare: Active"
echo ""
# Database
echo -e "${BOLD}Database:${NC}"
if [ "$SYS_DB_TYPE" != "none" ]; then
echo " Type: $SYS_DB_TYPE"
echo " Version: $SYS_DB_VERSION"
else
echo " Type: Not installed"
fi
echo ""
# PHP
echo -e "${BOLD}PHP Versions:${NC}"
if [ ${#SYS_PHP_VERSIONS[@]} -gt 0 ]; then
for version in "${SYS_PHP_VERSIONS[@]}"; do
echo " - PHP $version"
done
else
echo " None detected"
fi
echo ""
# System Resources
echo -e "${BOLD}System Resources:${NC}"
echo " CPU Cores: $CPU_CORES"
echo " CPU Usage: ${CPU_USED}% (actual)"
echo " I/O Wait: ${CPU_IOWAIT}%"
echo " Load Average: $LOAD_AVERAGE (${LOAD_PERCENT}% of core capacity)"
echo ""
echo " RAM Total: $MEM_TOTAL"
echo " RAM Used: $MEM_USED (${MEM_PERCENT}%)"
echo " RAM Available: $MEM_AVAILABLE"
echo ""
echo " Swap Total: $SWAP_TOTAL"
echo " Swap Used: $SWAP_USED (${SWAP_PERCENT}%)"
echo ""
echo " Disk (/) Total: $DISK_ROOT_TOTAL"
echo " Disk (/) Used: $DISK_ROOT_USED (${DISK_ROOT_PERCENT})"
echo ""
echo " Uptime: $UPTIME"
echo ""
# Paths
echo -e "${BOLD}Important Paths:${NC}"
echo " Log Directory: $SYS_LOG_DIR"
echo " User Home Base: $SYS_USER_HOME_BASE"
echo ""
}
#############################################################################
# INITIALIZE ALL DETECTION
#############################################################################
initialize_system_detection() {
# Create temp session directory
create_temp_session
# Run all detections
detect_control_panel
detect_os
detect_web_server
detect_database
detect_php_versions
detect_cloudflare
get_system_resources
# Mark as initialized
export SYS_DETECTION_COMPLETE="yes"
}
# Auto-initialize if not already done (when sourced)
if [ -z "${SYS_DETECTION_COMPLETE:-}" ]; then
# Just run initialization - output suppression was breaking variable assignment
initialize_system_detection
fi