Ease of Perfmon with Logman (and PowerShell)

                It is no mystery that, as data professionals, we are oftentimes put in sticky situations where a lot of quick and effective troubleshooting needs to happen in order to get to the bottom of a specific problem.  One of the most prominent tools for that is Performance Monitor (Perfmon):  A built-in performance counter capturing utility.  Perfmon is extremely effective, yet the setup time can be a bit of an obstacle, as you need to select your counters, configure data collector parameters… so on and so forth.
                But Microsoft has given us yet another tool to make this a bit easier and cut down our time through the use of the command-line utility Logman.  This tool allows us to setup and manage performance counters and collector sets.  But let’s take this a step further:  Let’s automate to utilization of Logman so that we can run a quick PowerShell script to quickly stand up a data collector set to get the information we are looking for.
                Before I jump right into the PowerShell script, it’s worth noting a few of the Logman optional parameters.  Like most all-encompassing tools, a lot of these parameters and options I will not be breaching here in this post, but feel free to play with them and leverage their full capability.  Logman can create, start, stop, delete, query, and update performance counter collections.  The significant parameters (at least for our needs right now) are as follows:
-v
nnnn | mmddhhmm
Output versioning
-c
“Counter 1” “Counter2”
Counter list
-cf
“C:\CounterDir\Counters.txt”
Counter list file
-o
“C:\OutputDir\OutputFile”
The path of the output file
-f
bin | bincirc | csv | tsv | sql
Output file format
-si
[[hh:]mm:]ss
Sample interval
*Note: these are only a few of many options.  Please refer to TechNet for the full listing.
                The above parameters are relatively straightforward, but I want to concentrate on the –cf switch.  This is going to be a file that contains a list of counters (one per line) to load into the new data collector set.  This is more significant than it might seem on the surface, as we can have a little library of text files that contain counters for each frequently used set of performance monitoring metrics.  In other words, you can have a text file called MemoryPressureCounters.txtthat’ll include a list of all the counters you’d typically use to troubleshoot memory pressure.
                Taking this idea of quick Perfmon collector config, what I’ve done is created a PowerShell script to make it even more streamlined.  There are some options that’ll probably remain relatively constant:  Maybe the sample interval is typically a desired five seconds in order to capture enough information, but not poll the counters too often in a mid-range log.  Also, I may want to always store my log files in a central location with a particular naming convention and that’ll typically never change.  So instead of always having to type all of this relatively static information in, I want to make this process as quickly as possible.
param
(
      [string]$mach,
      [string]$inf,
      [string]$logloc,
      [string]$colname
)
# <DEFAULTS>      [alter to environment specs]
$sampleInterval = 5
$defaultLogLocation = “C:\DefaultDir\Default_log”
# </DEFAULTS>
# check to see if input parameters were filled
if ($inf -eq “”) {
      Write-Host “ERROR!!!  -inf must be set to counter file”
      exit 1
}
# if the machine name wasn’t supplied, use local
if ($mach -eq “”) {
      $mach = “localhost”
}
# if log location wasn’t supplised, use default
if ($logloc -eq “”) {
      $logloc = $defaultLogLocation
}
# if no collector name specified, generate one
if($colname -eq “”) {
      $dataCollectorName = “pfs_” + [datetime]::Now.Ticks
}
else {
      $dataCollectorName = $colname
}
Write-Host “Collection Name: “ $dataCollectorName
Write-Host
Write-Host “Log File location…”
Write-Host $logloc
# create the collection and start it
Write-Host
Write-Host “Attempting to create collector set…”
logman create counter $dataCollectorName -s $mach -v mmddhhmm -cf $inf -o $logloc -f csv -si $sampleInterval
Write-Host
Write-Host “Attempting to start collector set…”
logman start $dataCollectorName-s $mach
                This is a very simple and concise script, but effective in saving some time and eliminating repetitive tasks.  It takes three parameters:
-inf (required)
The path to the file containing the list of counters.
-mach (optional)
                The name of a remote machine.  Omit if working with the local machine.
-logloc (optional)
The path of the log file.  If unspecified, it will use the default in the script.  Therefore, when pushing this out to your environment alter the default parameters accordingly.
-colname (optional)
                The desired name of the new data collector set.  If omitted, it will be automatically generated.
                So there you have it.  By setting initial defaults right within the script, it only actually requires one parameter to get a data collector set up and running.  There are a few things to note:  First off, the PowerShell script will start the collector provided that it was successfully created.  Also, it will run indefinitely and unscheduled.  In other words, as it is shipped right now you will have to manually stop the data collection.  Feel free to mold the code into your particular desires and environment.
                The code for this script can be found on my GitHub SQLSalt repository at the following location:  PerfMonStarter.ps1.  Check back frequently for additional “counter libraries” for common troubleshooting scenarios.  If there are any questions or comments regarding this post, please feel free to leave a comment below or email me at sqlsalt@gmail.com.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s