Skip to content

Commit

Permalink
Adds Performance Counter cache
Browse files Browse the repository at this point in the history
Implements #96
  • Loading branch information
LordHepipud committed Aug 7, 2020
1 parent 47272bd commit 4ada0d6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
39 changes: 39 additions & 0 deletions lib/core/perfcounter/Icinga_GlobalPerfCounterCache.psm1
Original file line number Diff line number Diff line change
@@ -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;
}
28 changes: 13 additions & 15 deletions lib/core/perfcounter/New-IcingaPerformanceCounter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,32 +89,29 @@ 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
# specificly assigned instances, like (_Total) CPU usage.

# 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);
}
12 changes: 6 additions & 6 deletions lib/core/perfcounter/New-IcingaPerformanceCounterArray.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 4ada0d6

Please sign in to comment.