다음은, WMI 를 통해서 CPU 사용량을 모니터링하는 스크립트 이다.
루프를 돌면서, 6초 간격으로 체크하며, 90% 이상일경우 초과 메시지를 출력한다.
지원OS는, Windows Server 2003과 XP 에서만 체크가 가능하다.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.Swbemrefresher")
Set objProcessor = objRefresher.AddEnum _
(objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet
intThresholdViolations = 0
objRefresher.Refresh
Do
For Each intProcessorUse in objProcessor
If intProcessorUse.PercentProcessorTime > 90 Then
intThresholdViolations = intThresholdViolations + 1
If intThresholdViolations = 10 Then
intThresholdViolations = 0
Wscript.Echo "Processor usage threshold exceeded."
End If
Else
intThresholdViolations = 0
End If
Next
Wscript.Sleep 6000
objRefresher.Refresh
Loop
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.Swbemrefresher")
Set objProcessor = objRefresher.AddEnum _
(objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet
intThresholdViolations = 0
objRefresher.Refresh
Do
For Each intProcessorUse in objProcessor
If intProcessorUse.PercentProcessorTime > 90 Then
intThresholdViolations = intThresholdViolations + 1
If intThresholdViolations = 10 Then
intThresholdViolations = 0
Wscript.Echo "Processor usage threshold exceeded."
End If
Else
intThresholdViolations = 0
End If
Next
Wscript.Sleep 6000
objRefresher.Refresh
Loop
만약, Windows 2000 이라면, 성능카운터의 원시데이터를 이용해야 한다.
Set objService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")
Set objInstance1 = objService.Get("Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
N1 = objInstance1.PercentProcessorTime
D1 = objInstance1.TimeStamp_Sys100NS
WScript.Sleep(1000)
Set perf_instance2 = objService.get("Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
N2 = perf_instance2.PercentProcessorTime
D2 = perf_instance2.TimeStamp_Sys100NS
PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100
WScript.Echo "% Processor Time=" , Round(PercentProcessorTime,2)
Set objInstance1 = objService.Get("Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
N1 = objInstance1.PercentProcessorTime
D1 = objInstance1.TimeStamp_Sys100NS
WScript.Sleep(1000)
Set perf_instance2 = objService.get("Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
N2 = perf_instance2.PercentProcessorTime
D2 = perf_instance2.TimeStamp_Sys100NS
PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100
WScript.Echo "% Processor Time=" , Round(PercentProcessorTime,2)
댓글 없음:
댓글 쓰기