KtvSetup/scripts/heartbeat.ps1

94 lines
2.7 KiB
PowerShell
Raw Permalink Normal View History

2025-05-22 13:57:37 +08:00
$logPath = "C:\scripts\heartbeat.log"
function Log($msg) {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $logPath -Value "[$timestamp] $msg"
}
# Load config
2025-05-22 09:03:51 +08:00
$configPath = "C:\scripts\config.json"
if (!(Test-Path $configPath)) {
2025-05-22 13:57:37 +08:00
Log "error Config file not found: $configPath"
2025-05-22 09:03:51 +08:00
exit 1
}
2025-05-22 13:57:37 +08:00
try {
$config = Get-Content $configPath -Encoding UTF8 | Out-String | ConvertFrom-Json
} catch {
Log "error Failed to read config.json: $($_.Exception.Message)"
exit 1
}
2025-05-22 09:03:51 +08:00
2025-05-22 13:57:37 +08:00
# Config values
$apiUrl = $config.apiUrl.TrimEnd('/') # 移除結尾的 /
$branchName = $config.branchName
2025-05-22 09:03:51 +08:00
$email = $config.email
$password = $config.password
2025-05-22 13:57:37 +08:00
Log "📡 Using API URL: $apiUrl"
# Get IP address
2025-05-22 09:03:51 +08:00
$ip = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {
$_.IPAddress -notlike "169.*" -and $_.IPAddress -notlike "127.*"
}).IPAddress
function Get-Token {
$body = @{
branch_name = $branchName
room_name = $env:COMPUTERNAME
2025-05-22 13:57:37 +08:00
room_ip = $ip
2025-05-22 09:03:51 +08:00
email = $email
password = $password
} | ConvertTo-Json -Depth 3
2025-05-22 13:57:37 +08:00
$url = "$apiUrl/api/room/receiveRegister"
Log "curl Requesting token from: $url"
# 將送出的 JSON log 起來
Log "curl Payload: $body"
2025-05-22 13:57:37 +08:00
try {
$response = Invoke-RestMethod -Uri $url -Method Post `
-Body $body `
-ContentType "application/json; charset=utf-8"
2025-05-22 09:03:51 +08:00
2025-05-22 13:57:37 +08:00
if ($response.data.token) {
Log "ok Token acquired: $($response.data.token)"
return $response.data.token
} else {
Log "error Failed to acquire token (no token in response)"
exit 1
}
} catch {
Log "error Token request failed: $($_.Exception.Message)"
2025-05-22 09:03:51 +08:00
exit 1
}
}
function Send-Heartbeat($token, $ip) {
Log "start Send - Heartbeat $token ,$ip"
2025-05-22 09:03:51 +08:00
while ($true) {
$heartbeat = @{
branch_name = $branchName
2025-05-22 09:03:51 +08:00
hostname = $env:COMPUTERNAME
ip = $ip
cpu = [math]::Round((Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue, 2)
memory = [math]::Round((Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue, 2)
disk = [math]::Round((Get-PSDrive -Name C).Used / 1MB, 2)
} | ConvertTo-Json -Depth 3
try {
Invoke-RestMethod -Uri "$apiUrl/api/room/heartbeat" -Method Post `
-Headers @{ Authorization = "Bearer $token" } `
-Body $heartbeat -ContentType "application/json; charset=utf-8"
2025-05-22 09:03:51 +08:00
} catch {
2025-05-22 13:57:37 +08:00
Log "error Heartbeat failed: $($_.Exception.Message)"
2025-05-22 09:03:51 +08:00
}
Start-Sleep -Seconds 60
}
}
2025-05-22 13:57:37 +08:00
# Main
2025-05-22 09:03:51 +08:00
$token = Get-Token
2025-05-22 13:57:37 +08:00
Send-Heartbeat -token $token -ip $ip