VMware Cloud Community
TFBETA
Contributor
Contributor

Get VMs CPU and Memory Daily Usage 2

Hi LucD,

You helped me few years ago with this script, it worked perfect, i would like to add something, at that time i was looking for ("cpu.usage.average","mem.active.average"), now i would like to get CPU Usage Average - CPU % ready and Memory Active and Granted average.

vSphere 5.1 Update 2

vSphere 5.5 Update 1

Thank you in advance LucD.

thread: Get VMs CPU and Memory Daily Usage

Script:

$report = @()
$metrics = "cpu.usage.average","mem.active.average"
$vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"}
$start = (get-date).AddDays(-1)

Get-Stat -Entity ($vms) -start $start -stat $metrics | `
  Group-Object -Property EntityId | %{
   
$row = ""| Select VmName, Timestamp, vCPU, MinCpu,AvgCpu,MaxCpu,MemAlloc,MinMem,AvgMem,MaxMem
   
$row.VmName = $_.Group[0].Entity.Name
   
$row.Timestamp = ($_.Group | Sort-Object -Property Timestamp)[0].Timestamp
   
$row.vCPU = $_.Group[0].Entity.NumCpu
   
$cpuStat = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Minimum -Maximum -Average
   
$row.MinCpu = "{0:f2}" -f ($cpuStat.Minimum)
   
$row.AvgCpu = "{0:f2}" -f ($cpuStat.Average)
   
$row.MaxCpu = "{0:f2}" -f ($cpuStat.Maximum)
   
$row.MemAlloc = $_.Group[0].Entity.MemoryMB
   
$memStat = $_.Group | where {$_.MetricId -eq "mem.active.average"} | Measure-Object -Property Value -Minimum -Maximum -Average
    $row.MinMem = "{0:f2}" -f ($memStat.Minimum)
   
$row.AvgMem = "{0:f2}" -f ($memStat.Average)
   
$row.MaxMem = "{0:f2}" -f ($memStat.Maximum)
   
$report += $row
}
$report | Export-Csv "C:\VM-stats.csv" -NoTypeInformation -UseCulture

$smtpServer
= "MySmtpServer"
$msg = new-object Net.Mail.MailMessage
$smtp
= new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "user@domain.com"
$msg.To.Add("user@domain.com")
$msg.Subject = "Report created"
$msg.Body = "The report has been created"
$smtp.Send($msg)

0 Kudos
7 Replies
LucD
Leadership
Leadership

The good news, you don't have to change a lot to basic structure of the script you are using.

We just need to add some counters to collect, and create new properties to display the results from these new counters.

First we need to change the line

$metrics = "cpu.usage.average","mem.active.average"

to add the new counters

$metrics = "cpu.usage.average","mem.active.average","cpu.ready.summation","mem.granted.average"


The next step is you decide which derived values you would like to have in the report (average, minimum, maximum), and in which format (for example 2 decimal places)


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

0 Kudos
TFBETA
Contributor
Contributor

Hi LucD,

Here is what i need:

VM Name | vCPUs Assigned | Average CPU Usage | % CPU Ready | Men Granted | Average Men Active |


Two Decimal Places: EX: 38.057 = 38.06


Thanks.


0 Kudos
LucD
Leadership
Leadership

Try like this

$report = @()
$metrics = "cpu.usage.average","mem.active.average","cpu.ready.summation","mem.granted.average"
$vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"}
$start = (Get-Date).AddDays(-1)

Get-Stat -Entity ($vms) -start $start -stat $metrics | `
 
Group-Object -Property EntityId | %{
   
$row = ""| Select VmName, Timestamp, vCPU, "Average CPU Usage","% CPU Ready","Mem Granted (GB)","Average Men Active (GB)"
   
$row.VmName = $_.Group[0].Entity.Name
   
$row.Timestamp = ($_.Group | Sort-Object -Property Timestamp)[0].Timestamp
   
$row.vCPU = $_.Group[0].Entity.NumCpu
   
$cpuAvgStat = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Average
   
$cpuRdyStat = $_.Group | where {$_.MetricId -eq "cpu.ready.summation"} | Measure-Object -Property Value -Average
   
$row."Average CPU Usage" = "{0:f2}" -f ($cpuAvgStat.Average)
   
$row."% CPU Ready" = "{0:f2}" -f ($cpuRdyStat.Average / $_.Group[0].IntervalSecs / 10)
   
$memActStat = $_.Group | where {$_.MetricId -eq "mem.active.average"} | Measure-Object -Property Value -Minimum -Maximum -Average
   
$memGrtStat = $_.Group | where {$_.MetricId -eq "mem.granted.average"} | Measure-Object -Property Value -Minimum -Maximum -Average
   
$row."Mem Granted (GB)" = "{0:f2}" -f ($memGrtStat.Average/1MB)
   
$row."Average Men Active (GB)" = "{0:f2}" -f ($memActStat.Average/1MB)
   
$report += $row
}
$report | Export-Csv "C:\VM-stats.csv" -NoTypeInformation -UseCulture

$smtpServer = "MySmtpServer"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "user@domain.com"
$msg.To.Add("user@domain.com")
$msg.Subject = "Report created"
$msg.Body = "The report has been created"
$smtp.Send($msg)


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

0 Kudos
TFBETA
Contributor
Contributor

Good morning LucD hope you doing ok.

Thank you for taking the time and help me, really appreciate.

Last thing, can you please modified the script to have MAX Memory Active instead of average Memory Active ?

VM Name | vCPUs Assigned | Average CPU Usage | % CPU Ready | Men Granted | MAX Men Active |


By the way, worked perfect.\ Smiley Happy


Thank you in advance.

0 Kudos
LucD
Leadership
Leadership

To capture the mem.active.maximum you will not to have Statistics Level 4 for Historical Interval 1.

Is that the case ?


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

0 Kudos
TFBETA
Contributor
Contributor

Hi LucD,

Here is how we had configured the stats in vCenter server.

Thanks.vCenter Stats.JPG

0 Kudos
LucD
Leadership
Leadership

Try like this

$report = @()
$metrics = "cpu.usage.average","mem.active.maximum","cpu.ready.summation","mem.granted.average"
$vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"}
$start = (Get-Date).AddDays(-1)

Get-Stat -Entity ($vms) -start $start -stat $metrics | `
 
Group-Object -Property EntityId | %{
   
$row = ""| Select VmName, Timestamp, vCPU, "Average CPU Usage","% CPU Ready","Mem Granted (GB)","Max Mem Active (GB)"
   
$row.VmName = $_.Group[0].Entity.Name
   
$row.Timestamp = ($_.Group | Sort-Object -Property Timestamp)[0].Timestamp
   
$row.vCPU = $_.Group[0].Entity.NumCpu
   
$cpuAvgStat = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Average
   
$cpuRdyStat = $_.Group | where {$_.MetricId -eq "cpu.ready.summation"} | Measure-Object -Property Value -Average
   
$row."Average CPU Usage" = "{0:f2}" -f ($cpuAvgStat.Average)
   
$row."% CPU Ready" = "{0:f2}" -f ($cpuRdyStat.Average / $_.Group[0].IntervalSecs / 10)
   
$memActStat = $_.Group | where {$_.MetricId -eq "mem.active.maximum"} | Measure-Object -Property Value -Maximum
   
$memGrtStat = $_.Group | where {$_.MetricId -eq "mem.granted.average"} | Measure-Object -Property Value -Average
   
$row."Mem Granted (GB)" = "{0:f2}" -f ($memGrtStat.Average/1MB)
   
$row."Max Mem Active (GB)" = "{0:f2}" -f ($memActStat.Maximum/1MB)

   
$report += $row
}
$report | Export-Csv "C:\VM-stats.csv" -NoTypeInformation -UseCulture

$smtpServer = "MySmtpServer"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "user@domain.com"
$msg.To.Add("user@domain.com")
$msg.Subject = "Report created"
$msg.Body = "The report has been created"
$smtp.Send($msg)


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

0 Kudos