From 4ada0d69f9fafd8492c31b2ad12377eb08b677b2 Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Fri, 7 Aug 2020 15:17:19 +0200 Subject: [PATCH] Adds Performance Counter cache Implements #96 --- .../Icinga_GlobalPerfCounterCache.psm1 | 39 +++++++++++++++++++ .../New-IcingaPerformanceCounter.psm1 | 28 +++++++------ .../New-IcingaPerformanceCounterArray.psm1 | 12 +++--- 3 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 lib/core/perfcounter/Icinga_GlobalPerfCounterCache.psm1 diff --git a/lib/core/perfcounter/Icinga_GlobalPerfCounterCache.psm1 b/lib/core/perfcounter/Icinga_GlobalPerfCounterCache.psm1 new file mode 100644 index 00000000..f4995072 --- /dev/null +++ b/lib/core/perfcounter/Icinga_GlobalPerfCounterCache.psm1 @@ -0,0 +1,39 @@ +function New-IcingaPerformanceCounterCache() +{ + if ($null -eq $global:Icinga_PerfCounterCache) { + $global:Icinga_PerfCounterCache = ( + [hashtable]::Synchronized( + @{} + ) + ); + } +} + +function Add-IcingaPerformanceCounterCache() +{ + param ( + $Counter, + $Instances + ); + + if ($global:Icinga_PerfCounterCache.ContainsKey($Counter)) { + $global:Icinga_PerfCounterCache[$Counter] = $Instances; + } else { + $global:Icinga_PerfCounterCache.Add( + $Counter, $Instances + ); + } +} + +function Get-IcingaPerformanceCounterCacheItem() +{ + param ( + $Counter + ); + + if ($global:Icinga_PerfCounterCache.ContainsKey($Counter)) { + return $global:Icinga_PerfCounterCache[$Counter]; + } + + return $null; +} diff --git a/lib/core/perfcounter/New-IcingaPerformanceCounter.psm1 b/lib/core/perfcounter/New-IcingaPerformanceCounter.psm1 index ee615b7d..ac9d9c33 100644 --- a/lib/core/perfcounter/New-IcingaPerformanceCounter.psm1 +++ b/lib/core/perfcounter/New-IcingaPerformanceCounter.psm1 @@ -54,10 +54,11 @@ function New-IcingaPerformanceCounter() # 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-IcingaPerformanceCounterResult -FullName $Counter -PerformanceCounters $Icinga2.Cache.PerformanceCounter[$Counter]); - }#> + $CachedCounter = Get-IcingaPerformanceCounterCacheItem -Counter $Counter; + + if ($null -ne $CachedCounter) { + return (New-IcingaPerformanceCounterResult -FullName $Counter -PerformanceCounters $CachedCounter); + } # If we need to build the array, load all instances from the counters and # create single performance counters and add them to a custom array and @@ -88,8 +89,7 @@ function New-IcingaPerformanceCounter() # Add the parent counter including the array of Performance Counters to our # 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); + Add-IcingaPerformanceCounterCache -Counter $Counter -Instances $AllCountersIntances; return (New-IcingaPerformanceCounterResult -FullName $Counter -PerformanceCounters $AllCountersIntances); } else { # This part will handle the counters without any instances as well as @@ -97,23 +97,21 @@ function New-IcingaPerformanceCounter() # In case we already have the counter within our cache, return the # cached informations - # TODO: Re-Implement caching for counters - <#if ($Icinga2.Cache.PerformanceCounter.ContainsKey($Counter) -eq $TRUE) { - return $Icinga2.Cache.PerformanceCounter[$Counter]; - }#> + $CachedCounter = Get-IcingaPerformanceCounterCacheItem -Counter $Counter; + + if ($null -ne $CachedCounter) { + return $CachedCounter; + } # If the cache is not present yet, create the Performance Counter object, # and add it to our cache $NewCounter = New-IcingaPerformanceCounterObject -FullName $Counter -Category $UseCounterCategory -Counter $UseCounterName -Instance $UseCounterInstance -SkipWait $SkipWait; - # TODO: Re-Implement caching for counters - #$Icinga2.Cache.PerformanceCounter.Add($Counter, $NewCounter); - return $NewCounter; #TODO: Remove once caching is implemented + Add-IcingaPerformanceCounterCache -Counter $Counter -Instances $NewCounter; } # This function will always return non-instance counters or # specificly defined instance counters. Performance Counter Arrays # are returned within their function. This is just to ensure that the # function looks finished from developer point of view - # TODO: Re-Implement caching for counters, right now we return $NewCounter by default - #return $Icinga2.Cache.PerformanceCounter[$Counter]; + return (Get-IcingaPerformanceCounterCacheItem -Counter $Counter); } diff --git a/lib/core/perfcounter/New-IcingaPerformanceCounterArray.psm1 b/lib/core/perfcounter/New-IcingaPerformanceCounterArray.psm1 index 2290ff34..e18fd83e 100644 --- a/lib/core/perfcounter/New-IcingaPerformanceCounterArray.psm1 +++ b/lib/core/perfcounter/New-IcingaPerformanceCounterArray.psm1 @@ -27,12 +27,12 @@ function New-IcingaPerformanceCounterArray() # 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; - # } - #} + $CachedCounter = Get-IcingaPerformanceCounterCacheItem -Counter $Counter; + + if ($null -ne $CachedCounter) { + $RequireSleep = $FALSE; + } + $obj = New-IcingaPerformanceCounter -Counter $counter -SkipWait $TRUE; if ($CounterResult.ContainsKey($obj.Name()) -eq $FALSE) { $CounterResult.Add($obj.Name(), $obj.Value());