Add support for dynamic background daemon loading

This commit is contained in:
Lord Hepipud 2019-10-22 19:53:50 +02:00
parent 0d59fc9a15
commit 7b8380bee8
4 changed files with 71 additions and 2 deletions

View file

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

View file

@ -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));
}

View file

@ -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
}

View file

@ -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';
}