Looking for a better way to log performance data than using screen captures of the Performance Monitor? Looking for a simple way to set up administrative functions based on the values of performance criteria? Microsoft has provided you with a tool for accessing the information, the Performance Data Helper (PDH), but until now, it has meant considerable C programming, access to the MS Developers Network, and a few headaches.
Now Python for Windows, the win32 version of the popular language, has added PDH, wrapped and ready to go, to its substantial toolbox. This article shows you how to create a simplistic PDH logger using this newly wrapped tool. To run the example, you will need:
Which can be obtained from: http://www.python.org/1.5
Start the Python interpreter.
import win32pdhquery, string # [1] query = win32pdhquery.Query() # [2] One user has reported a non-reproducible crash on the following line, likely due to a problem with the browser dialogue, take appropriate precautions. query.addcounterbybrowsing() #[3] query.collectdatawhile(0.25) # [4] The prompt immediately returns, we are logging the data in the background query.collectdatawhile_stop() #[5] Wait at least 0.25 seconds for thread to stopUse a valid filename below, the file will be overwritten without warning filehandle = open('c:\\temp\\testlog.log', 'w') # [6] if query.curpaths: # [7] press enter, then tab, then type next line... filehandle.write( string.join( query.curpaths,'\t')+'\n' ) # enter, enter... for dataset in query.curresults: # [8] enter,tab ... if dataset: # [9] enter, tab, tab... filehandle.write(string.join(map(str, dataset), '\t')+'\n') # enter, enter... filehandle.close() # [10] (optional) [ctrl]-z # to exit from console-mode Python, use Menu File|Exit in PythonWin
You can now open the output file in your favorite spreadsheet and analyze or annotate it as you wish. Of course, far more complex examples could be created, and both Python and Python for Windows extensive libraries make it possible to create more significant tools for administering your NT system, but that's another article. For more information, open the win32pdhquery.py module in the win32/lib subdirectory of your Python Directory. The module is heavily documented, and points you to other online sources.