From 957d46fd2ede8a5864db8e2919ef4950f7a90ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Doma=C5=84ski?= Date: Mon, 22 Jun 2026 12:15:48 +0000 Subject: [PATCH] Jun 22, 2026, 2:15 PM --- toolbox/DBAToolBox.ps1 | 81 +++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 20 deletions(-) diff --git a/toolbox/DBAToolBox.ps1 b/toolbox/DBAToolBox.ps1 index 9adbe9a..f029c47 100644 --- a/toolbox/DBAToolBox.ps1 +++ b/toolbox/DBAToolBox.ps1 @@ -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 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 { 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)) { $token = $inputToken.Trim() $settings.SplunkToken = $token $settings | ConvertTo-Json | Set-Content $configPath - Write-Log "Zapisano podany token w settings.json." + Write-Log "DEBUG: Token został zapisany w settings.json." } } catch { - # Ignoruj błędy ładowania VisualBasic w niektórych konfiguracjach + # Ignoruj błędy ładowania VisualBasic } } if ([string]::IsNullOrWhiteSpace($token) -or $token -eq "YOUR_TOKEN") { - Write-Log "Używam demonstracyjnej (symulowanej) listy hostów Splunk Observability Cloud." - 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: Używam symulacji - brak prawidłowego tokenu API." + 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 = @{ @@ -125,18 +126,44 @@ function Get-SplunkAPIHosts { "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) # 1. Metoda główna: GET /v2/dimension?query=host 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 $offset = 0 $hasMore = $true while ($hasMore) { $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 if ($null -ne $response) { @@ -145,9 +172,9 @@ function Get-SplunkAPIHosts { } 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" - $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) { $results = $response.results } @@ -159,27 +186,33 @@ function Get-SplunkAPIHosts { [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 { $hasMore = $false } } } 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:* if ($hostsSet.Count -eq 0) { 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 $offset = 0 $hasMore = $true while ($hasMore) { $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 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 { $hasMore = $false } } } 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) { $sorted = $hostsSet | Sort-Object - Write-Log "Pomyślnie pobrano $($sorted.Count) hostów ze Splunk Observability Cloud." - return $sorted + # Ograniczenie do pierwszych 20 wyników zgodnie z żądaniem + $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 { - Write-Log "Brak hostów z API. Używam demonstracyjnej (symulowanej) listy 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") + Write-Log "DEBUG: API nie zwróciło żadnych danych. Ładuję listę demonstracyjną (5 hostów)." + 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") } }