Monitor CPU Usage of All Running Processes - Part I

November 2, 2005

This article illustrates how to monitor CPU usage on different running process on one machine and on multiple machines. In many organizations, the SQL Server Database administrator and System administrators have the responsibility of trouble shooting and identifying the cause of high CPU usage during certain periods in a day on a machine.

This article demonstrates how to keep track of the CPU load history on each running process that is running on a machine. This article also discusses how to find new processes that are being run and any old process that are being killed, etc.

This article uses "Win32_PerfFormattedData_PerfProc_Process" WMI class to get the information from the performance counters of a machine.

Method 1

Method 1 illustrates how to query all the running process and their CPU usage on the local machine.

Pre-requisite

a.  Machine and the login used should be capable of accessing Windows management instrumentation.

b. Operating System on that machine should be above windows 2000.

Steps

a.  Create the Folder C:\monitorprocess. [Refer Fig 1.0]


[Fig 1.0]

b.  Create the file C:\monitorprocess\ListProcess.vbs and copy and paste the below code. [Refer Fig 1.1]. Download the code from listprocess.VBS_

Click for larger image

[Fig 1.1]



'Objective: To Find the CPU usage of each running process that are running on the local machine
'Created by : MAK
'Created Date: Oct 27, 2005
strComputer ="."
Set objWMIService = GetObject("winmgmts:\\" > strComputer > "\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_PerfFormattedData_PerfProc_Process",,48)
        wscript.echo "Computer Name",> "," > "Process Name" > ","> "CPU Usage"
For Each objItem in colProcess
if objItem.Name <> "Idle"  and objItem.Name <> "_Total" then 
        wscript.echo strcomputer > "," > objItem.Name > ","> objItem.PercentProcessorTime
end if
Next

c.  Execute the listprocess.vbs code using the command below. [Refer Fig 1.2]

cscript Listprocess.vbs


[Fig 1.2]

When this VBScript is executed, it displays all the processes that are running on a current machine and their CPU usage. [Refer Fig 1.3]


[Fig 1.3]

Computer Name,Process Name,CPU Usage
.,System, 0
.,smss, 0
.,csrss, 0
.,winlogon, 0
.,services, 0
.,lsass, 0
.,svchost, 0
.,svchost, 0
.,svchost, 0
.,svchost, 0
.,spoolsv, 0
.,residentAgent, 0
.,cpqalert, 0
.,DWRCS, 0
.,pmsvc, 0
.,LocalSch, 0
.,tmcsvc, 0
.,LCRMS, 0
.,FrameworkService, 0
.,Mcshield, 0
.,VsTskMgr, 0
.,mdm, 0
.,mgabg, 0
.,tcpsvcs, 0
.,snmp, 0
.,Win32sl, 0
.,wuser32, 0
.,cpqdmi, 0
.,naPrdMgr, 0
.,SoftMon, 0
.,explorer, 0
.,chkadmin, 0
.,UpdaterUI, 0
.,shstat, 0
.,ctfmon, 0
.,svchost, 0
.,notepad, 0
.,notepad, 0
.,notepad, 0

d. The output can be stored to a CSV file by executing the command below. [Refer Fig 1.4]

C:\monitorprocess>cscript/nologo  Listprocess.vbs > LocalProcess.csv


[Fig 1.4]

When executed, it is stored in a CSV format file that can be opened and viewed using MS-EXCEL. [Refer Fig 1.5]


[Fig 1.5]








The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers