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/bin/bash
2
+ # GPU Monitoring System Setup Script
3
+
4
+ set -e
5
+
6
+ # Colors for output
7
+ RED='\033[0;31m'
8
+ GREEN='\033[0;32m'
9
+ YELLOW='\033[1;33m'
10
+ BLUE='\033[0;34m'
11
+ NC='\033[0m' # No Color
12
+
13
+ # Configuration
14
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
15
+ INSTALL_DIR="/home/crhon/gpu_monitoring_system"
16
+ SERVICE_NAME="gpu-monitoring"
17
+ LOG_FILE="/var/log/gpu_monitoring_setup.log"
18
+
19
+ # Logging function
20
+ log() {
21
+ echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" | tee -a "$LOG_FILE"
22
+ }
23
+
24
+ warn() {
25
+ echo -e "${YELLOW}[WARNING]${NC} $1" | tee -a "$LOG_FILE"
26
+ }
27
+
28
+ error() {
29
+ echo -e "${RED}[ERROR]${NC} $1" | tee -a "$LOG_FILE"
30
+ exit 1
31
+ }
32
+
33
+ info() {
34
+ echo -e "${BLUE}[INFO]${NC} $1" | tee -a "$LOG_FILE"
35
+ }
36
+
37
+ # Check if running as root for system operations
38
+ check_root() {
39
+ if [[ $EUID -eq 0 ]]; then
40
+ return 0
41
+ else
42
+ return 1
43
+ fi
44
+ }
45
+
46
+ # Install system dependencies
47
+ install_system_deps() {
48
+ log "Installing system dependencies..."
49
+
50
+ # Update package list
51
+ sudo apt update
52
+
53
+ # Install required packages
54
+ sudo apt install -y \
55
+ python3 \
56
+ python3-pip \
57
+ python3-venv \
58
+ python3-pyqt5 \
59
+ python3-pyqt5.qtopengl \
60
+ python3-matplotlib \
61
+ python3-flask \
62
+ python3-flask-cors \
63
+ python3-psutil \
64
+ python3-numpy \
65
+ python3-pandas \
66
+ sqlite3
67
+
68
+ log "System dependencies installed successfully"
69
+ }
70
+
71
+ # Setup Python virtual environment
72
+ setup_venv() {
73
+ log "Setting up Python virtual environment..."
74
+
75
+ cd "$INSTALL_DIR"
76
+
77
+ # Create virtual environment
78
+ python3 -m venv venv
79
+
80
+ # Activate virtual environment
81
+ source venv/bin/activate
82
+
83
+ # Upgrade pip
84
+ pip install --upgrade pip
85
+
86
+ # Install Python requirements
87
+ pip install -r requirements.txt
88
+
89
+ log "Virtual environment setup complete"
90
+ }
91
+
92
+ # Create necessary directories
93
+ create_directories() {
94
+ log "Creating necessary directories..."
95
+
96
+ mkdir -p "$INSTALL_DIR/data"
97
+ mkdir -p "$INSTALL_DIR/logs"
98
+ mkdir -p "$INSTALL_DIR/config"
99
+ mkdir -p "$INSTALL_DIR/backups"
100
+ mkdir -p "$INSTALL_DIR/scripts"
101
+
102
+ log "Directories created successfully"
103
+ }
104
+
105
+ # Setup systemd service
106
+ setup_systemd_service() {
107
+ if ! check_root; then
108
+ warn "Systemd service installation requires root privileges. Skipping..."
109
+ return 1
110
+ fi
111
+
112
+ log "Setting up systemd service..."
113
+
114
+ # Copy service file
115
+ sudo cp "$INSTALL_DIR/systemd/$SERVICE_NAME.service" "/etc/systemd/system/$SERVICE_NAME.service"
116
+
117
+ # Reload systemd
118
+ sudo systemctl daemon-reload
119
+
120
+ # Enable service
121
+ sudo systemctl enable "$SERVICE_NAME"
122
+
123
+ log "Systemd service installed and enabled"
124
+ }
125
+
126
+ # Setup autostart for desktop environments
127
+ setup_autostart() {
128
+ log "Setting up desktop autostart..."
129
+
130
+ # Create autostart directory
131
+ mkdir -p "$HOME/.config/autostart"
132
+
133
+ # Create desktop entry for overlay monitor
134
+ cat > "$HOME/.config/autostart/gpu-monitor-overlay.desktop" << EOF
135
+ [Desktop Entry]
136
+ Type=Application
137
+ Name=GPU Monitor Overlay
138
+ Comment=GPU monitoring overlay for desktop
139
+ Exec=python3 $INSTALL_DIR/gpu_monitor_desktop.py --display overlay
140
+ Icon=video-display
141
+ Terminal=false
142
+ Hidden=false
143
+ NoDisplay=false
144
+ X-GNOME-Autostart-enabled=true
145
+ EOF
146
+
147
+ # Create desktop entry for web interface
148
+ cat > "$HOME/.config/autostart/gpu-monitor-web.desktop" << EOF
149
+ [Desktop Entry]
150
+ Type=Application
151
+ Name=GPU Monitor Web
152
+ Comment=GPU monitoring web interface
153
+ Exec=python3 $INSTALL_DIR/web_interface.py
154
+ Icon=video-display
155
+ Terminal=false
156
+ Hidden=false
157
+ NoDisplay=false
158
+ X-GNOME-Autostart-enabled=true
159
+ EOF
160
+
161
+ log "Desktop autostart configured"
162
+ }
163
+
164
+ # Setup permissions
165
+ setup_permissions() {
166
+ if ! check_root; then
167
+ warn "Permission setup requires root privileges. Skipping..."
168
+ return 1
169
+ fi
170
+
171
+ log "Setting up permissions..."
172
+
173
+ # Set proper permissions for GPU access
174
+ sudo usermod -a -G video "$USER"
175
+ sudo usermod -a -G render "$USER"
176
+
177
+ # Set permissions for log directory
178
+ sudo mkdir -p /var/log
179
+ sudo touch /var/log/gpu_fan_control.log
180
+ sudo chown "$USER":"$USER" /var/log/gpu_fan_control.log
181
+ sudo chmod 644 /var/log/gpu_fan_control.log
182
+
183
+ log "Permissions configured"
184
+ }
185
+
186
+ # Create convenience scripts
187
+ create_scripts() {
188
+ log "Creating convenience scripts..."
189
+
190
+ # Create start script
191
+ cat > "$INSTALL_DIR/scripts/start.sh" << 'EOF'
192
+ #!/bin/bash
193
+ cd "$(dirname "$0")/.."
194
+ source venv/bin/activate
195
+ python3 gpu_fan_controller.py --daemon
196
+ EOF
197
+
198
+ # Create stop script
199
+ cat > "$INSTALL_DIR/scripts/stop.sh" << 'EOF'
200
+ #!/bin/bash
201
+ pkill -f gpu_fan_controller.py
202
+ EOF
203
+
204
+ # Create status script
205
+ cat > "$INSTALL_DIR/scripts/status.sh" << 'EOF'
206
+ #!/bin/bash
207
+ echo "=== GPU Monitoring System Status ==="
208
+ echo "Service Status:"
209
+ if systemctl is-active --quiet gpu-monitoring 2>/dev/null; then
210
+ echo " ✓ Service is running"
211
+ else
212
+ echo " ✗ Service is not running"
213
+ fi
214
+
215
+ echo ""
216
+ echo "Process Status:"
217
+ if pgrep -f gpu_fan_controller.py > /dev/null; then
218
+ echo " ✓ Fan controller process is running"
219
+ else
220
+ echo " ✗ Fan controller process is not running"
221
+ fi
222
+
223
+ echo ""
224
+ echo "Web Interface:"
225
+ if pgrep -f web_interface.py > /dev/null; then
226
+ echo " ✓ Web interface is running"
227
+ echo " URL: http://localhost:5000"
228
+ else
229
+ echo " ✗ Web interface is not running"
230
+ fi
231
+
232
+ echo ""
233
+ echo "Log Files:"
234
+ echo " /var/log/gpu_fan_control.log"
235
+ echo " $HOME/gpu_monitoring_system/logs/"
236
+ EOF
237
+
238
+ # Create test script
239
+ cat > "$INSTALL_DIR/scripts/test.sh" << 'EOF'
240
+ #!/bin/bash
241
+ cd "$(dirname "$0")/.."
242
+ source venv/bin/activate
243
+
244
+ echo "=== Testing GPU Monitoring System ==="
245
+ echo ""
246
+
247
+ echo "1. Testing GPU Detection..."
248
+ python3 -c "
249
+ from gpu_monitoring import GPUManager
250
+ manager = GPUManager()
251
+ if manager.initialize():
252
+ print('✓ GPU detection successful')
253
+ gpus = manager.get_gpu_list()
254
+ print(f' Found {len(gpus)} GPU(s): {gpus}')
255
+ else:
256
+ print('✗ GPU detection failed')
257
+ "
258
+
259
+ echo ""
260
+ echo "2. Testing Fan Control..."
261
+ python3 -c "
262
+ from gpu_fan_controller import FanController
263
+ controller = FanController()
264
+ if controller.initialize():
265
+ print('✓ Fan control initialization successful')
266
+ profiles = controller.get_profiles()
267
+ print(f' Available profiles: {list(profiles.keys())}')
268
+ else:
269
+ print('✗ Fan control initialization failed')
270
+ "
271
+
272
+ echo ""
273
+ echo "3. Testing Web Interface..."
274
+ python3 -c "
275
+ import sys
276
+ sys.path.append('.')
277
+ try:
278
+ from web_interface import app
279
+ print('✓ Web interface import successful')
280
+ except Exception as e:
281
+ print(f'✗ Web interface import failed: {e}')
282
+ "
283
+
284
+ echo ""
285
+ echo "=== Test Complete ==="
286
+ EOF
287
+
288
+ # Make scripts executable
289
+ chmod +x "$INSTALL_DIR/scripts/"*.sh
290
+
291
+ log "Convenience scripts created"
292
+ }
293
+
294
+ # Create backup script
295
+ create_backup_script() {
296
+ log "Creating backup script..."
297
+
298
+ cat > "$INSTALL_DIR/scripts/backup.sh" << 'EOF'
299
+ #!/bin/bash
300
+ cd "$(dirname "$0")/.."
301
+
302
+ BACKUP_DIR="backups/$(date +%Y%m%d_%H%M%S)"
303
+ mkdir -p "$BACKUP_DIR"
304
+
305
+ echo "Creating backup in $BACKUP_DIR..."
306
+
307
+ # Backup configuration
308
+ cp -r config "$BACKUP_DIR/"
309
+
310
+ # Backup database
311
+ if [ -f "data/gpu_monitoring.db" ]; then
312
+ cp data/gpu_monitoring.db "$BACKUP_DIR/"
313
+ fi
314
+
315
+ # Backup logs (last 7 days)
316
+ find logs/ -name "*.log" -mtime -7 -exec cp {} "$BACKUP_DIR/" \;
317
+
318
+ # Create backup info
319
+ cat > "$BACKUP_DIR/backup_info.txt" << BACKUP_INFO
320
+ Backup created: $(date)
321
+ System: $(uname -a)
322
+ GPU Monitoring System Version: 1.0
323
+ BACKUP_INFO
324
+
325
+ echo "Backup created successfully in $BACKUP_DIR"
326
+ EOF
327
+
328
+ chmod +x "$INSTALL_DIR/scripts/backup.sh"
329
+
330
+ log "Backup script created"
331
+ }
332
+
333
+ # Setup cron jobs for maintenance
334
+ setup_cron_jobs() {
335
+ log "Setting up maintenance cron jobs..."
336
+
337
+ # Create cron job for database cleanup
338
+ (crontab -l 2>/dev/null; echo "0 2 * * * $INSTALL_DIR/scripts/backup.sh") | crontab -
339
+
340
+ # Create cron job for log rotation
341
+ (crontab -l 2>/dev/null; echo "0 0 * * 0 find $INSTALL_DIR/logs -name '*.log' -mtime +7 -delete") | crontab -
342
+
343
+ log "Cron jobs configured"
344
+ }
345
+
346
+ # Main installation function
347
+ main() {
348
+ echo "=========================================="
349
+ echo "GPU Monitoring System Setup"
350
+ echo "=========================================="
351
+ echo ""
352
+
353
+ # Check if running from correct directory
354
+ if [[ ! -f "$SCRIPT_DIR/requirements.txt" ]]; then
355
+ error "Please run this script from the GPU monitoring system directory"
356
+ fi
357
+
358
+ # Initialize log file
359
+ echo "Setup started at $(date)" > "$LOG_FILE"
360
+
361
+ log "Starting installation process..."
362
+
363
+ # Create directories
364
+ create_directories
365
+
366
+ # Install system dependencies
367
+ install_system_deps
368
+
369
+ # Setup virtual environment
370
+ setup_venv
371
+
372
+ # Setup systemd service (if root)
373
+ if check_root; then
374
+ setup_systemd_service
375
+ setup_permissions
376
+ fi
377
+
378
+ # Setup autostart
379
+ setup_autostart
380
+
381
+ # Create scripts
382
+ create_scripts
383
+ create_backup_script
384
+
385
+ # Setup cron jobs
386
+ setup_cron_jobs
387
+
388
+ log "Installation completed successfully!"
389
+ echo ""
390
+ echo "=========================================="
391
+ echo "Installation Summary"
392
+ echo "=========================================="
393
+ echo "✓ System dependencies installed"
394
+ echo "✓ Python virtual environment created"
395
+ echo "✓ Configuration files created"
396
+ echo "✓ Systemd service configured (if root)"
397
+ echo "✓ Desktop autostart configured"
398
+ echo "✓ Convenience scripts created"
399
+ echo "✓ Backup system configured"
400
+ echo ""
401
+ echo "Next steps:"
402
+ echo "1. Review configuration in config/"
403
+ echo "2. Test the system: ./scripts/test.sh"
404
+ echo "3. Start the service: sudo systemctl start $SERVICE_NAME"
405
+ echo "4. View status: ./scripts/status.sh"
406
+ echo ""
407
+ echo "Log file: $LOG_FILE"
408
+ echo "=========================================="
409
+ }
410
+
411
+ # Handle command line arguments
412
+ case "${1:-}" in
413
+ "install")
414
+ main
415
+ ;;
416
+ "uninstall")
417
+ if check_root; then
418
+ log "Removing systemd service..."
419
+ sudo systemctl stop "$SERVICE_NAME" 2>/dev/null || true
420
+ sudo systemctl disable "$SERVICE_NAME" 2>/dev/null || true
421
+ sudo rm -f "/etc/systemd/system/$SERVICE_NAME.service"
422
+ sudo systemctl daemon-reload
423
+ fi
424
+
425
+ log "Removing autostart entries..."
426
+ rm -f "$HOME/.config/autostart/gpu-monitor-*.desktop"
427
+
428
+ log "Removing cron jobs..."
429
+ crontab -l 2>/dev/null | grep -v "$INSTALL_DIR" | crontab -
430
+
431
+ log "Uninstallation completed"
432
+ ;;
433
+ "test")
434
+ cd "$INSTALL_DIR"
435
+ source venv/bin/activate
436
+ ./scripts/test.sh
437
+ ;;
438
+ "status")
439
+ cd "$INSTALL_DIR"
440
+ ./scripts/status.sh
441
+ ;;
442
+ *)
443
+ echo "Usage: $0 {install|uninstall|test|status}"
444
+ echo ""
445
+ echo "Commands:"
446
+ echo " install - Install the GPU monitoring system"
447
+ echo " uninstall - Remove the GPU monitoring system"
448
+ echo " test - Test the system components"
449
+ echo " status - Show system status"
450
+ exit 1
451
+ ;;
452
+ esac