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:
50
windows/README_PC.md
Normal file
50
windows/README_PC.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# VPS Monitor — Windows PC Agent
|
||||
|
||||
## Quick Install (one command)
|
||||
|
||||
Run in PowerShell as Administrator:
|
||||
|
||||
```powershell
|
||||
irm https://77.239.126.123.nip.io/static/downloads/install_agent.ps1 | iex -ServerUrl "http://77.239.126.123:7272" -AgentName "MyPC"
|
||||
```
|
||||
|
||||
Or download and run:
|
||||
```powershell
|
||||
Invoke-WebRequest -Uri "http://77.239.126.123:7272/static/downloads/install_agent.ps1" -OutFile install_agent.ps1
|
||||
powershell -ExecutionPolicy Bypass -File install_agent.ps1 -ServerUrl "http://77.239.126.123:7272" -AgentName "Office-PC"
|
||||
```
|
||||
|
||||
## What it monitors
|
||||
|
||||
- CPU usage (%)
|
||||
- RAM usage (total, used, %)
|
||||
- Disk usage (all drives)
|
||||
- Network adapters (speed, traffic)
|
||||
- GPU info
|
||||
- Uptime
|
||||
- Top 5 processes by CPU
|
||||
|
||||
## Files
|
||||
|
||||
| File | Purpose |
|
||||
|------|---------|
|
||||
| `install_agent.ps1` | Installer (creates task, config) |
|
||||
| `vps_monitor_agent.ps1` | Agent (collects & sends metrics) |
|
||||
| `C:\VPS-Monitor\agent_config.json` | Config (server URL, name) |
|
||||
|
||||
## Management
|
||||
|
||||
```powershell
|
||||
# Check status
|
||||
Get-ScheduledTask 'VPS-Monitor-Agent'
|
||||
|
||||
# Stop
|
||||
Stop-ScheduledTask 'VPS-Monitor-Agent'
|
||||
|
||||
# Start
|
||||
Start-ScheduledTask 'VPS-Monitor-Agent'
|
||||
|
||||
# Uninstall
|
||||
Unregister-ScheduledTask 'VPS-Monitor-Agent' -Confirm:$false
|
||||
Remove-Item -Recurse C:\VPS-Monitor
|
||||
```
|
||||
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 ""
|
||||
160
windows/vps_monitor_agent.ps1
Normal file
160
windows/vps_monitor_agent.ps1
Normal file
@@ -0,0 +1,160 @@
|
||||
# VPS Monitor — Windows PC Agent
|
||||
# Sends system metrics to VPS Monitoring server
|
||||
#
|
||||
# Install: Run install_agent.ps1
|
||||
# Manual: powershell -ExecutionPolicy Bypass -File vps_monitor_agent.ps1
|
||||
|
||||
param(
|
||||
[string]$ServerUrl = "",
|
||||
[string]$AgentName = "",
|
||||
[int]$Interval = 60
|
||||
)
|
||||
|
||||
# Load config
|
||||
$ConfigPath = Join-Path $PSScriptRoot "agent_config.json"
|
||||
if (Test-Path $ConfigPath) {
|
||||
$config = Get-Content $ConfigPath | ConvertFrom-Json
|
||||
if (-not $ServerUrl) { $ServerUrl = $config.server_url }
|
||||
if (-not $AgentName) { $AgentName = $config.agent_name }
|
||||
if ($config.interval) { $Interval = $config.interval }
|
||||
}
|
||||
|
||||
if (-not $ServerUrl -or -not $AgentName) {
|
||||
Write-Host "ERROR: server_url and agent_name required" -ForegroundColor Red
|
||||
Write-Host "Edit agent_config.json or pass -ServerUrl and -AgentName"
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "================================================" -ForegroundColor Cyan
|
||||
Write-Host " VPS Monitor — PC Agent" -ForegroundColor Cyan
|
||||
Write-Host " Server: $ServerUrl" -ForegroundColor Gray
|
||||
Write-Host " Name: $AgentName" -ForegroundColor Gray
|
||||
Write-Host " Interval: ${Interval}s" -ForegroundColor Gray
|
||||
Write-Host "================================================" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
function Get-SystemMetrics {
|
||||
$metrics = @{}
|
||||
|
||||
# Hostname
|
||||
$metrics["hostname"] = $env:COMPUTERNAME
|
||||
|
||||
# OS
|
||||
$os = Get-CimInstance Win32_OperatingSystem
|
||||
$metrics["os"] = "$($os.Caption) $($os.Version)"
|
||||
|
||||
# CPU
|
||||
$cpu = Get-CimInstance Win32_Processor
|
||||
$metrics["cpu_name"] = $cpu.Name
|
||||
$metrics["cpu_percent"] = [math]::Round((Get-Counter '\Processor(_Total)\% Processor Time' -ErrorAction SilentlyContinue).CounterSamples[0].CookedValue, 1)
|
||||
|
||||
# RAM
|
||||
$totalRam = [math]::Round($os.TotalVisibleMemorySize / 1024, 0)
|
||||
$freeRam = [math]::Round($os.FreePhysicalMemory / 1024, 0)
|
||||
$usedRam = $totalRam - $freeRam
|
||||
$metrics["ram_total_mb"] = $totalRam
|
||||
$metrics["ram_used_mb"] = $usedRam
|
||||
$metrics["ram_percent"] = [math]::Round(($usedRam / $totalRam) * 100, 1)
|
||||
|
||||
# Disk (all drives)
|
||||
$disks = @()
|
||||
Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3" | ForEach-Object {
|
||||
$totalGb = [math]::Round($_.Size / 1GB, 1)
|
||||
$freeGb = [math]::Round($_.FreeSpace / 1GB, 1)
|
||||
$usedGb = $totalGb - $freeGb
|
||||
$pct = if ($totalGb -gt 0) { [math]::Round(($usedGb / $totalGb) * 100, 1) } else { 0 }
|
||||
$disks += @{
|
||||
drive = $_.DeviceID
|
||||
total_gb = $totalGb
|
||||
used_gb = $usedGb
|
||||
free_gb = $freeGb
|
||||
percent = $pct
|
||||
}
|
||||
}
|
||||
$metrics["disks"] = $disks
|
||||
|
||||
# Primary disk summary
|
||||
$primary = $disks | Where-Object { $_.drive -eq "C:" } | Select-Object -First 1
|
||||
if ($primary) {
|
||||
$metrics["disk_total_gb"] = $primary.total_gb
|
||||
$metrics["disk_used_gb"] = $primary.used_gb
|
||||
$metrics["disk_percent"] = $primary.percent
|
||||
}
|
||||
|
||||
# Network
|
||||
$adapters = Get-NetAdapter -Physical -ErrorAction SilentlyContinue | Where-Object { $_.Status -eq "Up" }
|
||||
$netInfo = @()
|
||||
foreach ($a in $adapters) {
|
||||
$stats = Get-NetAdapterStatistics -Name $a.Name -ErrorAction SilentlyContinue
|
||||
$netInfo += @{
|
||||
name = $a.Name
|
||||
speed_mbps = [math]::Round($a.LinkSpeed.Replace(" Gbps","000").Replace(" Mbps","") -as [double], 0)
|
||||
sent_mb = if ($stats) { [math]::Round($stats.SentBytes / 1MB, 0) } else { 0 }
|
||||
received_mb = if ($stats) { [math]::Round($stats.ReceivedBytes / 1MB, 0) } else { 0 }
|
||||
}
|
||||
}
|
||||
$metrics["network"] = $netInfo
|
||||
|
||||
# Uptime
|
||||
$uptime = (Get-Date) - $os.LastBootUpTime
|
||||
$metrics["uptime"] = "up $($uptime.Days)d $($uptime.Hours)h $($uptime.Minutes)m"
|
||||
$metrics["uptime_seconds"] = [math]::Round($uptime.TotalSeconds, 0)
|
||||
|
||||
# GPU (if available)
|
||||
try {
|
||||
$gpu = Get-CimInstance Win32_VideoController | Select-Object -First 1
|
||||
if ($gpu) {
|
||||
$metrics["gpu_name"] = $gpu.Name
|
||||
$metrics["gpu_ram_mb"] = [math]::Round($gpu.AdapterRAM / 1MB, 0)
|
||||
}
|
||||
} catch {}
|
||||
|
||||
# Top processes by CPU
|
||||
$topProcs = Get-Process | Sort-Object CPU -Descending | Select-Object -First 5 |
|
||||
ForEach-Object { @{ name = $_.ProcessName; cpu = [math]::Round($_.CPU, 1); ram_mb = [math]::Round($_.WorkingSet64 / 1MB, 0) } }
|
||||
$metrics["top_processes"] = $topProcs
|
||||
|
||||
return $metrics
|
||||
}
|
||||
|
||||
function Send-Metrics($metrics) {
|
||||
$body = @{
|
||||
agent_name = $AgentName
|
||||
timestamp = (Get-Date).ToString("o")
|
||||
metrics = $metrics
|
||||
} | ConvertTo-Json -Depth 5
|
||||
|
||||
try {
|
||||
$response = Invoke-RestMethod -Uri "$ServerUrl/api/pc/heartbeat" `
|
||||
-Method POST `
|
||||
-Body $body `
|
||||
-ContentType "application/json" `
|
||||
-TimeoutSec 15
|
||||
|
||||
return $true
|
||||
} catch {
|
||||
Write-Host " [!] Send failed: $($_.Exception.Message)" -ForegroundColor Yellow
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
# Main loop
|
||||
Write-Host "Agent started. Press Ctrl+C to stop." -ForegroundColor Green
|
||||
Write-Host ""
|
||||
|
||||
while ($true) {
|
||||
$ts = Get-Date -Format "HH:mm:ss"
|
||||
|
||||
try {
|
||||
$metrics = Get-SystemMetrics
|
||||
$ok = Send-Metrics $metrics
|
||||
|
||||
if ($ok) {
|
||||
Write-Host "[$ts] OK — CPU: $($metrics.cpu_percent)% RAM: $($metrics.ram_percent)% Disk: $($metrics.disk_percent)%" -ForegroundColor Green
|
||||
}
|
||||
} catch {
|
||||
Write-Host "[$ts] ERROR: $($_.Exception.Message)" -ForegroundColor Red
|
||||
}
|
||||
|
||||
Start-Sleep -Seconds $Interval
|
||||
}
|
||||
Reference in New Issue
Block a user