Rajeev_S
Expert
Expert

get-stat monthly report

Jump to solution

Hi,

I'm trying to script the stats on a monthly basis. I'm using the below script,

Get-Stat -Entity (Get-Cluster -Name x) -start (get-date).AddDays(-30) -Finish (Get-Date) -MaxSamples 1000 -stat cpu.usagemhz.average | Export-Csv -Path c:\test.csv

It outputs based on time with 30 min interval.

Is it possible to get it as a single value for a month as it shows in VC ??

Also can we get the Max value reached in the cluster for the particular month.

Hope my question is clear!!!!!

Thanks

0 Kudos
33 Replies
RvdNieuwendijk
Leadership
Leadership

I found a way to get the beginning of the month in a language independent way:

$Now = Get-Date
$thisMonth = $Now.AddDays(1-$Now.Day).AddHours(-$Now.Hour).AddMinutes(-$Now.Minute).AddSeconds(-$Now.Second).AddMilliseconds(-$Now.Millisecond)

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership

As always, there are multiple ways to do something in PS.

You could also just do

$thisMonth = Get-Date -Day 1 -Hour 0 -Minute 0 -Second 0

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
RvdNieuwendijk
Leadership
Leadership

Luc,

your solution is nicer. And thanks for the link to Keith Hill's article. Very interesting.

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
greendxr
Contributor
Contributor

Luc,

How would I go about getting this information on a per datastore basis?

I'm looking to get disk utilization info for the last month. Need to know which VMs are busiest on each datastore. This would be either to run against all datastores of an esx host / cluster or would be even better if there was an option to put in a specific datastore as an argument. I hope this makes sense.

Thanks

0 Kudos
LucD
Leadership
Leadership

If I understood your question correctly, you want to see the top-5 virtual disk users for a specific datastore ?

PS: perhaps it would be better if you created a new thread for your question.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
greendxr
Contributor
Contributor

Ok new thread created.

0 Kudos
NinJy
Contributor
Contributor

Hello,

I have to retrieve stats of VMs for last the 30 days on a vCenter.

I used this code:


Connect-VIServer -Server -Protocol https -User -Password

$allvms = @()
$vms = Get-Vm

foreach($vm in $vms){
  $vmstat = "" | Select VmName, MemMax, MemAvg, CPUMax, CPUAvg, DiskMax, DiskAvg
  $vmstat.VmName = $vm.name
 
  $statcpu = Get-Stat -Entity ($vm) `
           -start (get-date).AddDays(-30) `
           -Finish (Get-Date) `
             -stat cpu.usagemhz.average
  $statmem = Get-Stat -Entity ($vm) `
           -Start (get-date).AddDays(-30) `
           -Finish (Get-Date) `
             -stat mem.consumed.average
  $statdisk = Get-Stat -Entity ($vm) `
           -Start (get-date).AddDays(-30) `
           -Finish (Get-Date) `
             -stat disk.usage.average


  $cpu = $statcpu | Measure-Object -Property value -Average -Maximum
  $mem = $statmem | Measure-Object -Property value -Average -Maximum
  $disk = $statdisk | Measure-Object -Property value -Average -Maximum

 
  $vmstat.CPUMax = $cpu.Maximum
  $vmstat.CPUAvg = $cpu.Average
  $vmstat.MemMax = $mem.Maximum
  $vmstat.MemAvg = $mem.Average
  $vmstat.DiskMax = $disk.Maximum
  $vmstat.DiskAvg = $disk.Average
 
  $allvms += $vmstat
}

$allvms | Select VmName, MemMax, MemAvg, CPUMax, CPUAvg, DiskMax, DiskAvg | Export-Csv "c:\VMs.csv" -noTypeInformation -UseCulture

It seems working, (although I have some difficulty to understand how to use the MaxSample and if I must use it).

But I also have to retrieve the host name, provisioned and used space on HDDs, Guest OS name and CPU count.. and I have some difficulties, I am a beginner in PowerShell scripting, C# and VMware SDK (I'm a student Smiley Happy ).

Can you help me ?

Thanks.

0 Kudos
RvdNieuwendijk
Leadership
Leadership

I have added the request features host name, provisioned and used space on HDDs, Guest OS name and CPU count to your script. You don't need the select cmdlet on the last line, because you selected all the properties and that is the default, so I removed that select cmdlet.

You can use the -MaxSamples parameter of the Get-Stat cmdlet to specify the maximum number of samples for each statistic. E.g. if you only want the latest value, you can use -MaxSamples 1.

Connect-VIServer -Server vCenter -Protocol https -User -Password 

$allvms = @()
$vms = Get-Vm

foreach($vm in $vms){
  $vmstat = "" | Select VmName, GuestName, NumCPU, ProvisionedSpaceGB, UsedSpaceGB, VmHost, MemMax, MemAvg, CPUMax, CPUAvg, DiskMax, DiskAvg 
  $vmstat.VmName = $vm.name
  $vmstat.GuestName = $vm.Guest.HostName 
  $vmstat.NumCPU = $vm.NumCPU
  $vmstat.ProvisionedSpaceGB = $vm.ProvisionedSpaceGB
  $vmstat.UsedSpaceGB = $vm.UsedSpaceGB
  $vmstat.VmHost = $vm.VMHost
  
  $statcpu = Get-Stat -Entity ($vm) `
           -start (get-date).AddDays(-30) `
           -Finish (Get-Date) `
             -stat cpu.usagemhz.average
  $statmem = Get-Stat -Entity ($vm) `
           -Start (get-date).AddDays(-30) `
           -Finish (Get-Date) `
             -stat mem.consumed.average
  $statdisk = Get-Stat -Entity ($vm) `
           -Start (get-date).AddDays(-30) `
           -Finish (Get-Date) `
             -stat disk.usage.average


  $cpu = $statcpu | Measure-Object -Property value -Average -Maximum
  $mem = $statmem | Measure-Object -Property value -Average -Maximum
  $disk = $statdisk | Measure-Object -Property value -Average -Maximum

  
  $vmstat.CPUMax = $cpu.Maximum
  $vmstat.CPUAvg = $cpu.Average
  $vmstat.MemMax = $mem.Maximum
  $vmstat.MemAvg = $mem.Average
  $vmstat.DiskMax = $disk.Maximum
  $vmstat.DiskAvg = $disk.Average
  
  $allvms += $vmstat
}

$allvms | Export-Csv "c:\VMs.csv" -noTypeInformation -UseCulture

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
NinJy
Contributor
Contributor

Thank you very much!

It seems to be not to difficult finaly. Smiley Happy

It work like a charm, thanks again!

I have an another question, I have to generate  graphics with max/avg stats for each VMs on each 30 days. Is it possible  to retrieve samples for each days ?

0 Kudos
RvdNieuwendijk
Leadership
Leadership

The next script gives you average and maximum performance statistics per virtual machine for the last 30 days for each day:

Function Get-AverageStatPerDay {
  param($vm,$NrOfdays,$Stat)
  
  $Today = Get-Date
  Get-Stat -Entity ($vm) `
           -start ($Today).AddDays(-$NrOfDays) `
           -Finish ($Today) `
           -stat $Stat | `
    ForEach-Object {
      $MetricId = $_.MetricId
      $Unit = $_.Unit
      $_ | `
        Select-Object -Property Description,Entity,EntityId,Instance,IntervalSecs,MetricId,Timestamp,Unit,Value,@{N="Date";E={$_.TimeStamp.Date.ToShortDateString()}}
    } | `
      Group-Object -Property Date | `
      Select-Object -Property @{N="VM";E={$vm.Name}},
        @{N="MetricId";E={$MetricId}},
        @{N="Date";E={$_.Name}},
        @{N="Value";E={($_.Group | Measure-Object -Property Value -Average).Average}},
        @{N="Unit";E={$Unit}}
}     

Function Get-MaximumStatPerDay {
  param($vm,$NrOfdays,$Stat)
  
  $Today = Get-Date
  Get-Stat -Entity ($vm) `
           -start ($Today).AddDays(-$NrOfDays) `
           -Finish ($Today) `
           -stat $Stat | `
    ForEach-Object {
      $MetricId = $_.MetricId
      $Unit = $_.Unit
      $_ | `
        Select-Object -Property Description,Entity,EntityId,Instance,IntervalSecs,MetricId,Timestamp,Unit,Value,@{N="Date";E={$_.TimeStamp.Date.ToShortDateString()}}
    } | `
      Group-Object -Property Date | `
      Select-Object -Property @{N="VM";E={$vm.Name}},
        @{N="MetricId";E={$MetricId}},
        @{N="Date";E={$_.Name}},
        @{N="Value";E={($_.Group | Measure-Object -Property Value -Maximum).Maximum}},
        @{N="Unit";E={$Unit}}
}     

Get-VM | ForEach-Object {
  $vm = $_
  Get-AverageStatPerDay -vm $vm -NrOfDays 30 -Stat cpu.usagemhz.average
  Get-AverageStatPerDay -vm $vm -NrOfDays 30 -Stat mem.consumed.average
  Get-AverageStatPerDay -vm $vm -NrOfDays 30 -Stat disk.usage.average
  Get-MaximumStatPerDay -vm $vm -NrOfDays 30 -Stat cpu.usagemhz.maximum
  Get-MaximumStatPerDay -vm $vm -NrOfDays 30 -Stat mem.consumed.maximum
  Get-MaximumStatPerDay -vm $vm -NrOfDays 30 -Stat disk.usage.maximum
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
NinJy
Contributor
Contributor

Thanks a lot.

0 Kudos
NinJy
Contributor
Contributor

Hello,

I have some trouble to export the output into a csv.

When it's "work", it only write this in the file:

"PowerState","Version","Description","Notes","Guest","NumCpu","MemoryMB","HardDisks","NetworkAdapters","UsbDevices","CDDrives","FloppyDrives","Host","HostId","VMHostId","VMHost","VApp","FolderId","Folder","ResourcePoolId","ResourcePool","PersistentId","UsedSpaceGB","ProvisionedSpaceGB","DatastoreIdList","HARestartPriority","HAIsolationResponse","DrsAutomationLevel","VMSwapfilePolicy","VMResourceConfiguration","CustomFields","ExtensionData","Id","Name","Uid"
"PoweredOff","v7","","","Windows_XP_SP3_x86:","1","512","VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.HardDisk[]","VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.NetworkAdapter[]","VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.UsbDevice[]","VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.CDDrive[]","VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.FloppyDrive[]","esx4.na.cetsi.corp","HostSystem-host-9","HostSystem-host-9","esx4.na.corp",,"Folder-group-v34","Template","ResourcePool-resgroup-8","Resources","5014693c-280c-1f71-5a9a-1a55b486ae49","1,443538","20,50018","System.String[]","ClusterRestartPriority","AsSpecifiedByCluster","AsSpecifiedByCluster","Inherit","CpuShares:Normal/1000 MemShares:Normal/5120","VMware.VimAutomation.ViCore.Impl.V1.Util.ReadOnlyDictionary`2[System.String,System.String]","VMware.Vim.VirtualMachine","VirtualMachine-vm-32","Windows_XP_SP3_x86","/VIServer=administrateur@vcenter01:443/VirtualMachine=VirtualMachine-vm-32/"

Or I get an error like this one:

Select-Object : Impossible de convertir System.Management.Automation.PSObject e
n l'un des types suivants {System.String, System.Management.Automation.ScriptBl
ock}.
Au niveau de ligne : 10 Caractère : 9
+   Select <<<<  $allvms | Export-Csv "c:\VMsGraph.csv" -noTypeInformation -Use
Culture
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupport
   edException
    + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Co
   mmands.SelectObjectCommand

I tried to use several commands like:

$allvms = @()
Get-VM | ForEach-Object {
  $vm = $_
  Get-AverageStatPerDay -vm $vm -NrOfDays 30 -Stat cpu.usagemhz.average
  Get-AverageStatPerDay -vm $vm -NrOfDays 30 -Stat mem.consumed.average
  Get-AverageStatPerDay -vm $vm -NrOfDays 30 -Stat disk.usage.average
  Get-MaximumStatPerDay -vm $vm -NrOfDays 30 -Stat cpu.usagemhz.maximum
  Get-MaximumStatPerDay -vm $vm -NrOfDays 30 -Stat mem.consumed.maximum
  Get-MaximumStatPerDay -vm $vm -NrOfDays 30 -Stat disk.usage.maximum
 
    $allvms += $vm

  Select $allvms | Export-Csv "c:\VMsGraph.csv" -noTypeInformation -UseCulture
}

or

Select $vm | Export-Csv "c:\VMsGraph.csv" -noTypeInformation -UseCulture

Select $allvms | Export-clixml c:\test.xml | import-clixml c:\test.xml | export-csv c:\test.csv

I don't understand why it don't write all the stats shown in the powershell window.

0 Kudos
RvdNieuwendijk
Leadership
Leadership

To export the output to a .csv file, you can pipe the output of the script in my previous post to a Export-Csv cmdlet. Like:

Get-VM | ForEach-Object {
  $vm = $_
  Get-AverageStatPerDay -vm $vm -NrOfDays 30 -Stat cpu.usagemhz.average
  Get-AverageStatPerDay -vm $vm -NrOfDays 30 -Stat mem.consumed.average
  Get-AverageStatPerDay -vm $vm -NrOfDays 30 -Stat disk.usage.average
  Get-MaximumStatPerDay -vm $vm -NrOfDays 30 -Stat cpu.usagemhz.maximum
  Get-MaximumStatPerDay -vm $vm -NrOfDays 30 -Stat mem.consumed.maximum
  Get-MaximumStatPerDay -vm $vm -NrOfDays 30 -Stat disk.usage.maximum
} | Export-Csv "C:\VMsGraph.csv" -noTypeInformation -UseCulture


Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
NinJy
Contributor
Contributor

Thanks it works great Smiley Happy

I have edited the script to retrieve the stats of a cluster if someone need it.

Function Get-AverageStatPerDay {
  param($clu,$NrOfdays,$Stat)
 
  $Today = Get-Date
  Get-Stat -Entity ($clu) `
           -start ($Today).AddDays(-$NrOfDays) `
           -Finish ($Today) `
           -stat $Stat | `
    ForEach-Object {
      $MetricId = $_.MetricId
      $Unit = $_.Unit
      $_ | `
        Select-Object -Property Description,Entity,EntityId,Instance,IntervalSecs,MetricId,Timestamp,Unit,Value,@{N="Date";E={$_.TimeStamp.Date.ToShortDateString()}}
    } | `
      Group-Object -Property Date | `
      Select-Object -Property @{N="Cluster";E={$clu.Name}},
        @{N="MetricId";E={$MetricId}},
        @{N="Date";E={$_.Name}},
        @{N="Value";E={($_.Group | Measure-Object -Property Value -Average).Average}},
        @{N="Unit";E={$Unit}}
}    

Get-Cluster | ForEach-Object {
  $clu = $_
  Get-AverageStatPerDay -clu $clu -NrOfDays 30 -stat cpu.usagemhz.average
  Get-AverageStatPerDay -clu $clu -NrOfDays 30 -stat mem.consumed.average
} | Export-Csv "C:\ClusterGraph.csv" -noTypeInformation -UseCulture

I have an another question, sorry.. , is it possible to get the multipathing status, the used space, the snapshot space and the number of disks for a list of VM in only one script ?

Thank you.

0 Kudos