#
# Name: Show-vCloudStatsHtml.ps1
# Version: 1.3
# Author: Timo Sugliani
# Date: 19/05/2013
#
# Based on Show-vCloudStats, and changing the display into an HTML htmlFile
# for better portability on generated report.
# Credits to Catalin Rosu for the html/css progress bars.
# http://www.red-team-design.com/stylish-css3-progress-bars
# Have been slightly modified to create guarantee bars under the progress bars.
#
# Changelog
#
# 31/12/2013 - Version 1.3
# Fixes:
# - Fix Storage profile issue (ignored when StorageTotal == 0)
# - Fix PowerCLI 5.5 R1 launch issue.
#
# 15/11/2013 - Version 1.2
# Added a new feature:
# - Number of VMs running vs Total VMs at the Provider VDC level
#
# 06/02/2013 - Version 1.1
# Added some new features:
# - Number of networks used per Org VDC vs allocation of network
# (Will be enabled at next PowerCLI release)
# - Number of VMs vs max allowed per Org VDC
#
# 05/18/2013 - Version 1.0
# Initial commit (Show-vCloudHtmlStats port)
function Html-Header() {
$htmlFile +=
'
vCloud Director Stats Report
vCloud Director - '+$global:DefaultCIServers[0].Name+' - '+$global:DefaultCIServers[0].Version+'
'
Add-content $htmlFilename -value $htmlFile
}
function Html-Footer() {
$htmlFile +=
'
'
Add-content $htmlFilename -value $htmlFile
}
function Show-UnlimitedPercentageGraph($text) {
$htmlFile =
"
";
Add-content $htmlFilename -value $htmlFile
}
function Show-PercentageGraph($percent, $text) {
$percent = [math]::Round($percent, 0)
$value = $percent
if ($percent -gt 100) {
$percent = 100
$color = "red"
}
elseif ($percent -gt 90) { $color = "red" }
elseif ($percent -le 60) { $color = "green" }
else { $color = "orange"; }
$htmlFile =
"
";
Add-content $htmlFilename -value $htmlFile
}
function Show-GuaranteeGraph($percent, $text) {
# Keep percent value simple
$percent = [math]::Round($percent, 0)
$value = $percent
if ($percent -gt 100) { $percent = 100 }
$htmlFile =
"
";
Add-content $htmlFilename -value $htmlFile
}
function Get-VMStats($OrganizationVDC) {
$OvDCVMsTotal = ((Get-CIVM -OrgVdc $OrganizationVDC) | measure).Count
if (!$OvDCVMsTotal) { $OvDCVMsTotal = 0 }
$OvDCVMsRunning = ((Get-CIVM -OrgVdc $OrganizationVDC) | ?{ $_.Status -eq "PoweredOn"} | measure).Count
if (!$OvDCVMsRunning) { $OvDCVMsRunning = 0 }
$VMStats = New-Object PsObject
$VMStats | Add-Member -type NoteProperty -name VMsRunning -value $OvDCVMsRunning
$VMStats | Add-Member -type NoteProperty -name VMsTotal -value $OvDCVMsTotal
return $VMStats
}
function Show-VMStats($OrganizationVDC) {
# Show VM stats (nb of VMs, VMs running against Quota)
$OvDCVMsTotal = ((Get-CIVM -OrgVdc $OrganizationVDC) | measure).Count
if (!$OvDCVMsTotal) { $OvDCVMsTotal = 0 }
$OvDCVMsRunning = ((Get-CIVM -OrgVdc $OrganizationVDC) | ?{ $_.Status -eq "PoweredOn"} | measure).Count
if (!$OvDCVMsRunning) { $OvDCVMsRunning = 0 }
$OvDCVMsStoredQuota = $OrganizationVDC.ExtensionData.VmQuota
$OrgSettings = $OrganizationVDC.Org.ExtensionData.Settings
$OrgVMsDeployedQuota = $OrgSettings.OrgGeneralSettings.DeployedVMQuota
$OrgVMsStoredQuota = $OrgSettings.OrgGeneralSettings.StoredVMQuota
Add-content $htmlFilename -value "
Virtual Machine Stats
"
if ($OvDCVMsStoredQuota -eq 0) {
$text = " VMs Count | Unlimited Quota"
$text += "- [$($OvDCVMsTotal) | Unlimited]"
Show-UnlimitedPercentageGraph $text
$text = " Running VMs Count | Total VMs Count "
$text += "- [$($OvDCVMsRunning) | $($OvDCVMsTotal)]"
Show-PercentageGraph ($OvDCVMsRunning/$OvDCVMsTotal*100) $text
}
else {
$text = " VMs Count | VMs Count Quota "
$text += "- [$($OvDCVMsTotal) | $($OvDCVMsStoredQuota)]"
Show-PercentageGraph ($OvDCVMsTotal/$OvDCVMsStoredQuota*100) $text
$text = " Running VMs Count | VMs Total Count "
$text += "- [$($OvDCVMsRunning) | $($OvDCVMsStoredQuota)]"
Show-PercentageGraph ($OvDCVMsRunning/$OvDCVMsStoredQuota*100) $text
}
}
function Show-NetworkStats($NetworksUsed, $NetworksTotal) {
# Show Network stats (total should not be 0, but by default is 100000)
# VMware tested until 5000 for VXLAN and 1000 for vCDNI
# Need additional tests
# In PowerCLI 5.1 R1 the $NetworkUsed metric doesn't exist.
# ($OrganizationVDC.ExtensionData.UsedNetworkCount)
if ($NetworksUsed -eq 0) {
$text = " Networks Used | Network Quota (No Quota) "
$text += "- [$($NetworksUsed) | 5000]"
Show-PercentageGraph ($NetworksUsed/5000*100) $text
}
else {
$text = " Networks Used | Network Quota "
$text += "- [$($NetworksUsed) | $($NetworksTotal)]"
Show-PercentageGraph ($NetworksUsed/$NetworksTotal*100) $text
}
}
function Show-StorageStats($storageUsed, $Storageallocated, $StorageTotal) {
# Show storage stats
if ($StorageAllocated -eq 0) {
$text = " Storage Used | PvDC Total Storage (No Quota) "
$text += "- [$($StorageUsed)GB | $($StorageTotal)GB]"
# Storage Profile Storage Metric issue
if ($StorageTotal -ne 0) {
Show-PercentageGraph ($StorageUsed/$StorageTotal*100) $text
}
}
else {
$text = " Storage Used | Allocated Quota "
$text += "- [$($StorageUsed)GB | $($StorageAllocated)GB]"
Show-PercentageGraph ($StorageUsed/$StorageAllocated*100) $text
}
}
function Show-ExternalNetworkStats() {
$extNetworks = Get-ExternalNetwork
Add-content $htmlFilename -value "External Networks
"
foreach($extNet in $extNetworks) {
$text = " IP Allocation for $($extNet)"
Show-PercentageGraph ($extNet.UsedIpCount/$extNet.TotalIpCount*100) $text
}
# Add Network Pool stats (VXLAN total OvDC Networks used / 5000*100)
}
function Show-ProviderVDCStats($ProviderVDC) {
Add-content $htmlFilename -value "Provider VDC : $ProviderVDC
"
$CpuAllocated = [math]::Round($ProviderVDC.CpuAllocatedGHz, 2)
$CpuUsed = [math]::Round($ProviderVDC.CpuUsedGHz, 2)
$CpuTotal = [math]::Round($ProviderVDC.CpuTotalGHz, 2)
$MemAllocated = [math]::Round($ProviderVDC.MemoryAllocatedGB, 2)
$MemUsed = [math]::Round($ProviderVDC.MemoryUsedGB, 2)
$MemTotal = [math]::Round($ProviderVDC.MemoryTotalGB, 2)
$StorageAllocated = [math]::Round($ProviderVDC.StorageAllocatedGB, 2)
$StorageUsed = [math]::Round($ProviderVDC.StorageUsedGB, 2)
$StorageTotal = [math]::Round($ProviderVDC.StorageTotalGB, 2)
Add-content $htmlFilename -value "Resources Used & Allocated vs Total
"
$text = "CPU Used | Total [$($CpuUsed)Ghz | $($CpuTotal)Ghz]"
Show-PercentageGraph ($CpuUsed/$CpuTotal*100) $text
$text = "CPU Allocated [$($CpuAllocated)Ghz]"
Show-GuaranteeGraph ($CpuAllocated/$CpuTotal*100) $text
$text = "MEM Used | Total [$($MemUsed)GB | $($MemTotal)GB]"
Show-PercentageGraph ($MemUsed/$MemTotal*100) $text
$text = "MEM Allocated [$($MemAllocated)GB]"
Show-GuaranteeGraph ($MemAllocated/$MemTotal*100) $text
# Storage Profile Storage Metric issue
if ($StorageTotal -ne 0) {
$text = "Storage Used | Total [$($StorageUsed)GB | $($StorageTotal)GB]"
Show-PercentageGraph ($StorageUsed/$StorageTotal*100) $text
$text = "Storage Allocated [$($StorageAllocated)GB]"
Show-GuaranteeGraph ($StorageAllocated/$StorageTotal*100) $text
}
# Fetch All Organization VDC in the current Provider VDC.
$OvDCs = Get-OrgVdc -ProviderVdc $PvDC
$PvDCTotalVMs = 0
$PvDCTotalVMsRunning = 0
foreach ($OvDC in $OvDCs) {
# Fetch VMs Stats in each Org vDC
$OvDCVMStats = Get-VMStats $OvDC
$PvDCTotalVMsRunning += $OvDCVMStats.VMsRunning
$PvDCTotalVMs += $OvDCVMStats.VMsTotal
}
if ($PvDCTotalVMs -gt 0) {
$text = "VMs Running | Total [$($PvDCTotalVMsRunning)VMs | $($PvDCTotalVMs)VMs]"
Show-PercentageGraph ($PvDCTotalVMsRunning/$PvDCTotalVMs*100) $text
}
}
function Show-AllocationModelPAYGStats($OrganizationVDC, $ProviderVDC) {
# Provider VDC Total Capacity
$CpuTotal = [math]::Round($ProviderVDC.CpuTotalGHz, 2)
$MemTotal = [math]::Round($ProviderVDC.MemoryTotalGB, 2)
$StorageTotal = [math]::Round($ProviderVDC.StorageTotalGB, 2)
# Organization VDC Stats
$CpuUsed = [math]::Round($OrganizationVDC.CpuUsedGhz, 2)
$CpuGuarantee = $OrganizationVDC.CpuGuaranteedPercent
$MemUsed = [math]::Round($OrganizationVDC.MemoryUsedGB, 2)
$MemGuarantee = $OrganizationVDC.MemoryGuaranteedPercent
$NetworksUsed = $OrganizationVDC.ExtensionData.UsedNetworkCount
$NetworksTotal = $OrganizationVDC.ExtensionData.NetworkQuota
$StorageUsed = [math]::Round($OrganizationVDC.StorageUsedGB, 2)
$StorageAllocated = [math]::Round($OrganizationVDC.StorageLimitGB, 2)
# Based on the Provider VDC stats as no allocation is done at the OvDC lvl
# Maybe using used resources / sum(vApps allocated in vDC) would be better ?
# TODO: Add vCPU Speed limit
Add-content $htmlFilename -value "Resources Used vs Provider VDC Total
"
$text = "CPU Used | Total [$($CpuUsed)Ghz|$($CpuTotal)Ghz]"
Show-PercentageGraph ($CpuUsed/$CpuTotal*100) $text
$text = "MEM Used | Total [$($MemUsed)GB|$($MemTotal)GB]"
Show-PercentageGraph ($MemUsed/$MemTotal*100) $text
# Waiting for next release of PowerCLI 5.x R3 ?
# Show-NetworkStats $NetworksUsed $NetworksTotal
Show-StorageStats $StorageUsed $StorageAllocated $StorageTotal
Show-VMStats $OrganizationVDC
}
function Show-AllocationModelAllocationStats($OrganizationVDC, $ProviderVDC) {
# Provider VDC Total Capacity
$CpuTotal = [math]::Round($ProviderVDC.CpuTotalGHz, 2)
$MemTotal = [math]::Round($ProviderVDC.MemoryTotalGB, 2)
$StorageTotal = [math]::Round($ProviderVDC.StorageTotalGB, 2)
# Organization VDC Stats
$CpuUsed = [math]::Round($OrganizationVDC.CpuUsedGhz, 2)
$CpuAllocated = [math]::Round($OrganizationVDC.CpuAllocationGhz, 2)
# check this, and add it into the calculations if $CpuUsed isn't valid
$CpuSpeedLimit = [math]::Round($OrganizationVDC.CpuSpeedLimitGhz, 2)
$CpuGuarantee = $OrganizationVDC.CpuGuaranteedPercent
$CpuGuaranteeTotal = [math]::Round($CpuAllocated*$CpuGuarantee/100, 2)
$MemUsed = [math]::Round($OrganizationVDC.MemoryUsedGB, 2)
$MemAllocated = [math]::Round($OrganizationVDC.MemoryAllocationGB, 2)
$MemGuarantee = $OrganizationVDC.MemoryGuaranteedPercent
$MemGuaranteeTotal = [math]::Round($MemAllocated*$MemGuarantee/100, 2)
$NetworksUsed = $OrganizationVDC.ExtensionData.UsedNetworkCount
$NetworksTotal = $OrganizationVDC.ExtensionData.NetworkQuota
$StorageUsed = [math]::Round($OrganizationVDC.StorageUsedGB, 2)
$StorageAllocated = [math]::Round($OrganizationVDC.StorageLimitGB, 2)
Add-content $htmlFilename -value "Resources Used vs Org VDC Allocatation
"
$text = "CPU Used | Total [$($CpuUsed)Ghz | $($CpuAllocated)Ghz]"
Show-PercentageGraph ($CpuUsed/$CpuAllocated*100) $text
$text = "Guarantee"
Show-GuaranteeGraph $CpuGuarantee $text
$text = "MEM Used | Total [$($MemUsed)GB | $($MemAllocated)GB]"
Show-PercentageGraph ($MemUsed/$MemAllocated*100) $text
$text = "Guarantee"
Show-GuaranteeGraph $MemGuarantee $text
Show-StorageStats $StorageUsed $StorageAllocated $StorageTotal
Add-content $htmlFilename -value "Resources Used & Allocated vs Provider VDC Total
"
$text = "CPU Used | Total [$($CpuUsed)Ghz | $($CpuTotal)Ghz]"
Show-PercentageGraph ($CpuUsed/$CpuTotal*100) $text
$text = "Guarantee [$($CpuGuaranteeTotal)Ghz]"
Show-GuaranteeGraph ($CpuGuaranteeTotal/$CpuTotal*100) $text
$text = "MEM Used | Total [$($MemUsed)GB | $($MemTotal)GB]"
Show-PercentageGraph ($MemUsed/$MemTotal*100) $text
$text = "Guarantee [$($MemGuaranteeTotal)GB]"
Show-GuaranteeGraph ($MemGuaranteeTotal/$MemTotal*100) $text
# Waiting for next release of PowerCLI 5.x R3 ?
# Show-NetworkStats $NetworksUsed $NetworksTotal
Show-StorageStats $StorageUsed 0 $StorageTotal
Show-VMStats $OrganizationVDC
}
function Show-AllocationModelReservationStats($OrganizationVDC, $ProviderVDC) {
# Provider VDC Total Capacity
$CpuTotal = [math]::Round($ProviderVDC.CpuTotalGHz, 2)
$MemTotal = [math]::Round($ProviderVDC.MemoryTotalGB, 2)
$StorageTotal = [math]::Round($ProviderVDC.StorageTotalGB, 2)
# Organization VDC Stats
$CpuUsed = [math]::Round($OrganizationVDC.CpuUsedGhz, 2)
$CpuAllocated = [math]::Round($OrganizationVDC.CpuAllocationGhz, 2)
$CpuGuarantee = $OrganizationVDC.CpuGuaranteedPercent # 100 in RP
$MemUsed = [math]::Round($OrganizationVDC.MemoryUsedGB, 2)
$MemAllocated = [math]::Round($OrganizationVDC.MemoryAllocationGB, 2)
$MemGuarantee = $OrganizationVDC.MemoryGuaranteedPercent # 100 in RP
$NetworksUsed = $OrganizationVDC.ExtensionData.UsedNetworkCount
$NetworksTotal = $OrganizationVDC.ExtensionData.NetworkQuota
$StorageUsed = [math]::Round($OrganizationVDC.StorageUsedGB, 2)
$StorageAllocated = [math]::Round($OrganizationVDC.StorageLimitGB, 2)
Add-content $htmlFilename -value "Resources Used vs Org VDC Allocatation
"
$text = "CPU Used | Allocated [$($CpuUsed)Ghz | $($CpuAllocated)Ghz]"
Show-PercentageGraph ($CpuUsed/$CpuAllocated*100) $text
$text = "Guarantee [$($CpuAllocated)Ghz]"
Show-GuaranteeGraph $CpuGuarantee $text
$text = "MEM Used | Allocated [$($MemUsed)GB | $($MemAllocated)GB]"
Show-PercentageGraph ($MemUsed/$MemAllocated*100) $text
$text = "Guarantee [$($MemAllocated)GB]"
Show-GuaranteeGraph $MemGuarantee $text
Show-StorageStats $StorageUsed $StorageAllocated $StorageTotal
Add-content $htmlFilename -value "Resources Used & Allocated vs Provider VDC Total
"
$text = "CPU Used | Total [$($CpuUsed)Ghz | $($CpuTotal)Ghz]"
Show-PercentageGraph ($CpuUsed/$CpuTotal*100) $text
$text = "Guarantee [$($CpuAllocated)Ghz]"
Show-GuaranteeGraph ($CpuAllocated/$CpuTotal*100) $text
$text = "MEM Used | Total [$($MemUsed)GB | $($MemTotal)GB]"
Show-PercentageGraph ($MemUsed/$MemTotal*100) $text
$text = "Guarantee [$($MemAllocated)GB]"
Show-GuaranteeGraph ($MemAllocated/$MemTotal*100) $text
# Waiting for next release of PowerCLI 5.x R3 ?
# Show-NetworkStats $NetworksUsed $NetworksTotal
Show-StorageStats $StorageUsed 0 $StorageTotal
Show-VMStats $OrganizationVDC
}
function Show-OrganizationVDCStats($OrganizationVDC, $ProviderVDC) {
$AllocationModel = $OrganizationVDC.AllocationModel.toString()
$Organization = $OrganizationVDC.Org.Name
$htmlFile = ""
$htmlFile += "Organization VDC:$($OrganizationVDC)"
$htmlFile += " - ($AllocationModel)"
$htmlFile += " - Organization: $($Organization)"
$htmlFile += "
"
Add-content $htmlFilename -value $htmlFile
# Check Allocation Model and display accordingly
switch ($AllocationModel) {
"PayAsYouGo" {
Show-AllocationModelPAYGStats $OrganizationVDC $ProviderVDC
}
"AllocationPool" {
Show-AllocationModelAllocationStats $OrganizationVDC $ProviderVDC
}
"ReservationPool" {
Show-AllocationModelReservationStats $OrganizationVDC $ProviderVDC
}
}
}
# Get PowerCLI version
$PowerCLIVersion = (Get-PowerCLIVersion).SnapinVersions[2].Build
if ($PowerCLIVersion -gt 793505)
{
Write-Host "Unfortunately VMware PowerCLI 5.1.0 R2 and newer versions have an issue with Storage metrics that are invalid (Storage Profiles)"
Write-Host "Those metrics will be ignored, and hence the report might be a bit less complete."
}
# Connect to vCloud Director as System Administrator
Connect-CIServer
# Create filename accordingly
$htmlFilename = $home+"\Desktop\Show-vCloudStats-"+$global:DefaultCIServers[0].Name+".html"
set-content $htmlFilename -value ""
# Set the HTML Header for the report
Html-Header
# Display some vCloud External Network stats
Show-ExternalNetworkStats
# Fetch Provider VDCs
$PvDCs = Get-ProviderVdc
# Loop through each Provider VDC
foreach($PvDC in $PvDCs) {
# Display some Provider VDC Stats
Show-ProviderVDCStats $PvDC
# Fetch All Organization VDC in the current Provider VDC.
$OvDCs = Get-OrgVdc -ProviderVdc $PvDC
foreach ($OvDC in $OvDCs) {
#Display some Organization VDC Stats
Show-OrganizationVDCStats $OvDC $PvDC
}
}
# Set the HTML Footer for the report
Html-Footer
# Disconnect from all vCloud Instance
Disconnect-CIServer * -confirm:$false