Issues Parsing custom powershell service output

So I was finally able to get my powershell script working. However, I’m now trying to parse the output and create OK, Warning, and Critical modifications.

Here’s the contents of the script - mail.ps1:

Import-Module ExchangeOnlineManagement
#Connect-ExchangeOnline

$secpasswd = ConvertTo-SecureString -String 'PASSWORD' -AsPlainText -Force

$o365cred = New-Object System.Management.Automation.PSCredential ("USER@DOMAIN", $secpasswd)

Connect-ExchangeOnline -Credential $o365cred

Get-EXOMailboxFolderStatistics -Identity support -FolderScope inbox | ?{$_.folderpath -eq "/inbox"} | select visibleItemsInFolder

I’m currently passing the command via by_ssh. Here is the service definition:

apply Service "check_support_mailbox" {
        import "generic-service"
        check_command = "by_ssh"
        check_interval = 30m
        vars.by_ssh_command = "pwsh /home/nagios/mail.ps1 | tail -2"
        vars.by_ssh_timeout = 60
        assign where host.vars.check_support_mailbox == true
}

When I run this from the command line I get the following output:

nagios@icinga2:/home/nagios$ /home/nagios/mail.ps1 | tail -2
                   0

However, the output that I see in the web interface is empty

Plugin Output
Problem handling

Can anyone tell me out I might process the output correctly, and also set OK, Warning, and Critical values for comparison against the expected value? I would want OK to be for 0, Warning to be >5, and critical to be > 10.

Icinga Version Information:


    Icinga2 Version: r2.6.0-1
    Operating System: Debian 9
    Enabled features: api checker command ido-mysql mainlog notification
    Icinga Web 2 version and modules 2.4.1; doc - 2.4.1, monitoring - 2.4.1
    Config validation (icinga2 daemon -C)
    information/cli: Icinga application loader (version: r2.6.0-1)
    information/cli: Loading configuration file(s).
    information/ConfigItem: Committing config item(s).
    information/ApiListener: My API identity: icinga2.phytec
    warning/ApplyRule: Apply rule ‘auto_reroute_status’ (in /etc/icinga2/conf.d/services.conf: 100:1-100:35) for type ‘Service’ does not match anywhere!
    warning/ApplyRule: Apply rule ‘check-speedtest’ (in /etc/icinga2/conf.d/services.conf: 198:1-198:31) for type ‘Service’ does not match anywhere!
    warning/ApplyRule: Apply rule ‘ilo’ (in /etc/icinga2/conf.d/services.conf: 226:1-226:19) for type ‘Service’ does not match anywhere!
    information/ConfigItem: Instantiated 2 ApiUsers.
    information/ConfigItem: Instantiated 1 ApiListener.
    information/ConfigItem: Instantiated 1 Zone.
    information/ConfigItem: Instantiated 1 FileLogger.
    information/ConfigItem: Instantiated 1 Endpoint.
    information/ConfigItem: Instantiated 2 NotificationCommands.
    information/ConfigItem: Instantiated 49 Notifications.
    information/ConfigItem: Instantiated 205 CheckCommands.
    information/ConfigItem: Instantiated 1 Downtime.
    information/ConfigItem: Instantiated 47 Hosts.
    information/ConfigItem: Instantiated 1 IcingaApplication.
    information/ConfigItem: Instantiated 2 HostGroups.
    information/ConfigItem: Instantiated 3 Comments.
    information/ConfigItem: Instantiated 3 UserGroups.
    information/ConfigItem: Instantiated 5 Users.
    information/ConfigItem: Instantiated 49 Services.
    information/ConfigItem: Instantiated 3 TimePeriods.
    information/ConfigItem: Instantiated 3 ServiceGroups.
    information/ConfigItem: Instantiated 1 ScheduledDowntime.
    information/ConfigItem: Instantiated 1 ExternalCommandListener.
    information/ConfigItem: Instantiated 1 CheckerComponent.
    information/ConfigItem: Instantiated 1 IdoMysqlConnection.
    information/ConfigItem: Instantiated 1 NotificationComponent.
    information/ScriptGlobal: Dumping variables to file ‘/var/cache/icinga2/icinga2.vars’
    information/cli: Finished validating the configuration file(s).

Don’t do it via SSH. Just install the Icinga 2 agent on your Windows host and run it locally.

The output is only used for display purposed and |.*$ is used for perfdata.
The Status of your service is determinated by the exit code (0 = ok, 1 = warn, 2 = crit, 3 = unknown). You can check this with $? in the terminal (yes works the same for PS and sh).

Also I hope you don’t need elevate or you’re screwed as you need to run your script as scheduled task with ‘Run With Highest Privileges’ checked and need to use the Icinga2 API to report the result back.

Here some pointers to how and why for writhing Nagios/Icinga checks:

For output formatting, see the above links.
You need to calculate the state in your script yourself and set:

  • exit 0 for OK
  • exit 1 for WARNING
  • exit 2 for CRITICAL
  • exit 3 for UNKNOWN