| #!/bin/bash |
| |
|
|
| set -e |
|
|
| |
| RED='\033[0;31m' |
| GREEN='\033[0;32m' |
| YELLOW='\033[1;33m' |
| BLUE='\033[0;34m' |
| NC='\033[0m' |
|
|
| |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| INSTALL_DIR="/home/crhon/gpu_monitoring_system" |
| SERVICE_NAME="gpu-monitoring" |
| LOG_FILE="/var/log/gpu_monitoring_setup.log" |
|
|
| |
| log() { |
| echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" | tee -a "$LOG_FILE" |
| } |
|
|
| warn() { |
| echo -e "${YELLOW}[WARNING]${NC} $1" | tee -a "$LOG_FILE" |
| } |
|
|
| error() { |
| echo -e "${RED}[ERROR]${NC} $1" | tee -a "$LOG_FILE" |
| exit 1 |
| } |
|
|
| info() { |
| echo -e "${BLUE}[INFO]${NC} $1" | tee -a "$LOG_FILE" |
| } |
|
|
| |
| check_root() { |
| if [[ $EUID -eq 0 ]]; then |
| return 0 |
| else |
| return 1 |
| fi |
| } |
|
|
| |
| install_system_deps() { |
| log "Installing system dependencies..." |
| |
| |
| sudo apt update |
| |
| |
| sudo apt install -y \ |
| python3 \ |
| python3-pip \ |
| python3-venv \ |
| python3-pyqt5 \ |
| python3-pyqt5.qtopengl \ |
| python3-matplotlib \ |
| python3-flask \ |
| python3-flask-cors \ |
| python3-psutil \ |
| python3-numpy \ |
| python3-pandas \ |
| sqlite3 |
| |
| log "System dependencies installed successfully" |
| } |
|
|
| |
| setup_venv() { |
| log "Setting up Python virtual environment..." |
| |
| cd "$INSTALL_DIR" |
| |
| |
| python3 -m venv venv |
| |
| |
| source venv/bin/activate |
| |
| |
| pip install --upgrade pip |
| |
| |
| pip install -r requirements.txt |
| |
| log "Virtual environment setup complete" |
| } |
|
|
| |
| create_directories() { |
| log "Creating necessary directories..." |
| |
| mkdir -p "$INSTALL_DIR/data" |
| mkdir -p "$INSTALL_DIR/logs" |
| mkdir -p "$INSTALL_DIR/config" |
| mkdir -p "$INSTALL_DIR/backups" |
| mkdir -p "$INSTALL_DIR/scripts" |
| |
| log "Directories created successfully" |
| } |
|
|
| |
| setup_systemd_service() { |
| if ! check_root; then |
| warn "Systemd service installation requires root privileges. Skipping..." |
| return 1 |
| fi |
| |
| log "Setting up systemd service..." |
| |
| |
| sudo cp "$INSTALL_DIR/systemd/$SERVICE_NAME.service" "/etc/systemd/system/$SERVICE_NAME.service" |
| |
| |
| sudo systemctl daemon-reload |
| |
| |
| sudo systemctl enable "$SERVICE_NAME" |
| |
| log "Systemd service installed and enabled" |
| } |
|
|
| |
| setup_autostart() { |
| log "Setting up desktop autostart..." |
| |
| |
| mkdir -p "$HOME/.config/autostart" |
| |
| |
| cat > "$HOME/.config/autostart/gpu-monitor-overlay.desktop" << EOF |
| [Desktop Entry] |
| Type=Application |
| Name=GPU Monitor Overlay |
| Comment=GPU monitoring overlay for desktop |
| Exec=python3 $INSTALL_DIR/gpu_monitor_desktop.py --display overlay |
| Icon=video-display |
| Terminal=false |
| Hidden=false |
| NoDisplay=false |
| X-GNOME-Autostart-enabled=true |
| EOF |
| |
| |
| cat > "$HOME/.config/autostart/gpu-monitor-web.desktop" << EOF |
| [Desktop Entry] |
| Type=Application |
| Name=GPU Monitor Web |
| Comment=GPU monitoring web interface |
| Exec=python3 $INSTALL_DIR/web_interface.py |
| Icon=video-display |
| Terminal=false |
| Hidden=false |
| NoDisplay=false |
| X-GNOME-Autostart-enabled=true |
| EOF |
| |
| log "Desktop autostart configured" |
| } |
|
|
| |
| setup_permissions() { |
| if ! check_root; then |
| warn "Permission setup requires root privileges. Skipping..." |
| return 1 |
| fi |
| |
| log "Setting up permissions..." |
| |
| |
| sudo usermod -a -G video "$USER" |
| sudo usermod -a -G render "$USER" |
| |
| |
| sudo mkdir -p /var/log |
| sudo touch /var/log/gpu_fan_control.log |
| sudo chown "$USER":"$USER" /var/log/gpu_fan_control.log |
| sudo chmod 644 /var/log/gpu_fan_control.log |
| |
| log "Permissions configured" |
| } |
|
|
| |
| create_scripts() { |
| log "Creating convenience scripts..." |
| |
| |
| cat > "$INSTALL_DIR/scripts/start.sh" << 'EOF' |
| |
| cd "$(dirname "$0")/.." |
| source venv/bin/activate |
| python3 gpu_fan_controller.py --daemon |
| EOF |
| |
| |
| cat > "$INSTALL_DIR/scripts/stop.sh" << 'EOF' |
| |
| pkill -f gpu_fan_controller.py |
| EOF |
| |
| |
| cat > "$INSTALL_DIR/scripts/status.sh" << 'EOF' |
| |
| echo "=== GPU Monitoring System Status ===" |
| echo "Service Status:" |
| if systemctl is-active --quiet gpu-monitoring 2>/dev/null; then |
| echo " ✓ Service is running" |
| else |
| echo " ✗ Service is not running" |
| fi |
|
|
| echo "" |
| echo "Process Status:" |
| if pgrep -f gpu_fan_controller.py > /dev/null; then |
| echo " ✓ Fan controller process is running" |
| else |
| echo " ✗ Fan controller process is not running" |
| fi |
|
|
| echo "" |
| echo "Web Interface:" |
| if pgrep -f web_interface.py > /dev/null; then |
| echo " ✓ Web interface is running" |
| echo " URL: http://localhost:5000" |
| else |
| echo " ✗ Web interface is not running" |
| fi |
|
|
| echo "" |
| echo "Log Files:" |
| echo " /var/log/gpu_fan_control.log" |
| echo " $HOME/gpu_monitoring_system/logs/" |
| EOF |
| |
| |
| cat > "$INSTALL_DIR/scripts/test.sh" << 'EOF' |
| |
| cd "$(dirname "$0")/.." |
| source venv/bin/activate |
|
|
| echo "=== Testing GPU Monitoring System ===" |
| echo "" |
|
|
| echo "1. Testing GPU Detection..." |
| python3 -c " |
| from gpu_monitoring import GPUManager |
| manager = GPUManager() |
| if manager.initialize(): |
| print('✓ GPU detection successful') |
| gpus = manager.get_gpu_list() |
| print(f' Found {len(gpus)} GPU(s): {gpus}') |
| else: |
| print('✗ GPU detection failed') |
| " |
|
|
| echo "" |
| echo "2. Testing Fan Control..." |
| python3 -c " |
| from gpu_fan_controller import FanController |
| controller = FanController() |
| if controller.initialize(): |
| print('✓ Fan control initialization successful') |
| profiles = controller.get_profiles() |
| print(f' Available profiles: {list(profiles.keys())}') |
| else: |
| print('✗ Fan control initialization failed') |
| " |
|
|
| echo "" |
| echo "3. Testing Web Interface..." |
| python3 -c " |
| import sys |
| sys.path.append('.') |
| try: |
| from web_interface import app |
| print('✓ Web interface import successful') |
| except Exception as e: |
| print(f'✗ Web interface import failed: {e}') |
| " |
|
|
| echo "" |
| echo "=== Test Complete ===" |
| EOF |
| |
| |
| chmod +x "$INSTALL_DIR/scripts/"*.sh |
| |
| log "Convenience scripts created" |
| } |
|
|
| |
| create_backup_script() { |
| log "Creating backup script..." |
| |
| cat > "$INSTALL_DIR/scripts/backup.sh" << 'EOF' |
| |
| cd "$(dirname "$0")/.." |
|
|
| BACKUP_DIR="backups/$(date +%Y%m%d_%H%M%S)" |
| mkdir -p "$BACKUP_DIR" |
|
|
| echo "Creating backup in $BACKUP_DIR..." |
|
|
| |
| cp -r config "$BACKUP_DIR/" |
|
|
| |
| if [ -f "data/gpu_monitoring.db" ]; then |
| cp data/gpu_monitoring.db "$BACKUP_DIR/" |
| fi |
|
|
| |
| find logs/ -name "*.log" -mtime -7 -exec cp {} "$BACKUP_DIR/" \; |
|
|
| |
| cat > "$BACKUP_DIR/backup_info.txt" << BACKUP_INFO |
| Backup created: $(date) |
| System: $(uname -a) |
| GPU Monitoring System Version: 1.0 |
| BACKUP_INFO |
|
|
| echo "Backup created successfully in $BACKUP_DIR" |
| EOF |
| |
| chmod +x "$INSTALL_DIR/scripts/backup.sh" |
| |
| log "Backup script created" |
| } |
|
|
| |
| setup_cron_jobs() { |
| log "Setting up maintenance cron jobs..." |
| |
| |
| (crontab -l 2>/dev/null; echo "0 2 * * * $INSTALL_DIR/scripts/backup.sh") | crontab - |
| |
| |
| (crontab -l 2>/dev/null; echo "0 0 * * 0 find $INSTALL_DIR/logs -name '*.log' -mtime +7 -delete") | crontab - |
| |
| log "Cron jobs configured" |
| } |
|
|
| |
| main() { |
| echo "==========================================" |
| echo "GPU Monitoring System Setup" |
| echo "==========================================" |
| echo "" |
| |
| |
| if [[ ! -f "$SCRIPT_DIR/requirements.txt" ]]; then |
| error "Please run this script from the GPU monitoring system directory" |
| fi |
| |
| |
| echo "Setup started at $(date)" > "$LOG_FILE" |
| |
| log "Starting installation process..." |
| |
| |
| create_directories |
| |
| |
| install_system_deps |
| |
| |
| setup_venv |
| |
| |
| if check_root; then |
| setup_systemd_service |
| setup_permissions |
| fi |
| |
| |
| setup_autostart |
| |
| |
| create_scripts |
| create_backup_script |
| |
| |
| setup_cron_jobs |
| |
| log "Installation completed successfully!" |
| echo "" |
| echo "==========================================" |
| echo "Installation Summary" |
| echo "==========================================" |
| echo "✓ System dependencies installed" |
| echo "✓ Python virtual environment created" |
| echo "✓ Configuration files created" |
| echo "✓ Systemd service configured (if root)" |
| echo "✓ Desktop autostart configured" |
| echo "✓ Convenience scripts created" |
| echo "✓ Backup system configured" |
| echo "" |
| echo "Next steps:" |
| echo "1. Review configuration in config/" |
| echo "2. Test the system: ./scripts/test.sh" |
| echo "3. Start the service: sudo systemctl start $SERVICE_NAME" |
| echo "4. View status: ./scripts/status.sh" |
| echo "" |
| echo "Log file: $LOG_FILE" |
| echo "==========================================" |
| } |
|
|
| |
| case "${1:-}" in |
| "install") |
| main |
| ;; |
| "uninstall") |
| if check_root; then |
| log "Removing systemd service..." |
| sudo systemctl stop "$SERVICE_NAME" 2>/dev/null || true |
| sudo systemctl disable "$SERVICE_NAME" 2>/dev/null || true |
| sudo rm -f "/etc/systemd/system/$SERVICE_NAME.service" |
| sudo systemctl daemon-reload |
| fi |
| |
| log "Removing autostart entries..." |
| rm -f "$HOME/.config/autostart/gpu-monitor-*.desktop" |
| |
| log "Removing cron jobs..." |
| crontab -l 2>/dev/null | grep -v "$INSTALL_DIR" | crontab - |
| |
| log "Uninstallation completed" |
| ;; |
| "test") |
| cd "$INSTALL_DIR" |
| source venv/bin/activate |
| ./scripts/test.sh |
| ;; |
| "status") |
| cd "$INSTALL_DIR" |
| ./scripts/status.sh |
| ;; |
| *) |
| echo "Usage: $0 {install|uninstall|test|status}" |
| echo "" |
| echo "Commands:" |
| echo " install - Install the GPU monitoring system" |
| echo " uninstall - Remove the GPU monitoring system" |
| echo " test - Test the system components" |
| echo " status - Show system status" |
| exit 1 |
| ;; |
| esac |