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

Configuring and testing SNMP traps in esxi server



I am fond of and very comfortable with powercli when doing activities on vsphere environment, but this time I thought let’s try vsphere cli for as a automation tool. My monitoring team asked to configure SNMP on all esxi servers in my organization, I generally follow vmware kb http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1008065 and found I will require to use vsphere cli for the same (It is possible to configure SNMP from Powercli as well I have posted the script in the end)

What is SNMP - is a standard protocol for network monitoring and network devices management. Almost all active network devices support SNMP. Moreover, SNMP is supported by the majority of operational systems and by many network applications.

I downloaded and installed vsphere cli from http://my.vmware.com


Once you open the command prompt for vsphere cli, change the directory to bin, if you dir you will find Perl scripts.

Below command shows current SNMP configuration on the esxi server

Vicfg-snmp.pl --server 192.168.33.21 –username root –password Computer@1 --show

This will set snmp server settings on the esxi server

Vicfg-snmp.pl --server 192.168.33.21 –username root –password Computer@1 –t 192.168.33.40@161/vcloud -c vcloud -E
 
In the screenshot I already have mentioned what the parameters are

Final show command to view the configured settings.




How do you quick test SNMP whether setting or working or not?
Here I have downloaded snmpwalk tool from http://www.snmpsoft.com/freetools/snmpwalk.html and extracted to one of windows server in the network


Open command prompt on the server and run the utility with parameters. Good help is available about the tool.




snmpWalk.exe –r:192.168.33.21 –c:vcloud

-r:192.168.33.21 = esxi server where settings are configured
-c:vcloud = Community string.

If everything is fine it will get response from the esxi server.
(In the case you are using linux run command snmpwalk -v2c -c rageesx 192.168.33.21)




On the esxi server you can verify connection success 




Configuring SNMP on esxi host with powercli
Run get-vmhostsnmp to fetch information



And below screenshot command will set the configuration.


If you have lots of esxi server and want to automate it (esxi server with same password), you can use below script to automate on all esxi servers.

Add-PSSnapin vmware.vimautomation.core
$root = "root"
$password = "Password"
$Listofesxi = "esxi001.vcloud.lab", "esxi002.vcloud.lab", "esxi003.vcloud.lab"

foreach ($esxi in $Listofesxi) {
    Connect-VIServer $esxi -User $root -Password $password
    Set-VMHostSnmp (Get-VMHostSnmp) -ReadOnlyCommunity vcloud -Enabled $true
    Disconnect-VIServer * -Confirm $false
}