KS-Soft. Network Management Solutions
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister    ProfileProfile    Log inLog in 

Monitoring CPU Usage (%) for a given process.

Post new topic   Reply to topic    KS-Soft Forum Index -> Library
View previous topic :: View next topic  
Author Message

Joined: 14 Dec 2005
Posts: 177

PostPosted: Thu Jan 31, 2008 11:43 am    Post subject: Monitoring CPU Usage (%) for a given process. Reply with quote

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/mframe.htm#tests.htm#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%
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>" )
   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   

   PercentProcessorTime = Round((N1 / D1) * 100)
   if PercentProcessorTime > Round(maxLimit) then
      WScript.StdOut.Write( statusBad & PercentProcessorTime & " %" )
      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.

Back to top
View user's profile Send private message
KS-Soft Europe

Joined: 16 May 2006
Posts: 2832

PostPosted: Fri Feb 01, 2008 6:07 am    Post subject: Reply with quote

Well done! Script works like a charm! Great job!

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

Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    KS-Soft Forum Index -> Library All times are GMT - 6 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group

KS-Soft Forum Index