From e84b5119122dce9fc813e06b0cbd5be08250dc61 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Wed, 14 Aug 2024 17:26:00 +0200 Subject: [PATCH] Adds cmdlet `Write-IcingaAgentEventLogConfig` to update eventlog severity and defaults to `warning`during installation and migration --- doc/100-General/10-Changelog.md | 1 + .../Invoke-IcingaForWindowsMigration.psm1 | 2 + .../misc/Start-IcingaAgentInstallWizard.psm1 | 2 + .../Write-IcingaAgentEventLogConfig.psm1 | 38 +++++++++++++++++++ .../Start-IcingaForWindowsInstallation.psm1 | 2 + 5 files changed, 45 insertions(+) create mode 100644 lib/core/icingaagent/writers/Write-IcingaAgentEventLogConfig.psm1 diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index 61cdc47b..b842ea91 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -19,6 +19,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic ### Enhancements +* [#711](https://github.com/Icinga/icinga-powershell-framework/issues/711) Adds cmdlet `Write-IcingaAgentEventLogConfig` to update eventlog severity and defaults to `warning` during installation and migration * [#732](https://github.com/Icinga/icinga-powershell-framework/pull/732) Adds support for TLS 1.3 and improves startup response * [#735](https://github.com/Icinga/icinga-powershell-framework/pull/735) Adds support to provide occuring problem event id's for the Eventlog and corresponding acknowledgement id's, providing an indicator if certain issues are resolved or still present * [#739](https://github.com/Icinga/icinga-powershell-framework/pull/739) Adds support to check the encoding of files to ensure we can properly load them and throw errors for unsupported encoding diff --git a/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 b/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 index 35118ee8..bcc9c876 100644 --- a/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 +++ b/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 @@ -149,6 +149,8 @@ function Invoke-IcingaForWindowsMigration() # Updates certificate renew task to handle changes made which now stores the Icinga CA inside the cert store Start-IcingaWindowsScheduledTaskRenewCertificate; + # Ensure the Icinga Agent is not spamming the Application log by default + Write-IcingaAgentEventLogConfig -Severity 'warning'; Set-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.13.0'); } diff --git a/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 b/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 index 9a12d1ba..bfd81382 100644 --- a/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 +++ b/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 @@ -750,6 +750,8 @@ function Start-IcingaAgentInstallWizard() # First cleanup the system by removing all old Firewalls Enable-IcingaFirewall -IcingaPort $CAPort -Force; } + # Ensure the Icinga Agent is not spamming the Application log by default + Write-IcingaAgentEventLogConfig -Severity 'warning'; Test-IcingaAgent; if ($InstallFrameworkService) { Restart-IcingaForWindows; diff --git a/lib/core/icingaagent/writers/Write-IcingaAgentEventLogConfig.psm1 b/lib/core/icingaagent/writers/Write-IcingaAgentEventLogConfig.psm1 new file mode 100644 index 00000000..b822bcc6 --- /dev/null +++ b/lib/core/icingaagent/writers/Write-IcingaAgentEventLogConfig.psm1 @@ -0,0 +1,38 @@ +<# +.SYNOPSIS + Writes the Icinga Agent Event Log configuration. + +.DESCRIPTION + The Write-IcingaAgentEventLogConfig function is used to write the configuration for the Icinga Agent Event Log. It creates a configuration file with the specified severity level for the Windows Event Log Logger. + +.PARAMETER Severity + Specifies the severity level for the Windows Event Log Logger. Valid values are 'debug', 'notice', 'information', 'warning', and 'critical'. The default value is 'information'. + +.EXAMPLE + Write-IcingaAgentEventLogConfig -Severity 'warning' + This example writes the Icinga Agent Event Log configuration with the severity level set to 'warning'. + +.NOTES + Please make sure to restart the Icinga Agent after applying any changes to the configuration. +#> + +function Write-IcingaAgentEventLogConfig() +{ + param ( + [ValidateSet('debug', 'notice', 'information', 'warning', 'critical')] + [string]$Severity = 'information' + ); + + $EventLogConf = New-Object System.Text.StringBuilder; + + $EventLogConf.AppendLine('/**') | Out-Null; + $EventLogConf.AppendLine(' * The WindowsEventLogLogger type writes log information to the Windows Event Log.') | Out-Null; + $EventLogConf.AppendLine(' */') | Out-Null; + $EventLogConf.AppendLine('') | Out-Null; + $EventLogConf.AppendLine('object WindowsEventLogLogger "windowseventlog" {') | Out-Null; + $EventLogConf.AppendLine([string]::Format(' severity = "{0}"', $Severity)) | Out-Null; + $EventLogConf.Append('}') | Out-Null; + + Write-IcingaFileSecure -File (Join-Path -Path (Get-IcingaAgentConfigDirectory) -ChildPath 'features-available\windowseventlog.conf') -Value $EventLogConf.ToString(); + Write-IcingaConsoleNotice 'Windows Eventlog configuration has been written successfully to use severity level: {0} - Please restart the Icinga Agent to apply this change' -Objects $Severity; +} diff --git a/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 b/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 index c368160d..33c89a61 100644 --- a/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 +++ b/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 @@ -203,6 +203,8 @@ function Start-IcingaForWindowsInstallation() Set-IcingaUserPermissions -IcingaUser $ServiceUser; Install-IcingaAgentBaseFeatures; Write-IcingaAgentApiConfig -Port $IcingaPort; + # Ensure the Icinga Agent is not spamming the Application log by default + Write-IcingaAgentEventLogConfig -Severity 'warning'; # Fixes an issue with the local Icinga for Windows listen port and the defined ports for communicating with the Icinga Parent/CA Nodes # This will check if we provided a custom port for the endpoints and use this one instead of the configured listen port if Icinga for Windows