Welcome, guest | Sign In | My Account | Store | Cart

most people want to gather data about programs etc. most people will use perfmon. what happens if you want to gather data but have to sit on the machine and set up perfmon counters and such. This is the basis for people to write system information gathering programs/counters.

Python, 29 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import win32pdhutil, win32pdh,wmi,win32api,win32process
c = wmi.WMI ()#<-- this takes a ling time to set up. but after this initialiseation it is very quick

#to get a process's attributes you have to name them
a=str(win32pdhutil.FindPerformanceAttributesByName("explorer", counter="Virtual Bytes"))

print "Explorer virtual Bytes: ",a #will give you virtual bytes

#to get system you do it slightly differently
z=str(win32pdhutil.GetPerformanceAttributes("Memory", "Page Faults/sec"))

print "memory Page Faults",z # will give you memory page faults a second

#Now here is the fun bit to get the process's cpu-usage is very difficult
# it is not simple like i thought this
cpu=str(win32pdhutil.FindPerformanceAttributesByName("explorer", counter="% Processor Time"))
print "Explorer CPU Time is: ",cpu

#after searching high and low on the internet i finally found how to do it using WMI (so windows only)
process_info = {}
for p in c.Win32_PerfRawData_PerfProc_Process (name='explorer'):
  n1, d1 = long (p.PercentProcessorTime), long(p.Timestamp_Sys100NS)
  n0, d0 = process_info.get (id, (0, 0))
    
  try:
    percent_processor_time = (float (n1 - n0) / float (d1 - d0)) *100.0
  except ZeroDivisionError:
    percent_processor_time = 0.0
  print "Explorer CPU Time actually is: ", percent_processor_time

This is just an example of how to do it. It is then a simple amount now using the data... hope this helps people trying to find this information..