Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/31-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
[Issue and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/15?closed=1)

* [#305](https://github.com/Icinga/icinga-powershell-framework/pull/305) Adds a new Cmdlet to test if functions with `Add-Type` are already present inside the current scope of the shell
* [#306](https://github.com/Icinga/icinga-powershell-framework/pull/306) Adds new Cmdlet `Exit-IcingaThrowCritical` to throw critical exit with a custom message, either by force or by using string filtering and adds storing of plugin exit codes internally

## 1.5.2 (2021-07-09)

Expand Down
32 changes: 32 additions & 0 deletions lib/icinga/exception/Exit-IcingaThrowCritical.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function Exit-IcingaThrowCritical()
{
param (
[string]$Message = '',
[string]$FilterString = $null,
[string]$SearchString = $null,
[switch]$Force = $FALSE
);

if ($Force -eq $FALSE) {
if ([string]::IsNullOrEmpty($FilterString) -Or [string]::IsNullOrEmpty($SearchString)) {
return;
}

if ($FilterString -NotLike "*$SearchString*") {
return;
}
}

[string]$OutputMessage = [string]::Format(
'[CRITICAL] {0}',
$Message
);

Set-IcingaInternalPluginExitCode -ExitCode $IcingaEnums.IcingaExitCode.Critical;
Set-IcingaInternalPluginException -PluginException $OutputMessage;

if ($null -eq $global:IcingaDaemonData -Or $global:IcingaDaemonData.FrameworkRunningAsDaemon -eq $FALSE) {
Write-IcingaConsolePlain $OutputMessage;
exit $IcingaEnums.IcingaExitCode.Critical;
}
}
3 changes: 3 additions & 0 deletions lib/icinga/exception/Exit-IcingaThrowException.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ function Exit-IcingaThrowException()
$ExceptionTypeString
);

Set-IcingaInternalPluginExitCode -ExitCode $IcingaEnums.IcingaExitCode.Unknown;
Set-IcingaInternalPluginException -PluginException $OutputMessage;

if ($null -eq $global:IcingaDaemonData -Or $global:IcingaDaemonData.FrameworkRunningAsDaemon -eq $FALSE) {
Write-IcingaConsolePlain $OutputMessage;
exit $IcingaEnums.IcingaExitCode.Unknown;
Expand Down
6 changes: 5 additions & 1 deletion lib/icinga/plugin/New-IcingaCheckResult.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ function New-IcingaCheckResult()
# Ensure we reset our internal cache once the plugin was executed
$Global:Icinga.ThresholdCache[$this.Check.__GetCheckCommand()] = $null;

return $this.Check.__GetCheckState();
$ExitCode = $this.Check.__GetCheckState();

Set-IcingaInternalPluginExitCode -ExitCode $ExitCode;

return $ExitCode;
}

if ($Compile) {
Expand Down
29 changes: 29 additions & 0 deletions lib/icinga/plugin/Set-IcingaInternalPluginException.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function Set-IcingaInternalPluginException()
{
param (
[string]$PluginException = ''
);

if ($null -eq $Global:Icinga) {
$Global:Icinga = @{ };
}

if ($Global:Icinga.ContainsKey('PluginExecution') -eq $FALSE) {
$Global:Icinga.Add(
'PluginExecution',
@{
'PluginException' = $PluginException;
}
)
} else {
if ($Global:Icinga.PluginExecution.ContainsKey('PluginException') -eq $FALSE) {
$Global:Icinga.PluginExecution.Add('PluginException', $PluginException);
return;
}

# Only catch the first exception
if ([string]::IsNullOrEmpty($Global:Icinga.PluginExecution.PluginException)) {
$Global:Icinga.PluginExecution.PluginException = $PluginException;
}
}
}
29 changes: 29 additions & 0 deletions lib/icinga/plugin/Set-IcingaInternalPluginExitCode.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function Set-IcingaInternalPluginExitCode()
{
param (
$ExitCode = 0
);

if ($null -eq $Global:Icinga) {
$Global:Icinga = @{ };
}

if ($Global:Icinga.ContainsKey('PluginExecution') -eq $FALSE) {
$Global:Icinga.Add(
'PluginExecution',
@{
'LastExitCode' = $ExitCode;
}
)
} else {
if ($Global:Icinga.PluginExecution.ContainsKey('LastExitCode') -eq $FALSE) {
$Global:Icinga.PluginExecution.Add('LastExitCode', $ExitCode);
return;
}

# Only add the first exit code we should cover during one runtime
if ($null -eq $Global:Icinga.PluginExecution.LastExitCode) {
$Global:Icinga.PluginExecution.LastExitCode = $ExitCode;
}
}
}