Skip to content

Commit e643f1a

Browse files
fix(windows): start telegraf on Windows Server 2025 hosts (#1782)
1 parent 94044a3 commit e643f1a

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

‎kubernetes/windows/main.ps1‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ function Remove-WindowsServiceIfItExists($name) {
3535
}
3636
}
3737

38+
function Test-TcpListenerByConnect {
39+
param (
40+
[int]$port,
41+
[int]$timeoutMs = 2000
42+
)
43+
44+
# Probe the listener by actually connecting to it rather than enumerating sockets.
45+
# On Windows Server 2025 hosts, netstat and Get-NetTCPConnection return no rows inside the
46+
# container even though the listener is present and accepting connections, which made the
47+
# previous netstat-based check fail forever and prevented telegraf from ever starting.
48+
$client = New-Object System.Net.Sockets.TcpClient
49+
try {
50+
$asyncResult = $client.BeginConnect('127.0.0.1', $port, $null, $null)
51+
if (-not $asyncResult.AsyncWaitHandle.WaitOne($timeoutMs)) {
52+
return $false
53+
}
54+
$client.EndConnect($asyncResult)
55+
return $client.Connected
56+
}
57+
catch {
58+
return $false
59+
}
60+
finally {
61+
$client.Close()
62+
}
63+
}
64+
3865
function Test-FluentbitTcpListener {
3966
param (
4067
[int]$port
@@ -54,17 +81,19 @@ function Test-FluentbitTcpListener {
5481
$retryCount = [int]$waitTimeSecs
5582
$retryAttempts = 0
5683
$retryDelaySeconds = 1
57-
while ($retryAttempts -lt $retryCount) {
84+
# Bound the loop by wall-clock time so the configured wait window is honored regardless of
85+
# how long an individual connect probe takes to fail.
86+
$deadline = (Get-Date).AddSeconds($retryCount)
87+
while ((Get-Date) -lt $deadline) {
5888
$retryAttempts++
59-
Write-Host "Test-FluentbitTcpListener: Retry attempt $retryAttempts/$retryCount..."
60-
$netstatOutput = netstat -an | Select-String "LISTENING"
61-
if ($netstatOutput -match ":$port") {
89+
Write-Host "Test-FluentbitTcpListener: Retry attempt $retryAttempts (waiting up to $retryCount seconds)..."
90+
if (Test-TcpListenerByConnect -port $port -timeoutMs 1000) {
6291
Write-Host "Test-FluentbitTcpListener: Fluentbit TCP listener is UP and running on port $port."
6392
return $true
6493
}
6594
Start-Sleep -Seconds $retryDelaySeconds
6695
}
67-
Write-Host "Test-FluentbitTcpListener: Failed to detect TCP listener after $retryCount attempts. Exiting script."
96+
Write-Host "Test-FluentbitTcpListener: Failed to detect TCP listener within $retryCount seconds ($retryAttempts attempts). Exiting script."
6897
return $false
6998
}
7099

0 commit comments

Comments
 (0)