Monitor CPU Usage of All Running Processes - Part I

Wednesday Nov 2nd 2005 by Muthusamy Anantha Kumar aka The MAK
Share:

MAK demonstrates how to keep track of the CPU load history on each running process that is running on SQL Server.

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]

Method 2

Method 2 illustrates how to query all the running process and their CPU usage on a remote machine.

Pre-requisite

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

b.  Operating System on the local machine and remote machine should be above windows 2000.

Steps

a.  Create the file C:\monitorprocess\ Listremoteprocess.vbs and copy and paste the code below into it. [Refer Fig 1.6]. Download Listremoteprocess.vbs_

'Objective: To Find the CPU usage of each process 
'that are running on a remote machine
'Created by : MAK
'Created Date: Oct 27, 2005
'Syntax: cscript Listremoteprocess.vbs machinename
'Example: cscript Listremoteprocess.vbs MyMachine
Set objArgs = WScript.Arguments
strComputer =objArgs(0)
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




[Fig 1.6]

b.  Execute the VBScript as shown below. [Refer Fig 1.7]

cscript listremoteprocess.vbs imlnode1


[Fig 1.7]

When this VBScript is executed, it displays all of the processes that are running on a remote machine and their CPU usage.

Computer Name,Process Name,CPU Usage
imlnode1, System, 0
imlnode1, smss, 0
imlnode1, csrss, 0
imlnode1, winlogon, 0
imlnode1, services, 0
imlnode1, lsass, 0
imlnode1, svchost, 0
imlnode1, svchost, 0
imlnode1, svchost, 0
imlnode1, svchost, 0
imlnode1, svchost, 0
imlnode1, spoolsv, 0
imlnode1, cimntfy, 0
imlnode1, vcagent, 0
imlnode1, DWRCS, 0
imlnode1, ECCService_MasterAgent, 0
imlnode1, EmcPowSrv, 0
imlnode1, mstragent, 0
imlnode1, svchost, 0
imlnode1, LocalSch, 0
imlnode1, PDS, 0
imlnode1, QIPCLNT, 0
imlnode1, tmcsvc, 0
imlnode1, FrameworkService, 0
imlnode1, mstragent, 0
imlnode1, Mcshield, 0
imlnode1, VsTskMgr, 0
imlnode1, nsrexecd, 0
imlnode1, nsrpm, 0
imlnode1, portmap, 0
imlnode1, qlremote, 0
imlnode1, svchost, 0
imlnode1, snmp, 0
imlnode1, smhstart, 0
imlnode1, CpqRcmc, 0
imlnode1, cqmgserv, 0
imlnode1, cqmgstor, 0
imlnode1, mssearch, 0
imlnode1, naPrdMgr, 0
imlnode1, hpsmhd, 0
imlnode1, sysdown, 0
imlnode1, clussvc, 0
imlnode1, cpqnimgt, 0
imlnode1, wmiprvse, 0
imlnode1, cqmghost, 0
imlnode1, mnragent, 0
imlnode1, resrcmon, 0
imlnode1, hpsmhd, 0
imlnode1, svchost, 0
imlnode1, wmiprvse, 60
imlnode1, svchost, 0
imlnode1, msdtc, 0
imlnode1, sqlservr, 0
imlnode1, sqlagent, 37
imlnode1, logon.scr, 0

Here you can see that the SQL Agent process uses more CPU when running resource intensive schedule jobs in SQL Server. [Refer Fig 1.8]


[Fig 1.8]

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

cscript/nologo  listremoteprocess.vbs imlnode1 > remoteprocesscpu.csv


[Fig 1.9]

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


[Fig 2.0]

Conclusion

This article illustrated how to monitor CPU usage of running process on either a local machine or from a remote machine. Part II of this article will discuss how to monitor CPU usage of different running process on different machines and collect the information in a database.

» See All Articles by Columnist MAK

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved