mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 15:19:58 -05:00
57 lines
2.6 KiB
PowerShell
57 lines
2.6 KiB
PowerShell
#
|
|
# Provide the name of a category to fetch all available counters and
|
|
# if there are any instances assigned to it
|
|
#
|
|
function Show-IcingaPerformanceCounters()
|
|
{
|
|
param (
|
|
[string]$CounterCategory
|
|
);
|
|
|
|
[hashtable]$counters = @{};
|
|
|
|
if ([string]::IsNullOrEmpty($CounterCategory)) {
|
|
$counters.Add('error', 'Please specify a counter category');
|
|
return $counters;
|
|
}
|
|
|
|
try {
|
|
# At first create our Performance Counter object for the category we specified
|
|
$Category = New-Object System.Diagnostics.PerformanceCounterCategory($CounterCategory);
|
|
|
|
# Now loop through all keys to find the name of available counters
|
|
foreach ($counter in $Category.ReadCategory().Keys) {
|
|
[string]$CounterInstanceAddition = '';
|
|
|
|
# As counters might also have instances (like interfaces, disks, paging file), we should
|
|
# try to load them as well
|
|
foreach ($instance in $Category.ReadCategory()[$counter].Keys) {
|
|
# If we do not match this magic string, we have multiple instances we can access
|
|
# to get informations for different disks, volumes and interfaces for example
|
|
if ($instance -ne 'systemdiagnosticsperfcounterlibsingleinstance') {
|
|
# Re-Write the name we return of the counter to something we can use directly
|
|
# within our modules to load data from. A returned counter will look like this
|
|
# for example:
|
|
# \PhysicalDisk(*)\avg. disk bytes/read
|
|
[string]$UsableCounterName = [string]::Format('\{0}(*)\{1}', $CounterCategory, $counter);
|
|
if ($counters.ContainsKey($UsableCounterName) -eq $TRUE) {
|
|
$counters[$UsableCounterName] += $Category.ReadCategory()[$counter][$instance];
|
|
} else {
|
|
$counters.Add($UsableCounterName, @( $Category.ReadCategory()[$counter][$instance] ));
|
|
}
|
|
} else {
|
|
# For counters with no instances, we still require to return a re-build Performance Counter
|
|
# output, to make later usage in our modules very easy. This can look like this:
|
|
# \System\system up time
|
|
[string]$UsableCounterName = [string]::Format('\{0}\{1}', $CounterCategory, $counter);
|
|
$counters.Add($UsableCounterName, $null);
|
|
}
|
|
}
|
|
};
|
|
} catch {
|
|
# In case we run into an error, return an error message
|
|
$counters.Add('error', $_.Exception.Message);
|
|
}
|
|
|
|
return $counters.Keys;
|
|
}
|