71b8595742
ZbOtelCollector (otelcol-contrib 0.162.0, NSSM, NT SERVICE\ZbOtelCollector, every listener on 127.0.0.1, file + Prometheus exporters, size-capped rotation) on DEV vd03 and QA vq03/vq04. OtOpcUa, ScadaBridge Central + Site, MxAccessGateway and HistorianGateway export OTLP to http://127.0.0.1:4317 via two service-env entries each; rolling restarts, standby-first on the QA pairs. Overview has no exporter binding and is unchanged. Config, install/switch/verify scripts, runbook and the dated rollout record under infra/otel-collector/wonder/; evidence doc, GAPS, index row and deployment.md updated. Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
117 lines
6.6 KiB
PowerShell
117 lines
6.6 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Installs (or removes) the ZbOtelCollector service on a wonder box (scadaproj#54).
|
|
|
|
.DESCRIPTION
|
|
Layout: E:\ApiInstall\OtelCollector\{bin\otelcol-contrib.exe, bin\nssm.exe, config\config.yaml, data\, logs\}
|
|
Service: ZbOtelCollector (NSSM), virtual account NT SERVICE\ZbOtelCollector, Automatic, restart on exit.
|
|
ACL: the root is protected (inherited ACEs copied, so an inherited OtOpcUaScriptWorker deny stays as an explicit
|
|
ACE), BUILTIN\Users removed; SYSTEM + Administrators full; the service account gets RX on the tree and
|
|
Modify on data\ and logs\.
|
|
Env: ZB_TIER (DEV|QA, stamped as deployment.environment.name) and NO_WINDOWS_SERVICE=1 (NSSM is the service
|
|
wrapper, so otelcol runs in console mode).
|
|
Every listener in config.yaml binds 127.0.0.1. The script checks that after start.
|
|
|
|
.PARAMETER StageDir Folder holding otelcol-contrib_0.162.0_windows_amd64.zip, config.yaml and SHA256SUMS.txt.
|
|
.PARAMETER Tier DEV or QA.
|
|
.PARAMETER NssmPath An existing nssm.exe on the box, copied into bin\.
|
|
.PARAMETER Uninstall Stop and remove the service. The folder is renamed to OtelCollector.removed.<ts> (kept).
|
|
#>
|
|
param(
|
|
[Parameter(Mandatory)] [string]$StageDir,
|
|
[ValidateSet('DEV','QA')] [string]$Tier,
|
|
[string]$NssmPath,
|
|
[switch]$Uninstall
|
|
)
|
|
$ErrorActionPreference = 'Stop'
|
|
$svcName = 'ZbOtelCollector'
|
|
$root = 'E:\ApiInstall\OtelCollector'
|
|
$acct = "NT SERVICE\$svcName"
|
|
$exeSha = 'F8F69BD67025F29353B44440069046CA5BDB50CBBFCD6F359D465F0FE84E436E'
|
|
function Say($m) { "[{0:HH:mm:ss}] {1}" -f (Get-Date), $m }
|
|
|
|
if ($Uninstall) {
|
|
$s = Get-Service $svcName -ErrorAction SilentlyContinue
|
|
if ($s) {
|
|
if ($s.Status -ne 'Stopped') { Stop-Service $svcName -Force; (Get-Service $svcName).WaitForStatus('Stopped', [TimeSpan]::FromSeconds(60)) }
|
|
& sc.exe delete $svcName | Out-Null
|
|
Say "service $svcName deleted"
|
|
}
|
|
if (Test-Path $root) { $dst = "$root.removed.$(Get-Date -Format yyyyMMddTHHmmss)"; Rename-Item $root $dst; Say "folder kept as $dst" }
|
|
return
|
|
}
|
|
|
|
if (-not $Tier -or -not $NssmPath) { throw '-Tier and -NssmPath are required for install' }
|
|
if (Get-Service $svcName -ErrorAction SilentlyContinue) { throw "$svcName already exists" }
|
|
if (Test-Path $root) { throw "$root already exists" }
|
|
|
|
# 1. Verify the staged files against SHA256SUMS.txt (written on the Mac from the release .sha256 + git).
|
|
$sums = @{}
|
|
Get-Content (Join-Path $StageDir 'SHA256SUMS.txt') | Where-Object { $_ -match '^\s*([0-9a-fA-F]{64})\s+\*?(\S+)\s*$' } |
|
|
ForEach-Object { $sums[$Matches[2]] = $Matches[1].ToUpperInvariant() }
|
|
foreach ($f in 'otelcol-contrib_0.162.0_windows_amd64.zip','config.yaml') {
|
|
$h = (Get-FileHash (Join-Path $StageDir $f) -Algorithm SHA256).Hash
|
|
if ($h -ne $sums[$f]) { throw "hash mismatch $f" }
|
|
Say "hash ok $f $($h.Substring(0,12))"
|
|
}
|
|
|
|
# 2. Layout
|
|
foreach ($d in 'bin','config','data','logs') { New-Item -ItemType Directory -Force -Path (Join-Path $root $d) | Out-Null }
|
|
$tmp = Join-Path $StageDir 'x'
|
|
if (Test-Path $tmp) { Remove-Item $tmp -Recurse -Force }
|
|
Expand-Archive (Join-Path $StageDir 'otelcol-contrib_0.162.0_windows_amd64.zip') -DestinationPath $tmp
|
|
$h = (Get-FileHash (Join-Path $tmp 'otelcol-contrib.exe') -Algorithm SHA256).Hash
|
|
if ($h -ne $exeSha) { throw "exe hash mismatch $h" }
|
|
Move-Item (Join-Path $tmp 'otelcol-contrib.exe') (Join-Path $root 'bin\otelcol-contrib.exe')
|
|
Remove-Item $tmp -Recurse -Force
|
|
Copy-Item $NssmPath (Join-Path $root 'bin\nssm.exe')
|
|
Copy-Item (Join-Path $StageDir 'config.yaml') (Join-Path $root 'config\config.yaml')
|
|
Say "layout ok, exe sha $($exeSha.Substring(0,12))"
|
|
|
|
# 3. Service
|
|
$nssm = Join-Path $root 'bin\nssm.exe'
|
|
& $nssm install $svcName (Join-Path $root 'bin\otelcol-contrib.exe') | Out-Null
|
|
$set = @(
|
|
@('AppParameters', "--config=$root\config\config.yaml"),
|
|
@('AppDirectory', $root),
|
|
@('DisplayName', 'ZB OpenTelemetry Collector'),
|
|
@('Description', 'otelcol-contrib 0.162.0, OTLP on 127.0.0.1:4317/4318 for the ZB.MOM.WW apps on this box (scadaproj#54)'),
|
|
@('Start', 'SERVICE_AUTO_START'),
|
|
@('AppStdout', "$root\logs\collector.log"),
|
|
@('AppStderr', "$root\logs\collector.log"),
|
|
@('AppRotateFiles', '1'), @('AppRotateOnline', '1'), @('AppRotateBytes', '10485760'),
|
|
@('AppExit', 'Default', 'Restart'), @('AppRestartDelay', '5000'))
|
|
foreach ($a in $set) { & $nssm set $svcName @a | Out-Null; if ($LASTEXITCODE -ne 0) { throw "nssm set $($a[0]) failed" } }
|
|
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\$svcName\Parameters" -Name AppEnvironmentExtra -Type MultiString -Value @("ZB_TIER=$Tier", 'NO_WINDOWS_SERVICE=1')
|
|
& sc.exe config $svcName obj= $acct | Out-Null
|
|
if ($LASTEXITCODE -ne 0) { throw "sc config obj failed $LASTEXITCODE" }
|
|
Say "service created, account $((Get-CimInstance Win32_Service -Filter "Name='$svcName'").StartName)"
|
|
|
|
# 4. ACL
|
|
& icacls $root /inheritance:d | Out-Null
|
|
& icacls $root /remove:g 'BUILTIN\Users' /T /C /Q | Out-Null
|
|
& icacls $root /grant "${acct}:(OI)(CI)(RX)" /Q | Out-Null
|
|
foreach ($d in 'data','logs') { & icacls (Join-Path $root $d) /grant "${acct}:(OI)(CI)(M)" /Q | Out-Null }
|
|
(icacls $root) | Where-Object { $_.Trim() } | ForEach-Object { " acl " + $_.Trim() }
|
|
|
|
# 5. Start + checks
|
|
Start-Service $svcName
|
|
$ok = $false
|
|
for ($i = 0; $i -lt 30; $i++) {
|
|
Start-Sleep 2
|
|
try { $r = Invoke-WebRequest -UseBasicParsing -TimeoutSec 3 'http://127.0.0.1:13133/'; if ($r.StatusCode -eq 200) { $ok = $true; break } } catch {}
|
|
}
|
|
Say "service $((Get-Service $svcName).Status); health_check ok=$ok"
|
|
if (-not $ok) { Get-Content "$root\logs\collector.log" -Tail 30; throw 'collector not healthy' }
|
|
$procId = (Get-CimInstance Win32_Service -Filter "Name='$svcName'").ProcessId
|
|
$child = Get-CimInstance Win32_Process -Filter "ParentProcessId=$procId" | Where-Object Name -eq 'otelcol-contrib.exe'
|
|
$listen = Get-NetTCPConnection -State Listen -OwningProcess $child.ProcessId
|
|
$listen | Sort-Object LocalPort | ForEach-Object { " listen $($_.LocalAddress):$($_.LocalPort)" }
|
|
$bad = $listen | Where-Object { $_.LocalAddress -ne '127.0.0.1' }
|
|
if ($bad) { throw "non-loopback listener: $($bad | ForEach-Object { "$($_.LocalAddress):$($_.LocalPort)" })" }
|
|
$r = Invoke-WebRequest -UseBasicParsing -Method Post -ContentType 'application/json' -Body '{}' 'http://127.0.0.1:4318/v1/traces'
|
|
Say "OTLP/HTTP empty POST -> $($r.StatusCode)"
|
|
$err = Select-String -Path "$root\logs\collector.log" -Pattern '"level":"error"|\terror\t' -SimpleMatch:$false
|
|
Say "collector.log error lines: $(@($err).Count)"
|
|
Get-Content "$root\logs\collector.log" | Select-String 'Everything is ready|Starting otelcol|Starting extensions' | ForEach-Object { " log " + $_.Line.Substring(0, [Math]::Min(160, $_.Line.Length)) }
|