2012-04-13 12 views
2

Ich habe versucht, den Prozentsatz der CPU-Auslastung für eine einfache Desktop-Anwendung zu erhalten, die ich erstelle.HTA-Überwachung CPU-Auslastung in Prozent mit WMI - nicht genau

Ich habe ein paar VBS-Skripte in eine HTA zusammengeschmuggelt, die zu funktionieren scheint, aber es ist nicht sehr genau, oder zumindest denke ich nicht, wenn ich es neben Task-Manager laufen lasse -

ich bin mit „Total“ von Win32_PerfRawData_PerfOS_Processor

können Sie einen Blick darauf werfen Sie und sehen, ob es etwas gibt, ich dies besser machen tun kann?

Vielen Dank im Voraus

<html> 
<head> 

<script type='text/javascript'> 
window.resizeTo(240,150); 
</SCRIPT> 

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
<meta name="description" content="Created by David Brennan, adapted from a couple of scripts - 1. To Get CPU Percentage - http://msdn.microsoft.com/en-us/library/windows/desktop/aa392397(v=vs.85).aspx and 2. To run the script over and over again - http://www.windowsitpro.com/article/vbscript/utility-monitors-crucial-processes"> 

<title>CPU + RAM usage</title> 

<HTA:APPLICATION 
    APPLICATIONNAME="CPU Usage Percentage" 
    ID="cpuUsagePercentage" 
    VERSION="0.1" 
    BORDER="dialog" 
    SCROLL="no" 
    CONTEXTMENU="no" 
    SINGLEINSTANCE="no" 
    WINDOWSTATE="normal"/> 

<style type="text/css"> 
h3{color:purple;} 
</style> 



</head> 
<body> 

<div id='DataArea'> 
waiting for info 
</div> 

<script language="VBScript"> 
'************************************************************************************************ 
Sub Window_OnLoad 
    Dim refrtime 
    refrTime = 1000 'Refresh the screen every 1000 millisecs (one seconds) 
    GetPercentage(refrtime) 

    TimeoutID = window.setInterval("GetPercentage(" & refrtime &")",refrTime) 
End sub 

'************************************************************************************************ 
Sub GetPercentage(refrtime) 
    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 

    Set perf_instance2 = objService.Get(_ 
     "Win32_PerfRawData_PerfOS_Processor.Name='_Total'") 
    N2 = perf_instance2.PercentProcessorTime 
    D2 = perf_instance2.TimeStamp_Sys100NS 
    ' Look up the CounterType qualifier for the PercentProcessorTime 
    ' and obtain the formula to calculate the meaningful data. 
    ' CounterType - PERF_100NSEC_TIMER_INV 
    ' Formula - (1- ((N2 - N1)/(D2 - D1))) x 100 

    PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100 
    CPUpercentage = Round(PercentProcessorTime,2) 
    DataArea.InnerHTML = "<h3>CPU Usage = " & CPUpercentage & "%" 
End sub 

'************************************************************************************************ 

</script> 

</body> 
</html> 

Antwort

1

i überarbeitete Ihr Skript eine Pause zwischen den beiden Messungen zu haben, seine jetzt einer Stufe mit dem Ergebnis von meinem Taskmanager.

<html> 
<head> 
<script language="vbscript"> 
    set objservice = getobject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2") 
    const refrtime=1000 
</script> 
<script type='text/javascript'> 
    window.resizeTo(240,150); 
</script> 
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
<meta name="description" content="Created by David Brennan, adapted from a couple of scripts - 1. To Get CPU Percentage - http://msdn.microsoft.com/en-us/library/windows/desktop/aa392397(v=vs.85).aspx and 2. To run the script over and over again - http://www.windowsitpro.com/article/vbscript/utility-monitors-crucial-processes"> 
<title>CPU + RAM usage</title> 

<HTA:APPLICATION 
    APPLICATIONNAME="CPU Usage Percentage" 
    ID="cpuUsagePercentage" 
    VERSION="0.1" 
    BORDER="dialog" 
    SCROLL="no" 
    CONTEXTMENU="no" 
    SINGLEINSTANCE="no" 
    WINDOWSTATE="normal"/> 

<style type="text/css"> 
    h3{color:purple;} 
</style> 
</head> 
<body> 
    <div id='DataArea'>waiting for info</div> 
    <script language="vbscript"> 
    '--- ---' 
    sub window_onload 
    call getpercentage1() 
    end sub 
    '--- ---' 
    sub getpercentage1() 
    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 
    timeoutid = window.settimeout("call getpercentage2(" & n1 &","& d1 & ")", refrtime/2) 
    end sub 
    '--- ---' 
    sub getpercentage2(n1, d1) 
    set objservice = getobject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2") 
    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 
    cpupercentage = round(percentprocessortime,2) 
    dataarea.innerhtml = "<h3>cpu usage = " & cpupercentage & "%" 
    timeoutid = window.settimeout("getpercentage1()", refrtime) 
    end sub 
    '--- ---' 
    </script> 
</body> 
</html> 
+0

Vielen Dank. Dies scheint viel genauer zu sein! – TheDavil