Skip to content

Commit

Permalink
Improves REST-Api memory management
Browse files Browse the repository at this point in the history
  • Loading branch information
LordHepipud committed Jul 24, 2023
1 parent 87a9d12 commit e92d7aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function New-IcingaForWindowsRESTThread()
Send-IcingaWebAuthMessage -Connection $Connection;
# Close the connection
Close-IcingaTCPConnection -Client $Connection.Client;
$Connection = $null;
continue;
}

Expand All @@ -56,6 +57,7 @@ function New-IcingaForWindowsRESTThread()
Send-IcingaWebAuthMessage -Connection $Connection;
# Close the connection
Close-IcingaTCPConnection -Client $Connection.Client;
$Connection = $null;
continue;
}
}
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions lib/daemons/modules/ApiChecks/Invoke-IcingaApiChecksRESTCall.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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 `
Expand Down

0 comments on commit e92d7aa

Please sign in to comment.