Fold alerts under their host, recurring maintenance, run playbooks from alerts, and an Updates page
- Servers can say what they run on (or it is worked out from the Proxmox node); when the host is down the guests' offline alerts fold under it: held back, not counted, not notified, still recorded. - Recurring maintenance schedules made into ordinary windows a week ahead (maintenance_schedules). - Run a Semaphore template limited to a server from its page or an alert, and watch the run. - Agents report pending updates (apt, dnf, zypper, Windows Update); an Updates page lists them. - Migrations 0024 and 0025. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
44e393f172
commit
171511c6db
45 files changed
+6772
-44
No files matched your search
@@ -182,6 +182,46 @@ collect_systemd_timers() {
|
||||
done < <(jq -c '.[]' <<<"$timers_json")
|
||||
}
|
||||
|
||||
# Updates waiting to be installed, as JSON in UPDATES_JSON ("null" when this host's package manager isn't one we know, or couldn't answer).
|
||||
# Nothing is installed or refreshed: this reads what the package manager already knows about, so it is quick and changes nothing.
|
||||
collect_updates() {
|
||||
UPDATES_JSON="null"
|
||||
local manager="" count="" security="null" reboot="false" out="" status=0
|
||||
|
||||
if command -v apt-get >/dev/null 2>&1; then
|
||||
manager="apt"
|
||||
# -s simulates: "Inst" lines are what an upgrade would install. Security ones come from a "-security" or "Security" archive.
|
||||
if out=$(LC_ALL=C timeout 60 apt-get -s -o Debug::NoLocking=true upgrade 2>/dev/null); then
|
||||
count=$(grep -c '^Inst ' <<<"$out" || true)
|
||||
security=$(grep '^Inst ' <<<"$out" | grep -ci 'security' || true)
|
||||
fi
|
||||
[[ -f /var/run/reboot-required ]] && reboot="true"
|
||||
elif command -v dnf >/dev/null 2>&1; then
|
||||
manager="dnf"
|
||||
# check-update exits 100 when there are updates, 0 when none, 1 on an error. --cacheonly: never goes to the network from here.
|
||||
out=$(LC_ALL=C timeout 120 dnf -q check-update --cacheonly 2>/dev/null) || status=$?
|
||||
if [[ "$status" -eq 0 || "$status" -eq 100 ]]; then
|
||||
count=$(awk 'NF >= 3 && $1 ~ /^[A-Za-z0-9_.+-]+\.[A-Za-z0-9_]+$/ && /Obsoleting/ == 0 { n++ } /^Obsoleting/ { exit } END { print n + 0 }' <<<"$out")
|
||||
security=$(LC_ALL=C timeout 120 dnf -q updateinfo list --security --cacheonly 2>/dev/null | grep -c '/Sec\.' || true)
|
||||
fi
|
||||
if command -v needs-restarting >/dev/null 2>&1; then
|
||||
needs-restarting -r >/dev/null 2>&1 || reboot="true"
|
||||
fi
|
||||
elif command -v zypper >/dev/null 2>&1; then
|
||||
manager="zypper"
|
||||
if out=$(LC_ALL=C timeout 120 zypper -q --no-refresh list-updates 2>/dev/null); then
|
||||
count=$(grep -c '^v |' <<<"$out" || true)
|
||||
fi
|
||||
[[ -f /run/reboot-needed ]] && reboot="true"
|
||||
fi
|
||||
|
||||
if [[ -n "$manager" && "$count" =~ ^[0-9]+$ ]]; then
|
||||
[[ "$security" =~ ^[0-9]+$ ]] || security="null"
|
||||
UPDATES_JSON=$(jq -n --arg manager "$manager" --argjson count "$count" --argjson security "$security" --argjson reboot "$reboot" \
|
||||
'{manager: $manager, count: $count, security: $security, reboot_required: $reboot}')
|
||||
fi
|
||||
}
|
||||
|
||||
collect_system_info() {
|
||||
local ip_json="[]"
|
||||
if command -v ip >/dev/null 2>&1; then
|
||||
@@ -243,7 +283,12 @@ collect_system_info() {
|
||||
')
|
||||
fi
|
||||
|
||||
# Best-effort like the rest: a package manager that misbehaves must not cost the report.
|
||||
UPDATES_JSON="null"
|
||||
collect_updates || UPDATES_JSON="null"
|
||||
|
||||
SYSTEM_JSON=$(jq -n \
|
||||
--argjson updates "$UPDATES_JSON" \
|
||||
--argjson ip_addresses "$ip_json" \
|
||||
--argjson listening_ports "$ports_json" \
|
||||
--arg cpu_model "$cpu_model" \
|
||||
@@ -257,7 +302,8 @@ collect_system_info() {
|
||||
cpu: { model: $cpu_model, cores: $cpu_cores, load_percent: $cpu_load_percent },
|
||||
memory: { total_bytes: $mem_total_bytes, used_bytes: $mem_used_bytes },
|
||||
disks: $disks,
|
||||
listening_ports: $listening_ports
|
||||
listening_ports: $listening_ports,
|
||||
updates: $updates
|
||||
}')
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ runs as a scheduled task (as SYSTEM, every 15 minutes) and pushes to `POST /api/
|
||||
the machine never needs to be reachable from Homelab Manager.
|
||||
|
||||
|
||||
Each report also says how many updates Windows Update has waiting (and how many are security or critical, and whether a restart is due). Searching Windows Update goes over the network and can take a minute or two, so it is done at most every six hours; the answer is kept in `%ProgramData%\HomelabManager\updates.json` and reused between. Nothing is downloaded or installed.
|
||||
|
||||
The installer limits `%ProgramData%\HomelabManager` to SYSTEM and Administrators. The agent runs as SYSTEM, so anyone who could change its script could run code as SYSTEM; the folder is not left to inherit ProgramData's default permissions. Re-running the install one-liner applies this to an existing install.
|
||||
## Install
|
||||
|
||||
|
||||
@@ -220,6 +220,50 @@ function Get-ListeningPorts {
|
||||
return @($rows | Where-Object { $_.port -ge 1 -and $_.port -le 65535 } | Select-Object -First 2000)
|
||||
}
|
||||
|
||||
# ---- updates waiting to be installed ------------------------------------------------------------
|
||||
$script:UpdatesCachePath = Join-Path $env:ProgramData 'HomelabManager\updates.json'
|
||||
|
||||
function Test-RebootRequired {
|
||||
foreach ($key in @(
|
||||
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired',
|
||||
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending')) {
|
||||
if (Test-Path $key) { return $true }
|
||||
}
|
||||
return $false
|
||||
}
|
||||
|
||||
# Asks Windows Update what is waiting. That goes over the network and can take a minute, so it is done at most every six hours and the
|
||||
# last answer is reused in between; whether a restart is due is cheap and always read fresh. Nothing is installed or downloaded.
|
||||
function Get-PendingUpdates {
|
||||
$result = $null
|
||||
try {
|
||||
if (Test-Path $script:UpdatesCachePath) {
|
||||
$cached = Get-Content -Raw -Path $script:UpdatesCachePath | ConvertFrom-Json
|
||||
$checked = [datetime]::Parse([string]$cached.checked_at, [Globalization.CultureInfo]::InvariantCulture, [Globalization.DateTimeStyles]::RoundtripKind)
|
||||
if (([datetime]::UtcNow - $checked.ToUniversalTime()).TotalHours -lt 6) { $result = $cached }
|
||||
}
|
||||
} catch { $result = $null }
|
||||
|
||||
if (-not $result) {
|
||||
$session = New-Object -ComObject Microsoft.Update.Session
|
||||
$found = $session.CreateUpdateSearcher().Search("IsInstalled=0 and IsHidden=0 and Type='Software'")
|
||||
$security = 0
|
||||
foreach ($u in $found.Updates) {
|
||||
foreach ($c in $u.Categories) {
|
||||
if ($c.Name -eq 'Security Updates' -or $c.Name -eq 'Critical Updates') { $security++; break }
|
||||
}
|
||||
}
|
||||
$result = [pscustomobject][ordered]@{ checked_at = [datetime]::UtcNow.ToString('o'); count = [int]$found.Updates.Count; security = [int]$security }
|
||||
try {
|
||||
$dir = Split-Path $script:UpdatesCachePath
|
||||
if (-not (Test-Path $dir)) { $null = New-Item -ItemType Directory -Path $dir -Force }
|
||||
ConvertTo-Json -InputObject $result | Set-Content -Path $script:UpdatesCachePath -Encoding UTF8
|
||||
} catch { }
|
||||
}
|
||||
|
||||
return [ordered]@{ manager = 'windows-update'; count = [int]$result.count; security = [int]$result.security; reboot_required = (Test-RebootRequired) }
|
||||
}
|
||||
|
||||
function Get-SystemInfo {
|
||||
$ips = @(Get-NetIPAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue |
|
||||
Where-Object { $_.IPAddress -notlike '127.*' -and $_.IPAddress -notlike '169.254.*' -and $_.AddressState -eq 'Preferred' } |
|
||||
@@ -240,7 +284,12 @@ function Get-SystemInfo {
|
||||
$disks += [pscustomobject][ordered]@{ mount = [string]$d.DeviceID; size_bytes = [int64]$d.Size; used_bytes = [int64]($d.Size - $d.FreeSpace) }
|
||||
}
|
||||
|
||||
# Best-effort: Windows Update being unreachable must not cost the rest of the report.
|
||||
$updates = $null
|
||||
try { $updates = Get-PendingUpdates } catch { Write-Warning "Checking for updates failed. ($($_.Exception.Message))" }
|
||||
|
||||
return [ordered]@{
|
||||
updates = $updates
|
||||
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) }
|
||||
|
||||
Reference in new issue
Block a user