2021-02-16 10:10:00 -05:00
function Exit-IcingaExecutePlugin ( )
{
param (
[ string ] $Command = ''
) ;
2022-02-16 10:36:07 -05:00
# We need to fix the argument encoding hell
2022-08-22 11:15:25 -04:00
[ hashtable ] $ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Command $Command -Arguments $args ;
2022-01-28 17:24:50 -05:00
[ string ] $JEAProfile = Get-IcingaJEAContext ;
[ bool ] $CheckByIcingaForWindows = $FALSE ;
[ bool ] $CheckByJEAShell = $FALSE ;
2021-08-06 12:12:27 -04:00
2022-01-28 17:24:50 -05:00
if ( $args -Contains '-IcingaForWindowsRemoteExecution' ) {
$CheckByIcingaForWindows = $TRUE ;
}
if ( $args -Contains '-IcingaForWindowsJEARemoteExecution' ) {
$CheckByJEAShell = $TRUE ;
}
# We use the plugin check_by_icingaforwindows.ps1 to execute
# checks from a Linux/Windows remote source
if ( $CheckByIcingaForWindows ) {
# First try to queue the check over the REST-Api
2022-02-16 10:36:07 -05:00
$CheckResult = Invoke-IcingaInternalServiceCall -Command $Command -Arguments $ConvertedArgs -NoExit ;
2022-01-28 17:24:50 -05:00
if ( $null -ne $CheckResult ) {
# Seems we got a result
Write-IcingaConsolePlain -Message $CheckResult ;
# Do not close the session, we need to read the ExitCode from Get-IcingaInternalPluginExitCode
# The plugin itself will terminate the session
return ;
}
# We couldn't use our Rest-Api and Api-Checks feature, then lets execute the plugin locally
# Set daemon true, because this will change internal handling for errors and plugin output
$Global:Icinga . Protected . RunAsDaemon = $TRUE ;
try {
# Execute our plugin
2022-02-16 10:36:07 -05:00
( & $Command @ConvertedArgs ) | Out-Null ;
2022-01-28 17:24:50 -05:00
} catch {
# Handle errors within our plugins
# If anything goes wrong handle the error very detailed
$Global:Icinga . Protected . RunAsDaemon = $FALSE ;
2022-02-16 10:36:07 -05:00
Write-IcingaExecutePluginException -Command $Command -ErrorObject $_ -Arguments $ConvertedArgs ;
$ConvertedArgs . Clear ( ) ;
2022-01-28 17:24:50 -05:00
# Do not close the session, we need to read the ExitCode from Get-IcingaInternalPluginExitCode
# The plugin itself will terminate the session
return ;
}
# Disable it again - we need to write data to our shell now. Not very intuitive, but it is the easiest
# solution to do it this way
$Global:Icinga . Protected . RunAsDaemon = $FALSE ;
# Now print the result to shell
Write-IcingaPluginResult -PluginOutput ( Get-IcingaInternalPluginOutput ) -PluginPerfData ( Get-IcingaCheckSchedulerPerfData ) ;
# Do not close the session, we need to read the ExitCode from Get-IcingaInternalPluginExitCode
# The plugin itself will terminate the session
return ;
}
# Regardless of JEA enabled or disabled, forward all checks to the internal API
# and check if we get a result from there
2022-02-16 10:36:07 -05:00
Invoke-IcingaInternalServiceCall -Command $Command -Arguments $ConvertedArgs ;
2021-02-16 10:10:00 -05:00
try {
2022-01-28 17:24:50 -05:00
# If the plugin is not installed, throw a good exception
2021-03-01 06:02:39 -05:00
Exit-IcingaPluginNotInstalled -Command $Command ;
2022-01-28 17:24:50 -05:00
# In case we have JEA enabled on our system, this shell currently open most likely has no
# JEA configuration installed. This is because a JEA shell will not return an exit code and
# Icinga relies on that. Therefor we will try to open a new PowerShell with the JEA configuration
# assigned for Icinga for Windows, execute the plugins there and return the result
2021-08-06 12:12:27 -04:00
if ( [ string ] :: IsNullOrEmpty ( $JEAProfile ) -eq $FALSE ) {
$ErrorHandler = ''
$JEARun = (
& powershell . exe -ConfigurationName $JEAProfile -NoLogo -NoProfile -Command {
2022-01-28 17:24:50 -05:00
# Load Icinga for Windows
2021-08-06 12:12:27 -04:00
Use-Icinga ;
2022-01-28 17:24:50 -05:00
# Enable our JEA context
2021-12-09 11:42:06 -05:00
$Global:Icinga . Protected . JEAContext = $TRUE ;
2021-08-06 12:12:27 -04:00
2022-01-28 17:24:50 -05:00
# Parse the arguments our previous shell received
2021-08-06 12:12:27 -04:00
$Command = $args [ 0 ] ;
$Arguments = $args [ 1 ] ;
$Output = '' ;
try {
2022-01-28 17:24:50 -05:00
# Try executing our checks, store the exit code and plugin output
2021-08-06 12:12:27 -04:00
$ExitCode = ( & $Command @Arguments ) ;
$Output = ( Get-IcingaInternalPluginOutput ) ;
$ExitCode = ( Get-IcingaInternalPluginExitCode ) ;
} catch {
2022-01-28 17:24:50 -05:00
# If we failed for some reason, print a detailed error and use exit code 3 to mark the check as unkown
2021-08-06 12:12:27 -04:00
$Output = [ string ] :: Format ( '[UNKNOWN] Icinga Exception: Error while executing plugin in JEA context{0}{0}{1}' , ( New-IcingaNewLine ) , $_ . Exception . Message ) ;
$ExitCode = 3 ;
}
2022-01-28 17:24:50 -05:00
# Return the result to our main PowerShell
2021-08-06 12:12:27 -04:00
return @ {
'Output' = $Output ;
'PerfData' = ( Get-IcingaCheckSchedulerPerfData )
'ExitCode' = $ExitCode ;
}
2022-02-16 10:36:07 -05:00
} -args $Command , $ConvertedArgs
2021-08-06 12:12:27 -04:00
) 2 > $ErrorHandler ;
2022-01-28 17:24:50 -05:00
# If we have an exit code larger or equal 0, the execution inside the JEA shell was successfully and we can share the result
# In case we had an error inside the JEA shell, it will returned here as well
2021-08-06 12:12:27 -04:00
if ( $LASTEXITCODE -ge 0 ) {
Write-IcingaPluginResult -PluginOutput $JEARun . Output -PluginPerfData $JEARun . PerfData ;
exit $JEARun . ExitCode ;
} else {
2022-01-28 17:24:50 -05:00
# If for some reason the PowerShell could not be started within JEA context, we can throw an exception with exit code 3
# to mark the check as unknown including our error message
2021-08-06 12:12:27 -04:00
Write-IcingaConsolePlain '[UNKNOWN] Icinga Exception: Unable to start the PowerShell.exe with the provided JEA profile "{0}" for CheckCommand: {1}' -Objects $JEAProfile , $Command ;
exit 3 ;
}
} else {
2022-01-28 17:24:50 -05:00
# If we simply run the check without JEA context or from remote, we can just execute the plugin and
# exit with the exit code received from the result
2022-02-16 10:36:07 -05:00
exit ( & $Command @ConvertedArgs ) ;
2021-08-06 12:12:27 -04:00
}
2021-02-16 10:10:00 -05:00
} catch {
2022-01-28 17:24:50 -05:00
# If anything goes wrong handle the error
2022-02-16 10:36:07 -05:00
Write-IcingaExecutePluginException -Command $Command -ErrorObject $_ -Arguments $ConvertedArgs ;
$ConvertedArgs . Clear ( ) ;
2021-07-16 07:15:21 -04:00
2021-02-16 10:10:00 -05:00
exit 3 ;
}
}