Skip to content
This repository has been archived by the owner on Jan 1, 2020. It is now read-only.

Windows Client Not Reporting Correct Powershell Exit Code #1919

Closed
dkorel-copperleaf opened this issue Oct 23, 2018 · 11 comments
Closed

Windows Client Not Reporting Correct Powershell Exit Code #1919

dkorel-copperleaf opened this issue Oct 23, 2018 · 11 comments
Assignees
Milestone

Comments

@dkorel-copperleaf
Copy link

When doing Exit 2 in powershell, sensu-client only seems to capture a status of 1.

Expected Behavior

I believe the status should be 2 when the exit code is 2.

Current Behavior

Status code 1 or 0 works as expected. If the process exits with something else, it translates to 1.

Possible Solution

I would guess windows Ruby implementation does not look to $LASTEXITCODE, but instead loots to $? which only contains a True|False value.

Steps to Reproduce (for bugs)

Run a powershell script with sensu client containing

Write-Host "TEST"
Exit 2

Sensu history and status should see a status of 2. It sees a status of 1.

Your Environment

Windows Server 2012R2
Multiple versions of sensu-client for windows. Including the latest (sensu-1.5.0-1-x86.msi)

@itamarshpak
Copy link

itamarshpak commented Oct 25, 2018

Hey, Use this one for exit code 2: [System.Environment]::Exit(2)

@dkorel-copperleaf
Copy link
Author

dkorel-copperleaf commented Oct 29, 2018

That one seems to exit the parent shell environment. Im not sure that's what we want... although it might work.

I'm assuming existing check scripts should work with standard exit... e.g: https://github.com/sensu-plugins/sensu-plugins-windows/blob/f772fc42129b113c493243d758560cef32fe52b1/bin/powershell/check-windows-log.ps1#L39

@itamarshpak
Copy link

itamarshpak commented Oct 29, 2018 via email

@dkorel-copperleaf
Copy link
Author

dkorel-copperleaf commented Oct 31, 2018

Its useful to know, but I think it's a workaround and there is an underlying issue with how exit codes are caught. As I pointed out - all the powershell scripts I've seen published by Sensu use 'Exit [code]'

I think that when a script exits, it's supposed to return control and exit codes to the calling shell. If we exit the parent shell, well it makes it difficult to develop and test code... and it goes against the intended design.

@itamarshpak
Copy link

well if you find another way that working, and the events get exit code 2 and seen as critical, update here plz :)

@dkorel-copperleaf
Copy link
Author

I don't have a solution... i think it's a bug... so i think the project maintainers will assess the priority, and assign resources appropriately to hopefully fix it like the champs they are 🥇 :)

I guess using ruby instead of powershell would be another possible workaround...

@itamarshpak
Copy link

itamarshpak commented Nov 1, 2018 via email

@amdprophet
Copy link
Member

amdprophet commented Jan 7, 2019

I can't seem to reproduce this. Please let me know if I'm doing something differently than you.

I copied your example into a file called test.ps1:

Write-Host "TEST"
Exit 2

I then created a check definition:

{
  "checks": {
    "check-test": {
      "command": "powershell.exe -f c:\\users\\vagrant\\test.ps1",
      "interval": 10,
      "subscribers": ["windows"]
    }
  }
}

The check results contain a status of 2 and a history only containing 2.

@dkorel-copperleaf
Copy link
Author

Looks like the issue is with omitting the '-f' param. Thanks for picking this up! much appreciated

@amdprophet
Copy link
Member

No problem! Just to confirm, adding -f fixes the issue for you? Can we consider this resolved?

@dkorel-copperleaf
Copy link
Author

Correct. -f solves the issue. Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants