Skip to content

Commit e436bd9

Browse files
authored
Merge pull request #648 from Icinga:fix/improves_rest_api_memory_management
Fix: Improves REST-Api memory management Fixes some memory management while using the REST-Api to clear connection objects once they are no longer required
2 parents 7ed2118 + e96711e commit e436bd9

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

doc/100-General/10-Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
2020
* [#621](https://github.com/Icinga/icinga-powershell-framework/pull/621) Fixes `-ThresholdInterval` key detection on newer systems
2121
* [#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
2222
* [#646](https://github.com/Icinga/icinga-powershell-framework/pull/646) Fixes REST-Api to allow arguments for check execution with and without leading `-`
23+
* [#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
2324

2425
### Enhancements
2526

lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1

+4
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,19 @@ function New-IcingaForWindowsRESTApi()
8787
if (Test-IcingaRESTClientBlacklisted -Client $Connection.Client -ClientList $Global:Icinga.Public.Daemons.RESTApi.ClientBlacklist) {
8888
Write-IcingaDebugMessage -Message 'A remote client which is trying to connect was blacklisted' -Objects $Connection.Client.Client;
8989
Close-IcingaTCPConnection -Client $Connection.Client;
90+
$Connection = $null;
9091
continue;
9192
}
9293

9394
if ((Test-IcingaRESTClientConnection -Connection $Connection) -eq $FALSE) {
95+
$Connection = $null;
9496
continue;
9597
}
9698

9799
# API not yet ready
98100
if ($Global:Icinga.Public.Daemons.RESTApi.ApiRequests.Count -eq 0) {
99101
Close-IcingaTCPConnection -Client $Connection.Client;
102+
$Connection = $null;
100103
continue;
101104
}
102105

@@ -105,6 +108,7 @@ function New-IcingaForWindowsRESTApi()
105108

106109
if ($Global:Icinga.Public.Daemons.RESTApi.ApiRequests.ContainsKey($NextRESTApiThreadId) -eq $FALSE) {
107110
Close-IcingaTCPConnection -Client $Connection.Client;
111+
$Connection = $null;
108112
continue;
109113
}
110114

lib/daemons/RestAPI/threads/New-IcingaForWindowsRESTThread.psm1

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function New-IcingaForWindowsRESTThread()
3939
Send-IcingaWebAuthMessage -Connection $Connection;
4040
# Close the connection
4141
Close-IcingaTCPConnection -Client $Connection.Client;
42+
$Connection = $null;
4243
continue;
4344
}
4445

@@ -56,6 +57,7 @@ function New-IcingaForWindowsRESTThread()
5657
Send-IcingaWebAuthMessage -Connection $Connection;
5758
# Close the connection
5859
Close-IcingaTCPConnection -Client $Connection.Client;
60+
$Connection = $null;
5961
continue;
6062
}
6163
}
@@ -99,6 +101,7 @@ function New-IcingaForWindowsRESTThread()
99101
# Finally close the clients connection as we are done here and
100102
# ensure this thread will close by simply leaving the function
101103
Close-IcingaTCPConnection -Client $Connection.Client;
104+
$Connection = $null;
102105

103106
# Force Icinga for Windows Garbage Collection
104107
Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC;

lib/daemons/modules/ApiChecks/Invoke-IcingaApiChecksRESTCall.psm1

+20
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ function Invoke-IcingaApiChecksRESTCall()
5757
[string]$ExecuteCommand = $Request.RequestArguments.command;
5858
}
5959

60+
if ((Test-IcingaFunction -Name $ExecuteCommand) -eq $FALSE) {
61+
62+
Add-IcingaHashtableItem `
63+
-Hashtable $ContentResponse `
64+
-Key $ExecuteCommand `
65+
-Value @{
66+
'exitcode' = 3;
67+
'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));
68+
'perfdata' = @();
69+
} | Out-Null;
70+
71+
Send-IcingaTCPClientMessage -Message (
72+
New-IcingaTCPClientRESTMessage `
73+
-HTTPResponse ($IcingaHTTPEnums.HTTPResponseType.'Not Found') `
74+
-ContentBody $ContentResponse
75+
) -Stream $Connection.Stream;
76+
77+
return;
78+
}
79+
6080
if ((Test-IcingaRESTApiCommand -Command $ExecuteCommand -Endpoint 'apichecks') -eq $FALSE) {
6181

6282
Add-IcingaHashtableItem `

0 commit comments

Comments
 (0)