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
# 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
# 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
# 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
# have been found
if ($UseCounterInstance -eq '*') {
# In case we already loaded the counters once, return the finished array
# TODO: Re-Implement caching for counters
<#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
@ -75,11 +75,11 @@
}
# 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
# TODO: Re-Implement caching for counters
# $Icinga2.Cache.PerformanceCounter.Add($Counter, $AllCountersIntances);
return (New-IcingaPerformanceCounterArray -FullName $Counter -PerformanceCounters $AllCountersIntances);
return (New-IcingaPerformanceCounterResult -FullName $Counter -PerformanceCounters $AllCountersIntances);
} else {
# This part will handle the counters without any instances as well as
# specificly assigned instances, like (_Total) CPU usage.

View file

@ -1,39 +1,71 @@
<#
# This function will provide a virtual object, containing an array
# of Performance Counters. The object has the following members:
# Name
# Value
# This will ensure we will not have to worry about looping an array
# of mutltiple instances within a counter handler, because this
# function will deal with everything, returning an hashtable
# containing the parent counter name including the values and
# samples for every single instance
# This function will make monitoring an entire list of
# Performance counters even more easier. We simply provide
# an array of Performance Counters to this module
# and we will receive a construct-save result of an
# hashtable with all performance counters including
# the corresponding values. In that case the code
# size decreases for larger modules.
# Example:
$counter = New-IcingaPerformanceCounterArray @(
'\Memory\Available Bytes',
'\Memory\% Committed Bytes In Use'
);
#>
function New-IcingaPerformanceCounterArray()
{
param(
[string]$FullName = '',
[array]$PerformanceCounters = @()
);
[array]$CounterArray = @()
)
$pc_instance = New-Object -TypeName PSObject;
$pc_instance | Add-Member -membertype NoteProperty -name 'FullName' -value $FullName;
$pc_instance | Add-Member -membertype NoteProperty -name 'Counters' -value $PerformanceCounters;
$pc_instance | Add-Member -membertype ScriptMethod -name 'Name' -value {
return $this.FullName;
[hashtable]$CounterResult = @{};
[bool]$RequireSleep = $TRUE;
foreach ($counter in $CounterArray) {
# 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
# 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 {
[hashtable]$CounterResults = @{};
# Above we initialse ever single counter and we only require a sleep once
# in case a new, yet unknown counter was added
if ($RequireSleep) {
Start-Sleep -Milliseconds 500;
foreach ($counter in $this.Counters) {
$CounterResults.Add($counter.Name(), $counter.Value());
# Agreed, this is some sort of code duplication but it wouldn't make
# 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:
# Name
# 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
# of the counter. Within the New-IcingaPerformanceCounterArray function,
# of the counter. Within the New-IcingaPerformanceCounterResult function,
# objects created by this function are used.
#>
function New-IcingaPerformanceCounterObject()

View file

@ -1,71 +1,38 @@
<#
# This function will make monitoring an entire list of
# Performance counters even more easier. We simply provide
# an array of Performance Counters to this module
# and we will receive a construct-save result of an
# hashtable with all performance counters including
# the corresponding values. In that case the code
# size decreases for larger modules.
# Example:
$counter = Get-Icinga-Counter -CounterArray @(
'\Memory\Available Bytes',
'\Memory\% Committed Bytes In Use'
);
# This function will provide a virtual object, containing an array
# of Performance Counters. The object has the following members:
# Name
# Value
# This will ensure we will not have to worry about looping an array
# of mutltiple instances within a counter handler, because this
# function will deal with everything, returning an hashtable
# containing the parent counter name including the values and
# samples for every single instance
#>
function New-IcingaPerformanceCounterResult()
{
param(
[array]$CounterArray = @()
)
[string]$FullName = '',
[array]$PerformanceCounters = @()
);
[hashtable]$CounterResult = @{};
[bool]$RequireSleep = $FALSE;
foreach ($counter in $CounterArray) {
# 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
# 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 $FALSE) {
$RequireSleep = $TRUE;
# }
#}
$obj = New-IcingaPerformanceCounter -Counter $counter -SkipWait $TRUE;
if ($CounterResult.ContainsKey($obj.Name()) -eq $FALSE) {
$CounterResult.Add($obj.Name(), $obj.Value());
}
$pc_instance = New-Object -TypeName PSObject;
$pc_instance | Add-Member -membertype NoteProperty -name 'FullName' -value $FullName;
$pc_instance | Add-Member -membertype NoteProperty -name 'Counters' -value $PerformanceCounters;
$pc_instance | Add-Member -membertype ScriptMethod -name 'Name' -value {
return $this.FullName;
}
# Above we initialse ever single counter and we only require a sleep once
# in case a new, yet unknown counter was added
if ($RequireSleep) {
Start-Sleep -Milliseconds 500;
$pc_instance | Add-Member -membertype ScriptMethod -name 'Value' -value {
[hashtable]$CounterResults = @{};
# Agreed, this is some sort of code duplication but it wouldn't make
# 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());
}
}
foreach ($counter in $this.Counters) {
$CounterResults.Add($counter.Name(), $counter.Value());
}
return $CounterResult;
return $CounterResults;
}
return $pc_instance;
}