Dependencies beyond servers, web push, inventory auto-fill with QR labels, Windows agent health

- Dependencies: servers and integrations can depend on each other; alerts are folded under the root cause, and a failure explained by something down isn't announced
- Web push: hand-written VAPID and RFC 8291 encryption, per-device subscriptions, a fifth notification channel, service worker handlers
- Inventory: maker/model/serial from the agents fill blank fields, "Refresh from agent", QR label printing and ?asset= deep links
- Windows agent (v4) reports failed services, disk health, Storage Spaces pools and hardware identity; Linux agent (v5) reports hardware identity

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-18 18:53:30 +02:00
1 parent 5cc51c57de
commit ec9c2f73f0
55 files changed
+12553 -65

No files matched your search

+125 -4
View File
@@ -34,14 +34,15 @@ $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
# 3 = can replace itself with the newer script when an administrator has switched that on in Homelab Manager
$script:AgentVersion = 3
# 4 = also reports services that have failed, disk health, Storage Spaces pools and what the machine is (maker, model, serial)
$script:AgentVersion = 4
$script:DefaultConfigPath = Join-Path $env:ProgramData 'HomelabManager\agent.json'
# ---- configuration ------------------------------------------------------------
function Get-AgentConfig {
$config = @{ ApiUrl = $null; ApiToken = $null; Insecure = $false; IncludeMicrosoftTasks = $false }
$config = @{ ApiUrl = $null; ApiToken = $null; Insecure = $false; IncludeMicrosoftTasks = $false; IgnoreServices = @() }
$path = if ($env:HLM_CONFIG) { $env:HLM_CONFIG } else { $script:DefaultConfigPath }
if (Test-Path -LiteralPath $path) {
@@ -50,6 +51,7 @@ function Get-AgentConfig {
if ($file.apiToken) { $config.ApiToken = [string]$file.apiToken }
if ($null -ne $file.insecure) { $config.Insecure = [bool]$file.insecure }
if ($null -ne $file.includeMicrosoftTasks) { $config.IncludeMicrosoftTasks = [bool]$file.includeMicrosoftTasks }
if ($file.ignoreServices) { $config.IgnoreServices = @($file.ignoreServices | ForEach-Object { [string]$_ }) }
}
# Environment variables win over the file, like the Linux agent.
@@ -57,6 +59,7 @@ function Get-AgentConfig {
if ($env:API_TOKEN) { $config.ApiToken = $env:API_TOKEN }
if ($env:API_INSECURE) { $config.Insecure = $env:API_INSECURE -match '^(1|true|yes)$' }
if ($env:INCLUDE_MICROSOFT_TASKS) { $config.IncludeMicrosoftTasks = $env:INCLUDE_MICROSOFT_TASKS -match '^(1|true|yes)$' }
if ($env:IGNORE_SERVICES) { $config.IgnoreServices = @($env:IGNORE_SERVICES -split ',' | ForEach-Object { $_.Trim() } | Where-Object { $_ }) }
if ($config.ApiUrl) { $config.ApiUrl = $config.ApiUrl.TrimEnd('/') }
return $config
@@ -288,7 +291,117 @@ function Get-PendingUpdates {
return [ordered]@{ manager = 'windows-update'; count = [int]$cached.count; security = [int]$cached.security; reboot_required = (Test-RebootRequired) }
}
function Get-SystemInfo {
# ---- failed services, disk health, storage pools and what the machine is -------------------------
# A service has "failed" when it is set to start by itself, isn't running, and stopped with an error: an exit code that is neither 0 (it
# finished its job, as many do) nor 1077 (it has never been started since boot, which is what a service that starts on a trigger
# looks like). Services named in IgnoreServices (names or patterns like 'gupdate*') are left out. $Services are Win32_Service objects.
function ConvertTo-FailedUnits($Services, [string[]]$Ignore = @()) {
$out = @()
foreach ($svc in @($Services)) {
if ($null -eq $svc -or [string]$svc.StartMode -ne 'Auto') { continue }
if ([string]$svc.State -eq 'Running' -or [string]$svc.State -like '*Pending*') { continue }
$code = 0
if ($null -ne $svc.ExitCode) { $code = [int]$svc.ExitCode }
if ($code -eq 0 -or $code -eq 1077) { continue }
$name = [string]$svc.Name
$skip = $false
foreach ($pattern in @($Ignore)) { if ($pattern -and $name -like $pattern) { $skip = $true; break } }
if ($skip) { continue }
$out += [pscustomobject][ordered]@{ unit = $name; description = [string]$svc.DisplayName }
}
return @($out | Select-Object -First 100)
}
# Windows' own verdict on each physical disk (Healthy passes; Warning and Unhealthy don't), with temperature and how much of an SSD's
# life is used where the drive says. $Disks are Get-PhysicalDisk objects; $Counters maps a disk's DeviceId to its reliability counter.
function ConvertTo-DiskHealth($Disks, $Counters = @{}) {
$out = @()
foreach ($d in @($Disks)) {
if ($null -eq $d -or [string]$d.BusType -eq 'File Backed Virtual') { continue }
$health = [string]$d.HealthStatus
$healthy = $null
if ($health -eq 'Healthy') { $healthy = $true } elseif ($health -eq 'Warning' -or $health -eq 'Unhealthy') { $healthy = $false }
$counter = $null
if ($Counters -is [hashtable] -and $Counters.ContainsKey([string]$d.DeviceId)) { $counter = $Counters[[string]$d.DeviceId] }
$temp = $null
$wear = $null
if ($null -ne $counter) {
if ($null -ne $counter.Temperature -and [int]$counter.Temperature -gt 0 -and [int]$counter.Temperature -lt 150) { $temp = [int]$counter.Temperature }
if ($null -ne $counter.Wear -and [int]$counter.Wear -ge 0 -and [int]$counter.Wear -le 255) { $wear = [int]$counter.Wear }
}
$out += [pscustomobject][ordered]@{ device = ('PhysicalDisk' + [string]$d.DeviceId); model = ([string]$d.FriendlyName).Trim(); healthy = $healthy; temperature_c = $temp; wear_percent = $wear }
}
return @($out | Select-Object -First 100)
}
# Storage Spaces pools, in the words the server uses for ZFS pools: ONLINE, DEGRADED or FAULTED. A pool Windows can't say anything about is left out.
function ConvertTo-PoolHealth($Pools) {
$out = @()
foreach ($p in @($Pools)) {
if ($null -eq $p) { continue }
$health = switch ([string]$p.HealthStatus) { 'Healthy' { 'ONLINE' } 'Warning' { 'DEGRADED' } 'Unhealthy' { 'FAULTED' } default { $null } }
if (-not $health) { continue }
$capacity = $null
if ($p.Size -and [double]$p.Size -gt 0 -and $null -ne $p.AllocatedSize) { $capacity = [math]::Round(([double]$p.AllocatedSize / [double]$p.Size) * 100, 0) }
$out += [pscustomobject][ordered]@{ name = [string]$p.FriendlyName; health = $health; capacity_percent = $capacity }
}
return @($out | Select-Object -First 100)
}
# The three parts are asked for separately and each only if it can be: a part that can't be read is left out of the report (the server
# then keeps what it last said), never reported as "all well".
function Get-HostHealth($Config = $null) {
$health = [ordered]@{}
$ignore = @()
if ($Config -and $Config.IgnoreServices) { $ignore = @($Config.IgnoreServices) }
try { $health['failed_units'] = @(ConvertTo-FailedUnits @(Get-CimInstance -ClassName Win32_Service -ErrorAction Stop) $ignore) } catch { Write-Warning "Reading services failed. ($($_.Exception.Message))" }
try {
$disks = @(Get-PhysicalDisk -ErrorAction Stop)
$counters = @{}
foreach ($d in $disks) {
try { $counters[[string]$d.DeviceId] = Get-StorageReliabilityCounter -PhysicalDisk $d -ErrorAction Stop } catch { }
}
$health['smart'] = @(ConvertTo-DiskHealth $disks $counters)
} catch { Write-Warning "Reading disk health failed. ($($_.Exception.Message))" }
try {
$health['zfs'] = @(ConvertTo-PoolHealth @(Get-StoragePool -ErrorAction Stop | Where-Object { -not $_.IsPrimordial }))
} catch {
# A machine with no Storage Spaces pools gets an error, not an empty list: that is an answer ("no pools"), not a failure to read.
if ($_.FullyQualifiedErrorId -like '*NotFound*') { $health['zfs'] = @() } else { Write-Warning "Reading storage pools failed. ($($_.Exception.Message))" }
}
if ($health.Count -eq 0) { return $null }
return $health
}
# Words a maker puts in the serial number or model when it has nothing to say there.
function Test-PlaceholderText([string]$Text) {
$t = $Text.Trim()
if ($t.Length -eq 0) { return $true }
return $t -match '^(to be filled by o\.?e\.?m\.?|default string|system serial number|system product name|system manufacturer|not specified|not applicable|none|n/a|unknown|0+|o\.?e\.?m\.?)$'
}
# What the machine is: maker, model and serial number, from the firmware. For inventory - a virtual machine says what it pretends to be.
# A home-built PC has nothing in the system's own fields, so the motherboard's are used when those say nothing.
function Get-HardwareInfo {
$cs = Get-CimInstance -ClassName Win32_ComputerSystem -ErrorAction Stop
$bios = $null
$board = $null
try { $bios = Get-CimInstance -ClassName Win32_BIOS -ErrorAction Stop } catch { }
try { $board = Get-CimInstance -ClassName Win32_BaseBoard -ErrorAction Stop } catch { }
$first = { param($values) foreach ($v in $values) { if (-not (Test-PlaceholderText ([string]$v))) { return ([string]$v).Trim() } } return $null }
$hw = [ordered]@{}
$maker = & $first @($cs.Manufacturer, $board.Manufacturer)
$model = & $first @($cs.Model, $board.Product)
$serial = & $first @($bios.SerialNumber, $board.SerialNumber)
if ($maker) { $hw['manufacturer'] = $maker }
if ($model) { $hw['model'] = $model }
if ($serial) { $hw['serial'] = $serial }
if ($hw.Count -eq 0) { return $null }
return $hw
}
function Get-SystemInfo($Config = $null) {
$ips = @(Get-NetIPAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue |
Where-Object { $_.IPAddress -notlike '127.*' -and $_.IPAddress -notlike '169.254.*' -and $_.AddressState -eq 'Preferred' } |
ForEach-Object { $_.IPAddress } | Select-Object -Unique)
@@ -312,8 +425,16 @@ function Get-SystemInfo {
$updates = $null
try { $updates = Get-PendingUpdates } catch { Write-Warning "Checking for updates failed. ($($_.Exception.Message))" }
# The same for what is wrong with the machine, and what it is: neither may cost the rest of the report.
$health = $null
try { $health = Get-HostHealth $Config } catch { Write-Warning "Reading host health failed. ($($_.Exception.Message))" }
$hardware = $null
try { $hardware = Get-HardwareInfo } catch { Write-Warning "Reading the hardware identity failed. ($($_.Exception.Message))" }
return [ordered]@{
updates = $updates
health = $health
hardware = $hardware
ip_addresses = @($ips)
cpu = [ordered]@{ model = [string]($cpus[0].Name).Trim(); cores = [int]$cores; load_percent = $(if ($null -ne $load) { [math]::Round([double]$load, 1) } else { $null }) }
memory = [ordered]@{ total_bytes = $totalBytes; used_bytes = ($totalBytes - $freeBytes) }
@@ -437,7 +558,7 @@ function Invoke-Agent {
# Hardware and network facts are best-effort, like the Linux agent: a quirk on one host must not cost the whole report.
$system = $null
try {
$system = Get-SystemInfo
$system = Get-SystemInfo $config
} catch {
Write-Warning "Collecting hardware/network info failed - reporting tasks without it. ($($_.Exception.Message))"
}