From 9df17e1068b6faeee433aeb4a1397e3d86835336 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Fri, 22 Nov 2019 19:42:48 +0100 Subject: [PATCH] Fixes Service Check Background Daemon Memory leak Fixes #20 This fixes the memory leak on the Service Check Background Daemon, mainly caused by not properly catching the boolean return values of `Add-IcingaHashtableItem`. The boolean values stayed within the scope, poluting the memory and causing functions to behave not as expected --- lib/core/tools/Add-IcingaHashtableItem.psm1 | 3 ++- .../Start-IcingaServiceCheckDaemon.psm1 | 26 ++++++++++--------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/core/tools/Add-IcingaHashtableItem.psm1 b/lib/core/tools/Add-IcingaHashtableItem.psm1 index 49a2b0e..a8c3c44 100644 --- a/lib/core/tools/Add-IcingaHashtableItem.psm1 +++ b/lib/core/tools/Add-IcingaHashtableItem.psm1 @@ -16,7 +16,8 @@ function Add-IcingaHashtableItem() return $TRUE; } else { if ($Override) { - $Hashtable[$Key] = $Value; + $Hashtable.Remove($Key); + $Hashtable.Add($Key, $Value); return $TRUE; } } diff --git a/lib/daemons/ServiceCheckDaemon/Start-IcingaServiceCheckDaemon.psm1 b/lib/daemons/ServiceCheckDaemon/Start-IcingaServiceCheckDaemon.psm1 index afbaa08..d1e7044 100644 --- a/lib/daemons/ServiceCheckDaemon/Start-IcingaServiceCheckDaemon.psm1 +++ b/lib/daemons/ServiceCheckDaemon/Start-IcingaServiceCheckDaemon.psm1 @@ -79,9 +79,10 @@ function Start-IcingaServiceCheckTask() $UnixTime = Get-IcingaUnixTime; foreach ($result in $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['results'].Keys) { - $SortedResult = $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['results'][$result].GetEnumerator() | Sort-Object name -Descending; - Add-IcingaHashtableItem -Hashtable $OldData -Key $result -Value @{}; - Add-IcingaHashtableItem -Hashtable $PerfCache -Key ([string]$result) -Value @{}; + [string]$HashIndex = $result; + $SortedResult = $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['results'][$HashIndex].GetEnumerator() | Sort-Object name -Descending; + Add-IcingaHashtableItem -Hashtable $OldData -Key $HashIndex -Value @{} | Out-Null; + Add-IcingaHashtableItem -Hashtable $PerfCache -Key $HashIndex -Value @{} | Out-Null; foreach ($index in $TimeIndexes) { $ObjectCount = 0; @@ -92,19 +93,20 @@ function Start-IcingaServiceCheckTask() if (($UnixTime - $TimeInSeconds) -le [int]$timeEntry.Key) { $ObjectCount += 1; $ObjectValue += $timeEntry.Value; - Remove-IcingaHashtableItem -Hashtable $OldData[$result] -Key $timeEntry; - Add-IcingaHashtableItem -Hashtable $PerfCache[$result] -Key ([string]$timeEntry.Key) -Value ([string]$timeEntry.Value); + Remove-IcingaHashtableItem -Hashtable $OldData[$HashIndex] -Key $timeEntry; + Add-IcingaHashtableItem -Hashtable $PerfCache[$HashIndex] -Key ([string]$timeEntry.Key) -Value ([string]$timeEntry.Value) | Out-Null; } else { - Add-IcingaHashtableItem -Hashtable $OldData[$result] -Key $timeEntry -Value $null; + Add-IcingaHashtableItem -Hashtable $OldData[$HashIndex] -Key $timeEntry -Value $null | Out-Null; } } - $AvarageValue = ($ObjectValue / $ObjectCount); - $MetricName = [string]::Format('{0}_{1}', $result, $index); + $AverageValue = ($ObjectValue / $ObjectCount); + [string]$MetricName = [string]::Format('{0}_{1}', $HashIndex, $index); + $MetricName = Format-IcingaPerfDataLabel $MetricName; Add-IcingaHashtableItem ` -Hashtable $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['average'] ` - -Key (Format-IcingaPerfDataLabel $MetricName) -Value $AvarageValue -Override; + -Key $MetricName -Value $AverageValue -Override | Out-Null; } } @@ -123,9 +125,9 @@ function Start-IcingaServiceCheckTask() } $PassedTime = 0; - $SortedResult = $null; - $OldData = @{}; - $PerfCache = @{}; + $SortedResult.Clear(); + $OldData.Clear(); + $PerfCache.Clear(); } $PassedTime += 1; Start-Sleep -Seconds 1;