From f07a14ec677c139f8e6a3b50d6099131d1ad1699 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Mon, 22 Jul 2019 14:04:32 +0200 Subject: [PATCH] Renamed PerfCounter functions to fit there results better --- .../New-IcingaPerformanceCounter.psm1 | 10 +- .../New-IcingaPerformanceCounterArray.psm1 | 100 ++++++++++++------ .../New-IcingaPerformanceCounterObject.psm1 | 4 +- .../New-IcingaPerformanceCounterResult.psm1 | 99 ++++++----------- 4 files changed, 106 insertions(+), 107 deletions(-) diff --git a/lib/core/perfcounter/New-IcingaPerformanceCounter.psm1 b/lib/core/perfcounter/New-IcingaPerformanceCounter.psm1 index 566d77f..fe24ddc 100644 --- a/lib/core/perfcounter/New-IcingaPerformanceCounter.psm1 +++ b/lib/core/perfcounter/New-IcingaPerformanceCounter.psm1 @@ -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. diff --git a/lib/core/perfcounter/New-IcingaPerformanceCounterArray.psm1 b/lib/core/perfcounter/New-IcingaPerformanceCounterArray.psm1 index bb78e36..5e0d86e 100644 --- a/lib/core/perfcounter/New-IcingaPerformanceCounterArray.psm1 +++ b/lib/core/perfcounter/New-IcingaPerformanceCounterArray.psm1 @@ -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 - #> - function New-IcingaPerformanceCounterArray() - { - param( - [string]$FullName = '', - [array]$PerformanceCounters = @() - ); + # 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( + [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; - } - - $pc_instance | Add-Member -membertype ScriptMethod -name 'Value' -value { - [hashtable]$CounterResults = @{}; - - foreach ($counter in $this.Counters) { - $CounterResults.Add($counter.Name(), $counter.Value()); + [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()); } + } - return $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; - return $pc_instance; - } - \ No newline at end of file + # 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 $CounterResult; + } diff --git a/lib/core/perfcounter/New-IcingaPerformanceCounterObject.psm1 b/lib/core/perfcounter/New-IcingaPerformanceCounterObject.psm1 index fd7596b..71d09c5 100644 --- a/lib/core/perfcounter/New-IcingaPerformanceCounterObject.psm1 +++ b/lib/core/perfcounter/New-IcingaPerformanceCounterObject.psm1 @@ -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() diff --git a/lib/core/perfcounter/New-IcingaPerformanceCounterResult.psm1 b/lib/core/perfcounter/New-IcingaPerformanceCounterResult.psm1 index 67afab3..97a924a 100644 --- a/lib/core/perfcounter/New-IcingaPerformanceCounterResult.psm1 +++ b/lib/core/perfcounter/New-IcingaPerformanceCounterResult.psm1 @@ -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' - ); - #> - function New-IcingaPerformanceCounterResult() - { - param( - [array]$CounterArray = @() - ) + # 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( + [string]$FullName = '', + [array]$PerformanceCounters = @() + ); + + $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; - [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 | Add-Member -membertype ScriptMethod -name 'Name' -value { + return $this.FullName; + } + + $pc_instance | Add-Member -membertype ScriptMethod -name 'Value' -value { + [hashtable]$CounterResults = @{}; + + foreach ($counter in $this.Counters) { + $CounterResults.Add($counter.Name(), $counter.Value()); } - } - # 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; + return $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()); - } - } - } - - return $CounterResult; - } + return $pc_instance; + }