[python-win32] WMI Performance API
Tim Golden
tim.golden at viacom-outdoor.co.uk
Tue Apr 11 09:10:01 CEST 2006
[Erik Dahl]
| I want to use the WMI Performance API to collect performance
| information from remote boxes.
The WMI stuff needs a certain amount of -- imho -- mildly
bizarre plumbing work. To save you the trouble, I've put
together a WMI module which I know a few people are using.
http://timgolden.me.uk/python/wmi.html
There isn't actually an example of the performance
counters on the cookbook page (have to do something
about that) but my usual advice is: find an example
from around the net -- there's loads of them -- and
then translate that example into Python. I'm happy
to help if there are any difficulties there.
Possible places to start looking:
http://www.activexperts.com/activmonitor/windowsmanagement/wmi/samples/p
erformancecounters/
http://groups.google.com/group/microsoft.public.win32.programmer.wmi?lnk
=lr
I've just found a not-too-complex example I implemented
here in the past. Entire code posted below (sorry for the
length):
<code>
import os, sys
import operator
import time
import smtpmail
import wmi
THRESHOLD = 30.0
BEST_OF = 3
SERVER = "voctx1"
try: server = sys.argv[1]
except IndexError: server = SERVER
print "Looking on", server, "for processes running over", THRESHOLD, "%
for more than", BEST_OF, "secs"
c = wmi.WMI (wmi=wmi.connect_server (server=server, user=r"username",
password="password"))
## c = wmi.WMI ()
session = smtpmail.smtp_session ()
process_info = {}
while 1:
items_shown = False
for p in c.Win32_PerfRawData_PerfProc_Process ():
id = long (p.IDProcess)
n1, d1 = long (p.PercentProcessorTime), long (p.Timestamp_Sys100NS)
n0, d0, so_far = process_info.get (id, (0, 0, []))
try:
percent_processor_time = (float (n1 - n0) / float (d1 - d0)) * 100
except ZeroDivisionError:
percent_processor_time = 0
if len (so_far) > BEST_OF:
so_far.pop ()
so_far.append (percent_processor_time)
process_info[id] = (n1, d1, so_far)
average_processor_time = reduce (operator.add, so_far) / len
(so_far)
if (len (so_far) >= BEST_OF) and (average_processor_time >
THRESHOLD):
for process in c.Win32_Process (Handle=id):
domain, return_value, user = process_owner = process.GetOwner ()
## print domain, return_value, user
alert_text = "%s running at %d on %s (%s)" % (p.Name,
average_processor_time, server, user)
smtpmail.send (
recipients=["goldent"],
subject=alert_text,
message_text = alert_text,
session=session
)
print str (id).ljust (6), p.Name.ljust (20),
average_processor_time
items_shown = True
if items_shown: print
time.sleep (1)
</code>
TJG
________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________
More information about the Python-win32
mailing list