Monitor CPU Usage of All Running Processes - Part IINovember 16, 2005 Part I of this article illustrated how to monitor CPU usage of running processes on a local machine or from a remote machine. This article illustrates how to monitor the CPU usage of different running processes on different machines and collect the information in a database. This article uses the "Win32_PerfFormattedData_PerfProc_Process" WMI class to get the information from the performance counters of a machine. Pre-requisitea. Machine and the login used should be capable of accessing Windows management instrumentation. b. Operating System on that machine should be above windows 2000. Stepsa. Create C:\monitorprocess\ Servers.txt as shown below. [Refer Fig 1.0]. IMLNODE1 ATDBQA IMLNODE2 JC022247 Note: Please replace the list of machine names available on your network. Click for larger image b. Create C:\monitorprocess\ Listremoteprocess2.vbs as shown below. [Refer Fig 1.1]. Please download Listremoteprocess2.vbs_. 'Objective: To Find the CPU usage of each process that are running on a remote machine
'Created by : MAK
'Created Date: Nov 2, 2005
'Syntax: cscript Listremoteprocess2.vbs machinename
'Example: cscript Listremoteprocess2.vbs MyMachine
Set objArgs = WScript.Arguments
strComputer =objArgs(0)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcesses =
Click for larger image c. Create the file C:\monitorprocess\ Listaprocessremote.bat as shown below. [Refer fig 1.2]. Download Listaprocessremote.bat_. REM Objective: To execute the Listremoteprocess2.vbsfor every server listed in servers.txt REM Created by: MAK REM Created by" Nov 2, 2005 REM Usage: Listaprocessremote.bat Allservers.csv dir %1 if %errorlevel% == 0 goto process goto delfile :delfile del %1 goto process :process for /f "tokens=1 delims=&" %%i in (c:\Monitorprocess\Servers.txt)
d. Execute the batch file as shown below. [Refer Fig 1.3]. Listaprocessremote.bat myserverprocess.csv Or c:\monitorprocess\Listaprocessremote.bat c:\monitorprocess\myserverprocess.csv
e. When this batch file is executed, it deletes the existing csv file, finds all of the processes that are running on a remote machine and their CPU usage and stores it in myserverprocess.csv file with the following message.
f. The .csv file can be viewed using Excel or notepad as shown below. [Refer Fig 1.5]. ATDBQA, sysdown, 0 ATDBQA, ClusSvc, 0 ATDBQA, cpqnimgt, 0 ATDBQA, cqmgserv, 0 ATDBQA, cqmgstor, 0 ATDBQA, cqmghost, 0 ATDBQA, cpqwmgmt, 0 ATDBQA, ResrcMon, 0 ATDBQA, naPrdMgr, 0 ATDBQA, wmiprvse, 0 ATDBQA, svchost, 0 ATDBQA, msdtc, 0 ATDBQA, sqlservr, 0 ATDBQA, sqlagent, 0 ATDBQA, DWRCS, 0 ATDBQA, locator, 0 ATDBQA, server, 0 ATDBQA, logon.scr, 0
g. Let us create a database, Table and login as shown below. create database MonitorProcesses go use MonitorProcesses go Create table Processes ( id int identity (1,1) not null, ServerName varchar(128), ProcessName varchar(256), CPU_Usage int not null, Time datetime constraint currentdate default getdate()) go Create view Processes_view as select ServerName,ProcessName, CPU_Usage from Processes go use master go sp_addlogin 'procuser','B07250BE','MonitorProcesses' go use MonitorProcesses go sp_adduser 'procuser' go sp_addrolemember 'db_datareader','procuser' go sp_addrolemember 'db_datawriter','procuser' go h. Let us import the myserverprocess.csv file using the following BULK INSERT command. use MonitorProcesses
go
BULK INSERT MonitorProcesses.dbo.Processes_view
FROM 'c:\Monitorprocess\myserverprocess.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
i. This import process can be created as a scheduled job that imports this file daily. [Refer Fig 1.6, 1.7].
j. The table Process collects information about all of the processes and CPU usage from all of the servers. The table can be queried as shown below. select * from Processes select * from Processes where processname like '%SQLServr' Select * from Processes where servername ='ATDBQA' The result would look similar to that shown below.
ConclusionThis article has illustrated how to monitor the CPU usage of different running processes on different machines, how to collect the information in a database table and automate the import process by scheduling. |