Implement structural adjustments and optimize performance across multiple modules

This commit is contained in:
Phoenix
2025-08-07 23:25:01 +08:00
parent e842b7d73e
commit 35828f189c
6 changed files with 468 additions and 0 deletions
+226
View File
@@ -0,0 +1,226 @@
# ResourceMonitorService - Packaging and VM Deployment Guide
This guide explains how to package the ResourceMonitorService for release and deploy it to a VM.
## 📦 Packaging for Release
### Quick Packaging
```powershell
# Simple packaging - creates a ZIP file with all necessary components
.\create-package.ps1
# Specify version
.\create-package.ps1 -Version "2.1.1"
```
### What Gets Packaged
The packaging script includes:
- ✅ Compiled .NET binaries (Release build)
- ✅ Installation scripts (`install-service.ps1`, `start-service.bat`)
- ✅ Configuration files (`appsettings.json`)
- ✅ Documentation files (`README.md`, etc.)
- ✅ Deployment instructions (`DEPLOYMENT.txt`)
### Output
- **Location**: `.\release-packages\`
- **Format**: `ResourceMonitorService-v{VERSION}-{TIMESTAMP}.zip`
- **Size**: ~2.3 MB
- **Example**: `ResourceMonitorService-v2.1.0-20250807-2322.zip`
## 🚀 VM Deployment Options
### Option 1: Manual Deployment (Recommended)
1. **Transfer the ZIP file to your VM**:
- Copy via RDP shared folders
- Use network file share
- Download from cloud storage
- USB transfer
2. **On the VM**:
```powershell
# Extract the ZIP file to a temporary directory
Expand-Archive -Path "ResourceMonitorService-v2.1.0-*.zip" -DestinationPath "C:\Temp\ResourceMonitor"
# Navigate to extracted directory
cd "C:\Temp\ResourceMonitor"
# Run installation as Administrator
.\install-service.ps1
```
3. **Access the service**:
- Web Dashboard: `http://VM-IP:5000`
- API Health: `http://VM-IP:5000/api/health`
### Option 2: Automated Deployment (Advanced)
If your VM has PowerShell Remoting enabled:
```powershell
# Deploy using WinRM (requires setup)
.\deploy-to-vm.ps1 -VMAddress "192.168.1.100" -UseWinRM
# Copy only (no auto-install)
.\deploy-to-vm.ps1 -VMAddress "192.168.1.100" -UseWinRM -CopyOnly
```
### Option 3: Complete Build & Deploy
```powershell
# Build, package, and deploy in one command
.\build-and-deploy.ps1 -VMAddress "192.168.1.100" -DeployToVM -UseWinRM
```
## 🔧 VM Prerequisites
### Required Software
- **Windows 10/11 or Windows Server 2019+**
- **.NET 9.0 Runtime** ([Download](https://dotnet.microsoft.com/download/dotnet/9.0))
- **PowerShell 5.1+** (Built into Windows)
### Administrator Privileges
The installation requires Administrator privileges to:
- Create Windows Service
- Configure firewall rules
- Create directories in Program Files
- Set service permissions
### Network Requirements
- **Port 5000**: Web Dashboard access
- **Port 5001**: HTTPS access (optional)
- Firewall rule is automatically created during installation
## 📋 Installation Process
The `install-service.ps1` script automatically:
1. ✅ **Creates installation directory** (`C:\Services\ResourceMonitor`)
2. ✅ **Copies all service files**
3. ✅ **Registers Windows Service** (`ResourceMonitorService`)
4. ✅ **Configures auto-start** (starts with Windows)
5. ✅ **Creates firewall rule** (port 5000)
6. ✅ **Starts the service**
7. ✅ **Tests web dashboard** availability
## 🎯 Post-Installation
### Service Management
```powershell
# Check service status
Get-Service ResourceMonitorService
# Start/Stop/Restart service
Start-Service ResourceMonitorService
Stop-Service ResourceMonitorService
Restart-Service ResourceMonitorService
# Uninstall service
.\install-service.ps1 -Uninstall
```
### Access Points
- **Web Dashboard**: `http://VM-IP:5000`
- **API Documentation**: `http://VM-IP:5000/swagger` (if enabled)
- **Health Check**: `http://VM-IP:5000/api/health`
- **Logs**: `C:\Services\ResourceMonitor\logs\`
### Configuration
Edit `C:\Services\ResourceMonitor\appsettings.json` to customize:
- Monitoring intervals
- Alert thresholds
- Telegram notifications
- API settings
- Logging levels
## 🔍 Troubleshooting
### Common Issues
**Service won't start**:
```powershell
# Check Windows Event Log
Get-EventLog -LogName Application -Source "ResourceMonitorService" -Newest 10
# Check service logs
Get-Content "C:\Services\ResourceMonitor\logs\*.txt" -Tail 50
```
**Port 5000 not accessible**:
```powershell
# Manually create firewall rule
New-NetFirewallRule -DisplayName "Resource Monitor Service" -Direction Inbound -Protocol TCP -LocalPort 5000 -Action Allow
# Check if port is listening
netstat -an | findstr :5000
```
**.NET Runtime not found**:
```powershell
# Check .NET installation
dotnet --version
dotnet --list-runtimes
# Download from: https://dotnet.microsoft.com/download/dotnet/9.0
```
### Log Locations
- **Service Logs**: `C:\Services\ResourceMonitor\logs\`
- **Windows Event Log**: Application > ResourceMonitorService
- **Installation Logs**: Console output during installation
## 📝 File Structure
After installation, the service directory contains:
```
C:\Services\ResourceMonitor\
├── ResourceMonitorService.exe # Main service executable
├── ResourceMonitorService.dll # Application library
├── appsettings.json # Configuration file
├── appsettings.Development.json # Development settings
├── install-service.ps1 # Installation script
├── start-service.bat # Manual start script
├── DEPLOYMENT.txt # Deployment instructions
├── logs\ # Log files directory
├── wwwroot\ # Web dashboard files
│ ├── index.html
│ ├── css\
│ └── js\
└── [various .NET runtime files]
```
## 🚀 Quick Reference
### Essential Commands
```powershell
# Package for deployment
.\create-package.ps1
# Install on VM (as Administrator)
.\install-service.ps1
# Check service status
Get-Service ResourceMonitorService
# Access dashboard
Start-Process "http://localhost:5000"
# Uninstall
.\install-service.ps1 -Uninstall
```
### Network Access
Replace `localhost` with your VM's IP address to access remotely:
- `http://192.168.1.100:5000` (Web Dashboard)
- `http://192.168.1.100:5000/api/health` (Health Check)
---
## 🔗 Additional Resources
- **Main README**: `README.md`
- **Web UI Guide**: `README_WebUI.md`
- **Telegram Setup**: `TELEGRAM_SETUP.md`
- **Project Repository**: [GitHub/ResourceMonitorService]
For support or issues, check the troubleshooting section above or review the service logs.
+51
View File
@@ -0,0 +1,51 @@
# Resource Monitor Service - Complete Build and Deploy Script
# Builds, packages, and optionally deploys to VM in one step
param(
[string]$VMAddress,
[string]$Version = "2.1.0",
[switch]$DeployToVM,
[switch]$UseWinRM,
[switch]$CopyOnly
)
Write-Host "=== Resource Monitor Service - Build & Deploy ===" -ForegroundColor Cyan
Write-Host
# Step 1: Package the release
Write-Host "Step 1: Building and packaging release..." -ForegroundColor Green
try {
& ".\package-release.ps1" -Version $Version
if ($LASTEXITCODE -ne 0) {
throw "Packaging failed"
}
} catch {
Write-Host "✗ Packaging failed: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
# Step 2: Deploy if requested
if ($DeployToVM -and $VMAddress) {
Write-Host
Write-Host "Step 2: Deploying to VM..." -ForegroundColor Green
$deployArgs = @("-VMAddress", $VMAddress)
if ($UseWinRM) { $deployArgs += "-UseWinRM" }
if ($CopyOnly) { $deployArgs += "-CopyOnly" }
try {
& ".\deploy-to-vm.ps1" @deployArgs
} catch {
Write-Host "✗ Deployment failed: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
} else {
Write-Host
Write-Host "Package created successfully!" -ForegroundColor Green
Write-Host "To deploy to VM, run:" -ForegroundColor Yellow
Write-Host " .\deploy-to-vm.ps1 -VMAddress YOUR_VM_IP" -ForegroundColor Cyan
Write-Host " .\deploy-to-vm.ps1 -VMAddress YOUR_VM_IP -UseWinRM" -ForegroundColor Cyan
}
Write-Host
Write-Host "=== Build & Deploy Complete ===" -ForegroundColor Cyan
+53
View File
@@ -0,0 +1,53 @@
# Simple Release Packaging Script
param(
[string]$Version = "2.1.0"
)
$PACKAGE_NAME = "ResourceMonitorService-v$Version-$(Get-Date -Format 'yyyyMMdd-HHmm')"
$TEMP_PATH = ".\temp-release"
$OUTPUT_PATH = ".\release-packages"
Write-Host "Creating release package: $PACKAGE_NAME" -ForegroundColor Green
# Clean up
if (Test-Path $TEMP_PATH) { Remove-Item $TEMP_PATH -Recurse -Force }
if (-not (Test-Path $OUTPUT_PATH)) { New-Item -ItemType Directory -Path $OUTPUT_PATH -Force | Out-Null }
New-Item -ItemType Directory -Path $TEMP_PATH -Force | Out-Null
# Build release
Write-Host "Building release..." -ForegroundColor Yellow
dotnet publish --configuration Release --output $TEMP_PATH --verbosity minimal
# Copy additional files
Start-Sleep -Seconds 2
Copy-Item "install-service.ps1" $TEMP_PATH
Copy-Item "start-service.bat" $TEMP_PATH
Copy-Item "appsettings.json" $TEMP_PATH
Copy-Item "README.md" $TEMP_PATH -ErrorAction SilentlyContinue
# Create deployment readme
@"
# ResourceMonitorService v$Version Deployment
## Installation
1. Extract ZIP to temporary folder
2. Run PowerShell as Administrator
3. Execute: .\install-service.ps1
## Access
- Web Dashboard: http://localhost:5000
- API Health: http://localhost:5000/api/health
Generated: $(Get-Date)
"@ | Out-File "$TEMP_PATH\DEPLOYMENT.txt" -Encoding UTF8
# Create ZIP
Start-Sleep -Seconds 2
$zipPath = "$OUTPUT_PATH\$PACKAGE_NAME.zip"
Compress-Archive -Path "$TEMP_PATH\*" -DestinationPath $zipPath -Force
# Clean up
Remove-Item $TEMP_PATH -Recurse -Force
$size = [math]::Round((Get-Item $zipPath).Length / 1MB, 2)
Write-Host "Package created: $zipPath ($size MB)" -ForegroundColor Green
+138
View File
@@ -0,0 +1,138 @@
# Resource Monitor Service - Remote VM Deployment Script
# Deploys the packaged service to a remote VM
param(
[Parameter(Mandatory=$true)]
[string]$VMAddress,
[Parameter(Mandatory=$false)]
[string]$Username,
[Parameter(Mandatory=$false)]
[string]$PackagePath,
[switch]$UseWinRM,
[switch]$CopyOnly
)
Write-Host "=== Resource Monitor Service - Remote VM Deployment ===" -ForegroundColor Cyan
Write-Host
# Find the latest package if not specified
if (-not $PackagePath) {
$latestPackage = Get-ChildItem -Path ".\release-packages\ResourceMonitorService-v*.zip" | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($latestPackage) {
$PackagePath = $latestPackage.FullName
Write-Host "Using latest package: $($latestPackage.Name)" -ForegroundColor Green
} else {
Write-Host "✗ No packages found. Run .\package-release.ps1 first" -ForegroundColor Red
exit 1
}
}
if (-not (Test-Path $PackagePath)) {
Write-Host "✗ Package not found: $PackagePath" -ForegroundColor Red
exit 1
}
$packageName = [System.IO.Path]::GetFileNameWithoutExtension($PackagePath)
Write-Host "Package: $packageName" -ForegroundColor White
Write-Host "Target VM: $VMAddress" -ForegroundColor White
Write-Host
if ($UseWinRM) {
Write-Host "Using WinRM for deployment..." -ForegroundColor Yellow
# Test WinRM connection
Write-Host "Testing WinRM connection to $VMAddress..." -ForegroundColor Yellow
try {
$session = New-PSSession -ComputerName $VMAddress -Credential (Get-Credential -Message "Enter credentials for $VMAddress")
Write-Host "✓ WinRM connection successful" -ForegroundColor Green
} catch {
Write-Host "✗ WinRM connection failed: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Make sure WinRM is enabled on the target VM:" -ForegroundColor Yellow
Write-Host " winrm quickconfig" -ForegroundColor Gray
Write-Host " Enable-PSRemoting -Force" -ForegroundColor Gray
exit 1
}
# Copy package to VM
Write-Host "Copying package to VM..." -ForegroundColor Yellow
try {
$remoteTemp = "C:\Temp\$packageName"
Invoke-Command -Session $session -ScriptBlock {
param($remotePath)
if (Test-Path $remotePath) { Remove-Item $remotePath -Recurse -Force }
New-Item -ItemType Directory -Path $remotePath -Force | Out-Null
} -ArgumentList $remoteTemp
Copy-Item -Path $PackagePath -Destination "$remoteTemp.zip" -ToSession $session
Write-Host "✓ Package copied to $remoteTemp.zip" -ForegroundColor Green
} catch {
Write-Host "✗ Failed to copy package: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
if (-not $CopyOnly) {
# Extract and install on VM
Write-Host "Extracting and installing on VM..." -ForegroundColor Yellow
try {
$installResult = Invoke-Command -Session $session -ScriptBlock {
param($remotePath, $packageName)
# Extract package
Expand-Archive -Path "$remotePath.zip" -DestinationPath $remotePath -Force
Set-Location $remotePath
# Run installation
$installOutput = & ".\install-service.ps1" 2>&1
return @{
Success = $LASTEXITCODE -eq 0
Output = $installOutput -join "`n"
ExitCode = $LASTEXITCODE
}
} -ArgumentList $remoteTemp, $packageName
if ($installResult.Success) {
Write-Host "✓ Installation completed successfully!" -ForegroundColor Green
Write-Host "Web Dashboard: http://$VMAddress:5000" -ForegroundColor Cyan
} else {
Write-Host "✗ Installation failed (Exit Code: $($installResult.ExitCode))" -ForegroundColor Red
Write-Host "Output:" -ForegroundColor Yellow
Write-Host $installResult.Output -ForegroundColor Gray
}
} catch {
Write-Host "✗ Installation failed: $($_.Exception.Message)" -ForegroundColor Red
}
}
# Clean up session
Remove-PSSession $session
} else {
# Manual deployment instructions
Write-Host "Manual Deployment Instructions:" -ForegroundColor Yellow
Write-Host
Write-Host "1. Copy the package to your VM:" -ForegroundColor White
Write-Host " - Use RDP, shared folders, or network copy" -ForegroundColor Gray
Write-Host " - Package location: $PackagePath" -ForegroundColor Gray
Write-Host
Write-Host "2. On the VM, extract the ZIP file to a temporary directory" -ForegroundColor White
Write-Host
Write-Host "3. Open PowerShell as Administrator and navigate to the extracted directory" -ForegroundColor White
Write-Host
Write-Host "4. Run the installation:" -ForegroundColor White
Write-Host " .\install-service.ps1" -ForegroundColor Cyan
Write-Host " OR" -ForegroundColor Gray
Write-Host " .\INSTALL.bat" -ForegroundColor Cyan
Write-Host
Write-Host "5. Access the web dashboard:" -ForegroundColor White
Write-Host " http://$VMAddress:5000" -ForegroundColor Cyan
Write-Host
Write-Host "Alternative deployment methods:" -ForegroundColor Yellow
Write-Host " PowerShell Remoting: .\deploy-to-vm.ps1 -VMAddress $VMAddress -UseWinRM" -ForegroundColor Gray
Write-Host " Copy only: .\deploy-to-vm.ps1 -VMAddress $VMAddress -UseWinRM -CopyOnly" -ForegroundColor Gray
}
Write-Host
Write-Host "=== Deployment Script Complete ===" -ForegroundColor Cyan
View File