akioichiban23
Contributor
Contributor

historical Avg CPU,MEM and diskusage

Jump to solution
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
.
.
.
 
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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

0 Kudos
6 Replies
LucD
Leadership
Leadership

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

0 Kudos
akioichiban23
Contributor
Contributor

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 ?

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
akioichiban23
Contributor
Contributor

thank you very much

it works perfect

0 Kudos
akioichiban23
Contributor
Contributor

Hi LucD

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos