VMware Cloud Community
E4F
Contributor
Contributor

Charting the TOP 10 ESX host CPU Usage - Enjoy

# load the appropriate assemblies
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")

# Create a chart

$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Width = 1150
$Chart.Height = 500
$Chart.Left = 20
$Chart.Top = 30

# Create a chartarea to draw on and add to chart
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$Chart.ChartAreas.Add($ChartArea)

# Add title and axes labels
[void]$Chart.Titles.Add((Get-Date -Format D)) 
$ChartArea.AxisX.Interval = 1
$ChartArea.AxisX.LabelStyle.Angle = -45
$ChartArea.AxisY.Title = "CPU Usage %"


# Add data to chart
#---------------------------------------------------------------------------------------

Add-PSSnapin vRanger.API.PowerShell -ErrorAction SilentlyContinue 
Add-PSSnapin vmware.VimAutomation.core -ErrorAction SilentlyContinue

$NumReportDays = 7
$Collection = @()
get-vmhost | % {
$avgCPU = ($_ | Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddDays($NumReportDays * -1) | Measure-Object Value -Average | Select-Object -expandproperty average)

$Record = new-Object -typename System.Object
$Record | Add-Member -memberType noteProperty -name Host -Value $_.Name
$Record | Add-Member -memberType noteProperty -name CPU -Value ([System.Math]::Round($avgCPU,1))
$Collection += $Record
}

$objColl = $Collection | sort-object CPU -descending | select-object Host, CPU -first 10

$H = @(Foreach ($item in $objColl) {

     $item.Host
})

$C = @(Foreach ($item in $objColl) {

     $item.CPU
})

[void]$Chart.Series.Add("Data")
$Chart.Series["Data"].Points.DataBindXY($H, $C)

#---------------------------------------------------------------------------------------

# Make bars into 3d cylinders
$Chart.Series["Data"]["DrawingStyle"] = "Cylinder"

# Save the Chart
$Chart.SaveImage("$pwd\Test.jpg",[System.Windows.Forms.DataVisualization.Charting.ChartImageFormat]::JPEG)

Tags (1)
Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership

Nice script, but why do you need the vRanger module ?


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

Reply
0 Kudos
E4F
Contributor
Contributor

I was using that to upload the chart to an API and forgot to take it out of the code that I pasted.  Sorry.

Reply
0 Kudos