Feeds:
Posts
Comments

Posts Tagged ‘Scripting’

If you have used PowerShell V2 (and above) for sometime now, you would know that you can use the Import-Counter Cmdlet to read Perfmon data from a .blg file.

This works fine when you want to look at data “as it is” and export to a CSV file or format it for display. I recently ran into a scenario where I had parse multiple perfmon files and then “look” at the data with different time intervals for a few specific counters only.

PS >dir D:\Sunit\Blog\PerfmonFiles -Include *.blg -Recurse | Select Fullname

FullName

——–

D:\Sunit\Blog\PerfmonFiles\T1HW\DataCollector01.blg

D:\Sunit\Blog\PerfmonFiles\T1K2\DataCollector01.blg

D:\Sunit\Blog\PerfmonFiles\T2HW\DataCollector01.blg

Also I needed to dump all the data into one CSV file so that I could use that for further analysis. So I wrote a function to allow me to do this and is included here in case it’s useful for someone else too.

The crux of the script is in this section where I calculate the averages depending on the interval:


$data = Import-Counter -Path $Path -Counter $counters
$d = $data | Where {$_.countersamples.status -eq 0}
$entries = @()
$runID = ($Path -split "\\")[-2] #Change this to your depth
Write-Debug "Key $runID"
for($i=1; $i -lt $d.Count; $i += $Interval)
{
$UBound = $i + ($Interval-1)
$entries += [pscustomobject] @{
Timestamp = ([datetime] $d[$i].Timestamp).ToLongTimeString()
CPUAvgProc = $d[$i..$UBound] | Select -ExpandProperty CounterSamples | `
Where {$_.Path -like $counters[0]} | measure -Property CookedValue -Average `
| select -ExpandProperty Average
CPUAvgPriv = $d[$i..$UBound] | Select -ExpandProperty CounterSamples | Where {$_.Path -like $counters[1]} `
| measure -Property CookedValue -Average | select -ExpandProperty Average
RunID = $runID
}
}

The script takes in pipeline input (from dir cmd) and lets you specify the interval and CSVFolder to dump individual files to. In my case the logging interval was 15 secs, so if I specified –Interval 4, it would average data for every 4 counters and display it in each minute interval.

It also has a –Summary option that let’s you see the logging-interval and the counter paths in the perfmon file. Something like this:

image

To aggregate the data for multiple files and place all data in one file, you would do:

dir D:\Sunit\Blog\ParseBLGFiles -Include *.blg –Recurse | ParseBLGFile -Interval 4 –AllInOneFile

Here’s a run with interval specified as 4 (data is avg. for each min since logging interval is 15 sec in this case) and option to save all of the data in one csv file:

dir D:\Sunit\Blog\PerfmonFiles -Include *.blg -Recurse | ParseBLGFile -Interval 4 -AllInOneFile –CSVFolder D:\Sunit\Blog\PerfmonFiles | ft -AutoSize

image

And the subsequent CSV file, AllRuns.csv

image

Here’s the entire script (rename to .zip). If it proves helpful, do please let me know.

ReadPFCounters

Advertisements

Read Full Post »