Sunday, April 27, 2014

Get peak CPU, Memory, Disk, Network stats with date and time.

This script gives you the details about peak CPU, Memor, Disk, Network Stats, plus it includes date and time of the peak usage.  

Check the section to define days back."#Define how long back you want to go and check the peak time and value", 
 #####################################    
 ## http://kunaludapi.blogspot.com    
 ## Version: 1    
 ## Tested this script on    
 ## 1) Powershell v3    
 ## 2) Powercli v5.5    
 ## 3) Vsphere 5.x    
 #####################################   
 Add-PSSnapin vmware.vimautomation.core   

 # vCenter username password   
 $vCenterServer = "vCenterserver"   
 $vcenteruser = "domain\user"   
 $vcenterpasswd = "Password"   

 #connect vCenter Server   
 Connect-VIServer -Server $vCenterServer -User $vcenteruser -Password $vcenterpasswd   
 $Result = @()  
 $metrics = "cpu.usage.average", "mem.usage.average", "disk.usage.average", "net.usage.average"  
 $vms = Get-VMHost | Get-VM | Where-Object {$_.PowerState -eq “PoweredOn”}  

 #Define how long back you want to go and check the peak time and value.
 $start = (Get-Date).AddDays(-7)  
 $finish = (Get-Date).AddDays(-6) 
 
Foreach ($vm in $vms) {  
   $AllStats = Get-Stat -Entity $vm -Stat $metrics -MaxSamples ([int]::MaxValue) -Start $start -Finish $finish -IntervalMins 5   

   #vCPU calculations   
   $CPUStats = $AllStats | Where-Object {$_.Metricid -eq "cpu.usage.average"} | Measure-Object -Property Value -Maximum | Select -ExpandProperty Maximum  
   $RoundCPUStats = [math]::Round($CPUStats)  
   $CPUStatsDate = $AllStats | Where-Object {$_.Metricid -eq "cpu.usage.average" -and $_.value -eq $CPUStats} | Select-Object -ExpandProperty Timestamp  

   #Memory calculations   
   $MemStats = $AllStats | Where-Object {$_.Metricid -eq "mem.usage.average"} | Measure-Object -Property Value -Maximum | Select -ExpandProperty Maximum  
   $RoundMemStats = [math]::Round($MemStats)  
   $MemStatsDate = $AllStats | Where-Object {$_.Metricid -eq "mem.usage.average" -and $_.value -eq $MemStats} | Select-Object -ExpandProperty Timestamp  

   #Disk calculations   
   $DiskStats = $AllStats | Where-Object {$_.Metricid -eq "disk.usage.average"} | Measure-Object -Property Value -Maximum | Select -ExpandProperty Maximum  
   $RoundDiskStats = [math]::Round($DiskStats)  
   $DiskStatsDate = $AllStats | Where-Object {$_.Metricid -eq "disk.usage.average" -and $_.value -eq $DiskStats} | Select-Object -ExpandProperty Timestamp  

   #Network calculations   
   $NetStats = $AllStats | Where-Object {$_.Metricid -eq "net.usage.average"} | Measure-Object -Property Value -Maximum | Select -ExpandProperty Maximum  
   $RoundNetStats = [math]::Round($NetStats)  
   $NetStatsDate = $AllStats | Where-Object {$_.Metricid -eq "net.usage.average" -and $_.value -eq $NetStats} | Select-Object -ExpandProperty Timestamp  

   $Obj = New-Object PSObject  
   $Obj | Add-Member -MemberType NoteProperty -Name "VMName" -Value $vm.Name  
   $Obj | Add-Member -MemberType NoteProperty -Name "Date_MaxUsage_vCPU" -Value $CPUStatsDate[0]     
   $Obj | Add-Member -MemberType NoteProperty -Name "vCPU_Usage_Max(%)" -Value $RoundCPUStats[0]  
   $Obj | Add-Member -MemberType NoteProperty -Name "Date_MaxUsage_Mem" -Value $MemStatsDate[0]  
   $Obj | Add-Member -MemberType NoteProperty -Name "Mem_Usage_Max(%)" -Value $RoundMemStats[0]  
   $Obj | Add-Member -MemberType NoteProperty -Name "Date_MaxUsage_Disk" -Value $DiskStatsDate[0]  
   $Obj | Add-Member -MemberType NoteProperty -Name "Disk_Usage_Max(KBps)" -Value $RoundDiskStats[0]  
   $Obj | Add-Member -MemberType NoteProperty -Name "Date_MaxUsage_Net" -Value $NetStatsDate[0]  
   $Obj | Add-Member -MemberType NoteProperty -Name "Net_Usage_Max(KBps)" -Value $RoundNetStats[0]  
   $result += $Obj   
 }  

 $result | Export-Csv -NoTypeInformation -Path C:\temp\Stats.csv  
Result should look like this

No comments: