Private/Checks/VCenter/Test-VcfVcenterVmdirDatabaseSizeCheck.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-VcfVcenterVmdirDatabaseSizeCheck {

    <#
        .SYNOPSIS
        Checks that the vCenter appliance vmdir database file is valid and within safe size thresholds.

        .DESCRIPTION
        Queries the vmdir database file (`/storage/db/vmware-vmdir/data.mdb`) size in megabytes across all vCenter appliances
        attached to SDDC Manager using Invoke-VcfCheckVCenterApplianceCliCheck.

        Evaluates database file size:
        - Fail: Database file size is 0 MB or unreadable (indicating data corruption), or size is 1024 MB or greater
          (exceeding default database capacity).
        - Warning: Database file size is 900 MB or greater up to 1024 MB (approaching database capacity limit).
        - Pass: Database file size is valid and below 900 MB.

        Populates a structured Rows table detailing DatabaseFile, SizeMB, and Status.
        Delegates per-vCenter execution and per-domain outcome packaging to Invoke-VcfCheckVCenterApplianceCliCheck.

        .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-VcfCheckVCenterApplianceCliCheck.
    #>


    [CmdletBinding()]
    [OutputType([PSObject])]
    Param (
        [Parameter(Mandatory = $true)] [PSObject]$Context,
        [Parameter(Mandatory = $false)] [String]$DisplayName = ''
    )

    $maxSizeMb = 1024
    $warnSizeMb = 900
    $sizeLimitKbUrl = 'https://knowledge.broadcom.com/external/article/344675/adding-new-a-license-or-sso-user-fails-w.html'
    $corruptionKbUrl = 'https://knowledge.broadcom.com/external/article/425157/vcenter-server-ui-login-fails-with-no-he.html'

    return Invoke-VcfCheckVCenterApplianceCliCheck -Context $Context -CheckId 'vcenter_vmdir_database_size_check' -DisplayName $DisplayName `
        -ScriptText 'du -m "/storage/db/vmware-vmdir/data.mdb"' `
        -ParseOutcome {
            param($CommandResult)

            $sizeMb = $null
            $firstToken = ($CommandResult.ScriptOutput.Trim() -split '\s+')[0]
            $parsedSize = 0
            if ([Int32]::TryParse($firstToken, [ref]$parsedSize)) {
                $sizeMb = $parsedSize
            }

            if ($null -eq $sizeMb -or $sizeMb -eq 0) {
                $rows = @([PSCustomObject]@{
                    DatabaseFile = '/storage/db/vmware-vmdir/data.mdb'
                    SizeMB = 0
                    Status = 'Fail'
                })
                $detail = "vmdir data file size is $(if ($null -eq $sizeMb) { 'unreadable' } else { '0 MB' }) (raw output: $($CommandResult.ScriptOutput.Trim())), indicating a corrupted/missing vmdir database - see $corruptionKbUrl to restore data.mdb from its snapshot and restart vmdird."
                return [PSCustomObject]@{ Status = 'Fail'; Detail = $detail; Rows = $rows }
            }

            if ($sizeMb -ge $maxSizeMb) {
                $rows = @([PSCustomObject]@{
                    DatabaseFile = '/storage/db/vmware-vmdir/data.mdb'
                    SizeMB = $sizeMb
                    Status = 'Fail'
                })
                $detail = "vmdir data file size is $sizeMb MB, at or above the default $maxSizeMb MB VMDIR database size limit - adding licenses or SSO users will fail with a storage error until this is remediated, see $sizeLimitKbUrl."
                return [PSCustomObject]@{ Status = 'Fail'; Detail = $detail; Rows = $rows }
            }

            if ($sizeMb -ge $warnSizeMb) {
                $rows = @([PSCustomObject]@{
                    DatabaseFile = '/storage/db/vmware-vmdir/data.mdb'
                    SizeMB = $sizeMb
                    Status = 'Warning'
                })
                $detail = "vmdir data file size is $sizeMb MB, approaching the default $maxSizeMb MB VMDIR database size limit, see $sizeLimitKbUrl."
                return [PSCustomObject]@{ Status = 'Warning'; Detail = $detail; Rows = $rows }
            }

            $rows = @([PSCustomObject]@{
                DatabaseFile = '/storage/db/vmware-vmdir/data.mdb'
                SizeMB = $sizeMb
                Status = 'Pass'
            })
            return [PSCustomObject]@{ Status = 'Pass'; Detail = "vmdir data file size is $sizeMb MB."; Rows = $rows }
        }
}