feat(worker): supervise VPN connections and recover batched telemetry
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
[CmdletBinding()]
|
||||
param([Parameter(Mandatory=$true)][string]$ExpectedNodeId)
|
||||
# Narrow migration for existing managed agents; fresh installs use the templates.
|
||||
$ErrorActionPreference='Stop'
|
||||
$ProgressPreference='SilentlyContinue'
|
||||
if ($env:COMPUTERNAME -cne $ExpectedNodeId) { throw 'Worker identity mismatch' }
|
||||
$root='C:\ProgramData\NDC\MissionCore\telemetry-agent'
|
||||
$config=Join-Path $root 'telegraf.conf'
|
||||
$exe='C:\Program Files\NDC\Mission Core\Telegraf\telegraf.exe'
|
||||
$service=Get-CimInstance Win32_Service -Filter "Name='telegraf'"
|
||||
if (-not $service -or $service.PathName -notlike '*NDC\Mission Core\Telegraf\telegraf.exe*') { throw 'Managed agent required' }
|
||||
if ($service.State -ne 'Running') { throw 'Running baseline required' }
|
||||
if ((Get-Item $config -Force).Attributes -band [IO.FileAttributes]::ReparsePoint) { throw 'Regular managed configuration required' }
|
||||
$before=[IO.File]::ReadAllText($config)
|
||||
$blocks=[regex]::Matches($before,'(?ms)^\[\[outputs\.mqtt\]\].*?(?=^\[\[|\z)')
|
||||
if ($blocks.Count -ne 2) { throw 'Expected separate host and pipeline outputs' }
|
||||
$after=$before
|
||||
foreach ($block in $blocks) {
|
||||
if ($block.Value -match '(?m)^\s*layout\s*=') {
|
||||
if ($block.Value -notmatch '(?m)^\s*layout\s*=\s*"batch"\s*$') { throw 'Unexpected existing MQTT layout' }
|
||||
continue
|
||||
}
|
||||
$after=$after.Replace($block.Value, $block.Value.Replace('[[outputs.mqtt]]', "[[outputs.mqtt]]`n layout = `"batch`""))
|
||||
}
|
||||
if ($after -eq $before) { @{changed=$false;mode='batch'} | ConvertTo-Json -Compress;exit 0 }
|
||||
$baselineHash=(Get-FileHash $config -Algorithm SHA256).Hash
|
||||
$scratch=Join-Path $root ('batch-update-'+[Guid]::NewGuid().ToString('N'))
|
||||
New-Item -ItemType Directory $scratch | Out-Null
|
||||
$candidate=Join-Path $scratch 'telegraf.conf'
|
||||
$backup=Join-Path $root ('telegraf-before-batch-'+$baselineHash.Substring(0,12)+'.conf')
|
||||
$changed=$false
|
||||
try {
|
||||
foreach ($entry in (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Services\telegraf').Environment) {
|
||||
$name,$value=$entry -split '=',2
|
||||
if ($name) { Set-Item "Env:$name" $value }
|
||||
}
|
||||
[IO.File]::WriteAllText($candidate,$after,[Text.UTF8Encoding]::new($false))
|
||||
$probe=Start-Process $exe -ArgumentList @('--config',('"'+$candidate+'"'),'--test') -PassThru -NoNewWindow -RedirectStandardOutput (Join-Path $scratch 'out.log') -RedirectStandardError (Join-Path $scratch 'error.log')
|
||||
$probeHandle=$probe.Handle
|
||||
if (-not $probe.WaitForExit(30000)) { $probe.Kill();throw 'Agent validation timed out' }
|
||||
if ($probe.ExitCode -ne 0) { throw 'Agent configuration validation failed' }
|
||||
if ((Get-FileHash $config -Algorithm SHA256).Hash -ne $baselineHash) { throw 'Configuration changed during validation' }
|
||||
if (-not (Test-Path $backup)) { Copy-Item $config $backup }
|
||||
Stop-Service telegraf
|
||||
(Get-Service telegraf).WaitForStatus([ServiceProcess.ServiceControllerStatus]::Stopped,[TimeSpan]::FromSeconds(20))
|
||||
$changed=$true
|
||||
Copy-Item $candidate $config -Force
|
||||
Start-Service telegraf
|
||||
(Get-Service telegraf).WaitForStatus([ServiceProcess.ServiceControllerStatus]::Running,[TimeSpan]::FromSeconds(20))
|
||||
@{changed=$true;mode='batch';backup=$backup;sha256=(Get-FileHash $config -Algorithm SHA256).Hash}|ConvertTo-Json -Compress
|
||||
} catch {
|
||||
if ($changed) {
|
||||
Stop-Service telegraf -ErrorAction SilentlyContinue
|
||||
Copy-Item $backup $config -Force
|
||||
}
|
||||
if ((Get-Service telegraf).Status -eq 'Stopped') { Start-Service telegraf }
|
||||
throw
|
||||
} finally { Remove-Item $scratch -Recurse -Force }
|
||||
@@ -29,6 +29,7 @@
|
||||
total = true
|
||||
|
||||
[[outputs.mqtt]]
|
||||
layout = "batch"
|
||||
startup_error_behavior = "retry"
|
||||
servers = ["tcp://${MISSIONCORE_MQTT_HOST}:${MISSIONCORE_MQTT_PORT}"]
|
||||
topic = "mission-core/v1/contours/${MISSIONCORE_CONTOUR_ID}/agents/${MISSIONCORE_AGENT_ID}/host"
|
||||
|
||||
@@ -80,6 +80,7 @@
|
||||
data_type = "string"
|
||||
|
||||
[[outputs.mqtt]]
|
||||
layout = "batch"
|
||||
startup_error_behavior = "retry"
|
||||
servers = ["tcp://${MISSIONCORE_MQTT_HOST}:${MISSIONCORE_MQTT_PORT}"]
|
||||
topic = "mission-core/v1/contours/${MISSIONCORE_CONTOUR_ID}/agents/${MISSIONCORE_AGENT_ID}/host"
|
||||
@@ -91,6 +92,7 @@
|
||||
namedrop = ["missioncore_pipeline", "missioncore_pipeline_event"]
|
||||
|
||||
[[outputs.mqtt]]
|
||||
layout = "batch"
|
||||
startup_error_behavior = "retry"
|
||||
servers = ["tcp://${MISSIONCORE_MQTT_HOST}:${MISSIONCORE_MQTT_PORT}"]
|
||||
topic = "mission-core/v1/contours/${MISSIONCORE_CONTOUR_ID}/agents/${MISSIONCORE_AGENT_ID}/pipeline"
|
||||
|
||||
Reference in New Issue
Block a user