Jun 22, 2026, 3:08 PM
This commit is contained in:
+18
-16
@@ -159,15 +159,15 @@ function Get-SplunkAPIHosts {
|
|||||||
|
|
||||||
$hostsSet = New-Object System.Collections.Generic.HashSet[string] ([System.StringComparer]::OrdinalIgnoreCase)
|
$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 {
|
try {
|
||||||
Write-Log "DEBUG: Rozpoczynam pobieranie wymiarów (Metoda 1: GET /v2/dimension?query=host)..."
|
Write-Log "DEBUG: Rozpoczynam pobieranie wymiarów (Metoda 1: GET /v2/dimension?query=key:host)..."
|
||||||
$limit = 20
|
$limit = 100
|
||||||
$offset = 0
|
$offset = 0
|
||||||
$hasMore = $true
|
$hasMore = $true
|
||||||
|
|
||||||
while ($hasMore) {
|
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
|
$response = &$InvokeSplunkRequest -targetUri $uri
|
||||||
|
|
||||||
$results = $null
|
$results = $null
|
||||||
@@ -176,19 +176,12 @@ function Get-SplunkAPIHosts {
|
|||||||
elseif ($response -is [System.Array]) { $results = $response }
|
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) {
|
if ($null -ne $results -and $results.Count -gt 0) {
|
||||||
foreach ($item in $results) {
|
foreach ($item in $results) {
|
||||||
if ($item.key -eq "host" -and -not [string]::IsNullOrWhiteSpace($item.value)) {
|
if ($item.key -eq "host" -and -not [string]::IsNullOrWhiteSpace($item.value)) {
|
||||||
[void]$hostsSet.Add($item.value.Trim())
|
[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
|
# Przerywamy natychmiast, gdy zebraliśmy już 20 unikalnych hostów
|
||||||
@@ -219,11 +212,14 @@ function Get-SplunkAPIHosts {
|
|||||||
if ($hostsSet.Count -eq 0) {
|
if ($hostsSet.Count -eq 0) {
|
||||||
try {
|
try {
|
||||||
Write-Log "DEBUG: Metoda 1 nie zwróciła hostów. Uruchamiam Metodę zapasową 2 (GET /v2/metrictimeseries)..."
|
Write-Log "DEBUG: Metoda 1 nie zwróciła hostów. Uruchamiam Metodę zapasową 2 (GET /v2/metrictimeseries)..."
|
||||||
$limit = 20
|
$limit = 100
|
||||||
$offset = 0
|
$offset = 0
|
||||||
$hasMore = $true
|
$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"
|
$uri = "https://api.$realm.observability.splunkcloud.com/v2/metrictimeseries?query=host:*&limit=$limit&offset=$offset"
|
||||||
$response = &$InvokeSplunkRequest -targetUri $uri
|
$response = &$InvokeSplunkRequest -targetUri $uri
|
||||||
|
|
||||||
@@ -234,6 +230,7 @@ function Get-SplunkAPIHosts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($null -ne $results -and $results.Count -gt 0) {
|
if ($null -ne $results -and $results.Count -gt 0) {
|
||||||
|
$prevCount = $hostsSet.Count
|
||||||
foreach ($mts in $results) {
|
foreach ($mts in $results) {
|
||||||
if ($null -ne $mts.dimensions -and $null -ne $mts.dimensions.host) {
|
if ($null -ne $mts.dimensions -and $null -ne $mts.dimensions.host) {
|
||||||
$val = $mts.dimensions.host
|
$val = $mts.dimensions.host
|
||||||
@@ -255,7 +252,12 @@ function Get-SplunkAPIHosts {
|
|||||||
} elseif ($results.Count -lt $limit) {
|
} elseif ($results.Count -lt $limit) {
|
||||||
$hasMore = $false
|
$hasMore = $false
|
||||||
} else {
|
} 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 {
|
} else {
|
||||||
$hasMore = $false
|
$hasMore = $false
|
||||||
|
|||||||
Reference in New Issue
Block a user