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 illustrates how to query all the running process and their CPU usage on the local machine.
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.
a. Create the Folder C:\monitorprocess. [Refer 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_
‘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
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
c. Execute the listprocess.vbs code using the command below. [Refer 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]
Computer Name,Process Name,CPU Usage
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
When executed, it is stored in a CSV format file that can be opened and viewed using MS-EXCEL. [Refer Fig 1.5]