Host health from the Linux agent, agent self-update, and disk forecast in /metrics
- Linux agent v4: failed systemd services, SMART disk health, ZFS pool state, pacman updates; SMART/ZFS and self-update run in the ExecStartPre=-+ step outside the sandbox - Server: hosts' health stored (servers.host_health, migration 0029), alerts under a new "Failing services" kind plus disk/ZFS alerts, Host health card, Prometheus series - Agent self-update (Linux v4, Windows v3): admin switch on the Servers page, checksum-verified download, atomic replace; GET /api/agent/report/update - homelab_server_disk_days_to_full in /metrics - Tests (server, real PowerShell against a fake server) and docs Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
540ab704b7
commit
5dc6cea69c
38 files changed
+3884
-73
No files matched your search
@@ -33,7 +33,8 @@ $ErrorActionPreference = 'Stop'
|
||||
|
||||
# Which version of this script this is, sent with every report so Homelab Manager can tell which agents are behind. Raise it whenever a
|
||||
# change here is worth installing the agent again for. 1 = before the number was reported 2 = reports pending updates, and its own version
|
||||
$script:AgentVersion = 2
|
||||
# 3 = can replace itself with the newer script when an administrator has switched that on in Homelab Manager
|
||||
$script:AgentVersion = 3
|
||||
|
||||
$script:DefaultConfigPath = Join-Path $env:ProgramData 'HomelabManager\agent.json'
|
||||
|
||||
@@ -344,6 +345,70 @@ function Send-Report($Config, [string]$Json) {
|
||||
return Invoke-RestMethod @params
|
||||
}
|
||||
|
||||
# ---- updating itself -------------------------------------------------------------
|
||||
|
||||
# A GET to the server with the token, set up the same way as the report (modern TLS on 5.1, and the self-signed switch).
|
||||
function New-GetParams($Config, [string]$Path) {
|
||||
$params = @{
|
||||
Uri = "$($Config.ApiUrl)$Path"
|
||||
Method = 'Get'
|
||||
Headers = @{ Authorization = "Bearer $($Config.ApiToken)" }
|
||||
TimeoutSec = 60
|
||||
}
|
||||
if ($PSVersionTable.PSVersion.Major -ge 6) {
|
||||
if ($Config.Insecure) { $params['SkipCertificateCheck'] = $true }
|
||||
} else {
|
||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||
if ($Config.Insecure) { [Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } }
|
||||
$params['UseBasicParsing'] = $true
|
||||
}
|
||||
return $params
|
||||
}
|
||||
|
||||
# Whether a downloaded copy may replace the running script: it is newer, it is exactly the file the server vouched for (SHA-256), it says the
|
||||
# version that was promised, and it parses. Anything else is thrown away.
|
||||
function Test-AgentDownload([string]$Path, [string]$ExpectedSha256, [int]$NewVersion, [int]$CurrentVersion) {
|
||||
if ($NewVersion -le $CurrentVersion) { return $false }
|
||||
if ($ExpectedSha256 -notmatch '^[0-9a-f]{64}$') { return $false }
|
||||
$got = (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()
|
||||
if ($got -ne $ExpectedSha256) { return $false }
|
||||
$text = [System.IO.File]::ReadAllText($Path)
|
||||
if ($text -notmatch ('(?m)^\$script:AgentVersion\s*=\s*' + $NewVersion + '\s*$')) { return $false }
|
||||
$tokens = $null
|
||||
$errors = $null
|
||||
[void][System.Management.Automation.Language.Parser]::ParseInput($text, [ref]$tokens, [ref]$errors)
|
||||
return ($errors.Count -eq 0)
|
||||
}
|
||||
|
||||
# Replaces this script with the one this Homelab Manager serves, when an administrator has switched automatic updates on and this agent is
|
||||
# behind. The new one runs from the next report. Nothing goes wrong if it can't: the report carries on as it was. Only a script that is
|
||||
# installed under its own name is ever replaced, so running a copy from somewhere else (a test, a checkout) never changes it.
|
||||
function Update-AgentSelf($Config, [string]$Self = $PSCommandPath) {
|
||||
$self = $Self
|
||||
if (-not $self -or (Split-Path -Leaf $self) -ne 'homelab-manager-agent.ps1') { return }
|
||||
$download = "$self.download"
|
||||
try {
|
||||
$ask = New-GetParams $Config "/api/agent/report/update?version=$($script:AgentVersion)"
|
||||
$reply = Invoke-RestMethod @ask
|
||||
if (-not $reply.update) { return }
|
||||
$version = [int]$reply.version
|
||||
$fetch = New-GetParams $Config '/agent/windows/report-tasks.ps1'
|
||||
$fetch.Remove('Headers')
|
||||
$fetch['OutFile'] = $download
|
||||
$null = Invoke-WebRequest @fetch
|
||||
if (-not (Test-AgentDownload $download ([string]$reply.sha256) $version $script:AgentVersion)) {
|
||||
[Console]::Error.WriteLine("Note: the newer agent (version $version) didn't check out against what the server said, so it was not installed.")
|
||||
return
|
||||
}
|
||||
Move-Item -LiteralPath $download -Destination $self -Force
|
||||
[Console]::Error.WriteLine("Updated the agent from version $($script:AgentVersion) to version $version; the new one runs at the next report.")
|
||||
} catch {
|
||||
[Console]::Error.WriteLine("Note: couldn't check for, or install, a newer agent: $($_.Exception.Message)")
|
||||
} finally {
|
||||
if (Test-Path -LiteralPath $download) { Remove-Item -LiteralPath $download -Force -ErrorAction SilentlyContinue }
|
||||
}
|
||||
}
|
||||
|
||||
# ---- main -----------------------------------------------------------------------
|
||||
|
||||
# A plain one-line message on stderr: Write-Error would bury it in a stack trace on every manual run.
|
||||
@@ -358,6 +423,9 @@ function Invoke-Agent {
|
||||
Stop-Agent "API_URL and API_TOKEN must be set (environment variables, or $script:DefaultConfigPath)."
|
||||
}
|
||||
|
||||
# A dry run reports nothing and changes nothing, so it never updates the agent either.
|
||||
if (-not $DryRun) { Update-AgentSelf $config }
|
||||
|
||||
$tasks = @()
|
||||
try {
|
||||
$tasks = @(Get-ReportedTasks $config.IncludeMicrosoftTasks)
|
||||
|
||||
Reference in new issue
Block a user