Private/Checks/VCenter/Test-VcfVcenterSnapshotSize.ps1
|
# Copyright (c) 2026 Broadcom. All Rights Reserved. # Broadcom Confidential. The term "Broadcom" refers to Broadcom Inc. # and/or its subsidiaries. # # ============================================================================= # # SOFTWARE LICENSE AGREEMENT # # Copyright (c) CA, Inc. All rights reserved. # # You are hereby granted a non-exclusive, worldwide, royalty-free license # under CA, Inc.'s copyrights to use, copy, modify, and distribute this # software in source code or binary form for use in connection with CA, Inc. # products. # # This copyright notice shall be included in all copies or substantial # portions of the software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. # # ============================================================================= function Test-VcfVcenterSnapshotSize { <# .SYNOPSIS Flags large or stale snapshots on VCF management-appliance VMs. .DESCRIPTION Queries virtual machine and snapshot inventory across all vCenter appliances connected to SDDC Manager using Get-VcfCheckVMInventory and Get-VcfCheckVMSnapshotInventory. Identifies core management-appliance VMs (SDDC Manager, vCenter, and Aria Suite Lifecycle Manager) by matching VM guest IP addresses and hostnames against infrastructure component endpoints. Evaluates snapshot sizes for identified management VMs based on the largest single snapshot: - Pass: No snapshots present, or the largest single snapshot is 5 GB or less. - Warning: Largest single snapshot is greater than 5 GB up to 50 GB. - Fail: Largest single snapshot exceeds 50 GB. Populates a structured Rows table detailing Type, VM, Snapshots, Largest (GB), Total (GB), and Status. Delegates per-vCenter execution and outcome packaging to Invoke-VcfCheckPerVCenterCheck. .PARAMETER Context The VcfCheck.Context object. Must already be connected to SDDC Manager. .PARAMETER DisplayName Optional friendly display name for the check result. .OUTPUTS [PSObject[]] Per-vCenter check results generated by Invoke-VcfCheckPerVCenterCheck. #> [CmdletBinding()] [OutputType([PSObject])] Param ( [Parameter(Mandatory = $true)] [PSObject]$Context, [Parameter(Mandatory = $false)] [String]$DisplayName = '' ) $startedAt = Get-Date $checkId = 'vcenter_snapshot_size' $catalogEntry = (Get-VcfCheckCatalog)[$checkId] $displayName = if ([String]::IsNullOrEmpty($DisplayName)) { $catalogEntry.displayName } else { $DisplayName } $validationCriteria = $catalogEntry.validationCriteria $remediation = $catalogEntry.remediation try { $managementIpTypes = @{} $managementHostnameTypes = @{} foreach ($sddcManager in @((Invoke-VcfGetSddcManagers -ErrorAction Stop).Elements)) { if (-not [String]::IsNullOrWhiteSpace($sddcManager.IpAddress)) { $managementIpTypes[$sddcManager.IpAddress] = 'SDDC Manager' } if (-not [String]::IsNullOrWhiteSpace($sddcManager.Fqdn)) { $shortName = ($sddcManager.Fqdn -split '\.')[0] $managementHostnameTypes[$shortName] = 'SDDC Manager' } } foreach ($vcenter in @((Invoke-VcfGetVcenters -ErrorAction Stop).Elements)) { if (-not [String]::IsNullOrWhiteSpace($vcenter.IpAddress)) { $managementIpTypes[$vcenter.IpAddress] = 'vCenter' } if (-not [String]::IsNullOrWhiteSpace($vcenter.Fqdn)) { $shortName = ($vcenter.Fqdn -split '\.')[0] $managementHostnameTypes[$shortName] = 'vCenter' } } $vrslcmConnection = Get-VcfCheckVrslcmConnection -Context $Context if ($vrslcmConnection -and -not [String]::IsNullOrWhiteSpace($vrslcmConnection.Fqdn)) { try { $resolved = [System.Net.Dns]::GetHostAddresses($vrslcmConnection.Fqdn) | Where-Object { $_.AddressFamily -eq 'InterNetwork' } | Select-Object -First 1 if ($resolved) { $managementIpTypes[$resolved.IPAddressToString] = 'VRSLCM' } } catch { Write-LogMessage -Type DEBUG -Message "Could not resolve VRSLCM FQDN '$($vrslcmConnection.Fqdn)' to an IP address: $($_.Exception.Message)" } $shortName = ($vrslcmConnection.Fqdn -split '\.')[0] $managementHostnameTypes[$shortName] = 'VRSLCM' } } catch { return New-VcfCheckResult -CheckId $checkId -Area vCenter -Status Error ` -Exception $_.Exception.Message -ValidationCriteria $validationCriteria -Remediation $remediation -StartedAt $startedAt -CompletedAt (Get-Date) -DisplayName $displayName } return Invoke-VcfCheckPerVCenterCheck -Context $Context -CheckId 'vcenter_snapshot_size' -Area vCenter -DisplayName $DisplayName -Body { param($Context, $VCenterFqdn) $allVms = @(Get-VcfCheckVMInventory -Server $VCenterFqdn) $managementVmTypes = @{} $managementVms = @($allVms | Where-Object { $guestIp = $_.ExtensionData.Summary.Guest.IpAddress $vmName = $_.Name $applianceType = if ($guestIp -and $managementIpTypes.ContainsKey($guestIp)) { $managementIpTypes[$guestIp] } elseif ($managementHostnameTypes.ContainsKey($vmName)) { $managementHostnameTypes[$vmName] } else { $null } if ($applianceType) { $managementVmTypes[$vmName] = $applianceType } [Boolean]$applianceType }) if ($managementVms.Count -eq 0) { return [PSCustomObject]@{ Status = 'Pass'; Detail = 'No management-appliance VMs were identified by guest IP or hostname - nothing to check.'; Rows = @() } } $allSnapshots = @(Get-VcfCheckVMSnapshotInventory -Server $VCenterFqdn) $vcenterStatus = 'Pass' $rows = foreach ($vm in $managementVms) { $vmSnapshots = @($allSnapshots | Where-Object { $_.VM.Name -eq $vm.Name }) if ($vmSnapshots.Count -eq 0) { [PSCustomObject]@{ Type = $managementVmTypes[$vm.Name] VM = $vm.Name Snapshots = 0 'Largest (GB)' = 'N/A' 'Total (GB)' = 'N/A' Status = 'Pass' } continue } $maxSizeGb = ($vmSnapshots | Measure-Object -Property SizeGB -Maximum).Maximum $totalSizeGb = ($vmSnapshots | Measure-Object -Property SizeGB -Sum).Sum if ($maxSizeGb -gt 50) { $vcenterStatus = 'Fail' $vmStatus = 'Fail' } elseif ($maxSizeGb -gt 5) { if ($vcenterStatus -ne 'Fail') { $vcenterStatus = 'Warning' } $vmStatus = 'Warning' } else { $vmStatus = 'Pass' } [PSCustomObject]@{ Type = $managementVmTypes[$vm.Name] VM = $vm.Name Snapshots = $vmSnapshots.Count 'Largest (GB)' = [Math]::Round($maxSizeGb, 1) 'Total (GB)' = [Math]::Round($totalSizeGb, 1) Status = $vmStatus } } $detail = if ($vcenterStatus -eq 'Pass') { "Checked $($managementVms.Count) management-appliance VM(s); no snapshots above the warning threshold." } else { "Checked $($managementVms.Count) management-appliance VM(s); see the table below for VMs with snapshots above threshold." } return [PSCustomObject]@{ Status = $vcenterStatus; Detail = $detail; Rows = @($rows) } } } |