ops: notify on production health transitions
CI / test (push) Successful in 11s
CI / test (pull_request) Successful in 10s

This commit is contained in:
m
2026-08-16 18:29:26 +03:00
parent 4016820541
commit 5a07a13ac8
6 changed files with 230 additions and 4 deletions
@@ -0,0 +1,20 @@
[CmdletBinding()]
param(
[string]$HealthScriptPath = (Join-Path $PSScriptRoot 'Test-DomovoyProductionHealth.ps1'),
[string]$NotificationScriptPath = (Join-Path $PSScriptRoot 'Send-DomovoyHealthNotification.ps1')
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
if (-not (Test-Path -LiteralPath $HealthScriptPath -PathType Leaf)) { throw 'Health monitoring script not found' }
if (-not (Test-Path -LiteralPath $NotificationScriptPath -PathType Leaf)) { throw 'Health notification script not found' }
$pwsh = (Get-Command pwsh.exe -ErrorAction Stop).Source
& $pwsh -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $HealthScriptPath
$healthExitCode = $LASTEXITCODE
& $pwsh -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $NotificationScriptPath
$notificationExitCode = $LASTEXITCODE
if ($notificationExitCode -ne 0) { exit 2 }
exit $healthExitCode
@@ -1,6 +1,6 @@
[CmdletBinding()]
param(
[string]$ScriptPath = '\\nas\MST\codex\assistent\deploy\windows\Test-DomovoyProductionHealth.ps1',
[string]$ScriptPath = '\\nas\MST\codex\assistent\deploy\windows\Invoke-DomovoyProductionMonitoring.ps1',
[string]$TaskName = 'Domovoy Production Health Monitor'
)
@@ -16,5 +16,5 @@ $repeat = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) -Repetitio
$logon = New-ScheduledTaskTrigger -AtLogOn -User $identity
$principal = New-ScheduledTaskPrincipal -UserId $identity -LogonType Interactive -RunLevel Limited
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -MultipleInstances IgnoreNew -ExecutionTimeLimit (New-TimeSpan -Minutes 10)
Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger @($repeat, $logon) -Principal $principal -Settings $settings -Description 'Monitor Domovoy production health, TLS, DNS, backups, and schedules.' -Force | Out-Null
Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger @($repeat, $logon) -Principal $principal -Settings $settings -Description 'Monitor Domovoy production health and notify the interactive user about transitions.' -Force | Out-Null
Write-Output "Scheduled task registered: $TaskName"
@@ -0,0 +1,125 @@
[CmdletBinding()]
param(
[string]$StatusPath = (Join-Path $env:LOCALAPPDATA 'Domovoy\monitoring\status.json'),
[string]$StatePath = (Join-Path $env:LOCALAPPDATA 'Domovoy\monitoring\notification-state.json'),
[ValidateRange(1, 168)]
[int]$ReminderHours = 6,
[switch]$Force,
[switch]$NoDisplay
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
function Write-AtomicJson {
param([Parameter(Mandatory)]$Value, [Parameter(Mandatory)][string]$Path)
$directory = Split-Path -Parent $Path
if (-not (Test-Path -LiteralPath $directory -PathType Container)) {
New-Item -ItemType Directory -Path $directory -Force | Out-Null
}
$temporary = Join-Path $directory ('.{0}.{1}.tmp' -f (Split-Path -Leaf $Path), [guid]::NewGuid().ToString('N'))
try {
$Value | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath $temporary -Encoding utf8NoBOM
Move-Item -LiteralPath $temporary -Destination $Path -Force
}
finally {
if (Test-Path -LiteralPath $temporary) { Remove-Item -LiteralPath $temporary -Force }
}
}
if (-not (Test-Path -LiteralPath $StatusPath -PathType Leaf)) { throw "Health status not found: $StatusPath" }
$status = Get-Content -LiteralPath $StatusPath -Raw | ConvertFrom-Json
if ($status.version -ne 1 -or $status.overall -notin @('healthy', 'degraded')) { throw 'Unsupported health status document' }
$now = [DateTimeOffset]::UtcNow
$previous = $null
if (Test-Path -LiteralPath $StatePath -PathType Leaf) {
$previous = Get-Content -LiteralPath $StatePath -Raw | ConvertFrom-Json
}
$kind = $null
if ($Force) {
$kind = if ($status.overall -eq 'healthy') { 'recovered' } else { 'degraded' }
}
elseif ($status.overall -eq 'degraded') {
if ($null -eq $previous -or $previous.last_status -ne 'degraded') {
$kind = 'degraded'
}
elseif ($null -ne $previous.last_notification_at_utc) {
$lastNotification = if ($previous.last_notification_at_utc -is [DateTime]) {
[DateTimeOffset]$previous.last_notification_at_utc
}
else {
[DateTimeOffset]::Parse([string]$previous.last_notification_at_utc, [Globalization.CultureInfo]::InvariantCulture)
}
if (($now - $lastNotification).TotalHours -ge $ReminderHours) { $kind = 'reminder' }
}
}
elseif ($null -ne $previous -and $previous.last_status -eq 'degraded') {
$kind = 'recovered'
}
$lastNotificationAt = if ($null -ne $previous -and $null -ne $previous.last_notification_at_utc) {
([DateTimeOffset]$previous.last_notification_at_utc).ToString('o')
}
else { $null }
$lastNotificationKind = if ($null -ne $previous) { $previous.last_notification_kind } else { $null }
if ($null -ne $kind) {
$failedNames = @($status.checks | Where-Object { -not $_.ok } | ForEach-Object { [string]$_.name })
switch ($kind) {
'degraded' {
$title = 'Домовой: требуется внимание'
$body = if ($failedNames.Count -gt 0) { 'Сбой: ' + ($failedNames -join ', ') } else { 'Мониторинг обнаружил сбой.' }
$icon = 'Error'
}
'reminder' {
$title = 'Домовой: сбой продолжается'
$body = if ($failedNames.Count -gt 0) { 'Не исправлено: ' + ($failedNames -join ', ') } else { 'Состояние остается ухудшенным.' }
$icon = 'Warning'
}
default {
$title = 'Домовой: работа восстановлена'
$body = 'Все проверки снова проходят успешно.'
$icon = 'Info'
}
}
if (-not $NoDisplay) {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$notification = [System.Windows.Forms.NotifyIcon]::new()
try {
$notification.Icon = switch ($icon) {
'Error' { [System.Drawing.SystemIcons]::Error }
'Warning' { [System.Drawing.SystemIcons]::Warning }
default { [System.Drawing.SystemIcons]::Information }
}
$notification.Visible = $true
$notification.BalloonTipIcon = [System.Enum]::Parse([System.Windows.Forms.ToolTipIcon], $icon)
$notification.BalloonTipTitle = $title
$notification.BalloonTipText = $body
$notification.ShowBalloonTip(8000)
Start-Sleep -Seconds 9
}
finally {
$notification.Dispose()
}
}
$lastNotificationAt = $now.ToString('o')
$lastNotificationKind = $kind
}
$state = [ordered]@{
version = 1
last_status = [string]$status.overall
last_observed_at_utc = $now.ToString('o')
last_notification_at_utc = $lastNotificationAt
last_notification_kind = $lastNotificationKind
}
Write-AtomicJson -Value $state -Path $StatePath
$result = if ($null -eq $kind) { 'suppressed' } elseif ($NoDisplay) { 'simulated' } else { 'sent' }
Write-Output "notification=$result status=$($status.overall) kind=$kind"