VMware Cloud Community
vmk2014
Expert
Expert
Jump to solution

CPU over commitment Report

Hi All,

I'm looking to convert the CPU over commitment report into CSV instead of table view format. Also, is it possible to get Memory over commitment as well ?

# Connect to vCenter using passthrough credentials

connect-viserver "vc.com";

# Get a list of hosts in a specific cluster (omit the get-cluster to get all hosts, and just use get-vmhost)

$hosts = get-cluster "MYCLUSTER" | get-vmhost;

# Define an empty table

$table = @();

# Loop through each host found

foreach ($vmhost in $hosts) {

    # Create an empty row

    $row = "" | select Hostname, LogicalCPUs, CPUsAssigned, Overcommited;

    # Get the number of vCPUs assigned

    $cpumeasure = $vmhost | get-vm | where {$_.powerstate -eq "poweredon"} | measure-object numcpu -sum;

    # Add the hostname to the row

    $row.Hostname = (($vmhost.name).split("."))[0];

    # Add the number of logical CPUs to the row

    $row.logicalcpus = $vmhost.numcpu * 2;

    # Add the number of allocated vCPUs to the row

    $row.cpusassigned = [int]$cpumeasure.sum;

    # Get the overcommitment level as a percentage

    $perc = [int](($cpumeasure.sum / $row.logicalcpus)*100);

    # Warn if overcommitted

    if ($perc -gt 100) {

        $row.Overcommited = "YES - " + $perc + "%";

    }

    else {

        $row.Overcommited = "No";

    }

# Add the current row to the table

$table = $table + $row;

}

# Display the table

$table | out-gridview;

Thanks

V

Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sure, try like this

Connect-VIServer "vc.com"


# Define an empty table

$table = @()


Get-Cluster -PipelineVariable cluster | ForEach-Object -Process {

    Get-VMHost -Location $cluster -PipelineVariable vmhost | ForEach-Object -Process {


        # Create an empty row

        $row = "" | Select-Object Cluster, Hostname, LogicalCPUs, CPUsAssigned, CPUOvercommited, MemoryGB, MemoryAssignedGB, MemoryOvercomitted

        # Add the clustername to the row

        $row.Cluster = $cluster.Name


        # Add the hostname to the row

        $row.Hostname = (($vmhost.name).split("."))[0]

        # Add the number of logical CPUs to the row

        $row.LogicalCPUs = ($vmhost.numcpu | Measure-Object -Sum).Sum


        # Get the number of vCPUs assigned

        $row.CPUsAssigned = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property numcpu -Sum).Sum

        if ($row.CPUsAssigned -eq $null) {

            $row.CPUsAssigned = 0

        }


        # Get the CPU overcommitment level as a percentage

        $perc = [int](($row.CPUsAssigned / $row.LogicalCPUs) * 100)

        # Warn if CPU overcommitted

        if ($perc -gt 100) {

            $row.CPUOvercommited = "YES - " + $perc + "%"

        }

        else {

            $row.CPUOvercommited = "No"

        }


        # Add the available memory to the row

        $row.MemoryGB = [math]::Round(($vmhost.MemoryTotalGB | Measure-Object -Sum).Sum, 1)


        # Get the memory assigned

        $row.MemoryAssignedGB = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property MemoryGB -Sum).Sum

        if ($row.MemoryAssignedGB -eq $null) {

            $row.MemoryAssignedGB = 0

        }

        # Get the memory overcommitment level as a percentage

        $perc = [int](($row.MemoryAssignedGB / $row.MemoryGB) * 100)

        # Warn if memory overcommitted

        if ($perc -gt 100) {

            $row.MemoryOvercomitted = "YES - " + $perc + "%"

        }

        else {

            $row.MemoryOvercomitted = "No"

        }


        # Add the current row to the table

        $table = $table + $row

    }

}

# Save the table to CSV

$table | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

11 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

# Connect to vCenter using passthrough credentials

Connect-VIServer "vc.com"


# Get a list of hosts in a specific cluster (omit the get-cluster to get all hosts, and just use get-vmhost)

$hosts = get-cluster "MYCLUSTER" | Get-VMHost


# Define an empty table

$table = @()


# Loop through each host found

foreach ($vmhost in $hosts) {

    # Create an empty row

    $row = "" | Select-Object Hostname, LogicalCPUs, CPUsAssigned, CPUOvercommited, MemoryGB, MemoryAssignedGB, MemoryOvercomitted

    # Add the hostname to the row

    $row.Hostname = (($vmhost.name).split("."))[0]

    # Add the number of logical CPUs to the row

    $row.LogicalCPUs = ($vmhost.numcpu | Measure-Object -Sum).Sum

    # Get the number of vCPUs assigned

    $row.CPUsAssigned = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property numcpu -Sum).Sum

    if ($row.CPUsAssigned -eq $null) {

        $row.CPUsAssigned = 0

    }


    # Get the CPU overcommitment level as a percentage

    $perc = [int](($row.CPUsAssigned / $row.LogicalCPUs) * 100)

    # Warn if CPU overcommitted

    if ($perc -gt 100) {

        $row.CPUOvercommited = "YES - " + $perc + "%"

    }

    else {

        $row.CPUOvercommited = "No"

    }


    # Add the available memory to the row

    $row.MemoryGB = [math]::Round(($vmhost.MemoryTotalGB | Measure-Object -Sum).Sum, 1)

    # Get the memory assigned

    $row.MemoryAssignedGB = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property MemoryGB -Sum).Sum

    if ($row.MemoryAssignedGB -eq $null) {

        $row.MemoryAssignedGB = 0

    }

    # Get the memory overcommitment level as a percentage

    $perc = [int](($row.MemoryAssignedGB / $row.MemoryGB) * 100)

    # Warn if memory overcommitted

    if ($perc -gt 100) {

        $row.MemoryOvercomitted = "YES - " + $perc + "%"

    }

    else {

        $row.MemoryOvercomitted = "No"

    }


    # Add the current row to the table

    $table = $table + $row

}

# Save the table to CSV

$table | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

LucD,

Thanks for quick turn around. My apologies, i forgot to mention. Can we get the report for all the cluster in a vCenter  instead of specific cluster ?

Thanks

V

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, try like this

Connect-VIServer "vc.com"


# Define an empty table

$table = @()


Get-Cluster -PipelineVariable cluster | ForEach-Object -Process {

    Get-VMHost -Location $cluster -PipelineVariable vmhost | ForEach-Object -Process {


        # Create an empty row

        $row = "" | Select-Object Cluster, Hostname, LogicalCPUs, CPUsAssigned, CPUOvercommited, MemoryGB, MemoryAssignedGB, MemoryOvercomitted

        # Add the clustername to the row

        $row.Cluster = $cluster.Name


        # Add the hostname to the row

        $row.Hostname = (($vmhost.name).split("."))[0]

        # Add the number of logical CPUs to the row

        $row.LogicalCPUs = ($vmhost.numcpu | Measure-Object -Sum).Sum


        # Get the number of vCPUs assigned

        $row.CPUsAssigned = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property numcpu -Sum).Sum

        if ($row.CPUsAssigned -eq $null) {

            $row.CPUsAssigned = 0

        }


        # Get the CPU overcommitment level as a percentage

        $perc = [int](($row.CPUsAssigned / $row.LogicalCPUs) * 100)

        # Warn if CPU overcommitted

        if ($perc -gt 100) {

            $row.CPUOvercommited = "YES - " + $perc + "%"

        }

        else {

            $row.CPUOvercommited = "No"

        }


        # Add the available memory to the row

        $row.MemoryGB = [math]::Round(($vmhost.MemoryTotalGB | Measure-Object -Sum).Sum, 1)


        # Get the memory assigned

        $row.MemoryAssignedGB = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property MemoryGB -Sum).Sum

        if ($row.MemoryAssignedGB -eq $null) {

            $row.MemoryAssignedGB = 0

        }

        # Get the memory overcommitment level as a percentage

        $perc = [int](($row.MemoryAssignedGB / $row.MemoryGB) * 100)

        # Warn if memory overcommitted

        if ($perc -gt 100) {

            $row.MemoryOvercomitted = "YES - " + $perc + "%"

        }

        else {

            $row.MemoryOvercomitted = "No"

        }


        # Add the current row to the table

        $table = $table + $row

    }

}

# Save the table to CSV

$table | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

vmk2014
Expert
Expert
Jump to solution

Thanks LucD.  It worked  smoothly.

Thanks

V

Reply
0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution

Hi LucD​,

Is there any reason why the result is duplicating like this:

  

ClusterHostname
PROD1172.135.25.4
PROD1172.135.25.5
PROD1192.168.25.3
PROD1192.168.25.4
PROD1192.168.25.5
PROD1192.168.25.6
PROD1192.168.25.7
PROD2172.135.25.4
PROD2172.135.25.5
PROD2192.168.25.3
PROD2192.168.25.4
PROD2192.168.25.5
PROD2192.168.25.6
PROD2192.168.25.7

Where it should look like these below:

  

ClusterHostname
PROD1172.135.25.4
PROD1172.135.25.5
PROD2192.168.25.3
PROD2192.168.25.4
PROD2192.168.25.5
PROD2192.168.25.6
PROD2192.168.25.7
/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Unless you have multiple connections to the same vCenter open, it shouldn't.

Check what is in $global:defaultVIServers


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

AlbertWT
Virtuoso
Virtuoso
Jump to solution

So far only one VCenter returned.

/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Found the issue, the Get-VMHost line should be

    Get-VMHost -Location $cluster -PipelineVariable vmhost | ForEach-Object -Process {

I corrected the code above as well.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

AlbertWT
Virtuoso
Virtuoso
Jump to solution

Yes, that does work great.

Thanks, Luc.

/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution

How can I add the VCenter hostname next to the VMHost/ESXi servers in a row when I supply more than 2 VCenter ?

$VcenterServerName = "VCSA-1", "VCSA-2"

$paramConnectVIServer = @{

Server   = $VcenterServerName

    Credential = Get-Credential -Message "Please enter your Admin / service account"

}

Connect-VIServer @paramConnectVIServer

/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Add the vCenter property to the $row, then use the [uri] type to get the vCenter hostname.

$row = "" | Select-Object vCenter, Cluster, Hostname, LogicalCPUs, CPUsAssigned, CPUOvercommited, MemoryGB, MemoryAssignedGB, MemoryOvercomitted

$row.vCenter = ([uri]$vmhost.ExtensionData.Client.ServiceUrl).Host


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos