VMware Cloud Community
akioichiban23
Contributor
Contributor
Jump to solution

historical Avg CPU,MEM and diskusage

Hello,
I was wondering if I can get a historical data (for the past week with interval 120min) to export in CSV with CPUaverage, Memoryaverage, and Diskusage with a date for specific Vmhost
 
export to something like this:
hostname,date,cpuavg,memavg,diskusage
a,yyyymmdd_hh:mm,80,80,80
a,yyyymmdd_hh:mm,80,80,80
a,yyyymmdd_hh:mm,80,80,80
a,yyyymmdd_hh:mm,80,80,80
.
.
.
 
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The ForEach doesn't place anything on the pipeline.

You can do something like this

$esxName = 'MyEsx'

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

$stat = 'cpu.usage.average','mem.usage.average','disk.usage.average'

$esx = Get-VMHost -Name $esxName

$report = Get-Stat -Entity $esx -Start $start -Stat $stat -IntervalMins 120 |

Group-Object -Property Timestamp | %{

    New-Object PSObject -Property @{

        HostName = $esxName

        Date = $_.Name

        CpuAvg = $_.Group | where{$_.MetricId -eq 'cpu.usage.average'} | select -ExpandProperty Value

        MemAvg = $_.Group | where{$_.MetricId -eq 'mem.usage.average'} | select -ExpandProperty Value

        DiskUsage = $_.Group | where{$_.MetricId -eq 'disk.usage.average'} | select -ExpandProperty Value

    }

}

$report | Select HostName,Date,CpuAvg,MemAvg,DiskUsage |

Export-Csv -Path C:\Temp\report.csv -NoTypeInformation -UseCulture


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$esxName = 'MyEsx'

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

$stat = 'cpu.usage.average','mem.usage.average','disk.usage.average'

$esx = Get-VMHost -Name $esxName

Get-Stat -Entity $esx -Start $start -Stat $stat -IntervalMins 120 |

Group-Object -Property Timestamp | %{

    [ordered]@{

        HostName = $esxName

        Date = $_.Name

        CpuAvg = $_.Group | where{$_.MetricId -eq 'cpu.usage.average'} | select -ExpandProperty Value

        MemAvg = $_.Group | where{$_.MetricId -eq 'mem.usage.average'} | select -ExpandProperty Value

        DiskUsage = $_.Group | where{$_.MetricId -eq 'disk.usage.average'} | select -ExpandProperty Value

    }

}


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

Reply
0 Kudos
akioichiban23
Contributor
Contributor
Jump to solution

Hi LucD,

Thank you for your help!!

The script worked fine but when I tried to export it to csv with Export-Csv by adding it to the end, 

the file gives me something completely different .

Is it not supposed to work by adding Export-Csv at the end ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The ForEach doesn't place anything on the pipeline.

You can do something like this

$esxName = 'MyEsx'

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

$stat = 'cpu.usage.average','mem.usage.average','disk.usage.average'

$esx = Get-VMHost -Name $esxName

$report = Get-Stat -Entity $esx -Start $start -Stat $stat -IntervalMins 120 |

Group-Object -Property Timestamp | %{

    New-Object PSObject -Property @{

        HostName = $esxName

        Date = $_.Name

        CpuAvg = $_.Group | where{$_.MetricId -eq 'cpu.usage.average'} | select -ExpandProperty Value

        MemAvg = $_.Group | where{$_.MetricId -eq 'mem.usage.average'} | select -ExpandProperty Value

        DiskUsage = $_.Group | where{$_.MetricId -eq 'disk.usage.average'} | select -ExpandProperty Value

    }

}

$report | Select HostName,Date,CpuAvg,MemAvg,DiskUsage |

Export-Csv -Path C:\Temp\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
akioichiban23
Contributor
Contributor
Jump to solution

thank you very much

it works perfect

Reply
0 Kudos
akioichiban23
Contributor
Contributor
Jump to solution

Hi LucD

I wanted to ask if I could add command or line to get memory ballooning average in this script

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, try like this

$esxName = 'MyEsx'

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

$stat = 'cpu.usage.average','mem.usage.average','disk.usage.average','mem.vmmemctl.average'

$esx = Get-VMHost -Name $esxName

$report = Get-Stat -Entity $esx -Start $start -Stat $stat -IntervalMins 120 |

Group-Object -Property Timestamp | %{

    New-Object PSObject -Property @{

        HostName = $esxName

        Date = $_.Name

        CpuAvg = $_.Group | where{$_.MetricId -eq 'cpu.usage.average'} | select -ExpandProperty Value

        MemAvg = $_.Group | where{$_.MetricId -eq 'mem.usage.average'} | select -ExpandProperty Value

        DiskUsage = $_.Group | where{$_.MetricId -eq 'disk.usage.average'} | select -ExpandProperty Value

        BallooningKB = $_.Group | where{$_.MetricId -eq 'mem.vmmemctl.average'} | select -ExpandProperty Value

    }

}

$report | Select HostName,Date,CpuAvg,MemAvg,DiskUsage |

Export-Csv -Path C:\Temp\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos