From 7b8380bee8c72ebd01b0ce0e6f6dca9e809d2f51 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Tue, 22 Oct 2019 19:53:50 +0200 Subject: [PATCH] Add support for dynamic background daemon loading --- lib/daemon/Get-IcingaBackgroundDaemons.psm1 | 22 +++++++++++++++++++ .../Register-IcingaBackgroundDaemon.psm1 | 22 +++++++++++++++++++ lib/daemon/Start-IcingaPowerShellDaemon.psm1 | 12 ++++++++-- .../Unregister-IcingaBackgroundDaemon.psm1 | 17 ++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 lib/daemon/Get-IcingaBackgroundDaemons.psm1 create mode 100644 lib/daemon/Register-IcingaBackgroundDaemon.psm1 create mode 100644 lib/daemon/Unregister-IcingaBackgroundDaemon.psm1 diff --git a/lib/daemon/Get-IcingaBackgroundDaemons.psm1 b/lib/daemon/Get-IcingaBackgroundDaemons.psm1 new file mode 100644 index 0000000..fc6c152 --- /dev/null +++ b/lib/daemon/Get-IcingaBackgroundDaemons.psm1 @@ -0,0 +1,22 @@ +function Get-IcingaBackgroundDaemons() +{ + $Daemons = Get-IcingaPowerShellConfig -Path 'BackgroundDaemon.EnabledDaemons'; + + if ($null -eq $Daemons) { + return $null; + } + + [hashtable]$Output = @{}; + + foreach ($daemon in $Daemons.PSObject.Properties) { + $Arguments = @{}; + + foreach ($argument in $daemon.Value.Arguments.PSObject.Properties) { + $Arguments.Add($argument.Name, $argument.Value); + } + + $Output.Add($daemon.Name, $Arguments); + } + + return $Output; +} diff --git a/lib/daemon/Register-IcingaBackgroundDaemon.psm1 b/lib/daemon/Register-IcingaBackgroundDaemon.psm1 new file mode 100644 index 0000000..83aa6b7 --- /dev/null +++ b/lib/daemon/Register-IcingaBackgroundDaemon.psm1 @@ -0,0 +1,22 @@ +function Register-IcingaBackgroundDaemon() +{ + param( + [string]$Command, + [hashtable]$Arguments + ); + + if ([string]::IsNullOrEmpty($Command)) { + throw 'Please specify a Cmdlet to run as Background Daemon'; + } + + if (-Not (Test-IcingaFunction $Command)) { + throw ([string]::Format('The Cmdlet "{0}" is not available in your session. Please restart the session and try again or verify your input', $Command)); + } + + $Path = [string]::Format('BackgroundDaemon.EnabledDaemons.{0}', $Command); + + Set-IcingaPowerShellConfig -Path ([string]::Format('{0}.Command', $Path)) -Value $Command; + Set-IcingaPowerShellConfig -Path ([string]::Format('{0}.Arguments', $Path)) -Value $Arguments; + + Write-Host ([string]::Format('Background daemon Cmdlet "{0}" has been configured', $Command)); +} diff --git a/lib/daemon/Start-IcingaPowerShellDaemon.psm1 b/lib/daemon/Start-IcingaPowerShellDaemon.psm1 index fa8951d..4fe858c 100644 --- a/lib/daemon/Start-IcingaPowerShellDaemon.psm1 +++ b/lib/daemon/Start-IcingaPowerShellDaemon.psm1 @@ -6,8 +6,16 @@ function Start-IcingaPowerShellDaemon() Use-Icinga -LibOnly -Daemon; try { - # Todo: Add dynamic loading of enabled background tasks - Start-IcingaServiceCheckDaemon; + $EnabledDaemons = Get-IcingaBackgroundDaemons; + + foreach ($daemon in $EnabledDaemons.Keys) { + if (-Not (Test-IcingaFunction $daemon)) { + continue; + } + + $daemonArgs = $EnabledDaemons[$daemon]; + &$daemon @daemonArgs; + } } catch { # Todo: Add exception handling } diff --git a/lib/daemon/Unregister-IcingaBackgroundDaemon.psm1 b/lib/daemon/Unregister-IcingaBackgroundDaemon.psm1 new file mode 100644 index 0000000..a836b11 --- /dev/null +++ b/lib/daemon/Unregister-IcingaBackgroundDaemon.psm1 @@ -0,0 +1,17 @@ +function Unregister-IcingaBackgroundDaemon() +{ + param( + [string]$BackgroundDaemon, + [hashtable]$Arguments + ); + + if ([string]::IsNullOrEmpty($BackgroundDaemon)) { + throw 'Please specify a Cmdlet to remove from running as Background Daemon'; + } + + $Path = [string]::Format('BackgroundDaemon.EnabledDaemons.{0}', $BackgroundDaemon); + + Remove-IcingaPowerShellConfig -Path $Path; + + Write-Host 'Background daemon has been removed'; +}