mirror of
https://github.com/andrey271192/vps_monitoring.git
synced 2026-09-21 12:01:58 +00:00
feat: add PC monitoring tab with Windows agent
- New /api/pc endpoints (heartbeat, list, delete) - Dashboard tabs: Серверы / ПК with auto-refresh - PowerShell agent: CPU, RAM, Disk, Network, GPU, Top processes - One-command Windows installer with Scheduled Task - PC setup instructions modal Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
114
windows/install_agent.ps1
Normal file
114
windows/install_agent.ps1
Normal file
@@ -0,0 +1,114 @@
|
||||
# VPS Monitor — Windows Agent Installer
|
||||
# Usage: powershell -ExecutionPolicy Bypass -File install_agent.ps1 -ServerUrl "http://IP:7272" -AgentName "MyPC"
|
||||
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$ServerUrl,
|
||||
|
||||
[string]$AgentName = $env:COMPUTERNAME,
|
||||
|
||||
[int]$Interval = 60
|
||||
)
|
||||
|
||||
$InstallDir = "C:\VPS-Monitor"
|
||||
$TaskName = "VPS-Monitor-Agent"
|
||||
|
||||
Write-Host "================================================" -ForegroundColor Cyan
|
||||
Write-Host " VPS Monitor — Agent Installer" -ForegroundColor Cyan
|
||||
Write-Host "================================================" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
Write-Host " Server: $ServerUrl" -ForegroundColor White
|
||||
Write-Host " Name: $AgentName" -ForegroundColor White
|
||||
Write-Host " Interval: ${Interval}s" -ForegroundColor White
|
||||
Write-Host " Install: $InstallDir" -ForegroundColor White
|
||||
Write-Host ""
|
||||
|
||||
# Create install directory
|
||||
if (-not (Test-Path $InstallDir)) {
|
||||
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
|
||||
Write-Host "[+] Created $InstallDir" -ForegroundColor Green
|
||||
}
|
||||
|
||||
# Download agent script
|
||||
Write-Host "[*] Downloading agent..." -ForegroundColor Yellow
|
||||
$AgentUrl = "$ServerUrl/static/downloads/vps_monitor_agent.ps1"
|
||||
try {
|
||||
Invoke-WebRequest -Uri $AgentUrl -OutFile "$InstallDir\vps_monitor_agent.ps1" -UseBasicParsing
|
||||
Write-Host "[+] Agent downloaded" -ForegroundColor Green
|
||||
} catch {
|
||||
Write-Host "[!] Download failed, using local copy..." -ForegroundColor Yellow
|
||||
# Fallback: copy from same directory
|
||||
$localAgent = Join-Path $PSScriptRoot "vps_monitor_agent.ps1"
|
||||
if (Test-Path $localAgent) {
|
||||
Copy-Item $localAgent "$InstallDir\vps_monitor_agent.ps1"
|
||||
Write-Host "[+] Local copy used" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[-] No agent script found!" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
# Create config
|
||||
$config = @{
|
||||
server_url = $ServerUrl
|
||||
agent_name = $AgentName
|
||||
interval = $Interval
|
||||
} | ConvertTo-Json
|
||||
|
||||
Set-Content -Path "$InstallDir\agent_config.json" -Value $config
|
||||
Write-Host "[+] Config saved" -ForegroundColor Green
|
||||
|
||||
# Create scheduled task (run at startup + every 5 min check)
|
||||
$existingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
|
||||
if ($existingTask) {
|
||||
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
|
||||
Write-Host "[*] Removed old task" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
$action = New-ScheduledTaskAction `
|
||||
-Execute "powershell.exe" `
|
||||
-Argument "-ExecutionPolicy Bypass -WindowStyle Hidden -File `"$InstallDir\vps_monitor_agent.ps1`"" `
|
||||
-WorkingDirectory $InstallDir
|
||||
|
||||
$triggerStartup = New-ScheduledTaskTrigger -AtStartup
|
||||
$triggerNow = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 1)
|
||||
|
||||
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
|
||||
|
||||
$settings = New-ScheduledTaskSettingsSet `
|
||||
-AllowStartIfOnBatteries `
|
||||
-DontStopIfGoingOnBatteries `
|
||||
-RestartCount 3 `
|
||||
-RestartInterval (New-TimeSpan -Minutes 1) `
|
||||
-ExecutionTimeLimit (New-TimeSpan -Days 365)
|
||||
|
||||
Register-ScheduledTask `
|
||||
-TaskName $TaskName `
|
||||
-Action $action `
|
||||
-Trigger $triggerStartup `
|
||||
-Principal $principal `
|
||||
-Settings $settings `
|
||||
-Description "VPS Monitor PC Agent — sends metrics to $ServerUrl" | Out-Null
|
||||
|
||||
Write-Host "[+] Scheduled task created" -ForegroundColor Green
|
||||
|
||||
# Start now
|
||||
Start-ScheduledTask -TaskName $TaskName
|
||||
Write-Host "[+] Agent started!" -ForegroundColor Green
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "================================================" -ForegroundColor Cyan
|
||||
Write-Host " Installation complete!" -ForegroundColor Green
|
||||
Write-Host "================================================" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
Write-Host " Files: $InstallDir" -ForegroundColor White
|
||||
Write-Host " Task: $TaskName" -ForegroundColor White
|
||||
Write-Host " Status: Running" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host " Commands:" -ForegroundColor Gray
|
||||
Write-Host " Check: Get-ScheduledTask '$TaskName'" -ForegroundColor Gray
|
||||
Write-Host " Stop: Stop-ScheduledTask '$TaskName'" -ForegroundColor Gray
|
||||
Write-Host " Start: Start-ScheduledTask '$TaskName'" -ForegroundColor Gray
|
||||
Write-Host " Remove: Unregister-ScheduledTask '$TaskName'" -ForegroundColor Gray
|
||||
Write-Host " Logs: type $InstallDir\agent_config.json" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Reference in New Issue
Block a user