Jun 22, 2026, 3:08 PM

This commit is contained in:
Paweł Domański
2026-06-22 13:08:02 +00:00
parent fc3a687de4
commit 220b198496
+18 -16
View File
@@ -159,15 +159,15 @@ function Get-SplunkAPIHosts {
$hostsSet = New-Object System.Collections.Generic.HashSet[string] ([System.StringComparer]::OrdinalIgnoreCase)
# 1. Metoda główna: GET /v2/dimension?query=host
# 1. Metoda główna: GET /v2/dimension?query=key:host
try {
Write-Log "DEBUG: Rozpoczynam pobieranie wymiarów (Metoda 1: GET /v2/dimension?query=host)..."
$limit = 20
Write-Log "DEBUG: Rozpoczynam pobieranie wymiarów (Metoda 1: GET /v2/dimension?query=key:host)..."
$limit = 100
$offset = 0
$hasMore = $true
while ($hasMore) {
$uri = "https://api.$realm.observability.splunkcloud.com/v2/dimension?query=host&limit=$limit&offset=$offset"
$uri = "https://api.$realm.observability.splunkcloud.com/v2/dimension?query=key:host&limit=$limit&offset=$offset"
$response = &$InvokeSplunkRequest -targetUri $uri
$results = $null
@@ -176,19 +176,12 @@ function Get-SplunkAPIHosts {
elseif ($response -is [System.Array]) { $results = $response }
}
if (($null -eq $results -or $results.Count -eq 0) -and $offset -eq 0) {
Write-Log "DEBUG: Metoda query=host pusta. Próba z precyzyjnym query=key:host..."
$uri = "https://api.$realm.observability.splunkcloud.com/v2/dimension?query=key:host&limit=$limit&offset=$offset"
$response = &$InvokeSplunkRequest -targetUri $uri
if ($null -ne $response -and $null -ne $response.results) {
$results = $response.results
}
}
if ($null -ne $results -and $results.Count -gt 0) {
foreach ($item in $results) {
if ($item.key -eq "host" -and -not [string]::IsNullOrWhiteSpace($item.value)) {
[void]$hostsSet.Add($item.value.Trim())
} elseif (-not [string]::IsNullOrWhiteSpace($item.value)) {
[void]$hostsSet.Add($item.value.Trim())
}
# Przerywamy natychmiast, gdy zebraliśmy już 20 unikalnych hostów
@@ -219,11 +212,14 @@ function Get-SplunkAPIHosts {
if ($hostsSet.Count -eq 0) {
try {
Write-Log "DEBUG: Metoda 1 nie zwróciła hostów. Uruchamiam Metodę zapasową 2 (GET /v2/metrictimeseries)..."
$limit = 20
$limit = 100
$offset = 0
$hasMore = $true
$maxPages = 5
$pageCount = 0
while ($hasMore) {
while ($hasMore -and $pageCount -lt $maxPages) {
$pageCount++
$uri = "https://api.$realm.observability.splunkcloud.com/v2/metrictimeseries?query=host:*&limit=$limit&offset=$offset"
$response = &$InvokeSplunkRequest -targetUri $uri
@@ -234,6 +230,7 @@ function Get-SplunkAPIHosts {
}
if ($null -ne $results -and $results.Count -gt 0) {
$prevCount = $hostsSet.Count
foreach ($mts in $results) {
if ($null -ne $mts.dimensions -and $null -ne $mts.dimensions.host) {
$val = $mts.dimensions.host
@@ -255,7 +252,12 @@ function Get-SplunkAPIHosts {
} elseif ($results.Count -lt $limit) {
$hasMore = $false
} else {
$offset += $limit
if ($hostsSet.Count -eq $prevCount) {
Write-Log "DEBUG: Brak nowych unikalnych hostów na tej stronie. Przerywam dalsze skanowanie."
$hasMore = $false
} else {
$offset += $limit
}
}
} else {
$hasMore = $false