Home > plugins, VbScripts, Windows > check iis cpu

check iis cpu

September 5th, 2013 Leave a comment Go to comments

This nagios plugin VBS script, gets exactly the amount of CPU a specific Aplication Pool is using, it helps a lot to identify witch site is using more CPU.
It was tested on Windows 2003 only.

I had the help of a MS consultant on this one, thanks Guilherme. His blog is full of cool tips on IIS
http://blogs.technet.com/b/carnevale/

Here is the code:

'Script to check a specific IIS App Pool CPU usage
' SINTAXE 
' CheckCPUSite.vbs    
' By Felipe Ferreira and Guilherme Carnevale Jul. 2013 

' VARIAVEIS
Computername = "."
processName = "w3wp.exe"
CPUCount = 8
MaxCPU = Wscript.Arguments (1)
AppPoolName = Wscript.Arguments (0)
'


Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerName &"\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select ProcessID,CommandLine from Win32_Process Where Name = '" + ProcessName + "'")
'Set colSystemData = objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

    existe = 0
    n = 0 
    AppPoolNameAnterior = ""
For Each objProcess in colProcess '//objProcess
  
    ProcessID = objProcess.ProcessId  
    Set colPerfData = objWMIService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process  Where IDProcess = " & ProcessID)
    For Each objPerfData in colPerfData
        N1 = objPerfData.PercentProcessorTime
        D1 = objPerfData.TimeStamp_Sys100NS
    Next

    WScript.Sleep 300
    Set colPerfData = objWMIService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process  Where IDProcess = " & ProcessID)

    For Each objPerfData in colPerfData
        N2 = objPerfData.PercentProcessorTime
        D2 = objPerfData.TimeStamp_Sys100NS

    Next

    Nd = N2 - N1
    Dd = D2 - D1

    PercentProcessorTime = Round((( (Nd/Dd))  * 100)/CPUCount)

    AppPool = Split(objProcess.CommandLine,"""")
     '  if AppPoolNameAnterior <> AppPool(1)  then n = 1
    AppPoolNameAnterior = AppPool(1)


    MaxCPU = cint(MaxCPU)
  if (trim(lcase(AppPoolName)) = trim(lcase(AppPool(1))) ) then
      existe = 1
	  n = n + 1 
       If PercentProcessorTime > MaxCPU  then
          ResultadoEstado =  "CRITICAL -" 
		  Resultado = Resultado & AppPool(1)  & n & "=" & PercentProcessorTime & " "  
		  ExitEstado = "2"
		  Wscript.echo ResultadoEstado & " " & Resultado & " | " &  Resultado
		  wscript.quit "(" & ExitEstado & ")"
       else
          ResultadoEstado =  "OK -" 
		  Resultado = Resultado & AppPool(1)  & n & "=" & PercentProcessorTime & " "   
		  ExitEstado = "0"
       End If
  End If

Next 

 if existe = 1 then
   Wscript.echo ResultadoEstado & " " & Resultado & " | " &  Resultado
   wscript.quit "(" & ExitEstado & ")"
 else
   Wscript.echo "UNKNOWN - Nao existem processos w3wp para este " &  AppPoolName
   wscript.quit(3)
 End If

  1. egrimisu
    December 29th, 2014 at 10:09 | #1

    I have a problem in using the script with pools that have spaces in theyr names, can this be fixed? vb for me it’s just to much.

    Thanks

  1. No trackbacks yet.

VAMOVE *

*