vSphere Environment Analysis

vSphere Infrastructure analysis is important, so I wrote this script to help me to easily get a quick understanding of a given environment.  This grew out of the CPU Ready script that I wrote several months ago, in addition to CPU Ready detection, it can check for Memory problems as well as report the maximum CPU Usage and Active Memory for every VM in an environment.

Usage: vm-analyze.ps1 -Stat <metric to examine> -VM <VM to examine> -Days <# of days to examine> -Threshold <Threshold % as an integer above which to report CPU Ready>

The script accepts the following for the Stat option:

  • cpuReady
  • memProblem
  • cpuMax
  • memMax
The -VM option is optional - if it is included (as the name of a VM), the script will perform its work on that specific VM, otherwise it will analyze all VMs in the inventory.  The -Days option specifies how far back to analyze; it defaults to 6 days if not specified.  This can be important to play around with, as some of the examined metrics are not retained by vCenter for long durations (memMax uses Active Memory, which is a level 2 statistic).

When using cpuReady, the script will use all of the options, reporting any VM that has CPU Ready (as a percentage) above the specified Threshold during the specified interval.

When using memProblem, the script only uses the -VM and -Days options.  It examines the VM(s) and checks for any times that have memory ballooning.  It then outputs the size of the balloon, as well as the swap in and swap out rates at that time.  Ballooning by itself isn't necessarily a problem, but if the system is doing a lot of swapping, that's definitely a problem.

cpuMax and memMax are two sides of the same coin; each checks the VM(s) and reports the highest recorded value for either CPU or Active Memory.  If the script is reporting 0, that means that the metric isn't available for that object.  That can be because either that metric has not been retained or because the VM was not powered on during the interval (thus causing that metric to be blank for that object).
Once again, this is posted for educational purposes only and is a "use at your own risk" sort of thing.  I've used this a little bit and it worked for me, but that's no guarantee that it will work in your environment.  Also, beware of unintended line breaks, since this blog is significantly narrower than my notepad++ window.


#By Jason Coleman - http://virtuallyjason.blogger.com
#This script assists with VMware ESXi analysis.  It may be used to pull vital metrics from Virtual Machines and is designed to be redirected to an output .csv file.
#
#Usage:
#CPU Ready Analysis: reports on all VMs that have had CPU Ready amounts above the specified Threshold (defaults to 5%)
#VM-Analyze.ps1 -Stat cpuReady -VM <Virtual Machine Name> -Days <# of days to attempt to read> -Threshold <% as an integer above which to report>
#
#Memory Maximum Analysis: reports the maximum Active memory of each VM during the specified period.  This reads Memory Active, which is a level 2 statistic and so may not be available.
#VM-Analyze.ps1 -Stat memMax -VM <Virtual Machine Name> -Days <# of days to attempt to read>
#
#CPU Maximum Analysis: reports the maximum used CPU of each VM during the specified period
#VM-Analyze.ps1 -Stat cpuMax -VM <Virtual Machine Name> -Days <# of days to attempt to read>
#
#Memory Balloon/Swap Detection: reports each instance of VM Balloon and Swap, as well as the values for each metric.
#VM-Analyze.ps1 -Stat memProblem


[CmdletBinding()]
param (
[alias("v")]
[string]$VM = "",
[alias("d")]
[int]$Days = 6,
[alias("t")]
[int]$Threshold = 5,
[alias("s")]
[string]$Stat = $(read-host -Prompt "Enter the Stat that you wish to analyze (cpuReady | cpuMax | memMax | memProblem):")
)

if ($VM -eq ""){
$VMs = Get-VM
}
else{
$VMs = Get-VM -name $VM
}

if ($Stat -eq "cpuReady"){
$metric = "cpu.ready.summation"
}
elseif ($Stat -eq "cpuMax"){
$metric = "cpu.usagemhz.average"
$Stat = "computeMax"
}
elseif ($Stat -eq "memMax"){
$metric = "mem.active.average"
$Stat = "computeMax"
}
elseif ($Stat -eq "computeMax"){
$metric = "cpu.usagemhz.average"
write-output "Reporting CPU Mhz Usage"
}
else{
}

$start = (Get-Date).AddDays(-$Days)

# Discovers any VMs that have CPU Ready values above the specified threshold.
if ($Stat -eq "cpuReady"){
write-output "VM Name, Date of Entry, Value"
foreach ($ThisVM in $VMs){
if ($ThisVM.PowerState -eq "PoweredOn"){
foreach ($Report in $(Get-Stat -Entity $ThisVM -Stat $metric -Start $start -Erroraction "silentlycontinue")){
$ReadyPercentage = (($Report.Value/10)/$Report.IntervalSecs)
if ($ReadyPercentage -gt $Threshold){
$PerReadable = "$ReadyPercentage".substring(0,4)
write-output "$($Report.Entity), $($Report.Timestamp), $PerReadable%"
}
}
}
}
}

# Compute Max reporting, reports either max CPU Mhz or RAM KB depending on the specified metric
elseif ($Stat -eq "computeMax"){
write-output "VM Name, Date of Entry, Value"
foreach ($ThisVM in $VMs){
$MaxActive = 0
$DateOfInterest = 0
foreach ($Report in $(Get-Stat -Entity $ThisVM -Stat $metric -Start $start -Erroraction "silentlycontinue")){
if ($Report.Value -gt $MaxActive){
$MaxActive = $Report.Value
$DateOfInterest = $Report.Timestamp
}
}
write-output "$ThisVM, $DateOfInterest, $MaxActive"
}
}
# Memory swap/balloon detection; largely untested.
elseif ($Stat -eq "memProblem"){
write-output "VM Name, Date of Entry, Value"
foreach ($ThisVM in $VMs){
foreach ($Report in $(Get-Stat -Entity $ThisVM -Stat mem.vmmemctl.average -Start $start -Erroraction "silentlycontinue")){
if ($Report.value -gt 0){
write-output "$($Report.Entity), $($Report.Timestamp), $($Report.Value) KB Balloon"
#write swap rates at that time.  Figure out how to target the specific timeframe.
$SwapIn = get-stat -entity $ThisVM -stat "mem.swapinrate.average" -start $report.Timestamp -finish $Report.Timestamp -erroraction "silentlycontinue"
write-output "$($Report.Entity), $($Report.Timestamp), $($SwapIn.Value) KBps Swap In"
$SwapOut = get-stat -entity $ThisVM -stat "mem.swapoutrate.average" -start $report.Timestamp -finish $Report.Timestamp -erroraction "silentlycontinue"
write-output "$($Report.Entity), $($Report.Timestamp), $($SwapIn.Value) KBps Swap Out"
}
}
}
}
else{
write-output "please use a supported Stat"
}

Comments

Popular posts from this blog

Orphaned VMDK Files

Deleting Orphaned (AKA Zombie) VMDK Files

Clone a Standard vSwitch from one ESXi Host to Another