diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index b5aab162..08bc7008 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -18,6 +18,8 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic ### Enhancements +* [#756](https://github.com/Icinga/icinga-powershell-framework/pull/756) Adds feature to set process priority for Icinga for Windows to BelowNormal for improved Host performance + ## 1.13.0 Beta-1 (2024-08-30) [Issues and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/32) diff --git a/jobs/SetProcessPriority.ps1 b/jobs/SetProcessPriority.ps1 new file mode 100644 index 00000000..11f9ca98 --- /dev/null +++ b/jobs/SetProcessPriority.ps1 @@ -0,0 +1,22 @@ +# Load the basic framework data +Use-Icinga -Minimal; + +# Wait 10 seconds before procedding +Start-Sleep -Seconds 10; + +# Fetch the process information for JEA and the Icinga for Windows PID +$JeaProcess = Get-Process -Id (Get-IcingaJEAServicePid) -ErrorAction SilentlyContinue; +$IfWProcess = Get-Process -Id (Get-IcingaForWindowsServicePid) -ErrorAction SilentlyContinue; + +# Set the JEA pid to below normal +if ($null -ne $JeaProcess -And $JeaProcess.ProcessName -eq 'wsmprovhost') { + $JeaProcess.PriorityClass = 'BelowNormal'; +} + +# Set the Icinga for Windows pid to below normal +if ($null -ne $IfWProcess -And $IfWProcess.ProcessName -eq 'powershell') { + $IfWProcess.PriorityClass = 'BelowNormal'; +} + +# Exit with okay +exit 0; diff --git a/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 b/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 index bcc9c876..98cc4711 100644 --- a/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 +++ b/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 @@ -151,7 +151,18 @@ function Invoke-IcingaForWindowsMigration() Start-IcingaWindowsScheduledTaskRenewCertificate; # Ensure the Icinga Agent is not spamming the Application log by default Write-IcingaAgentEventLogConfig -Severity 'warning'; + # Set our newly added process update task + Register-IcingaWindowsScheduledTaskProcessPriority -Force; Set-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.13.0'); } + + if (Test-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.13.0.1')) { + Write-IcingaConsoleNotice 'Applying pending migrations required for Icinga for Windows v1.13.0.1'; + + # Set our newly added process update task + Register-IcingaWindowsScheduledTaskProcessPriority -Force; + + Set-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.13.0.1'); + } } diff --git a/lib/core/windows/Restart-IcingaWindowsService.psm1 b/lib/core/windows/Restart-IcingaWindowsService.psm1 index cf448e53..38cdb6cf 100644 --- a/lib/core/windows/Restart-IcingaWindowsService.psm1 +++ b/lib/core/windows/Restart-IcingaWindowsService.psm1 @@ -9,6 +9,8 @@ function Restart-IcingaForWindows() } Restart-IcingaService -Service 'icingapowershell'; + # Update the process priority after each restart + Start-IcingaWindowsScheduledTaskProcessPriority; } Set-Alias -Name 'Restart-IcingaWindowsService' -Value 'Restart-IcingaForWindows'; diff --git a/lib/core/wintasks/daemon/Register-TaskIcingaForWindowsProcessPriority.psm1 b/lib/core/wintasks/daemon/Register-TaskIcingaForWindowsProcessPriority.psm1 new file mode 100644 index 00000000..840352e8 --- /dev/null +++ b/lib/core/wintasks/daemon/Register-TaskIcingaForWindowsProcessPriority.psm1 @@ -0,0 +1,25 @@ +function Register-IcingaWindowsScheduledTaskProcessPriority() +{ + param ( + [switch]$Force = $FALSE + ); + + [string]$TaskName = 'Set Process Priority'; + [string]$TaskPath = '\Icinga\Icinga for Windows\'; + + $SetProcessPriorityTask = Get-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -ErrorAction SilentlyContinue; + + if ($null -ne $SetProcessPriorityTask -And $Force -eq $FALSE) { + Write-IcingaConsoleWarning -Message 'The {0} task is already present. User -Force to enforce the re-creation' -Objects $TaskName; + return; + } + + $ScriptPath = Join-Path -Path (Get-IcingaFrameworkRootPath) -ChildPath '\jobs\SetProcessPriority.ps1'; + $TaskAction = New-ScheduledTaskAction -Execute 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -Argument ([string]::Format("-WindowStyle Hidden -Command &{{ & '{0}' }}", $ScriptPath)); + $TaskPrincipal = New-ScheduledTaskPrincipal -UserId 'S-1-5-18' -RunLevel 'Highest' -LogonType ServiceAccount; + $TaskSettings = New-ScheduledTaskSettingsSet -DontStopIfGoingOnBatteries -AllowStartIfOnBatteries -StartWhenAvailable; + + Register-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Force -Principal $TaskPrincipal -Action $TaskAction -Settings $TaskSettings | Out-Null; + + Write-IcingaConsoleNotice -Message 'The task "{0}" has been successfully registered at location "{1}".' -Objects $TaskName, $TaskPath; +} diff --git a/lib/core/wintasks/daemon/Start-TaskSetProcessPriority.psm1 b/lib/core/wintasks/daemon/Start-TaskSetProcessPriority.psm1 new file mode 100644 index 00000000..7c9cec40 --- /dev/null +++ b/lib/core/wintasks/daemon/Start-TaskSetProcessPriority.psm1 @@ -0,0 +1,14 @@ +function Start-IcingaWindowsScheduledTaskProcessPriority() +{ + [string]$TaskName = 'Set Process Priority'; + [string]$TaskPath = '\Icinga\Icinga for Windows\'; + + $SetProcessPriorityTask = Get-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -ErrorAction SilentlyContinue; + + if ($null -eq $SetProcessPriorityTask) { + Write-IcingaConsoleNotice -Message 'The "{0}" task is not present on this system.' -Objects $TaskName; + return; + } + + Start-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath; +}