Public/Start-tpcEnvironmentMonitor.ps1
|
function Start-tpcEnvironmentMonitor { <# .SYNOPSIS .DESCRIPTION Starts parallel environment monitoring across multiple remote servers using a JSON environment configuration. .PARAMETER EnvConfigPath Absolute path to the environment JSON configuration file. .PARAMETER UpdateInterval Seconds between updates. Default: uses interval from JSON config (fallback: 2s). .PARAMETER Tui (Beta) Launches an interactive Terminal GUI (Terminal.Gui) instead of the scrolling console output. For environment monitoring the TUI shows a table-only view (counters are not graphed across servers). .EXAMPLE Start-tpcEnvironmentMonitor -EnvConfigPath "C:\Configs\SQL_PROD.json" Starts environment monitoring using the interval defined in the JSON config. .EXAMPLE Start-tpcEnvironmentMonitor -EnvConfigPath "C:\Configs\SQL_PROD.json" -Tui (Beta) Starts environment monitoring in the interactive Terminal GUI (table-only view). .PARAMETER ExportCsv Enables CSV export of counter values after each batch cycle (append mode, long format). .PARAMETER CsvPath Directory path for the CSV export file. Default: Desktop. .PARAMETER ExportHtml Enables HTML report export after each batch cycle using PSWriteHTML. The report contains a counter overview table, a combined chart and individual charts per counter. .PARAMETER HtmlPath Directory path for the HTML report file. Default: Desktop. .PARAMETER HtmlGroupBy Controls how individual tabs are grouped in the HTML report. 'Counter' (default): one tab per counter type, series per host — best for comparing hosts. 'Host': one tab per host, series per counter — best for viewing a single host's metrics. .EXAMPLE Start-tpcEnvironmentMonitor -EnvConfigPath "C:\Configs\SQL_PROD.json" -UpdateInterval 5 Starts environment monitoring with a 5-second update interval. .EXAMPLE Start-tpcEnvironmentMonitor -EnvConfigPath "C:\Configs\SQL_PROD.json" -ExportCsv -CsvPath "C:\Exports" Starts environment monitoring with CSV export. #> [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $EnvConfigPath, [Parameter()] [int] $UpdateInterval = 0, # 0 = use from config [switch] $Tui, [switch] $ExportCsv, [string] $CsvPath = [Environment]::GetFolderPath('Desktop'), [switch] $ExportHtml, [string] $HtmlPath = [Environment]::GetFolderPath('Desktop'), [ValidateSet('Counter', 'Host')] [string] $HtmlGroupBy = 'Counter' ) $environment = $null try { # Load environment configuration Write-Host "Loading environment configuration from '$EnvConfigPath'..." -ForegroundColor Yellow $environment = Get-EnvironmentConfiguration -EnvConfigPath $EnvConfigPath if ( -not $environment ) { Write-Warning "Failed to load environment configuration" Return } # Determine update interval (parameter overrides config) $effectiveInterval = if ( $UpdateInterval -gt 0 ) { $UpdateInterval } else { if ( $environment.Interval -gt 0 ) { $environment.Interval } else { 2 # Fallback default } } Write-Host "" Write-Host "=== Environment Monitoring ===" -ForegroundColor Cyan Write-Host "Environment : $($environment.Name)" -ForegroundColor White Write-Host "Description : $($environment.Description)" -ForegroundColor White Write-Host "Servers : $($environment.Servers.Count)" -ForegroundColor White Write-Host "Update Interval: $effectiveInterval second(s)" -ForegroundColor White Write-Host "" # Display server overview Write-Host "Servers in environment:" -ForegroundColor Cyan foreach ( $server in $environment.Servers ) { $status = if ($server.IsAvailable) { "Available" } else { "Unavailable - $($server.LastError)" } $statusColor = if ($server.IsAvailable) { "Green" } else { "Red" } Write-Host " [$status] $($server.ComputerName) - $($server.Comment) ($($server.Counters.Count) counter(s))" -ForegroundColor $statusColor } Write-Host "" # Start monitoring loop $MonitoringParams = @{ MonitorType = 'environment' Config = $environment UpdateInterval = $effectiveInterval Tui = $Tui ExportCsv = $ExportCsv CsvPath = $CsvPath ExportHtml = $ExportHtml HtmlPath = $HtmlPath HtmlGroupBy = $HtmlGroupBy } Start-MonitoringLoop @MonitoringParams } catch [System.Management.Automation.HaltCommandException] { Write-Host "`n=== Monitoring stopped by user ===" -ForegroundColor Green } catch { Write-Host "`n=== ERROR ===" -ForegroundColor Red Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Yellow throw } finally { # Display summary if environment was loaded if ( $environment ) { Write-Host "" Write-Host "=== Environment Monitoring Summary ===" -ForegroundColor Cyan Write-Host "Environment : $($environment.Name)" -ForegroundColor White Write-Host "Last Query Time : $($environment.QueryTimestamp)" -ForegroundColor White Write-Host "Last Query Duration: $($environment.QueryDuration)ms" -ForegroundColor White Write-Host "" # Show statistics per server Write-Host "Server Statistics:" -ForegroundColor Cyan foreach ( $server in $environment.Servers ) { if ( $server.IsAvailable ) { $availableCounters = $server.Counters | Where-Object { $_.IsAvailable } Write-Host " $($server.ComputerName): $($availableCounters.Count)/$($server.Counters.Count) counters available, Last Update: $($server.LastUpdate)" -ForegroundColor Green } else { Write-Host " $($server.ComputerName): Unavailable - $($server.LastError)" -ForegroundColor Red } } Write-Host "" Write-Host "Environment statistics:" -ForegroundColor Cyan $stats = $environment.GetEnvironmentStatistics() $stats.GetEnumerator() | Sort-Object Name | ForEach-Object { Write-Host " $($_.Key): $($_.Value)" -ForegroundColor White } } } } |