<img height="1" width="1" style="display:none;" alt="" src="https://dc.ads.linkedin.com/collect/?pid=1005900&amp;fmt=gif">

Insights

Python's subprocess module to extract counters from Perfmon logs

I recently found myself needing to extract data from Perfmon's blg files based on time.  Python is often my go-to language for automation, and I had heard previously about the subprocess module, but had yet to experiment with it, and thought this might be the perfect introduction to it.

Python's subprocess module is very complex and allows one, as the official documentation states (http://docs.python.org/2/library/subprocess.html), "to spawn new processes, connect to their input/output/error pipes, and obtain their return codes".  I'm using it in possibly its simplest form, to call other commands.

It is possible to use the module to call features and programs built into operating systems, and then, if required, do something with that output.  For example, you could use it to call ipconfig on Windows, to obtain IP configuration data, or use it to call 'ls' on a Unix-like OS to obtain directory listings.

For my purposes, I'm using subprocess to call the inbuilt Windows command-line application relog.

I'll now run through my script that performs this function.

Firstly, we need to import the modules required for this script:

===
import subprocess
import os
import sys

===

Now we need to obtain the name of the file we wish to work on.  This grabs the name of the file from the commandline input:

===
 blg_name = sys.argv[1]
===

Next, we set up a variable and dictionary to store the date and times:

===
 date = '19/04/2013'
times = {
'01' : ['12:30:00','12:30:15'],
'02' : ['12:36:00','12:36:30'],
'03' : ['12:43:00','12:44:00'],
'04' : ['12:52:00','12:54:00'],
'05' : ['13:03:00','13:06:00'],
'06' : ['13:16:00','13:20:45'],
'07' : ['13:32:00','14:00:15'],
'08' : ['14:50:00','15:27:00'],
'09' : ['16:09:00','16:24:15'],
'10' : ['17:30:00','18:10:45'],
'11' : ['18:54:00','19:35:00'],
'12' : ['20:21:00','21:14:00'],
}

===

A dictionary is used to store the stage of the particular section from the Perfmon log I wish to extract, and the start and end times in a list as the value for each key.  As you will soon see, the Python is really just here to control the logic which is calling relog and to piece a string together that will form the command we wish to run.

Finally, the part that does all the work:

===
 for num, time in times.iteritems():
command = 'relog ' + blg_name + ' -f csv -o ' + blg_name[:-4] + '_' + num + '.csv -b ' + date + ' ' + time[0] + ' -e ' + date + ' ' +  time[1]
subprocess.call(command.split(), shell=True)

===

This loop iterates over our 'times' dictionary, and, using some string manipulation, allows us to form the command as we need it, to then be able to pass it simply to relog using the subprocess' 'call' function.

  • There are no suggestions because the search field is empty.