Monitoring CPU Usage (%) for a given process.

If you have information, script, utility, or idea that can be useful for HostMonitor community, you welcome to share information in this forum.
Post Reply
Yoorix
Posts: 177
Joined: Wed Dec 14, 2005 8:28 am

Monitoring CPU Usage (%) for a given process.

Post by Yoorix »

Hello community. :-)
I am glad to introduce you my solution to monitor CPU usage (%) for certain process using HostMonitor. The VB script below uses WMI and is designed for "Shell Script" test method: http://www.ks-soft.net/hostmon.eng/mfra ... m#chkShell
Of course it is possible to use "Performance Counter" test method to do the same, but I think (and KS-Soft suggests ;-) ) that WMI technology is much more reliable.

There are three parameters required for the script: <Computername>, <ProcessName>, <MaxLimit>
E.g.: MySuperServer winamp 50
If script will check local host, you may specify dot (".") instead of computer's name, e.g.: . winamp 50

Start cmd: cmd /c cscript /B /E:VBScript %Script% %Params%
Script:

Code: Select all

Option Explicit

const statusAlive       = "scriptRes:Host is alive:"
const statusDead        = "scriptRes:No answer:"
const statusUnknown     = "scriptRes:Unknown:"
const statusNotResolved = "scriptRes:Unknown host:"
const statusOk          = "scriptRes:Ok:"
const statusBad         = "scriptRes:Bad:"
const statusBadContents = "scriptRes:Bad contents:"

dim objArgs, strComputer, strProcess, maxLimit, objWMIService, processProp, process, N1, D1,PercentProcessorTime, i
Set objArgs = WScript.Arguments

if objArgs.Count <> 3 then
   WScript.StdOut.Write( statusUnknown & "Parameters are required: <Computername>, <ProcessName>, <MaxLimit>" )
else
   strComputer = objArgs(0)
   strProcess = objArgs(1)
   maxLimit = objArgs(2)
   'WScript.StdOut.Write( strComputer & " " & strProcess & " " &  maxLimit)
  
   Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
   N1 = 0
   D1 = 0

   For i = 1 To 2
   Set processProp = objWMIService.ExecQuery("select IdProcess, Name, PercentProcessorTime,TimeStamp_Sys100NS from Win32_PerfRawData_PerfProc_Process WHERE Name = '" & strProcess & "'") 
   For Each process in processProp 
      N1 = process.PercentProcessorTime - N1
      D1 = process.TimeStamp_Sys100NS - D1   
   Next 
   wscript.sleep(1000)    
   next

   PercentProcessorTime = Round((N1 / D1) * 100)
   if PercentProcessorTime > Round(maxLimit) then
      WScript.StdOut.Write( statusBad & PercentProcessorTime & " %" )
   else
      WScript.StdOut.Write( statusOk & PercentProcessorTime & " %" )   
   end if       
 
End If
Please note: If there are several processes, with the same name, running on the target system, I do not recommend to use this script because it will show the wrong result. However, you are free to use and modify this script on your own. ;-)

------------------
Yoorix
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Well done! Script works like a charm! Great job! :-)

Thank you for your contribution.
We really appreciate your help.

Regards,
Max
Post Reply