Monitor-VMGuests


posted by Richard Giles
02-27-2009

Downloads: 347
File size: 2.6kB
Views: 1,672

Embed
Monitor-VMGuests
  1. # Jonathan Medd, Alan Renouf  
  2. # http://jonathanmedd.blogspot.com/2009/01/using-powershell-to-monitor-vmware.html 
  3. # check the cpu.usage.average statistic of a period of the last few minutes (watch out for the -IntervalMins parameter it can produce a period slightly different to what you would expect) and if its over 99% run the CheckHighCPU function and send the results by email. 
  4. # Requires: VI Toolkit 
  5.  
  6. Function EmailWarning () 
  7.     param ($ServerName,$Attachment
  8.     #Email warning 
  9.     Write-Output "Creating E-Mail Structure" 
  10.      
  11.     $smtpServer = "servername" 
  12.      
  13.     $msg = new-object Net.Mail.MailMessage 
  14.     $att = new-object Net.Mail.Attachment($attachment
  15.     $smtp = new-object Net.Mail.SmtpClient($smtpServer
  16.      
  17.     $msg.From = "sender" 
  18.     $msg.To.Add("recipient"
  19.     $msg.Subject = "Server Warning - High CPU on $Servername" 
  20.     $msg.Body = "$Servername has a CPU value of $HighCPU %" 
  21.     $msg.Attachments.Add($att
  22.      
  23.     Write-Output "Send E-Mail" 
  24.     $smtp.Send($msg
  25.      
  26.     $att.Dispose(); 
  27.  
  28. Function CheckHighCPU () 
  29.     param ($Target
  30.      
  31.     $procs_total = Get-WmiObject -Class Win32_PerfRawData_PerfProc_Process -Filter 'name="_total"' -ComputerName $Target 
  32.     $procs = Get-WmiObject -Class Win32_PerfRawData_PerfProc_Process -Filter 'name<>"_total"' -ComputerName $Target 
  33.      
  34.     [int64]$totalpercentuser =
  35.     foreach ($proc in $procs_total
  36.     
  37.         $totalpercentuser = $totalpercentuser + $proc.PercentUserTime
  38.          
  39.         [decimal] $perc = [System.Convert]::ToDecimal($totalpercentuser
  40.          
  41.         $myCol = @() 
  42.         foreach ($proc in $procs
  43.         
  44.             $proc_perct = (($proc.PercentUserTime / $perc) * 100) 
  45.             if ($proc_perct -gt 1){ 
  46.             $Process = Get-WmiObject win32_process -ComputerName $target | where {$_.ProcessID -eq $proc.IDProcess
  47.             $MYInfo = "" | select-Object Name, CPUUsage,Owner, ProcessID 
  48.             $MYInfo.Name = $proc.name 
  49.             $MYInfo.ProcessID = $proc.IdProcess 
  50.             $MYInfo.CPUUsage = [Math]::Round($proc_perct, 0) 
  51.             $MYInfo.Owner = $process.GetOwner().user 
  52.             $myCol += $MYInfo 
  53.         
  54.     
  55.  
  56.     $myCol | Sort-Object CPUUsage -Descending | Out-File $file 
  57.     EmailWarning $VMname $file 
  58.  
  59. Connect-VIServer servername 
  60. $vms = Get-Cluster | where {$_.name -eq "ClusterName"} | get-vm 
  61. $time = Get-Date 
  62.  
  63. do  
  64.     foreach ($vm in $vms
  65.     
  66.         $VMname = $vm.name 
  67.         $filename = $VMname + '.txt' 
  68.         $file = "C:\Scripts\$filename" 
  69.         $stats = Get-Stat -entity $vm -IntervalMins 2 -stat cpu.usage.average -MaxSamples
  70.         write-host $VMname 
  71.         $stats 
  72.          
  73.         if ($stats.value -ge 99) 
  74.         
  75.             $HighCPU = $stats.value 
  76.             Write-Host "Warning!" -ForegroundColor red 
  77.             CheckHighCPU $VMname 
  78.         
  79.         else 
  80.         
  81.         
  82.     
  83.  
  84.     Start-Sleep -Seconds 30 
  85. } until ($time.hour -ge 17) 
Filed under: , ,

Check the cpu.usage.average statistic of a period of the last few minutes (watch out for the -IntervalMins parameter it can produce a period slightly different to what you would expect) and if its over 99% run the CheckHighCPU function and send the results by email.

Attributed To: Jonathan Medd, Alan Renouf

URL: http://jonathanmedd.blogspot.com/2009/01/using-powershell-to-monitor-vmware.html

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.