Jun 22, 2026, 2:15 PM
This commit is contained in:
+61
-20
@@ -100,24 +100,25 @@ function Get-SplunkAPIHosts {
|
|||||||
|
|
||||||
# Jeśli brak tokenu lub jest domyślny, spróbujemy poprosić użytkownika o podanie, lub użyjemy demo
|
# Jeśli brak tokenu lub jest domyślny, spróbujemy poprosić użytkownika o podanie, lub użyjemy demo
|
||||||
if ([string]::IsNullOrWhiteSpace($token) -or $token -eq "YOUR_TOKEN") {
|
if ([string]::IsNullOrWhiteSpace($token) -or $token -eq "YOUR_TOKEN") {
|
||||||
Write-Log "Brak zdefiniowanego tokenu API Splunk Observability. Uruchamiam wersję demo (lub wpisz token w InputBox)."
|
Write-Log "DEBUG: Brak tokenu w SF_TOKEN lub settings.json. Pytam użytkownika..."
|
||||||
try {
|
try {
|
||||||
Add-Type -AssemblyName Microsoft.VisualBasic
|
Add-Type -AssemblyName Microsoft.VisualBasic
|
||||||
$inputToken = [Microsoft.VisualBasic.Interaction]::InputBox("Wprowadź swój token API Splunk Observability (lub pozostaw puste, aby wyświetlić hosty demonstracyjne):", "Wymagana Autoryzacja Splunk", "")
|
$inputToken = [Microsoft.VisualBasic.Interaction]::InputBox("Wprowadź swój token API Splunk Observability (pozostaw puste dla trybu DEMO):", "Wymagana Autoryzacja Splunk", "")
|
||||||
if (-not [string]::IsNullOrWhiteSpace($inputToken)) {
|
if (-not [string]::IsNullOrWhiteSpace($inputToken)) {
|
||||||
$token = $inputToken.Trim()
|
$token = $inputToken.Trim()
|
||||||
$settings.SplunkToken = $token
|
$settings.SplunkToken = $token
|
||||||
$settings | ConvertTo-Json | Set-Content $configPath
|
$settings | ConvertTo-Json | Set-Content $configPath
|
||||||
Write-Log "Zapisano podany token w settings.json."
|
Write-Log "DEBUG: Token został zapisany w settings.json."
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
# Ignoruj błędy ładowania VisualBasic w niektórych konfiguracjach
|
# Ignoruj błędy ładowania VisualBasic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ([string]::IsNullOrWhiteSpace($token) -or $token -eq "YOUR_TOKEN") {
|
if ([string]::IsNullOrWhiteSpace($token) -or $token -eq "YOUR_TOKEN") {
|
||||||
Write-Log "Używam demonstracyjnej (symulowanej) listy hostów Splunk Observability Cloud."
|
Write-Log "DEBUG: Używam symulacji - brak prawidłowego tokenu API."
|
||||||
return @("splunk-host-eu-prod-01", "splunk-host-eu-prod-02", "splunk-host-eu-prod-03", "splunk-host-eu-staging-01", "splunk-host-eu-dev-01")
|
Write-Log "DEBUG: Pokazuję pierwszych 5 wyników demonstracyjnych (zgodnie z ograniczeniem)."
|
||||||
|
return @("demo-splunk-host-eu-prod-01", "demo-splunk-host-eu-prod-02", "demo-splunk-host-eu-prod-03", "demo-splunk-host-eu-staging-01", "demo-splunk-host-eu-dev-01")
|
||||||
}
|
}
|
||||||
|
|
||||||
$headers = @{
|
$headers = @{
|
||||||
@@ -125,18 +126,44 @@ function Get-SplunkAPIHosts {
|
|||||||
"Content-Type" = "application/json"
|
"Content-Type" = "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Wewnętrzny, kompatybilny helper do zapytań z logowaniem postępu na żywo
|
||||||
|
$InvokeSplunkRequest = {
|
||||||
|
param([string]$targetUri)
|
||||||
|
Write-Log "DEBUG: Zapytanie HTTP GET do: $targetUri"
|
||||||
|
|
||||||
|
$params = @{
|
||||||
|
Uri = $targetUri
|
||||||
|
Method = "GET"
|
||||||
|
Headers = $headers
|
||||||
|
ErrorAction = "Stop"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Kompatybilność parametrów timeout w zależności od wersji PowerShell (PS 5.1 vs Core)
|
||||||
|
if ($PSVersionTable.PSVersion.Major -ge 6) {
|
||||||
|
$params.TimeoutSec = 10
|
||||||
|
} else {
|
||||||
|
$params.Timeout = 10000 # 10 sekund w ms dla PS 5.1
|
||||||
|
}
|
||||||
|
|
||||||
|
$startTime = Get-Date
|
||||||
|
$responseObj = Invoke-RestMethod @params
|
||||||
|
$duration = [Math]::Round(((Get-Date) - $startTime).TotalMilliseconds)
|
||||||
|
Write-Log "DEBUG: Odpowiedź otrzymana pomyślnie w $duration ms."
|
||||||
|
return $responseObj
|
||||||
|
}
|
||||||
|
|
||||||
$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=host
|
||||||
try {
|
try {
|
||||||
Write-Log "Pobieranie wymiarów hosta z API Splunk (Realm: $realm)..."
|
Write-Log "DEBUG: Rozpoczynam pobieranie wymiarów (Metoda 1: GET /v2/dimension?query=host)..."
|
||||||
$limit = 100
|
$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=host&limit=$limit&offset=$offset"
|
||||||
$response = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -TimeoutSec 10 -ErrorAction Stop
|
$response = &$InvokeSplunkRequest -targetUri $uri
|
||||||
|
|
||||||
$results = $null
|
$results = $null
|
||||||
if ($null -ne $response) {
|
if ($null -ne $response) {
|
||||||
@@ -145,9 +172,9 @@ function Get-SplunkAPIHosts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (($null -eq $results -or $results.Count -eq 0) -and $offset -eq 0) {
|
if (($null -eq $results -or $results.Count -eq 0) -and $offset -eq 0) {
|
||||||
# Alternatywne query=key:host
|
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"
|
$uri = "https://api.$realm.observability.splunkcloud.com/v2/dimension?query=key:host&limit=$limit&offset=$offset"
|
||||||
$response = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -TimeoutSec 10 -ErrorAction Stop
|
$response = &$InvokeSplunkRequest -targetUri $uri
|
||||||
if ($null -ne $response -and $null -ne $response.results) {
|
if ($null -ne $response -and $null -ne $response.results) {
|
||||||
$results = $response.results
|
$results = $response.results
|
||||||
}
|
}
|
||||||
@@ -159,27 +186,33 @@ function Get-SplunkAPIHosts {
|
|||||||
[void]$hostsSet.Add($item.value.Trim())
|
[void]$hostsSet.Add($item.value.Trim())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($results.Count -lt $limit) { $hasMore = $false } else { $offset += $limit }
|
Write-Log "DEBUG: Pobrano $($results.Count) obiektów wymiarów (offset: $offset). Dotychczas unikalnych hostów: $($hostsSet.Count)"
|
||||||
|
|
||||||
|
if ($results.Count -lt $limit) {
|
||||||
|
$hasMore = $false
|
||||||
|
} else {
|
||||||
|
$offset += $limit
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$hasMore = $false
|
$hasMore = $false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
Write-Log "Metoda 1 (dimension) nie powiodła się: $($_.Exception.Message). Próba Metody 2 (MTS)..."
|
Write-Log "DEBUG: Metoda 1 zakończyła się błędem: $($_.Exception.Message)"
|
||||||
}
|
}
|
||||||
|
|
||||||
# 2. Metoda Fallback: GET /v2/metrictimeseries?query=host:*
|
# 2. Metoda Fallback: GET /v2/metrictimeseries?query=host:*
|
||||||
if ($hostsSet.Count -eq 0) {
|
if ($hostsSet.Count -eq 0) {
|
||||||
try {
|
try {
|
||||||
Write-Log "Uruchamiam fallback: Pobieranie MTS z API Splunk..."
|
Write-Log "DEBUG: Metoda 1 nie zwróciła hostów. Uruchamiam Metodę zapasową 2 (GET /v2/metrictimeseries)..."
|
||||||
$limit = 100
|
$limit = 100
|
||||||
$offset = 0
|
$offset = 0
|
||||||
$hasMore = $true
|
$hasMore = $true
|
||||||
|
|
||||||
while ($hasMore) {
|
while ($hasMore) {
|
||||||
$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 = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -TimeoutSec 10 -ErrorAction Stop
|
$response = &$InvokeSplunkRequest -targetUri $uri
|
||||||
|
|
||||||
$results = $null
|
$results = $null
|
||||||
if ($null -ne $response) {
|
if ($null -ne $response) {
|
||||||
@@ -196,24 +229,32 @@ function Get-SplunkAPIHosts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($results.Count -lt $limit) { $hasMore = $false } else { $offset += $limit }
|
Write-Log "DEBUG: Pobrano $($results.Count) rekordów MTS (offset: $offset). Dotychczas unikalnych hostów: $($hostsSet.Count)"
|
||||||
|
|
||||||
|
if ($results.Count -lt $limit) {
|
||||||
|
$hasMore = $false
|
||||||
|
} else {
|
||||||
|
$offset += $limit
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$hasMore = $false
|
$hasMore = $false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
Write-Log "Metoda 2 (fallback) nie powiodła się: $($_.Exception.Message)"
|
Write-Log "DEBUG: Metoda 2 zakończyła się błędem: $($_.Exception.Message)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($hostsSet.Count -gt 0) {
|
if ($hostsSet.Count -gt 0) {
|
||||||
$sorted = $hostsSet | Sort-Object
|
$sorted = $hostsSet | Sort-Object
|
||||||
Write-Log "Pomyślnie pobrano $($sorted.Count) hostów ze Splunk Observability Cloud."
|
# Ograniczenie do pierwszych 20 wyników zgodnie z żądaniem
|
||||||
return $sorted
|
$limited = $sorted | Select-Object -First 20
|
||||||
|
Write-Log "DEBUG: Pobrano łącznie $($sorted.Count) unikalnych hostów. Ograniczam listę i pokazuję pierwszych $($limited.Count) wyników."
|
||||||
|
return $limited
|
||||||
} else {
|
} else {
|
||||||
Write-Log "Brak hostów z API. Używam demonstracyjnej (symulowanej) listy hostów."
|
Write-Log "DEBUG: API nie zwróciło żadnych danych. Ładuję listę demonstracyjną (5 hostów)."
|
||||||
return @("splunk-host-eu-prod-01", "splunk-host-eu-prod-02", "splunk-host-eu-prod-03", "splunk-host-eu-staging-01", "splunk-host-eu-dev-01")
|
return @("demo-splunk-host-eu-prod-01", "demo-splunk-host-eu-prod-02", "demo-splunk-host-eu-prod-03", "demo-splunk-host-eu-staging-01", "demo-splunk-host-eu-dev-01")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user