Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update Request-SoSHealthJson to use the api #102

Merged
merged 9 commits into from
Mar 28, 2023
158 changes: 155 additions & 3 deletions VMware.CloudFoundation.Reporting.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Function Invoke-VcfHealthReport {
Write-LogMessage -Type INFO -Message "Setting up the log file to path $logfile."
Write-LogMessage -Type INFO -Message "Setting up report folder and report $reportName."
Write-LogMessage -Type INFO -Message "Running an SoS Health Check for $workflowMessage, process takes time."
$jsonFilePath = Invoke-Expression "Request-SoSHealthJson -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -rootPass $sddcManagerRootPass -reportPath $reportFolder $($commandSwitch)"
$jsonFilePath = Invoke-Expression "Request-SoSHealthJson -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -reportPath $reportFolder $($commandSwitch)"

# Generating the Service Health Data Using the SoS Data
Write-LogMessage -Type INFO -Message "Generating the Service Health Report using the SoS output for $workflowMessage."
Expand Down Expand Up @@ -824,7 +824,159 @@ Function Request-SoSHealthJson {
JSON file to the local file system.

.EXAMPLE
tenthirtyam marked this conversation as resolved.
Show resolved Hide resolved
Request-SoSHealthJson -server sfo-vcf01.sfo.rainpole.io -user admin@local -pass VMw@re1!VMw@re1! -rootPass VMw@re1! -reportPath F:\Precheck\HealthReports -allDomains
Request-SoSHealthJson -server sfo-vcf01.sfo.rainpole.io -user admin@local -pass VMw@re1!VMw@re1! -reportPath F:\Precheck\HealthReports -allDomains
tenthirtyam marked this conversation as resolved.
Show resolved Hide resolved
This example runs an SoS Health collection on all domains on the SDDC and saves the JSON output to the local file system.
#>

Param (
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$server,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$user,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$pass,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$reportPath,
[Parameter (ParameterSetName = 'All-WorkloadDomains', Mandatory = $true)] [ValidateNotNullOrEmpty()] [Switch]$allDomains,
[Parameter (ParameterSetName = 'Specific-WorkloadDomain', Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$workloadDomain
)

# Build Default Health Summary Check POST payload
garlicNova marked this conversation as resolved.
Show resolved Hide resolved
$healthChecksPayload = New-Object -TypeName psobject
$healthChecksPayload | Add-Member -notepropertyname 'certificateHealth' -notepropertyvalue $true
$healthChecksPayload | Add-Member -notepropertyname 'composabilityHealth' -notepropertyvalue $true
$healthChecksPayload | Add-Member -notepropertyname 'computeHealth' -notepropertyvalue $true
$healthChecksPayload | Add-Member -notepropertyname 'connectivityHealth' -notepropertyvalue $true
$healthChecksPayload | Add-Member -notepropertyname 'dnsHealth' -notepropertyvalue $true
$healthChecksPayload | Add-Member -notepropertyname 'generalHealth' -notepropertyvalue $true
$healthChecksPayload | Add-Member -notepropertyname 'hardwareCompatibilityHealth' -notepropertyvalue $true
$healthChecksPayload | Add-Member -notepropertyname 'ntpHealth' -notepropertyvalue $true
$healthChecksPayload | Add-Member -notepropertyname 'passwordHealth' -notepropertyvalue $true
$healthChecksPayload | Add-Member -notepropertyname 'servicesHealth' -notepropertyvalue $true
$healthChecksPayload | Add-Member -notepropertyname 'storageHealth' -notepropertyvalue $true
$healthChecksPayload | Add-Member -notepropertyname 'versionHealth' -notepropertyvalue $true
$optionsConfigPayload = New-Object -TypeName psobject
$optionsConfigPayload | Add-Member -notepropertyname 'force' -notepropertyvalue $false
$optionsConfigPayload | Add-Member -notepropertyname 'skipKnownHostCheck' -notepropertyvalue $true
$optionsIncludePayload = New-Object -TypeName psobject
$optionsIncludePayload | Add-Member -notepropertyname 'precheckReport' -notepropertyvalue $false
$optionsIncludePayload | Add-Member -notepropertyname 'summaryReport' -notepropertyvalue $false
$optionsPayload = New-Object -TypeName psobject
$optionsPayload | Add-Member -notepropertyname 'config' -notepropertyvalue $optionsConfigPayload
$optionsPayload | Add-Member -notepropertyname 'include' -notepropertyvalue $optionsIncludePayload
$scopeDomainsPayload = New-Object -TypeName psobject
$scopeDomainsPayload | Add-Member -notepropertyname 'clusterNames' -notepropertyvalue @()
$scopeDomainsPayload | Add-Member -notepropertyname 'domainName' -notepropertyvalue ""
$scopeDomainsPayload.clusterNames += @("")
$domainsArray = @()
$domainsArray += $scopeDomainsPayload
$scopePayload = New-Object -TypeName psobject
$scopePayload | Add-Member -notepropertyname 'domains' -notepropertyvalue @()
$scopePayload | Add-Member -notepropertyname 'includeAllDomains' -notepropertyvalue $false
$scopePayload | Add-Member -notepropertyname 'includeFreeHosts' -notepropertyvalue $false
$scopePayload.domains += $domainsArray
$healthSummarySpec = New-Object -TypeName psobject
$healthSummarySpec | Add-Member -notepropertyname 'healthChecks' -notepropertyvalue $healthChecksPayload
$healthSummarySpec | Add-Member -notepropertyname 'options' -notepropertyvalue $optionsPayload
$healthSummarySpec | Add-Member -notepropertyname 'scope' -notepropertyvalue $scopePayload

# Request VCF Token
Request-VCFToken -fqdn $server -Username $user -Password $pass -skipCertificateCheck -ErrorAction SilentlyContinue -ErrorVariable ErrMsg | Out-Null
garlicNova marked this conversation as resolved.
Show resolved Hide resolved

Try {
if ($PsBoundParameters.ContainsKey("allDomains")) {
$healthSummarySpec.scope.includeAllDomains = $true
if ($PSEdition -eq "Core" -and ($PSVersionTable.OS).Split(' ')[0] -eq "Linux") {
$reportDestination = ($reportDestination = ($reportPath + "\" + $server.Split(".")[0] + "-all-health-results.json")).split('\') -join '/' | Split-Path -NoQualifier
} else {
$reportDestination = ($reportPath + "\" + $server.Split(".")[0] + "-all-health-results.json")
}
} elseif ($PsBoundParameters.ContainsKey("workloadDomain")) {
$healthSummarySpec.scope.domains[0].domainName = $workloadDomain
if ($PSEdition -eq "Core" -and ($PSVersionTable.OS).Split(' ')[0] -eq "Linux") {
$reportDestination = ($reportDestination = ($reportPath + "\" + $workloadDomain + "-all-health-results.json")).split('\') -join '/' | Split-Path -NoQualifier
} else {
$reportDestination = ($reportPath + "\" + $workloadDomain + "-all-health-results.json")
}
}
if (Test-VCFConnection -server $server) {
# Create a temporary directory under reportDirectory
$createPathCounter = 0
for ($createPathCounter -lt 4) {
$randomOutput = -join (((48..57)+(65..90)+(97..122)) * 80 |Get-Random -Count 6 |%{[char]$_})
$outFilePath = $reportPath + "\" + $randomOutput
if (!(Test-Path -Path $outFilePath) -and (Test-Path -Path $reportPath)) {
break
garlicNova marked this conversation as resolved.
Show resolved Hide resolved
} else {
if ($createPathCounter -eq 3) {
Write-Error "Unable to write to $reportPath."
}
$createPathCounter++
}
}
New-Item -Path $outFilePath -ItemType Directory | Out-NULL

# Use REST API method to request the health summary.
garlicNova marked this conversation as resolved.
Show resolved Hide resolved
ConvertTo-JSON $healthSummarySpec -depth 10 | Out-File $outFilePath"\healthSummaryPayload.json"
$response = Start-VCFHealthSummary -json $outFilePath"\healthSummaryPayload.json"
if ($response.id -eq "") {
Write-Error "The Health Summary request encountered an issue. Please try again."
Return $false
}
$requestID = $response.id

# Retrieve the request status.
$response = Get-VCFHealthSummaryTask -id $requestID
$escapeCounter = 0
While (($escapeCounter -lt 30) -and !($response.status -match "COMPLETED")) {
sleep(30)
$escapeCounter++
$response = Get-VCFHealthSummaryTask -id $requestID
}
if ($escapeCounter -eq 20) {
Write-Error " The Health Summary request is taking an unusual amount of time to complete."
Return $false
}

# Download the summary tar.gz to a temporary directory.
garlicNova marked this conversation as resolved.
Show resolved Hide resolved
Request-VCFHealthSummaryBundle -id $requestID
$outFile = $outFilePath + "\health-summary.tar.gz"
$savedFile = "health-summary-"+$requestID+".tar"
if (Test-Path -Path $savedFile) {
Copy-Item $savedFile $outFile | Out-NULL
Remove-Item -Force $savedFile | Out-NULL
} else {
write-error "Encounter issues while downloading HealthSummaryBundle"
garlicNova marked this conversation as resolved.
Show resolved Hide resolved
Return $false
}

# Unzip/untar tar.gz file and extract health-results.json file
garlicNova marked this conversation as resolved.
Show resolved Hide resolved
tar -xzf $outFile -C $outFilePath | Out-NULL
tenthirtyam marked this conversation as resolved.
Show resolved Hide resolved
$healthSummaryPath = gci -recurse -filter "health-results.json" -Path $outFilePath
$healthSummaryFile = $healthSummaryPath.DirectoryName + "\health-results.json"
Copy-Item $healthSummaryFile $reportDestination | Out-NULL

# Remove the temporary Directory
garlicNova marked this conversation as resolved.
Show resolved Hide resolved
Remove-Item -Recurse -Force $outFilePath | Out-NULL

# Convert to JSON.
$temp = Get-Content -Path $reportDestination; $temp = $temp -replace '""', '"-"'; $temp | Out-File $reportDestination
Return $reportDestination
}
}
Catch {
Debug-CatchWriter -object $_
}
}
Export-ModuleMember -Function Request-SoSHealthJson

Function Request-SoSHealthJsonOld {
garlicNova marked this conversation as resolved.
Show resolved Hide resolved
<#
.SYNOPSIS
Run SoS and save the JSON output.

.DESCRIPTION
The Request-SoSHealthJsonOld cmdlet connects to SDDC Manager, runs an SoS Health collection to JSON, and saves the
JSON file to the local file system. This cmdlet method is outdated

.EXAMPLE
Request-SoSHealthJsonOld -server sfo-vcf01.sfo.rainpole.io -user admin@local -pass VMw@re1!VMw@re1! -rootPass VMw@re1! -reportPath F:\Precheck\HealthReports -allDomains
This example runs an SoS Health collection on all domains on the SDDC and saves the JSON output to the local file system.
#>

Expand Down Expand Up @@ -874,7 +1026,7 @@ Function Request-SoSHealthJson {
Debug-CatchWriter -object $_
}
}
Export-ModuleMember -Function Request-SoSHealthJson
Export-ModuleMember -Function Request-SoSHealthJsonOld

Function Publish-CertificateHealth {
<#
Expand Down