VMware Cloud Community
hakhak
Contributor
Contributor
Jump to solution

Need a script for 2 minutes interval for last 12 hour and 24 hours

Can i know how to get a script that works with interval 12 hours and 24 hours? I am able to get 2 minutes interval for 1 hours, but it seems different to get in 12 and 24 hours. can anyone help me?

Exp 2 minutes interval 1 hour:

$report =@()
$vms = Get-VM
foreach($vm in $vms){
$gg = Get-Stat -Entity ($vm) -Stat cpu.usagemhz.average  -Realtime -Start (Get-Date).AddHours(-1)
  $gg| Group-Object -Property EntityId,{[math]::Floor(($_.Timestamp.Hour * 60 + $_.Timestamp.Minute)/2)} |%{
         $group = $_
        $row = ""| Select VM,Timestamp,CpuAvg2
        $row.VM = $_.Group[0].Entity.Name
        $row.Timestamp = (@($_.Group | Sort-Object -Property Timestamp))[0].Timestamp
        $row.CpuAvg2 = [math]::Round((($_.Group | Measure-Object -Property Value -Average).Average/1000),2)
        $report = $report + $row                 
}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The reason you can't get 2-minute intervals for a time range longer than +/- 1 hour is due to the aggregation jobs that run on the vCenter.

Have a look at my PowerCLI & vSphere statistics – Part 1 – The basics post, where I try to explain all this.

In the Historical Interval 1 the intervals are aggregated to 5-minute intervals.

If you want to have samples on 2-minute intervals, you will have to do the calculation in the script itself.

But I wonder what this will add to the report.

You have 2 options if you absolutely need 2-minute intervals

1) in the vCenter go to <Administration><vCenter Server Settings><Statistics> and change the interval for Historical Interval 1 from 5 to 2 minutes.

Be aware that this will increase the size of your vCenter database.

2) Collect the Realtime statistics (20 seconds interval) every hour and store in an external file.

Then run your report against this external file.


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The reason you can't get 2-minute intervals for a time range longer than +/- 1 hour is due to the aggregation jobs that run on the vCenter.

Have a look at my PowerCLI & vSphere statistics – Part 1 – The basics post, where I try to explain all this.

In the Historical Interval 1 the intervals are aggregated to 5-minute intervals.

If you want to have samples on 2-minute intervals, you will have to do the calculation in the script itself.

But I wonder what this will add to the report.

You have 2 options if you absolutely need 2-minute intervals

1) in the vCenter go to <Administration><vCenter Server Settings><Statistics> and change the interval for Historical Interval 1 from 5 to 2 minutes.

Be aware that this will increase the size of your vCenter database.

2) Collect the Realtime statistics (20 seconds interval) every hour and store in an external file.

Then run your report against this external file.


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

0 Kudos
Damukles
Contributor
Contributor
Jump to solution

If you want to collect realtime data for more than one hour, you may  use the following code. It saves the data in a csv file. You may then e.g. import the CSV to Excel and use pivot charts to get a nive view of your  realtime data.

# ----- Settings section ----- #
# vCenter to connect to (make sure script runs in user context with at least read-only permissions)
$vcenterfqdn = "myvcenter.domain.local"
# List VMs to be monitored as Strings delimited by a semicolon
$vmlist = @("SRV1";"SRV2")
# Time in minutes that the script should collect data
$timetorunmin = 240
# Time to wait in seconds between query intervals
$waitsec = 3000
# Folder for output with a backslash at the end
$outfolder = "C:\Script\"

# ----- Script section ----- #
# Initialize
$StartTime = Get-Date
$EndTime = $StartTime.AddMinutes($timetorunmin)

# Create Master table
$master = @()
Foreach ($vmstr in $vmlist) {
    $tmp = "" | Select VMName, Stats
    $tmp.VMName = $vmstr
    $tmp.Stats = @()
    $master += $tmp
}

# Loop until "time to run" is exceeded
Do {
    # Get Stats
    $VIServer = Connect-VIServer $vcenterfqdn
    Foreach ($target in $master) {
        $vm = Get-VM $target.VMName
        $stats = Get-Stat -Entity $vm -Stat virtualdisk.totalreadlatency.average -Realtime | Select Timestamp, Value, Unit, Instance | Sort TimeStamp

        # Compare time stamps with stats in master to avoid duplicate entries
        Foreach ($entry in $stats) {
            #if ($target.Stats -notcontains $entry) { $target.Stats += $entry } #compares objects, not values
            $addentry = $true
            if ($target.Stats.Count -gt 0) {
                Foreach ($tmp in $target.Stats) {
                    If (($tmp.Timestamp.Tostring() -eq $entry.Timestamp.ToString()) -and ($tmp.Instance.ToString() -eq $entry.Instance.ToString())) {
                        $addentry = $false
                    }
                }
            }
            if ($addentry) { $target.Stats += $entry }
        }
    }
    $VIServer | Disconnect-VIServer -Confirm:$false
    Start-Sleep -Seconds $waitsec

} Until ((Get-Date) -ge $EndTime)

# Save to file and quit
Foreach ($target in $master) {
    $outfile = $outfolder + $target.VMName + "-" + $StartTime.ToShortDateString() + ".csv"
    $target.Stats | Sort TimeStamp | Export-Csv $outfile -Delimiter ";"
}


Have Fun

0 Kudos