Renamed PerfCounter functions to fit there results better

This commit is contained in:
Lord Hepipud 2019-07-22 14:04:32 +02:00
parent 164f03343c
commit f07a14ec67
4 changed files with 106 additions and 107 deletions

View file

@ -1,7 +1,7 @@
<# <#
# This is the main function which is called from this script, constructing our counters # This is the main function which is called from this script, constructing our counters
# and loading possible sub-instances from our Performance Counter. # and loading possible sub-instances from our Performance Counter.
# It will return either an New-IcingaPerformanceCounterObject or New-IcingaPerformanceCounterArray # It will return either an New-IcingaPerformanceCounterObject or New-IcingaPerformanceCounterResult
# which both contain the same members, allowing us to dynamicly use the objects # which both contain the same members, allowing us to dynamicly use the objects
# without having to worry about exception. # without having to worry about exception.
#> #>
@ -49,14 +49,14 @@
# the different values, we need to know how to handle the instances of the counter # the different values, we need to know how to handle the instances of the counter
# If we specify a instance with (*) we want the module to automaticly fetch all # If we specify a instance with (*) we want the module to automaticly fetch all
# instances for this counter. This will result in an New-IcingaPerformanceCounterArray # instances for this counter. This will result in an New-IcingaPerformanceCounterResult
# which contains the parent name including counters for all instances that # which contains the parent name including counters for all instances that
# have been found # have been found
if ($UseCounterInstance -eq '*') { if ($UseCounterInstance -eq '*') {
# In case we already loaded the counters once, return the finished array # In case we already loaded the counters once, return the finished array
# TODO: Re-Implement caching for counters # TODO: Re-Implement caching for counters
<#if ($Icinga2.Cache.PerformanceCounter.ContainsKey($Counter) -eq $TRUE) { <#if ($Icinga2.Cache.PerformanceCounter.ContainsKey($Counter) -eq $TRUE) {
return (New-IcingaPerformanceCounterArray -FullName $Counter -PerformanceCounters $Icinga2.Cache.PerformanceCounter[$Counter]); return (New-IcingaPerformanceCounterResult -FullName $Counter -PerformanceCounters $Icinga2.Cache.PerformanceCounter[$Counter]);
}#> }#>
# If we need to build the array, load all instances from the counters and # If we need to build the array, load all instances from the counters and
@ -75,11 +75,11 @@
} }
# Add the parent counter including the array of Performance Counters to our # Add the parent counter including the array of Performance Counters to our
# caching mechanism and return the New-IcingaPerformanceCounterArray object for usage # caching mechanism and return the New-IcingaPerformanceCounterResult object for usage
# within the monitoring modules # within the monitoring modules
# TODO: Re-Implement caching for counters # TODO: Re-Implement caching for counters
# $Icinga2.Cache.PerformanceCounter.Add($Counter, $AllCountersIntances); # $Icinga2.Cache.PerformanceCounter.Add($Counter, $AllCountersIntances);
return (New-IcingaPerformanceCounterArray -FullName $Counter -PerformanceCounters $AllCountersIntances); return (New-IcingaPerformanceCounterResult -FullName $Counter -PerformanceCounters $AllCountersIntances);
} else { } else {
# This part will handle the counters without any instances as well as # This part will handle the counters without any instances as well as
# specificly assigned instances, like (_Total) CPU usage. # specificly assigned instances, like (_Total) CPU usage.

View file

@ -1,39 +1,71 @@
<# <#
# This function will provide a virtual object, containing an array # This function will make monitoring an entire list of
# of Performance Counters. The object has the following members: # Performance counters even more easier. We simply provide
# Name # an array of Performance Counters to this module
# Value # and we will receive a construct-save result of an
# This will ensure we will not have to worry about looping an array # hashtable with all performance counters including
# of mutltiple instances within a counter handler, because this # the corresponding values. In that case the code
# function will deal with everything, returning an hashtable # size decreases for larger modules.
# containing the parent counter name including the values and # Example:
# samples for every single instance $counter = New-IcingaPerformanceCounterArray @(
'\Memory\Available Bytes',
'\Memory\% Committed Bytes In Use'
);
#> #>
function New-IcingaPerformanceCounterArray() function New-IcingaPerformanceCounterArray()
{ {
param( param(
[string]$FullName = '', [array]$CounterArray = @()
[array]$PerformanceCounters = @() )
);
$pc_instance = New-Object -TypeName PSObject; [hashtable]$CounterResult = @{};
$pc_instance | Add-Member -membertype NoteProperty -name 'FullName' -value $FullName; [bool]$RequireSleep = $TRUE;
$pc_instance | Add-Member -membertype NoteProperty -name 'Counters' -value $PerformanceCounters; foreach ($counter in $CounterArray) {
# We want to speed up things with loading, so we will check if a specified
$pc_instance | Add-Member -membertype ScriptMethod -name 'Name' -value { # Counter is already cached within our hashtable. If it is not, we sleep
return $this.FullName; # at the end of the function the required 500ms and don't have to wait
# NumOfCounters * 500 milliseconds for the first runs. This will speed
# up the general loading of counters and will not require some fancy
# pre-caching / configuration handler
# TODO: Re-Implement caching for counters
#if ($Icinga2.Cache.PerformanceCounter -ne $null) {
# if ($Icinga2.Cache.PerformanceCounter.ContainsKey($counter) -eq $TRUE) {
$RequireSleep = $FALSE;
# }
#}
$obj = New-IcingaPerformanceCounter -Counter $counter -SkipWait $TRUE;
if ($CounterResult.ContainsKey($obj.Name()) -eq $FALSE) {
$CounterResult.Add($obj.Name(), $obj.Value());
}
} }
$pc_instance | Add-Member -membertype ScriptMethod -name 'Value' -value { # Above we initialse ever single counter and we only require a sleep once
[hashtable]$CounterResults = @{}; # in case a new, yet unknown counter was added
if ($RequireSleep) {
Start-Sleep -Milliseconds 500;
foreach ($counter in $this.Counters) { # Agreed, this is some sort of code duplication but it wouldn't make
$CounterResults.Add($counter.Name(), $counter.Value()); # any sense to create a own function for this. Why are we doing
# this anway?
# Simple: In case we found counters which have yet not been initialised
# we did this above. Now we have waited 500 ms to receive proper
# values from these counters. As the previous generated result
# might have contained counters with 0 results, we will now
# check all counters again to receive the proper values.
# Agreed, might sound like a overhead, but the impact only
# applies to the first call of the module with the counters.
# This 'duplication' however decreased the execution from
# certain modules from 25s to 1s on the first run. Every
# additional run is then beeing executed within 0.x s
# which sounds like a very good performance and solution
$CounterResult = @{};
foreach ($counter in $CounterArray) {
$obj = New-IcingaPerformanceCounter -Counter $counter -SkipWait $TRUE;
if ($CounterResult.ContainsKey($obj.Name()) -eq $FALSE) {
$CounterResult.Add($obj.Name(), $obj.Value());
}
}
} }
return $CounterResults; return $CounterResult;
} }
return $pc_instance;
}

View file

@ -4,9 +4,9 @@
# following members: # following members:
# Name # Name
# Value # Value
# Like the New-IcingaPerformanceCounterArray, this will allow to fetch the # Like the New-IcingaPerformanceCounterResult, this will allow to fetch the
# current values of a single counter instance including the name # current values of a single counter instance including the name
# of the counter. Within the New-IcingaPerformanceCounterArray function, # of the counter. Within the New-IcingaPerformanceCounterResult function,
# objects created by this function are used. # objects created by this function are used.
#> #>
function New-IcingaPerformanceCounterObject() function New-IcingaPerformanceCounterObject()

View file

@ -1,71 +1,38 @@
<# <#
# This function will make monitoring an entire list of # This function will provide a virtual object, containing an array
# Performance counters even more easier. We simply provide # of Performance Counters. The object has the following members:
# an array of Performance Counters to this module # Name
# and we will receive a construct-save result of an # Value
# hashtable with all performance counters including # This will ensure we will not have to worry about looping an array
# the corresponding values. In that case the code # of mutltiple instances within a counter handler, because this
# size decreases for larger modules. # function will deal with everything, returning an hashtable
# Example: # containing the parent counter name including the values and
$counter = Get-Icinga-Counter -CounterArray @( # samples for every single instance
'\Memory\Available Bytes',
'\Memory\% Committed Bytes In Use'
);
#> #>
function New-IcingaPerformanceCounterResult() function New-IcingaPerformanceCounterResult()
{ {
param( param(
[array]$CounterArray = @() [string]$FullName = '',
) [array]$PerformanceCounters = @()
);
[hashtable]$CounterResult = @{}; $pc_instance = New-Object -TypeName PSObject;
[bool]$RequireSleep = $FALSE; $pc_instance | Add-Member -membertype NoteProperty -name 'FullName' -value $FullName;
foreach ($counter in $CounterArray) { $pc_instance | Add-Member -membertype NoteProperty -name 'Counters' -value $PerformanceCounters;
# We want to speed up things with loading, so we will check if a specified
# Counter is already cached within our hashtable. If it is not, we sleep $pc_instance | Add-Member -membertype ScriptMethod -name 'Name' -value {
# at the end of the function the required 500ms and don't have to wait return $this.FullName;
# NumOfCounters * 500 milliseconds for the first runs. This will speed
# up the general loading of counters and will not require some fancy
# pre-caching / configuration handler
# TODO: Re-Implement caching for counters
#if ($Icinga2.Cache.PerformanceCounter -ne $null) {
# if ($Icinga2.Cache.PerformanceCounter.ContainsKey($counter) -eq $FALSE) {
$RequireSleep = $TRUE;
# }
#}
$obj = New-IcingaPerformanceCounter -Counter $counter -SkipWait $TRUE;
if ($CounterResult.ContainsKey($obj.Name()) -eq $FALSE) {
$CounterResult.Add($obj.Name(), $obj.Value());
}
} }
# Above we initialse ever single counter and we only require a sleep once $pc_instance | Add-Member -membertype ScriptMethod -name 'Value' -value {
# in case a new, yet unknown counter was added [hashtable]$CounterResults = @{};
if ($RequireSleep) {
Start-Sleep -Milliseconds 500;
# Agreed, this is some sort of code duplication but it wouldn't make foreach ($counter in $this.Counters) {
# any sense to create a own function for this. Why are we doing $CounterResults.Add($counter.Name(), $counter.Value());
# this anway?
# Simple: In case we found counters which have yet not been initialised
# we did this above. Now we have waited 500 ms to receive proper
# values from these counters. As the previous generated result
# might have contained counters with 0 results, we will now
# check all counters again to receive the proper values.
# Agreed, might sound like a overhead, but the impact only
# applies to the first call of the module with the counters.
# This 'duplication' however decreased the execution from
# certain modules from 25s to 1s on the first run. Every
# additional run is then beeing executed within 0.x s
# which sounds like a very good performance and solution
$CounterResult = @{};
foreach ($counter in $CounterArray) {
$obj = New-IcingaPerformanceCounter -Counter $counter -SkipWait $TRUE;
if ($CounterResult.ContainsKey($obj.Name()) -eq $FALSE) {
$CounterResult.Add($obj.Name(), $obj.Value());
}
}
} }
return $CounterResult; return $CounterResults;
}
return $pc_instance;
} }