Files
Scripts/pc-monitor-tui.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

589 lines
22 KiB
Bash
Executable File

#!/bin/bash
# PC Anti-Freeze Monitor - Text User Interface (TUI)
# A terminal-based configuration and control interface
set -euo pipefail
readonly CONFIG_FILE="/etc/pc-monitor.conf"
readonly LOG_FILE="/var/log/pc-monitor.log"
readonly SERVICE_NAME="pc-monitor"
# Colors for TUI
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
MAGENTA='\033[0;35m'
WHITE='\033[1;37m'
BOLD='\033[1m'
NC='\033[0m'
# Clear screen and position cursor
clear_screen() {
clear
tput cup 0 0
}
# Print header
print_header() {
echo -e "${BLUE}╔══════════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}${BOLD}${WHITE} PC Anti-Freeze Monitor - Control Center ${NC}${BLUE}${NC}"
echo -e "${BLUE}${WHITE} Enhanced AI-Powered Protection ${NC}${BLUE}${NC}"
echo -e "${BLUE}╚══════════════════════════════════════════════════════════════════════╝${NC}"
echo
}
# Print status line
print_status_line() {
local status_text="$1"
local color="$2"
echo -e "${color}${status_text}${NC}"
}
# Get service status
get_service_status() {
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo -e "${GREEN}ACTIVE${NC}"
else
echo -e "${RED}INACTIVE${NC}"
fi
}
# Load current configuration
load_config() {
# Default values
CPU_THRESHOLD=85
MEMORY_THRESHOLD=90
TEMP_THRESHOLD=80
DISK_THRESHOLD=95
PROCESS_HANG_TIME=30
SWAP_THRESHOLD=80
LOAD_AVG_THRESHOLD=10
NETWORK_THRESHOLD=50
IO_THRESHOLD=100
BROWSER_TAB_LIMIT=20
AGGRESSIVE_ON_BROWSERS="true"
AGGRESSIVE_CPU_THRESHOLD=75
AGGRESSIVE_MEMORY_THRESHOLD=80
BROWSER_CPU_THRESHOLD=60
BROWSER_MEMORY_THRESHOLD=70
GAME_CPU_THRESHOLD=95
GAME_MEMORY_THRESHOLD=85
IDE_MEMORY_THRESHOLD=75
FIREFOX_MAX_TABS=15
CHROME_MAX_TABS=15
EDGE_MAX_TABS=10
NOTIFICATION_TIMEOUT=5000
# Load from config file if exists
if [[ -f "$CONFIG_FILE" ]]; then
source "$CONFIG_FILE" 2>/dev/null || true
fi
}
# Save configuration
save_config() {
local temp_config="/tmp/pc-monitor-config.tmp"
cat > "$temp_config" << EOF
# PC Monitor Enhanced Configuration
# ===================================
# Basic Thresholds
CPU_THRESHOLD=$CPU_THRESHOLD
MEMORY_THRESHOLD=$MEMORY_THRESHOLD
TEMP_THRESHOLD=$TEMP_THRESHOLD
DISK_THRESHOLD=$DISK_THRESHOLD
PROCESS_HANG_TIME=$PROCESS_HANG_TIME
SWAP_THRESHOLD=$SWAP_THRESHOLD
LOAD_AVG_THRESHOLD=$LOAD_AVG_THRESHOLD
# Advanced Features
NETWORK_THRESHOLD=$NETWORK_THRESHOLD
IO_THRESHOLD=$IO_THRESHOLD
BROWSER_TAB_LIMIT=$BROWSER_TAB_LIMIT
AGGRESSIVE_ON_BROWSERS=$AGGRESSIVE_ON_BROWSERS
AGGRESSIVE_CPU_THRESHOLD=$AGGRESSIVE_CPU_THRESHOLD
AGGRESSIVE_MEMORY_THRESHOLD=$AGGRESSIVE_MEMORY_THRESHOLD
# Application-Specific Thresholds
BROWSER_CPU_THRESHOLD=$BROWSER_CPU_THRESHOLD
BROWSER_MEMORY_THRESHOLD=$BROWSER_MEMORY_THRESHOLD
GAME_CPU_THRESHOLD=$GAME_CPU_THRESHOLD
GAME_MEMORY_THRESHOLD=$GAME_MEMORY_THRESHOLD
IDE_MEMORY_THRESHOLD=$IDE_MEMORY_THRESHOLD
# Browser Tab Limits
FIREFOX_MAX_TABS=$FIREFOX_MAX_TABS
CHROME_MAX_TABS=$CHROME_MAX_TABS
EDGE_MAX_TABS=$EDGE_MAX_TABS
# Notification Settings
NOTIFICATION_TIMEOUT=$NOTIFICATION_TIMEOUT
EOF
if sudo cp "$temp_config" "$CONFIG_FILE"; then
print_status_line "Configuration saved successfully!" "$GREEN"
rm -f "$temp_config"
else
print_status_line "Failed to save configuration!" "$RED"
rm -f "$temp_config"
return 1
fi
}
# Main menu
show_main_menu() {
clear_screen
print_header
local service_status=$(get_service_status)
echo -e "${BOLD}Service Status:${NC} $service_status"
echo
echo -e "${CYAN}┌─────────────────────────────────────────────────────────────────────┐${NC}"
echo -e "${CYAN}${BOLD}${WHITE} MAIN MENU ${NC}${CYAN}${NC}"
echo -e "${CYAN}├─────────────────────────────────────────────────────────────────────┤${NC}"
echo -e "${CYAN}${NC} ${BOLD}1.${NC} Basic Settings ${CYAN}${NC} ${BOLD}7.${NC} System Status ${CYAN}${NC}"
echo -e "${CYAN}${NC} ${BOLD}2.${NC} Advanced Settings ${CYAN}${NC} ${BOLD}8.${NC} View Logs ${CYAN}${NC}"
echo -e "${CYAN}${NC} ${BOLD}3.${NC} Browser Control ${CYAN}${NC} ${BOLD}9.${NC} Service Control ${CYAN}${NC}"
echo -e "${CYAN}${NC} ${BOLD}4.${NC} App-Specific Settings ${CYAN}${NC} ${BOLD}10.${NC} Test Notification ${CYAN}${NC}"
echo -e "${CYAN}${NC} ${BOLD}5.${NC} Configuration Export ${CYAN}${NC} ${BOLD}11.${NC} Reset to Defaults ${CYAN}${NC}"
echo -e "${CYAN}${NC} ${BOLD}6.${NC} Configuration Import ${CYAN}${NC} ${BOLD}0.${NC} Exit ${CYAN}${NC}"
echo -e "${CYAN}└─────────────────────────────────────────────────────────────────────┘${NC}"
echo
echo -n -e "${BOLD}Select option (0-11): ${NC}"
}
# Basic settings menu
show_basic_settings() {
while true; do
clear_screen
print_header
echo -e "${YELLOW}Basic Monitoring Thresholds${NC}"
echo -e "${CYAN}═══════════════════════════${NC}"
echo
echo -e " ${BOLD}1.${NC} CPU Threshold: ${GREEN}${CPU_THRESHOLD}%${NC}"
echo -e " ${BOLD}2.${NC} Memory Threshold: ${GREEN}${MEMORY_THRESHOLD}%${NC}"
echo -e " ${BOLD}3.${NC} Temperature Threshold: ${GREEN}${TEMP_THRESHOLD}°C${NC}"
echo -e " ${BOLD}4.${NC} Disk Threshold: ${GREEN}${DISK_THRESHOLD}%${NC}"
echo -e " ${BOLD}5.${NC} Swap Threshold: ${GREEN}${SWAP_THRESHOLD}%${NC}"
echo -e " ${BOLD}6.${NC} Load Average Threshold: ${GREEN}${LOAD_AVG_THRESHOLD}${NC}"
echo -e " ${BOLD}7.${NC} Process Hang Time: ${GREEN}${PROCESS_HANG_TIME}s${NC}"
echo -e " ${BOLD}8.${NC} Save Changes"
echo -e " ${BOLD}0.${NC} Back to Main Menu"
echo
echo -n -e "${BOLD}Select option: ${NC}"
read -r choice
case "$choice" in
1) read -p "Enter CPU threshold (50-100): " CPU_THRESHOLD ;;
2) read -p "Enter Memory threshold (50-100): " MEMORY_THRESHOLD ;;
3) read -p "Enter Temperature threshold (60-100): " TEMP_THRESHOLD ;;
4) read -p "Enter Disk threshold (80-99): " DISK_THRESHOLD ;;
5) read -p "Enter Swap threshold (50-100): " SWAP_THRESHOLD ;;
6) read -p "Enter Load Average threshold (1-20): " LOAD_AVG_THRESHOLD ;;
7) read -p "Enter Process hang time in seconds (10-300): " PROCESS_HANG_TIME ;;
8) save_config; read -p "Press Enter to continue..." ;;
0) return ;;
*) echo "Invalid option!" ;;
esac
done
}
# Advanced settings menu
show_advanced_settings() {
while true; do
clear_screen
print_header
echo -e "${YELLOW}Advanced Monitoring Features${NC}"
echo -e "${CYAN}════════════════════════════${NC}"
echo
echo -e " ${BOLD}1.${NC} Network Threshold: ${GREEN}${NETWORK_THRESHOLD} MB/s${NC}"
echo -e " ${BOLD}2.${NC} I/O Threshold: ${GREEN}${IO_THRESHOLD} MB/s${NC}"
echo -e " ${BOLD}3.${NC} Aggressive Browser Mode: ${GREEN}${AGGRESSIVE_ON_BROWSERS}${NC}"
echo -e " ${BOLD}4.${NC} Aggressive CPU Threshold: ${GREEN}${AGGRESSIVE_CPU_THRESHOLD}%${NC}"
echo -e " ${BOLD}5.${NC} Aggressive Memory Threshold: ${GREEN}${AGGRESSIVE_MEMORY_THRESHOLD}%${NC}"
echo -e " ${BOLD}6.${NC} Notification Timeout: ${GREEN}${NOTIFICATION_TIMEOUT}ms${NC}"
echo -e " ${BOLD}7.${NC} Save Changes"
echo -e " ${BOLD}0.${NC} Back to Main Menu"
echo
echo -n -e "${BOLD}Select option: ${NC}"
read -r choice
case "$choice" in
1) read -p "Enter Network threshold (10-500 MB/s): " NETWORK_THRESHOLD ;;
2) read -p "Enter I/O threshold (10-500 MB/s): " IO_THRESHOLD ;;
3)
echo "Current: $AGGRESSIVE_ON_BROWSERS"
read -p "Enable aggressive browser mode? (true/false): " AGGRESSIVE_ON_BROWSERS
;;
4) read -p "Enter Aggressive CPU threshold (50-95): " AGGRESSIVE_CPU_THRESHOLD ;;
5) read -p "Enter Aggressive Memory threshold (50-95): " AGGRESSIVE_MEMORY_THRESHOLD ;;
6) read -p "Enter Notification timeout (1000-10000 ms): " NOTIFICATION_TIMEOUT ;;
7) save_config; read -p "Press Enter to continue..." ;;
0) return ;;
*) echo "Invalid option!" ;;
esac
done
}
# Browser control menu
show_browser_settings() {
while true; do
clear_screen
print_header
echo -e "${YELLOW}Browser Tab Control${NC}"
echo -e "${CYAN}═══════════════════${NC}"
echo
echo -e " ${BOLD}1.${NC} Global Browser Tab Limit: ${GREEN}${BROWSER_TAB_LIMIT}${NC}"
echo -e " ${BOLD}2.${NC} Firefox Max Tabs: ${GREEN}${FIREFOX_MAX_TABS}${NC}"
echo -e " ${BOLD}3.${NC} Chrome Max Tabs: ${GREEN}${CHROME_MAX_TABS}${NC}"
echo -e " ${BOLD}4.${NC} Edge Max Tabs: ${GREEN}${EDGE_MAX_TABS}${NC}"
echo -e " ${BOLD}5.${NC} Browser CPU Threshold: ${GREEN}${BROWSER_CPU_THRESHOLD}%${NC}"
echo -e " ${BOLD}6.${NC} Browser Memory Threshold: ${GREEN}${BROWSER_MEMORY_THRESHOLD}%${NC}"
echo -e " ${BOLD}7.${NC} Save Changes"
echo -e " ${BOLD}0.${NC} Back to Main Menu"
echo
echo -n -e "${BOLD}Select option: ${NC}"
read -r choice
case "$choice" in
1) read -p "Enter global browser tab limit (5-50): " BROWSER_TAB_LIMIT ;;
2) read -p "Enter Firefox max tabs (5-30): " FIREFOX_MAX_TABS ;;
3) read -p "Enter Chrome max tabs (5-30): " CHROME_MAX_TABS ;;
4) read -p "Enter Edge max tabs (5-30): " EDGE_MAX_TABS ;;
5) read -p "Enter Browser CPU threshold (30-90): " BROWSER_CPU_THRESHOLD ;;
6) read -p "Enter Browser Memory threshold (30-90): " BROWSER_MEMORY_THRESHOLD ;;
7) save_config; read -p "Press Enter to continue..." ;;
0) return ;;
*) echo "Invalid option!" ;;
esac
done
}
# App-specific settings menu
show_app_settings() {
while true; do
clear_screen
print_header
echo -e "${YELLOW}Application-Specific Thresholds${NC}"
echo -e "${CYAN}═══════════════════════════════${NC}"
echo
echo -e " ${BOLD}1.${NC} Game CPU Threshold: ${GREEN}${GAME_CPU_THRESHOLD}%${NC}"
echo -e " ${BOLD}2.${NC} Game Memory Threshold: ${GREEN}${GAME_MEMORY_THRESHOLD}%${NC}"
echo -e " ${BOLD}3.${NC} IDE Memory Threshold: ${GREEN}${IDE_MEMORY_THRESHOLD}%${NC}"
echo -e " ${BOLD}4.${NC} Save Changes"
echo -e " ${BOLD}0.${NC} Back to Main Menu"
echo
echo -e "${CYAN}Note: Games have higher thresholds (less aggressive)${NC}"
echo -e "${CYAN} IDEs have moderate thresholds for development work${NC}"
echo
echo -n -e "${BOLD}Select option: ${NC}"
read -r choice
case "$choice" in
1) read -p "Enter Game CPU threshold (70-100): " GAME_CPU_THRESHOLD ;;
2) read -p "Enter Game Memory threshold (70-100): " GAME_MEMORY_THRESHOLD ;;
3) read -p "Enter IDE Memory threshold (50-90): " IDE_MEMORY_THRESHOLD ;;
4) save_config; read -p "Press Enter to continue..." ;;
0) return ;;
*) echo "Invalid option!" ;;
esac
done
}
# System status display
show_system_status() {
clear_screen
print_header
echo -e "${YELLOW}Real-Time System Status${NC}"
echo -e "${CYAN}═══════════════════════${NC}"
echo
# Service status
echo -e "${BOLD}Service Status:${NC}"
systemctl status "$SERVICE_NAME" --no-pager -l | head -10
echo
# System resources
echo -e "${BOLD}System Resources:${NC}"
echo -n "CPU: "
top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//'
echo -n "Memory: "
free -h | grep '^Mem:' | awk '{printf "%s / %s (%.1f%%)\n", $3, $2, ($3/$2)*100}'
echo -n "Disk: "
df -h / | tail -1 | awk '{printf "%s / %s (%s)\n", $3, $2, $5}'
# Temperature
if command -v sensors >/dev/null 2>&1; then
echo -e "${BOLD}Temperature:${NC}"
sensors | grep -E "(Core|Package|CPU)" | head -3
fi
echo
read -p "Press Enter to return to main menu..."
}
# View logs
show_logs() {
clear_screen
print_header
echo -e "${YELLOW}Recent Monitor Logs${NC}"
echo -e "${CYAN}═══════════════════${NC}"
echo
if [[ -f "$LOG_FILE" ]]; then
echo -e "${BOLD}Last 20 entries:${NC}"
sudo tail -20 "$LOG_FILE" 2>/dev/null || echo "Cannot read log file"
else
echo "Log file not found"
fi
echo
echo -e "${BOLD}Service Logs (last 10 entries):${NC}"
journalctl -u "$SERVICE_NAME" --no-pager -n 10 2>/dev/null || echo "Cannot read service logs"
echo
read -p "Press Enter to return to main menu..."
}
# Service control menu
show_service_control() {
while true; do
clear_screen
print_header
local service_status=$(get_service_status)
echo -e "${BOLD}Service Status:${NC} $service_status"
echo
echo -e "${YELLOW}Service Control${NC}"
echo -e "${CYAN}═══════════════${NC}"
echo
echo -e " ${BOLD}1.${NC} Start Service"
echo -e " ${BOLD}2.${NC} Stop Service"
echo -e " ${BOLD}3.${NC} Restart Service"
echo -e " ${BOLD}4.${NC} Enable Auto-start"
echo -e " ${BOLD}5.${NC} Disable Auto-start"
echo -e " ${BOLD}6.${NC} View Service Status"
echo -e " ${BOLD}0.${NC} Back to Main Menu"
echo
echo -n -e "${BOLD}Select option: ${NC}"
read -r choice
case "$choice" in
1)
echo "Starting service..."
if sudo systemctl start "$SERVICE_NAME"; then
print_status_line "Service started successfully!" "$GREEN"
else
print_status_line "Failed to start service!" "$RED"
fi
read -p "Press Enter to continue..."
;;
2)
echo "Stopping service..."
if sudo systemctl stop "$SERVICE_NAME"; then
print_status_line "Service stopped successfully!" "$GREEN"
else
print_status_line "Failed to stop service!" "$RED"
fi
read -p "Press Enter to continue..."
;;
3)
echo "Restarting service..."
if sudo systemctl restart "$SERVICE_NAME"; then
print_status_line "Service restarted successfully!" "$GREEN"
else
print_status_line "Failed to restart service!" "$RED"
fi
read -p "Press Enter to continue..."
;;
4)
echo "Enabling auto-start..."
if sudo systemctl enable "$SERVICE_NAME"; then
print_status_line "Auto-start enabled!" "$GREEN"
else
print_status_line "Failed to enable auto-start!" "$RED"
fi
read -p "Press Enter to continue..."
;;
5)
echo "Disabling auto-start..."
if sudo systemctl disable "$SERVICE_NAME"; then
print_status_line "Auto-start disabled!" "$GREEN"
else
print_status_line "Failed to disable auto-start!" "$RED"
fi
read -p "Press Enter to continue..."
;;
6)
systemctl status "$SERVICE_NAME" --no-pager
read -p "Press Enter to continue..."
;;
0) return ;;
*) echo "Invalid option!" ;;
esac
done
}
# Test notification
test_notification() {
clear_screen
print_header
echo -e "${YELLOW}Testing Notification System${NC}"
echo -e "${CYAN}═══════════════════════════${NC}"
echo
if command -v notify-send >/dev/null 2>&1; then
notify-send --urgency=normal --expire-time=3000 \
"PC Monitor TUI" "Test notification from PC Monitor control interface"
print_status_line "Test notification sent!" "$GREEN"
else
print_status_line "notify-send not available" "$RED"
fi
echo
read -p "Press Enter to return to main menu..."
}
# Export configuration
export_config() {
clear_screen
print_header
echo -e "${YELLOW}Export Configuration${NC}"
echo -e "${CYAN}════════════════════${NC}"
echo
read -p "Enter export filename (e.g., my-config.conf): " filename
if [[ -n "$filename" ]]; then
if cp "$CONFIG_FILE" "$filename" 2>/dev/null; then
print_status_line "Configuration exported to $filename" "$GREEN"
else
print_status_line "Failed to export configuration" "$RED"
fi
fi
echo
read -p "Press Enter to return to main menu..."
}
# Import configuration
import_config() {
clear_screen
print_header
echo -e "${YELLOW}Import Configuration${NC}"
echo -e "${CYAN}════════════════════${NC}"
echo
read -p "Enter import filename: " filename
if [[ -f "$filename" ]]; then
if sudo cp "$filename" "$CONFIG_FILE"; then
print_status_line "Configuration imported from $filename" "$GREEN"
print_status_line "Restarting service to apply changes..." "$YELLOW"
sudo systemctl restart "$SERVICE_NAME" 2>/dev/null || true
load_config # Reload in TUI
else
print_status_line "Failed to import configuration" "$RED"
fi
else
print_status_line "File not found: $filename" "$RED"
fi
echo
read -p "Press Enter to return to main menu..."
}
# Reset to defaults
reset_to_defaults() {
clear_screen
print_header
echo -e "${YELLOW}Reset to Default Configuration${NC}"
echo -e "${CYAN}══════════════════════════════${NC}"
echo
echo -e "${RED}Warning: This will reset all settings to defaults!${NC}"
read -p "Are you sure? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Reset variables to defaults
CPU_THRESHOLD=85
MEMORY_THRESHOLD=90
TEMP_THRESHOLD=80
DISK_THRESHOLD=95
PROCESS_HANG_TIME=30
SWAP_THRESHOLD=80
LOAD_AVG_THRESHOLD=10
NETWORK_THRESHOLD=50
IO_THRESHOLD=100
BROWSER_TAB_LIMIT=20
AGGRESSIVE_ON_BROWSERS="true"
AGGRESSIVE_CPU_THRESHOLD=75
AGGRESSIVE_MEMORY_THRESHOLD=80
BROWSER_CPU_THRESHOLD=60
BROWSER_MEMORY_THRESHOLD=70
GAME_CPU_THRESHOLD=95
GAME_MEMORY_THRESHOLD=85
IDE_MEMORY_THRESHOLD=75
FIREFOX_MAX_TABS=15
CHROME_MAX_TABS=15
EDGE_MAX_TABS=10
NOTIFICATION_TIMEOUT=5000
save_config
print_status_line "Configuration reset to defaults!" "$GREEN"
else
print_status_line "Reset cancelled." "$YELLOW"
fi
echo
read -p "Press Enter to return to main menu..."
}
# Main program loop
main() {
# Check if running with proper permissions
if [[ ! -r "$CONFIG_FILE" ]] && [[ $EUID -ne 0 ]]; then
echo -e "${RED}Warning: Some features may require sudo privileges${NC}"
echo
fi
load_config
while true; do
show_main_menu
read -r choice
case "$choice" in
1) show_basic_settings ;;
2) show_advanced_settings ;;
3) show_browser_settings ;;
4) show_app_settings ;;
5) export_config ;;
6) import_config ;;
7) show_system_status ;;
8) show_logs ;;
9) show_service_control ;;
10) test_notification ;;
11) reset_to_defaults ;;
0)
clear_screen
echo -e "${GREEN}Thank you for using PC Monitor TUI!${NC}"
echo -e "${CYAN}Your system is protected with advanced monitoring.${NC}"
echo
exit 0
;;
*)
echo -e "${RED}Invalid option! Please try again.${NC}"
sleep 1
;;
esac
done
}
# Run main function
main "$@"