Files
Scripts/install.sh
Wiktor Olszewski 2c0000079b Add PC Anti-Freeze Monitor with enhanced features
- System protection script with custom enhancements and TUI interface
- Browser tab limiting and application-specific monitoring
- AI behavior learning and predictive analysis
- Terminal-based configuration interface
- Multi-distro installation support
2025-07-01 19:51:06 +02:00

407 lines
12 KiB
Bash
Executable File

#!/bin/bash
# PC Monitor Installation Script
# Installs and configures the PC Anti-Freeze Monitor
set -euo pipefail
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SERVICE_NAME="pc-monitor"
readonly SCRIPT_NAME="pc-monitor.sh"
readonly SERVICE_FILE="pc-monitor.service"
readonly CONFIG_FILE="/etc/pc-monitor.conf"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_header() {
echo -e "${BLUE}================================${NC}"
echo -e "${BLUE} PC Anti-Freeze Monitor Setup${NC}"
echo -e "${BLUE}================================${NC}"
}
# Check if running as root
check_root() {
if [[ $EUID -ne 0 ]]; then
print_error "This script must be run as root (use sudo)"
exit 1
fi
}
# Check if required files exist
check_files() {
if [[ ! -f "$SCRIPT_DIR/$SCRIPT_NAME" ]]; then
print_error "Script file $SCRIPT_NAME not found in $SCRIPT_DIR"
exit 1
fi
if [[ ! -f "$SCRIPT_DIR/$SERVICE_FILE" ]]; then
print_error "Service file $SERVICE_FILE not found in $SCRIPT_DIR"
exit 1
fi
}
# Install required packages
install_dependencies() {
print_status "Installing required packages..."
# Core required packages
local core_packages=("bc" "psmisc" "lm_sensors" "procps-ng" "coreutils")
# Enhanced feature packages (optional)
local optional_packages=("vnstat" "iotop" "jq" "paccache")
if command -v pacman >/dev/null 2>&1; then
print_status "Detected Arch Linux - using pacman"
# Install core packages
pacman -Sy --noconfirm "${core_packages[@]}" || {
print_error "Failed to install core dependencies"
exit 1
}
# Install optional packages (don't fail if some are missing)
for pkg in "${optional_packages[@]}"; do
if pacman -S --noconfirm --needed "$pkg" 2>/dev/null; then
print_status "Installed optional package: $pkg"
else
print_warning "Optional package $pkg not available (some features may be limited)"
fi
done
elif command -v apt >/dev/null 2>&1; then
print_status "Detected Debian/Ubuntu - using apt"
apt update
apt install -y bc psmisc lm-sensors procps coreutils || {
print_error "Failed to install core dependencies"
exit 1
}
# Optional packages for Debian/Ubuntu
apt install -y vnstat iotop jq 2>/dev/null || print_warning "Some optional packages not available"
elif command -v yum >/dev/null 2>&1; then
print_status "Detected RHEL/CentOS - using yum"
yum install -y bc psmisc lm_sensors procps-ng coreutils || {
print_error "Failed to install core dependencies"
exit 1
}
# Optional packages
yum install -y vnstat iotop jq 2>/dev/null || print_warning "Some optional packages not available"
elif command -v dnf >/dev/null 2>&1; then
print_status "Detected Fedora - using dnf"
dnf install -y bc psmisc lm_sensors procps-ng coreutils || {
print_error "Failed to install core dependencies"
exit 1
}
# Optional packages
dnf install -y vnstat iotop jq 2>/dev/null || print_warning "Some optional packages not available"
else
print_error "Package manager not detected. Please manually install:"
echo "Core: bc, psmisc, lm-sensors, procps, coreutils"
echo "Optional: vnstat, iotop, jq"
read -p "Continue anyway? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
# Check if core dependencies are available
local missing_deps=()
for cmd in bc ps free df top sensors; do
if ! command -v "$cmd" >/dev/null 2>&1; then
missing_deps+=("$cmd")
fi
done
if [[ ${#missing_deps[@]} -gt 0 ]]; then
print_error "Missing required dependencies: ${missing_deps[*]}"
print_error "Please install missing packages manually"
exit 1
fi
# Initialize sensors
if command -v sensors-detect >/dev/null 2>&1; then
print_status "Initializing hardware sensors..."
echo "yes" | sensors-detect >/dev/null 2>&1 || true
fi
}
# Create configuration file
create_config() {
if [[ ! -f "$CONFIG_FILE" ]]; then
print_status "Creating enhanced configuration file at $CONFIG_FILE"
cat > "$CONFIG_FILE" << 'EOF'
# PC Monitor Enhanced Configuration
# ===================================
# Basic Thresholds
CPU_THRESHOLD=85
MEMORY_THRESHOLD=90
TEMP_THRESHOLD=80
DISK_THRESHOLD=95
PROCESS_HANG_TIME=30
SWAP_THRESHOLD=80
LOAD_AVG_THRESHOLD=10
# Advanced Features
NETWORK_THRESHOLD=50
IO_THRESHOLD=100
BROWSER_TAB_LIMIT=20
AGGRESSIVE_ON_BROWSERS=true
AGGRESSIVE_CPU_THRESHOLD=75
AGGRESSIVE_MEMORY_THRESHOLD=80
# Application-Specific Thresholds
BROWSER_CPU_THRESHOLD=60
BROWSER_MEMORY_THRESHOLD=70
GAME_CPU_THRESHOLD=95
GAME_MEMORY_THRESHOLD=85
IDE_MEMORY_THRESHOLD=75
# Browser Tab Limits
FIREFOX_MAX_TABS=15
CHROME_MAX_TABS=15
EDGE_MAX_TABS=10
# Notification Settings
NOTIFICATION_TIMEOUT=5000
EOF
chmod 644 "$CONFIG_FILE"
print_status "Enhanced configuration file created successfully"
else
print_status "Configuration file already exists at $CONFIG_FILE"
fi
}
# Install script and TUI
install_script() {
print_status "Installing PC Monitor script and TUI..."
# Make script executable
chmod +x "$SCRIPT_DIR/$SCRIPT_NAME"
# Copy script to system location
cp "$SCRIPT_DIR/$SCRIPT_NAME" "/usr/local/bin/pc-monitor"
chmod +x "/usr/local/bin/pc-monitor"
# Install TUI if available
if [[ -f "$SCRIPT_DIR/pc-monitor-tui.sh" ]]; then
chmod +x "$SCRIPT_DIR/pc-monitor-tui.sh"
cp "$SCRIPT_DIR/pc-monitor-tui.sh" "/usr/local/bin/pc-monitor-tui"
print_status "TUI installed - run with 'pc-monitor-tui'"
fi
# Update service file with correct script path
sed -i "s|ExecStart=.*|ExecStart=/usr/local/bin/pc-monitor|" "$SCRIPT_DIR/$SERVICE_FILE"
print_status "Script and TUI installation completed"
}
# Install systemd service
install_service() {
print_status "Installing systemd service..."
# Copy service file
cp "$SCRIPT_DIR/$SERVICE_FILE" "/etc/systemd/system/$SERVICE_NAME.service"
# Reload systemd
systemctl daemon-reload
# Enable service
systemctl enable "$SERVICE_NAME.service"
print_status "Systemd service installed and enabled"
}
# Create log directory
create_log_directory() {
print_status "Setting up logging..."
# Ensure log file exists with proper permissions
touch "/var/log/pc-monitor.log"
chmod 644 "/var/log/pc-monitor.log"
# Create logrotate configuration
cat > "/etc/logrotate.d/pc-monitor" << 'EOF'
/var/log/pc-monitor.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 644 root root
postrotate
systemctl reload pc-monitor.service >/dev/null 2>&1 || true
endscript
}
EOF
print_status "Logging configuration completed"
}
# Test installation
test_installation() {
print_status "Testing installation..."
# Test script syntax
if bash -n "$SCRIPT_DIR/$SCRIPT_NAME"; then
print_status "Script syntax is valid"
else
print_error "Script syntax error detected"
exit 1
fi
# Test service file
if systemctl status "$SERVICE_NAME.service" >/dev/null 2>&1; then
print_status "Service is active"
else
print_status "Service is ready to start"
fi
print_status "Installation test completed"
}
# Start service
start_service() {
print_status "Starting PC Monitor service..."
if systemctl start "$SERVICE_NAME.service"; then
print_status "Service started successfully"
sleep 2
if systemctl is-active --quiet "$SERVICE_NAME.service"; then
print_status "Service is running properly"
else
print_error "Service failed to start properly"
systemctl status "$SERVICE_NAME.service"
exit 1
fi
else
print_error "Failed to start service"
exit 1
fi
}
# Show status
show_status() {
echo
print_status "Installation Summary:"
echo " ✓ Script installed: $SCRIPT_DIR/$SCRIPT_NAME"
echo " ✓ Service installed: /etc/systemd/system/$SERVICE_NAME.service"
echo " ✓ Configuration: $CONFIG_FILE"
echo " ✓ Log file: /var/log/pc-monitor.log"
echo
print_status "Service Status:"
systemctl status "$SERVICE_NAME.service" --no-pager --lines=5
echo
print_status "Useful Commands:"
echo " • Check status: systemctl status $SERVICE_NAME"
echo " • View logs: journalctl -u $SERVICE_NAME -f"
echo " • View monitor log: tail -f /var/log/pc-monitor.log"
echo " • Stop service: systemctl stop $SERVICE_NAME"
echo " • Restart service: systemctl restart $SERVICE_NAME"
echo " • Edit config: nano $CONFIG_FILE"
echo
print_status "🛡️ Your PC is now protected with enhanced AI-powered monitoring!"
echo
print_status "New Features Added:"
echo " ✓ Browser tab limiting (Firefox, Chrome, Edge)"
echo " ✓ Application-specific aggressive parameters"
echo " ✓ Network and I/O monitoring"
echo " ✓ AI-powered process behavior learning"
echo " ✓ Predictive resource trend analysis"
echo " ✓ Smart process prioritization"
echo " ✓ Terminal User Interface (TUI)"
echo
print_status "TUI Control Interface:"
echo " • Command line: pc-monitor-tui"
echo " • Features: Settings, Service Control, System Status, Logs"
echo " • No GUI dependencies required"
echo
}
# Uninstall function
uninstall() {
print_status "Uninstalling PC Monitor..."
# Stop and disable service
systemctl stop "$SERVICE_NAME.service" 2>/dev/null || true
systemctl disable "$SERVICE_NAME.service" 2>/dev/null || true
# Remove service file
rm -f "/etc/systemd/system/$SERVICE_NAME.service"
# Remove configuration and logs
rm -f "$CONFIG_FILE"
rm -f "/var/log/pc-monitor.log"
rm -f "/etc/logrotate.d/pc-monitor"
# Reload systemd
systemctl daemon-reload
print_status "PC Monitor uninstalled successfully"
}
# Main installation function
main() {
print_header
case "${1:-install}" in
"install")
check_root
check_files
install_dependencies
create_config
install_script
install_service
create_log_directory
test_installation
start_service
show_status
;;
"uninstall")
check_root
uninstall
;;
"status")
systemctl status "$SERVICE_NAME.service" --no-pager
;;
"logs")
journalctl -u "$SERVICE_NAME.service" -f
;;
*)
echo "Usage: $0 [install|uninstall|status|logs]"
echo " install - Install and start PC Monitor (default)"
echo " uninstall - Remove PC Monitor completely"
echo " status - Show service status"
echo " logs - Show live logs"
exit 1
;;
esac
}
# Run main function
main "$@"