Skip to main content

Collect Performance Counter data for Grafana Visualization

· 3 min read
Naw Awn

This was a small weekend personal project to try out Grafana visualisation tool for a test Hyper-V server. InfluxDB was used for storing the time series database. Since performance counter values are based on a series of event happening over time, it is the best one for what it does. The extra tool NSSM was also needed for running influxdb as a service on Windows machine. To make life easier, I also used the PowerShell module called Influx to feed the data to the Influx database.

See the links below for downloading and configuring them.

I won't be going through how these tools needed to be set up on this post. One thing to note though, you will need to download the open source data collector version on InfluxDB for your lab and create the database.

What the script does:
The PowerShell script will go through all the counters defined on the config file (.psd1) and will start gathering the samples on the Influx database.

You could run this directly from the PowerShell Console or from the scheduled task. I would recommend using Start-Job for this kind of data gathering. Using these data, you could then create a dashboard on Grafana for visualisation. This is one of the free solutions you could use for monitoring your servers in your network environment.

Get-PerfMetrics.ps1
#Requires -Module Influx

$Config = Import-PowerShellDataFile -Path "$PSScriptRoot\Metrics-Config.psd1"

Function Get-PerfMetrics{
[CmdletBinding()]
[OutputType([Hashtable])]
Param(
[String]$ComputerName = $env:COMPUTERNAME,
[String[]]$Counter
)
Process{
$Metrics = @{}
Foreach($item in $Counter){
Write-Verbose "PROCESSING... $item"
$Sample = (Get-Counter -Counter $item -ComputerName $ComputerName).CounterSamples
If ($Sample){
Foreach($value in $Sample){
$Path = $($value.Path) -Replace [regex]::Escape("\\$ComputerName\"),"" -Replace " ",""
$CookedValue = $value.CookedValue
Write-Verbose "$Path - $CookedValue"
$Metrics[$Path] = $CookedValue
}
}
}
return ($Metrics)
}
<#
.Synopsis
Show the counter and its value
.EXAMPLE
Get-PerfMetrics -Counter "\LogicalDisk(*)\Disk Transfers/sec" -Verbose
.OUTPUTS
Hashtable
#>
}

#region - Controller Script
While($true){
$Metrics = Get-PerfMetrics -Counter $Config.Counter -ComputerName $Config.ComputerName
Write-Influx -Measure Server -Tags @{Server=$($Config.ComputerName)} -Metrics $Metrics -Database $Config.Database -Server $Config.InfluxUri
Start-Sleep -Seconds $Config.Interval
}
#endregion

If you need to use different counters on your project, just add and remove the counters on the config file as required. Surely, the counter you want to capture will depend on the service or application you run on the server.

PerfMetrics-Config.psd1
@{
Description = 'Counter list, avoid wildcard entry where possible'
InfluxUri = 'http://localhost:8086/'
Database = 'DbName'
Interval = 5 #Adjust this according to your counter
ComputerName = 'ServerName'
Counter = @(
'\Processor Information(_total)\% Processor Time',
'\Memory\Available Mbytes',
'\Network Interface(*)\Bytes Total/sec',
'\Hyper-V Hypervisor Logical Processor(_total)\% Total Run Time',
'\Hyper-V Hypervisor Virtual Processor(_total)\% Total Run Time',
'\Hyper-V Hypervisor Root Virtual Processor(_total)\% Total Run Time',
'\Hyper-V Dynamic Memory Balancer(system balancer)\Available Memory',
'\Hyper-V Virtual Network Adapter(*)\Bytes/sec',
'\LogicalDisk(_total)\Avg. Disk sec/Read',
'\LogicalDisk(_total)\Avg. Disk sec/Write',
'\LogicalDisk(_total)\Disk Transfers/sec',
'\PhysicalDisk(_total)\Avg. Disk sec/Read',
'\PhysicalDisk(_total)\Avg. Disk sec/Write',
'\PhysicalDisk(_total)\Disk Transfers/sec'
)
}
note

PowerShell v5 could throw an error for using -ComputerName $env:ComputerName on Get-Counter cmdlet. Run this on PS v7 to avoid the error.