diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index cd1d93e..85f834c 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -20,6 +20,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#621](https://github.com/Icinga/icinga-powershell-framework/pull/621) Fixes `-ThresholdInterval` key detection on newer systems * [#645](https://github.com/Icinga/icinga-powershell-framework/pull/645) Fixes error and exception handling while using API-Checks, which now will in most cases always return a proper check-result object and also abort while running into plugin execution errors, in case a server is not reachable by the time sync plugin for example * [#646](https://github.com/Icinga/icinga-powershell-framework/pull/646) Fixes REST-Api to allow arguments for check execution with and without leading `-` +* [#648](https://github.com/Icinga/icinga-powershell-framework/pull/648) Fixes some memory management while using the REST-Api to clear connection objects once they are no longer required ### Enhancements diff --git a/lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1 b/lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1 index b8c0f21..f249e9a 100644 --- a/lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1 +++ b/lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1 @@ -87,16 +87,19 @@ function New-IcingaForWindowsRESTApi() if (Test-IcingaRESTClientBlacklisted -Client $Connection.Client -ClientList $Global:Icinga.Public.Daemons.RESTApi.ClientBlacklist) { Write-IcingaDebugMessage -Message 'A remote client which is trying to connect was blacklisted' -Objects $Connection.Client.Client; Close-IcingaTCPConnection -Client $Connection.Client; + $Connection = $null; continue; } if ((Test-IcingaRESTClientConnection -Connection $Connection) -eq $FALSE) { + $Connection = $null; continue; } # API not yet ready if ($Global:Icinga.Public.Daemons.RESTApi.ApiRequests.Count -eq 0) { Close-IcingaTCPConnection -Client $Connection.Client; + $Connection = $null; continue; } @@ -105,6 +108,7 @@ function New-IcingaForWindowsRESTApi() if ($Global:Icinga.Public.Daemons.RESTApi.ApiRequests.ContainsKey($NextRESTApiThreadId) -eq $FALSE) { Close-IcingaTCPConnection -Client $Connection.Client; + $Connection = $null; continue; } diff --git a/lib/daemons/RestAPI/threads/New-IcingaForWindowsRESTThread.psm1 b/lib/daemons/RestAPI/threads/New-IcingaForWindowsRESTThread.psm1 index 3e00104..d1656f4 100644 --- a/lib/daemons/RestAPI/threads/New-IcingaForWindowsRESTThread.psm1 +++ b/lib/daemons/RestAPI/threads/New-IcingaForWindowsRESTThread.psm1 @@ -39,6 +39,7 @@ function New-IcingaForWindowsRESTThread() Send-IcingaWebAuthMessage -Connection $Connection; # Close the connection Close-IcingaTCPConnection -Client $Connection.Client; + $Connection = $null; continue; } @@ -56,6 +57,7 @@ function New-IcingaForWindowsRESTThread() Send-IcingaWebAuthMessage -Connection $Connection; # Close the connection Close-IcingaTCPConnection -Client $Connection.Client; + $Connection = $null; continue; } } @@ -99,6 +101,7 @@ function New-IcingaForWindowsRESTThread() # Finally close the clients connection as we are done here and # ensure this thread will close by simply leaving the function Close-IcingaTCPConnection -Client $Connection.Client; + $Connection = $null; # Force Icinga for Windows Garbage Collection Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC; diff --git a/lib/daemons/modules/ApiChecks/Invoke-IcingaApiChecksRESTCall.psm1 b/lib/daemons/modules/ApiChecks/Invoke-IcingaApiChecksRESTCall.psm1 index 0dcbe65..bb92543 100644 --- a/lib/daemons/modules/ApiChecks/Invoke-IcingaApiChecksRESTCall.psm1 +++ b/lib/daemons/modules/ApiChecks/Invoke-IcingaApiChecksRESTCall.psm1 @@ -57,6 +57,26 @@ function Invoke-IcingaApiChecksRESTCall() [string]$ExecuteCommand = $Request.RequestArguments.command; } + if ((Test-IcingaFunction -Name $ExecuteCommand) -eq $FALSE) { + + Add-IcingaHashtableItem ` + -Hashtable $ContentResponse ` + -Key $ExecuteCommand ` + -Value @{ + 'exitcode' = 3; + 'checkresult' = [string]::Format('[UNKNOWN] Icinga plugin not found exception: Command "{0}" is not present on the system{1}{1}The command "{0}" you are trying to execute over the REST-Api endpoint "apichecks" is not available on the system.', $ExecuteCommand, (New-IcingaNewLine)); + 'perfdata' = @(); + } | Out-Null; + + Send-IcingaTCPClientMessage -Message ( + New-IcingaTCPClientRESTMessage ` + -HTTPResponse ($IcingaHTTPEnums.HTTPResponseType.'Not Found') ` + -ContentBody $ContentResponse + ) -Stream $Connection.Stream; + + return; + } + if ((Test-IcingaRESTApiCommand -Command $ExecuteCommand -Endpoint 'apichecks') -eq $FALSE) { Add-IcingaHashtableItem `