VMware Cloud Community
langleyj
Contributor
Contributor
Jump to solution

Update this script?

Hi-

I have this script (various pieces found on this forum) that gives me some stats for daily and weekly activity on my VM. How would I be able to update this to a mini-script to also give me the below details in intervals of 120 min with full detail. So for example this pulls one number for avg daily cpu and avg daily memory...this is great, but how would I also get the numbers every 120 minutes that make up this average sample?So what I am looking for is essentially an extension of below to include the numbers that make up those averages...it makes it easier to graph...

Also, is there any sampling of DiskIO that is possible to get like Mem and CPU? I had no idea how to get that.

function VM-statavg ($vmImpl, $StatStart, $StatFinish, $statId) {

$stats = $vmImpl | get-stat -stat $statId -intervalmin 120 -maxsamples 360 `

-Start $StatStart -Finish $StatFinish

$statAvg = "{0,9:#.00}" -f ($stats | Measure-Object value -average).average

$statAvg

}

$DaysBack = 1 # Number of days to go back

$DaysPeriod = 1 # Number of days in the interval

$DayStart = (Get-Date).Date.adddays(- $DaysBack)

$DayFinish = (Get-Date).Date.adddays(- $DaysBack + $DaysPeriod).addminutes(-1)

$DaysBack = 7 # Number of days to go back

$DaysPeriod = 7 # Number of days in the interval

$WeekStart = (Get-Date).Date.adddays(- $DaysBack)

$WeekFinish = (Get-Date).Date.adddays(- $DaysBack + $DaysPeriod).addminutes(-1)

$Report = @()

get-vm | Sort Name -Descending | % {

$vm = Get-View $_.ID

$vms = "" | Select-Object VMName,DayAvgCpuUsage, WeekAvgCpuUsage, TotalCPU, TotalMemory, MemoryUsage, DayAvgMemUsage, WeekAvgMemUsage

$vms.VMName = $vm.Name

$vms.DayAvgCpuUsage = VM-statavg $_ $DayStart $DayFinish "cpu.usage.average"

$vms.WeekAvgCpuUsage = VM-statavg $_ $WeekStart $WeekFinish "cpu.usage.average"

$vms.TotalCPU = $vm.summary.config.numcpu

$vms.TotalMemory = $vm.summary.config.memorysizemb

$vms.MemoryUsage = $vm.summary.quickStats.guestMemoryUsage

$vms.DayAvgMemUsage = VM-statavg $_ $DayStart $DayFinish "mem.usage.average"

$vms.WeekAvgMemUsage = VM-statavg $_ $WeekStart $WeekFinish "mem.usage.average"

}

$Report |

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

For the first part of your question, most of the information that you want is already available.

Since you wanted an average over the day (or week) the script piped the statistical data for each interval to the Measure-Object cmdlet.

So when we just leave it out that cmdlet you will get all the statistical data not just the average.

In the following script I did just that:

  • create a new function, called VM-statavgArray, that leaves out the Measure-Object cmdlet and returns the array with all the statistical data

  • added some functionality to write all the statistical data to an individual CSV file for each guest

From there it should be easy to put these values in a graph.

The script only shows the average CPU usage for 1 day.

The data for a week and for the memory statistics can be done in a similar way.

function VM-statavgArray ($vmImpl, $StatStart, $StatFinish, $statId) { 
        $vmImpl | get-stat -stat $statId -intervalmin 120 -maxsamples 360 ` 
        -Start $StatStart -Finish $StatFinish 
} 
#Report for previous day 
$DaysBack = 1 # Number of days to go back 
$DaysPeriod = 1 # Number of days in the interval 
$DayStart = (Get-Date).Date.adddays(- $DaysBack) 
$DayFinish = (Get-Date).Date.adddays(- $DaysBack + $DaysPeriod).addminutes(-1) 

$Report = @() 
get-vm | Sort Name -Descending | % { 
        $vm = Get-View $_.ID 
        $DayCpuUsage = VM-statavgArray $_ $DayStart $DayFinish "cpu.usage.average" 
        if($DayCpuUsage -ne $null){ 
                $report = @() 
                foreach($line in $DayCpuUsage){ 
                        $row = "" | Select Timestamp, Value 
                        $row.Timestamp = $line.Timestamp 
                        $row.Value = $line.Value 
                        $report += $row 
                } 
                $Report | Export-Csv ("C:\" + $vm.Name + "-DayCpuUsage.csv") -noTypeInformation 
        } 
} 

For the second part of your question.

You can get the disk usage statistics with the disk.usage.average metric.

Note that this will return, in KBps, the sum of the data read and written to all of disk instances of the virtual machine.

You can use the similar script logic as in the previous script for this metric.

If you want to see disk IO statistics for individual disks you need to use the disk.read.average and disk.write.average metrics.

But note that you will have to filter on the Instance property to select the disk you want.

Also note that the instances are the disks connected to the ESX server not the virtual disks connected to the virtual machine.

You will need to add the disk.read.average and disk.write.average values together to get the total IO to the individual disk.

function VM-statavgArray ($vmImpl, $StatStart, $StatFinish, $statId) { 
        $vmImpl | get-stat -stat $statId -intervalmin 120 -maxsamples 360 ` 
        -Start $StatStart -Finish $StatFinish 
} 
#Report for previous day 
$DaysBack = 1 # Number of days to go back 
$DaysPeriod = 1 # Number of days in the interval 
$DayStart = (Get-Date).Date.adddays(- $DaysBack) 
$DayFinish = (Get-Date).Date.adddays(- $DaysBack + $DaysPeriod).addminutes(-1) 

$Report = @() 
get-vm | Sort Name -Descending | % { 
        $vm = Get-View $_.ID 
        $DayDiskRead = VM-statavgArray $_ $DayStart $DayFinish "disk.read.average" 
        if($DayDiskRead -ne $null){ 
                $report = @() 
                foreach($line in $DayDiskRead){ 
                        $row = "" | Select Timestamp, Instance, Value 
                        $row.Timestamp = $line.Timestamp 
                        $row.Value = $line.Value
                        $row.Instance = $line.Instance
                        $report += $row 
                } 
                $Report | Export-Csv ("C:\" + $vm.Name + "-DayDiskReadAvg.csv") -noTypeInformation 
        } 
} 

Note that you will need to set the historical interval that corresponds with 120 minutes at least to level 3 to collect these device statistics.


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

For the first part of your question, most of the information that you want is already available.

Since you wanted an average over the day (or week) the script piped the statistical data for each interval to the Measure-Object cmdlet.

So when we just leave it out that cmdlet you will get all the statistical data not just the average.

In the following script I did just that:

  • create a new function, called VM-statavgArray, that leaves out the Measure-Object cmdlet and returns the array with all the statistical data

  • added some functionality to write all the statistical data to an individual CSV file for each guest

From there it should be easy to put these values in a graph.

The script only shows the average CPU usage for 1 day.

The data for a week and for the memory statistics can be done in a similar way.

function VM-statavgArray ($vmImpl, $StatStart, $StatFinish, $statId) { 
        $vmImpl | get-stat -stat $statId -intervalmin 120 -maxsamples 360 ` 
        -Start $StatStart -Finish $StatFinish 
} 
#Report for previous day 
$DaysBack = 1 # Number of days to go back 
$DaysPeriod = 1 # Number of days in the interval 
$DayStart = (Get-Date).Date.adddays(- $DaysBack) 
$DayFinish = (Get-Date).Date.adddays(- $DaysBack + $DaysPeriod).addminutes(-1) 

$Report = @() 
get-vm | Sort Name -Descending | % { 
        $vm = Get-View $_.ID 
        $DayCpuUsage = VM-statavgArray $_ $DayStart $DayFinish "cpu.usage.average" 
        if($DayCpuUsage -ne $null){ 
                $report = @() 
                foreach($line in $DayCpuUsage){ 
                        $row = "" | Select Timestamp, Value 
                        $row.Timestamp = $line.Timestamp 
                        $row.Value = $line.Value 
                        $report += $row 
                } 
                $Report | Export-Csv ("C:\" + $vm.Name + "-DayCpuUsage.csv") -noTypeInformation 
        } 
} 

For the second part of your question.

You can get the disk usage statistics with the disk.usage.average metric.

Note that this will return, in KBps, the sum of the data read and written to all of disk instances of the virtual machine.

You can use the similar script logic as in the previous script for this metric.

If you want to see disk IO statistics for individual disks you need to use the disk.read.average and disk.write.average metrics.

But note that you will have to filter on the Instance property to select the disk you want.

Also note that the instances are the disks connected to the ESX server not the virtual disks connected to the virtual machine.

You will need to add the disk.read.average and disk.write.average values together to get the total IO to the individual disk.

function VM-statavgArray ($vmImpl, $StatStart, $StatFinish, $statId) { 
        $vmImpl | get-stat -stat $statId -intervalmin 120 -maxsamples 360 ` 
        -Start $StatStart -Finish $StatFinish 
} 
#Report for previous day 
$DaysBack = 1 # Number of days to go back 
$DaysPeriod = 1 # Number of days in the interval 
$DayStart = (Get-Date).Date.adddays(- $DaysBack) 
$DayFinish = (Get-Date).Date.adddays(- $DaysBack + $DaysPeriod).addminutes(-1) 

$Report = @() 
get-vm | Sort Name -Descending | % { 
        $vm = Get-View $_.ID 
        $DayDiskRead = VM-statavgArray $_ $DayStart $DayFinish "disk.read.average" 
        if($DayDiskRead -ne $null){ 
                $report = @() 
                foreach($line in $DayDiskRead){ 
                        $row = "" | Select Timestamp, Instance, Value 
                        $row.Timestamp = $line.Timestamp 
                        $row.Value = $line.Value
                        $row.Instance = $line.Instance
                        $report += $row 
                } 
                $Report | Export-Csv ("C:\" + $vm.Name + "-DayDiskReadAvg.csv") -noTypeInformation 
        } 
} 

Note that you will need to set the historical interval that corresponds with 120 minutes at least to level 3 to collect these device statistics.


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

Reply
0 Kudos
langleyj
Contributor
Contributor
Jump to solution

Thanks LucD that answered it and it works like a charm. For the second part of the question I appreciate the detailed explanation. I should be ok with just using disk.usage.average to get the data but your explanation on individual disks is interesting! I wonder if net.usage.average and disk.usage.average together would help formulate some form of any latency numbers? What do you think?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid I'll have to give a bit of a "yes and no" answer to this.

The disk and net averages can surely help you in pinpointing possible latencies.

But you would have to determine some valid watermarks for your environment before using these.

And I think you will have to combine this with cpu.wait.summation data.

If there are no serious increases (deltas) in this number I don't think there can be any suspicion of potential latency.

A tool like esxtop for example, is better suited to determine possible latency.

Have a look at

And there are a number of free and commercial tools available for this kind of analysis.

Perhaps launch the question in the Performance community.


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

Reply
0 Kudos
langleyj
Contributor
Contributor
Jump to solution

Yes, I knew esxtop would do that...was just hoping it can be done through code somehow and it seems it "sort of" can be Smiley Happy Good to know..thanks!

Reply
0 Kudos