Private/Start-MonitoringLoop.ps1
|
function Start-MonitoringLoop { [CmdletBinding(DefaultParameterSetName = 'Default')] param( [Parameter(Mandatory=$true)] [string] $monitorType, [Parameter(Mandatory=$true)] [psobject] $Config, [Parameter(Mandatory=$true)] [int] $UpdateInterval, [Parameter()] [switch] $Tui, [Parameter(ParameterSetName = 'CsvExport', Mandatory)] [switch] $ExportCsv, [Parameter(ParameterSetName = 'CsvExport')] [string] $CsvPath = [Environment]::GetFolderPath('Desktop'), [Parameter()] [switch] $ExportHtml, [Parameter()] [string] $HtmlPath = [Environment]::GetFolderPath('Desktop'), [Parameter()] [ValidateSet('Counter', 'Host')] [string] $HtmlGroupBy = 'Counter' ) $SampleCount = 0 $StartTime = Get-Date if ( $monitorType -in @('local','remoteSingle') ) { # TUI mode: launch interactive Terminal.Gui application if ( $Tui ) { Show-TuiMainApplication -Counters $Config.Counters ` -ConfigName $Config.Name ` -Interval $UpdateInterval ` -ExportCsv:$ExportCsv ` -CsvPath $CsvPath ` -ExportHtml:$ExportHtml ` -HtmlPath $HtmlPath ` -HtmlGroupBy $HtmlGroupBy return } while ( $true ) { $SampleCount++ # Collect data from all counters [psTPCCLASSES.CounterConfiguration]::GetValuesBatched($Config.Counters) # CSV Export if ( $ExportCsv ) { $csvFilePath = Join-Path $CsvPath "psTPC_$($Config.Name)_$(Get-Date -Format 'ddMMyy').csv" [psTPCCLASSES.CounterConfiguration]::ExportCsv($Config.Counters, $csvFilePath) } # HTML Export if ( $ExportHtml ) { $htmlFilePath = Join-Path $HtmlPath "psTPC_$($Config.Name)_report.html" Export-HtmlReport -Counters $Config.Counters ` -ConfigName $Config.Name ` -HtmlFilePath $htmlFilePath ` -StartTime $StartTime ` -SampleCount $SampleCount ` -UpdateInterval $UpdateInterval ` -GroupBy $HtmlGroupBy } Show-SessionHeader -ConfigName $Config.Name -StartTime $StartTime -SampleCount $SampleCount # Separate counters by format $graphCounters = [System.Collections.Generic.List[psTPCCLASSES.CounterConfiguration]]::new() $tableCounters = [System.Collections.Generic.List[psTPCCLASSES.CounterConfiguration]]::new() foreach ( $Counter in $Config.Counters ) { if ( $Counter.HistoricalData.Count -gt 3 ) { # Need some data points switch ($Counter.Format) { "graph" { $graphCounters.Add($Counter) } "table" { $tableCounters.Add($Counter) } "both" { $graphCounters.Add($Counter) $tableCounters.Add($Counter) } default { $tableCounters.Add($Counter) } # Default to table } } else { Write-Host "$($Counter.GetFormattedTitle()): Collecting data... ($($Counter.HistoricalData.Count)/3 samples)" -ForegroundColor Yellow Write-Host "" } } # Show graphs first foreach ( $Counter in $graphCounters ) { try { Show-CounterGraph -Counter $Counter } catch { Write-Host "$($Counter.Title) Graph error: $($_.Exception.Message)" -ForegroundColor Red Write-Host "" } } # Show table if there are any table counters if ( $tableCounters.Count -gt 0 ) { try { Show-CounterTable -Counters $tableCounters } catch { Write-Host "Table display error: $($_.Exception.Message)" -ForegroundColor Red Write-Host "" } } Start-Sleep -Seconds $UpdateInterval } } elseif ( $monitorType -eq 'environment' ) { # TUI mode for environment: collect all counters from all servers if ( $Tui ) { $allCounters = [System.Collections.Generic.List[psTPCCLASSES.CounterConfiguration]]::new() foreach ( $server in $Config.Servers ) { if ( $server.IsAvailable ) { foreach ( $counter in $server.Counters ) { if ( $counter.IsAvailable ) { $allCounters.Add($counter) } } } } Show-TuiMainApplication -Counters $allCounters ` -ConfigName $Config.Name ` -Interval $UpdateInterval ` -ExportCsv:$ExportCsv ` -CsvPath $CsvPath ` -ExportHtml:$ExportHtml ` -HtmlPath $HtmlPath ` -HtmlGroupBy $HtmlGroupBy ` -ShowGraphs $false return } while ( $true ) { $SampleCount++ $Config.GetAllValuesBatched() # CSV Export if ( $ExportCsv ) { $csvFilePath = Join-Path $CsvPath "psTPC_$($Config.Name)_$(Get-Date -Format 'ddMMyy').csv" $Config.ExportCsv($csvFilePath) } # Clear screen Clear-Host # Show header with environment info Write-Host "=== $($Config.Name) ===" -ForegroundColor Cyan Write-Host "Sample: $SampleCount | Started: $($StartTime.ToString('HH:mm:ss')) | Query Time: $($Config.QueryTimestamp.ToString('HH:mm:ss.fff')) | Duration: $($Config.QueryDuration)ms" -ForegroundColor Gray Write-Host "" # Collect all counters from all servers for display $allCounters = [System.Collections.Generic.List[psTPCCLASSES.CounterConfiguration]]::new() foreach ( $server in $Config.Servers ) { if ( $server.IsAvailable ) { foreach ( $counter in $server.Counters ) { if ( $counter.IsAvailable -and $counter.HistoricalData.Count -gt 0 ) { $allCounters.Add($counter) } } } } # Display table with all counters from all servers if ( $allCounters.Count -gt 0 ) { try { Show-CounterTable -Counters $allCounters } catch { Write-Host "Table display error: $($_.Exception.Message)" -ForegroundColor Red Write-Host "" } } else { Write-Host "No data available from any server." -ForegroundColor Yellow } # HTML Export if ( $ExportHtml -and $allCounters.Count -gt 0 ) { $htmlFilePath = Join-Path $HtmlPath "psTPC_$($Config.Name)_report.html" Export-HtmlReport -Counters $allCounters ` -ConfigName $Config.Name ` -HtmlFilePath $htmlFilePath ` -StartTime $StartTime ` -SampleCount $SampleCount ` -UpdateInterval $UpdateInterval ` -GroupBy $HtmlGroupBy } # Show environment statistics Write-Host "" Write-Host "Environment Statistics:" -ForegroundColor Cyan $stats = $Config.GetEnvironmentStatistics() Write-Host " Total Servers: $($stats['TotalServers']) | Available: $($stats['AvailableServers'])" -ForegroundColor White Write-Host " Total Counters: $($stats['TotalCounters']) | Available: $($stats['AvailableCounters'])" -ForegroundColor White Write-Host " Last Query: $($stats['LastQueryTimestamp']) | Duration: $($stats['LastQueryDuration']) | Interval: $($stats['Interval'])" -ForegroundColor White Start-Sleep -Seconds $UpdateInterval } } } |