Powershell module exit with wrong exit code

Hi All,

I have a custom powershell check which is set to scan the event viewer logs for particular entries, module is below;

Param(
[Int32]$periodcount=4,
[Int32]$hours=4,
[string]$source=“MSSQL*”,
[string]$logname=“application”,
[string]$entity=“Information”,
[string]$message=“The log for database
)

$ts = New-TimeSpan -hours $hours
$after=(get-date) - $ts
$logentries=Get-EventLog -LogName “$logname” -EntryType “$entity” -Source “$source” -After “$after” -Message “$message”
$count=($logentries | measure).Count

if ( $count -gt $periodcount ) {
write-host “FAIL | Check Event Viewer | logentries=$count”
($logentries | % { $_.Message }) | write-host
exit 2
} else {
write-host “OK | logentries=$count”
exit 0
}

Here is my service;

apply Service “DatabaseLog” {
import “dba-service”
display_name = “Database log not available”
command_endpoint = host.vars.client_endpoint
check_command = “database-log-missing”
vars.notification = {
dba.pagerduty = false
dba.mail = false
}
assign where host.vars.lgs
}

And the command;

object CheckCommand “database-log-missing” {
import “plugin-check-command”
command = [ “powershell.exe” ]
arguments = {
“-command” = {
value = “& ‘C:\Program Files\ICINGA2\sbin\modules\database_not_available.ps1’”
order = -1
}
“;exit” = {
value = “$LastExitCode$”
}
}
}

The “LastExitCode” part of the command is something I recently added in an attempt to resolve this issue, but the same problem occurs regardless of its presence.

Now what I’m seeing is the module will exit with the correct code, either 0 or 2, but Icinga will see exit code other than 0 as a warning state. What can I do to get Icinga exit as critical?

Are you sure, you get exit 2? Warning would be exit 1
You can check with echo $?

I have never seen this:

“;exit” = {
value = “$LastExitCode$”
}

What is the reasoning?

$LastExitCode is a powershell command which can print the last exit code it produced. Here is my example with it failing;

PS C:> C:\Program Files\ICINGA2\sbin\modules\database_not_available.ps1
FAIL | Check Event Viewer | logentries=1
Error: 50001 Severity: 10 State: 1 The log for database MSSQL$TEST2 is not available

PS C:> $LastExitCode
2

I had seen it referenced in other Icinga work and hoped it would help > PowerShell custom check exits with "OK" if script is not found

For further context, this is OK output;

PS C:> C:\Program Files\ICINGA2\sbin\modules\database_not_available.ps1
OK | logentries=0

PS C:> $LastExitCode
0

value = “$LastExitCode$”

looked like you want to inject the content of the Icinga variable ($varname$) into ; exit.
You need to escape $ with $ to get the PS variable. So I guess it should be $$LastExitCode but I don’t think that ;exit is a good idea as it looks like a command injection an should not work if you pass the command parameters as array and not as string.

I use the posix (?) $? to get last exit code.

Anyway I never needed such workarounds to get the exit code of a powershell script but launch it like this C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file C:\some\path\Icinga.ps1

Thanks @rivad , the incorrect placement of the second $ was the issue. This now works as expected