Skip to content

perfhelper.ps1 should check if perf counters started before creating perfhash.hsh #83

@robertkaucher

Description

@robertkaucher

We had a few new systems added this week and the "Server Manager Performance Monitor" collector set had not been started on these and the metric plugins were sending strange/no data. I realized the counters were not started and started them on all the servers but this did not have any impact on the metrics. I found that Get-PerformanceCounterByID in perfhelper.ps1 was creating a cache file but was not checking if the performance counters were started and if they were not most of the metrics would not function correctly. I'm not sure if the scripts should start the counters or simply not create the $hashFile unless the counters are started.

$datacollectorset = New-Object -COM Pla.DataCollectorSet
$datacollectorset.Query("Server Manager Performance Monitor",$null)
if ($datacollectorset.Status -ne 0) {
	Export-Clixml -InputObject $perfHash -Path $hashfile
}

Or perhaps the counters should be started since anyone attempting to use the plugins will need this to be the case to use them any way.

function Get-PerformanceCounterByID
{
    param
    (
        [Parameter(Mandatory=$true)]
        $Name
    )
    $hashfile = (Join-Path $PSScriptRoot perfhash.hsh)
   $datacollectorset = New-Object -COM Pla.DataCollectorSet
   $datacollectorset.Query("Server Manager Performance Monitor",$null)
   if ($datacollectorset.Status -eq 0) {
	$datacollectorset.Start($true)
        if((Test-Path $hashFile)){
             Remove-Item $hashFile
        }
   }
    if ([System.IO.File]::Exists($hashfile)) {       
        $perfHash = Import-Clixml -Path $hashfile
    }
    if ($perfHash -eq $null)
    {
        $key = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009'
        $counters = (Get-ItemProperty -Path $key -Name Counter).Counter
        $perfHash = @{}
        $all = $counters.Count
         for($i = 0; $i -lt $all; $i+=2)
        {
           $perfHash.$($counters[$i+1]) = $counters[$i]
        }
        Export-Clixml -InputObject $perfHash -Path $hashfile
    }
    $perfHash.$Name
}

It's probably wise to perform a synchronous start on the data collector set and delete the $hashFile if it has been previously created.
https://docs.microsoft.com/en-us/windows/desktop/api/pla/nf-pla-idatacollectorset-start

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions