forked from v-team/powercli-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-Load.ps1
More file actions
191 lines (182 loc) · 11.6 KB
/
Get-Load.ps1
File metadata and controls
191 lines (182 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
function Get-Load {
<#
.SYNOPSIS
Get load average for multiple object
.DESCRIPTION
The function will display average load for vSphere object passed through pipeline.
.NOTES
Author: www.vmdude.fr
.PARAMETER LoadType
Specify wich load to display when cmdlet run on standalone
Valid LoadType are 'VirtualMachine', 'HostSystem', 'ClustercomputeResource', 'Datastore'
.PARAMETER Quickstat
Switch, when true the method to get stats is based
on quickstats through summary child properties.
If not, the method will use PerfManager instance
with QueryPerf method in order to get non computed stats.
The default for this switch is $true.
.EXAMPLE
PS> Get-Load -LoadType ClusterComputeResource
PS> Get-Cluster | Get-Load
Get a graphical list for all cluster load
.EXAMPLE
PS> Get-VMHost ESX01, ESX02 | Get-Load
Get a graphical list for host load for ESX01 and 02
.EXAMPLE
PS> Get-VM "vmtest*" | Get-Load
Get a graphical load list for all VM with name started with vmtest
.EXAMPLE
PS> Get-Datastore -Location STORAGEPOD01 | Get-Load
Get a graphical space usage for all datastores in storage pod STORAGEPOD01
#>
[CmdletBinding(DefaultParameterSetName='GetViewByVIObject')]
param(
[Parameter(ParameterSetName='GetViewByVIObject', Position=0, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[VMware.VimAutomation.Sdk.Types.V1.VIObject[]]
${VIObject},
[Parameter(ParameterSetName='GetEntity', Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
${LoadType},
[parameter(Mandatory = $false)]
[switch]$Quickstat = $true
)
begin{
function Show-PercentageGraph([int]$percent, [int]$maxSize=20) {
if ($percent -gt 100) { $percent = 100 }
$warningThreshold = 60 # percent
$alertThreshold = 80 # percent
[string]$g = [char]9632 #this is the graph character, use [char]9608 for full square character
if ($percent -lt 10) { write-host -nonewline "0$percent [ " } else { write-host -nonewline "$percent [ " }
for ($i=1; $i -le ($barValue = ([math]::floor($percent * $maxSize / 100)));$i++) {
if ($i -le ($warningThreshold * $maxSize / 100)) { write-host -nonewline -foregroundcolor darkgreen $g }
elseif ($i -le ($alertThreshold * $maxSize / 100)) { write-host -nonewline -foregroundcolor yellow $g }
else { write-host -nonewline -foregroundcolor red $g }
}
for ($i=1; $i -le ($traitValue = $maxSize - $barValue);$i++) { write-host -nonewline "-" }
write-host -nonewline " ]"
}
function Show-GetLoadUsage() {
Write-Host -Foreground Yellow "Standalone usage for this cmdlet : Get-Load -LoadType <LoadType>"`n
Write-Host -Foreground Yellow "Valid LoadType are:"
Write-Host -Foreground Yellow "'VirtualMachine' for virtual machine"
Write-Host -Foreground Yellow "'HostSystem' for ESXi hosts"
Write-Host -Foreground Yellow "'ClustercomputeResource' for vSphere cluster"
Write-Host -Foreground Yellow "'Datastore' for VMFS/NFS datastore"
}
function VM-Load([VMware.VimAutomation.Sdk.Types.V1.VIObject]$VMName) {
$VM = Get-View $VMName -Property summary,name
if ($Quickstat) {
if ($VM.name.length -gt 20) { write-host -nonewline $VM.name.substring(0, 22 -5) "... CPU:" } else { write-host -nonewline $VM.name (" "*(20-$VM.name.length)) "CPU:" }
# using QuickStat for getting "realtime"-ish stats
if ($VM.summary.quickStats.overallCpuUsage -ne $null -And $VM.summary.quickStats.guestMemoryUsage -ne $null -And $VM.summary.runtime.powerState -eq "poweredOn") {
Show-PercentageGraph([math]::floor(($VM.summary.quickStats.overallCpuUsage*100)/($VM.summary.config.numCpu * $VM.summary.runtime.maxCpuUsage)))
write-host -nonewline `t"MEM:"
Show-PercentageGraph([math]::floor(($VM.summary.quickStats.guestMemoryUsage*100)/($VM.summary.config.memorySizeMB)))
} else {
Show-PercentageGraph(0)
write-host -nonewline `t"MEM:"
Show-PercentageGraph(0)
}
write-host ""
} else {
# using PerfManager instance in order to bypass Get-Stat cmdlet for speed
# but this method is quite low to get stats
if ($VM.name.length -gt 20) { write-host -nonewline $VM.name.substring(0, 22 -5) "... CPU:" } else { write-host -nonewline $VM.name (" "*(20-$VM.name.length)) "CPU:" }
Show-PercentageGraph([math]::floor((($objPerfManager.QueryPerf((New-Object VMware.Vim.PerfQuerySpec -property @{entity = $VM.moref;format = "normal";IntervalId = "300";StartTime=((Get-Date).AddDays(-1));EndTime=(Get-Date);MetricId = (New-Object VMware.Vim.PerfMetricId -property @{instance = "";counterId = $avgUsageCpuCounter})})) |%{$_.value}|%{$_.value}|measure -Average).average/100)))
write-host -nonewline `t"MEM:"
Show-PercentageGraph([math]::floor((($objPerfManager.QueryPerf((New-Object VMware.Vim.PerfQuerySpec -property @{entity = $VM.moref;format = "normal";IntervalId = "300";StartTime=((Get-Date).AddDays(-1));EndTime=(Get-Date);MetricId = (New-Object VMware.Vim.PerfMetricId -property @{instance = "";counterId = $avgConsumedMemCounter})})) |%{$_.value}|%{$_.value}|measure -Average).average/100)))
write-host ""
}
}
function VMHost-Load([VMware.VimAutomation.Sdk.Types.V1.VIObject]$VMHostName) {
$VMHost = Get-View $VMHostName -Property summary,name
if ($Quickstat) {
if ($VMHost.name.length -gt 20) { write-host -nonewline $VMHost.name.substring(0, 22 -5) "... CPU:" } else { write-host -nonewline $VMHost.name (" "*(20-$VMHost.name.length)) "CPU:" }
# using QuickStat for getting "realtime"-ish stats
if ($VMHost.summary.quickStats.overallCpuUsage -ne $null -And $VMHost.summary.quickStats.overallMemoryUsage -ne $null -And $VMHost.summary.runtime.powerState -eq "poweredOn") {
Show-PercentageGraph([math]::floor(($VMHost.summary.quickStats.overallCpuUsage*100)/($VMHost.summary.hardware.numCpuCores * $VMHost.summary.hardware.cpuMhz)))
write-host -nonewline `t"MEM:"
Show-PercentageGraph([math]::floor(($VMHost.summary.quickStats.overallMemoryUsage*1024*1024*100)/($VMHost.summary.hardware.memorySize)))
} else {
Show-PercentageGraph(0)
write-host -nonewline `t"MEM:"
Show-PercentageGraph(0)
}
write-host ""
} else {
# using PerfManager instance in order to bypass Get-Stat cmdlet for speed
# but this method is quite low to get stats
if ($VMHost.name.length -gt 20) { write-host -nonewline $VMHost.name.substring(0, 22 -5) "... CPU:" } else { write-host -nonewline $VMHost.name (" "*(20-$VMHost.name.length)) "CPU:" }
Show-PercentageGraph([math]::floor((($objPerfManager.QueryPerf((New-Object VMware.Vim.PerfQuerySpec -property @{entity = $VMHost.moref;format = "normal";IntervalId = "300";StartTime=((Get-Date).AddDays(-1));EndTime=(Get-Date);MetricId = (New-Object VMware.Vim.PerfMetricId -property @{instance = "";counterId = $avgUsageCpuCounter})})) |%{$_.value}|%{$_.value}|measure -Average).average/100)))
write-host -nonewline `t"MEM:"
Show-PercentageGraph([math]::floor((($objPerfManager.QueryPerf((New-Object VMware.Vim.PerfQuerySpec -property @{entity = $VMHost.moref;format = "normal";IntervalId = "300";StartTime=((Get-Date).AddDays(-1));EndTime=(Get-Date);MetricId = (New-Object VMware.Vim.PerfMetricId -property @{instance = "";counterId = $avgConsumedMemCounter})})) |%{$_.value}|%{$_.value}|measure -Average).average/100)))
write-host ""
}
}
function Cluster-Load([VMware.VimAutomation.Sdk.Types.V1.VIObject]$ClusterName) {
$cluster = Get-View $ClusterName -Property resourcePool,name
if ($Quickstat) {
if ($cluster.name.length -gt 20) { write-host -nonewline $cluster.name.substring(0, 22 -5) "... CPU:" } else { write-host -nonewline $cluster.name (" "*(20-$cluster.name.length)) "CPU:" }
# using QuickStat for getting "realtime"-ish stats
$rootResourcePool = get-view $cluster.resourcePool -Property summary
if ($rootResourcePool.summary.runtime.cpu.maxUsage -ne 0 -And $rootResourcePool.summary.runtime.memory.maxUsage -ne 0) {
Show-PercentageGraph([math]::floor(($rootResourcePool.summary.runtime.cpu.overallUsage*100)/($rootResourcePool.summary.runtime.cpu.maxUsage)))
write-host -nonewline `t"MEM:"
Show-PercentageGraph([math]::floor(($rootResourcePool.summary.runtime.memory.overallUsage*100)/($rootResourcePool.summary.runtime.memory.maxUsage)))
} else {
Show-PercentageGraph(0)
write-host -nonewline `t"MEM:"
Show-PercentageGraph(0)
}
write-host ""
} else {
# using PerfManager instance in order to bypass Get-Stat cmdlet for speed
# but this method is quite low to get stats
if ($cluster.name.length -gt 20) { write-host -nonewline $cluster.name.substring(0, 22 -5) "... CPU:" } else { write-host -nonewline $cluster.name (" "*(20-$cluster.name.length)) "CPU:" }
Show-PercentageGraph([math]::floor((($objPerfManager.QueryPerf((New-Object VMware.Vim.PerfQuerySpec -property @{entity = $cluster.moref;format = "normal";IntervalId = "300";StartTime=((Get-Date).AddDays(-1));EndTime=(Get-Date);MetricId = (New-Object VMware.Vim.PerfMetricId -property @{instance = "";counterId = $avgUsageCpuCounter})})) |%{$_.value}|%{$_.value}|measure -Average).average/100)))
write-host -nonewline `t"MEM:"
Show-PercentageGraph([math]::floor((($objPerfManager.QueryPerf((New-Object VMware.Vim.PerfQuerySpec -property @{entity = $cluster.moref;format = "normal";IntervalId = "300";StartTime=((Get-Date).AddDays(-1));EndTime=(Get-Date);MetricId = (New-Object VMware.Vim.PerfMetricId -property @{instance = "";counterId = $avgConsumedMemCounter})})) |%{$_.value}|%{$_.value}|measure -Average).average/100)))
write-host ""
}
}
function Datastore-Load([VMware.VimAutomation.Sdk.Types.V1.VIObject]$DatastoreName) {
$Datastore = Get-View $DatastoreName -Property summary,name
if ($Datastore.name.length -gt 30) { write-host -nonewline $Datastore.name.substring(0, 22 -5) "... USED:" } else { write-host -nonewline $Datastore.name (" "*(30-$Datastore.name.length)) "USED:" }
if ($Datastore.summary.Capacity -ne 0 -And $Datastore.summary.FreeSpace -ne 0) {
Show-PercentageGraph([math]::floor((($Datastore.summary.Capacity-$Datastore.summary.FreeSpace)*100)/($Datastore.summary.Capacity)))
} else {
Show-PercentageGraph(0)
}
write-host ""
}
$objPerfManager = Get-View (Get-View ServiceInstance -Property content).content.PerfManager
$avgConsumedMemCounter = ($objPerfManager.PerfCounter | ?{ $_.groupinfo.key -match "mem" } | ?{ $_.nameinfo.key -match "usage$" } | ?{ $_.RollupType -match "average" -And $_.Level -eq 1}).key
$avgUsageCpuCounter = ($objPerfManager.PerfCounter | ?{ $_.groupinfo.key -match "cpu" } | ?{ $_.nameinfo.key -match "usage$" } | ?{ $_.RollupType -match "average" -And $_.Level -eq 1 }).key
}
process{
# Test if function is used through a pipeline or as a regular cmdlet
if ($VIObject) {
# get all objects passed by pipeline or specified in command
foreach ($objVI in $VIObject) {
# Differents load method regarding VIobject type
switch ($objVI.gettype()) {
"VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl" { VM-Load($objVI) }
"VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl" { VMHost-Load($objVI) }
"VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl" { Cluster-Load($objVI) }
"VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.VmfsDatastoreImpl" { Datastore-Load($objVI) }
default { Write-Error -Message ("Unknown type " + $objVI.gettype() + " for object " + $objVI.name) }
}
}
} else {
switch ($LoadType) {
"VirtualMachine" { Get-VM | %{ VM-Load($_) } }
"HostSystem" { Get-VMHost | %{ VMHost-Load($_) } }
"ClustercomputeResource" { Get-Cluster | %{ Cluster-Load($_) } }
"Datastore" { Get-Datastore | %{ Datastore-Load($_) } }
default { Show-GetLoadUsage }
}
}
}
}