NRPE Status Appears in the "Status Information" Field

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
jimmlegs
Posts: 49
Joined: Thu Jun 20, 2013 11:50 am

NRPE Status Appears in the "Status Information" Field

Post by jimmlegs »

Hello,

I have a Powershell script being executed by NRPE, the check works but does not display description in the "Status Information" field rather it displays what appears to be the $SERVICESTATUS$ variable with the description underneath (please see attached).

The output I get from the console has the NRPE status output as the first line:

[root@bos-nagios libexec]# ./check_nrpe -H 192.168.3.127 -c check_log_shipping
1
OK: Log Shipping: Last Copy Was: 11/21/2013 9:45:01 AM

Other Poweshell check work fine with NRPE, here is the output from an Exchange check:
[root@bos-nagios libexec]# ./check_nrpe -H 192.168.105.53 -c check_mailbox_health
OK: All Mailbox Databases are mounted and healthy.

Any guidance would be appreciated
You do not have the required permissions to view the files attached to this post.
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: NRPE Status Appears in the "Status Information" Field

Post by abrist »

Are you echoing the '1'? If so, you should be exiting with '1' or the other status codes, not using a write-output/print/echo. Use write-output/echo/print for the status string and performance, not the exit code. If the state was getting received correctly, that '1' would make the object report a warning state, but as it is still green, I assume you echoed it instead of exited with it. Can you post the script?
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
jimmlegs
Posts: 49
Joined: Thu Jun 20, 2013 11:50 am

Re: NRPE Status Appears in the "Status Information" Field

Post by jimmlegs »

Thanks, one thing I forgot to mention in the original post is that I am using the same code for the Exchange check that works.

$NagiosStatus = "0"
$NagiosDescription = ""
$date = get-date

$sqlServer = "***"
$dbUser = "*****"
$dbPassword = "****"
$DB = "msdb"
$Qry = "select `secondary_server`,` primary_server`, `last_copied_date`, `last_restored_latency` from log_shipping_monitor_secondary"

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source = $sqlServer;Initial Catalog=$DB;uid=$dbUser;pwd=$dbPassword"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $Qry
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
Clear
$Results = $DataSet.Tables[0]

$primary_server = $Results | select primary_server
$secondary_server = $Results | select secondary_server
$last_copied_date = $Results | select last_copied_date | % {$_.last_copied_date }
$last_restored_latency = $Results | select last_restored_latency
$last_copied_date = $last_copied_date.ToString()

if ($date.AddMinutes(-16) -gt $last_copied_date) {
if ($NagiosDescription -ne "") {
$NagiosDescription = $NagiosDescription + ", "}
$NagiosDescription = $NagiosDescription + "Log Shipping Stale. Last Copy Was: $last_copied_date"
# Set the status to failed.
$NagiosStatus = '2'
}
elseif ($date.AddMinutes(-15) -gt $last_copied_date) {
if ($NagiosDescription -ne "") {
$NagiosDescription = $NagiosDescription + ", "}
$NagiosDescription = $NagiosDescription + "Log Shipping Stale. Last Copy Was: $last_copied_date"
# Set the status to failed.
$NagiosStatus = '1'
}
else {
$NagiosDescription = $NagiosDescription = "Log Shipping: Last Copy Was: $last_copied_date"
$NagiosStatus = '0'
}

$NagiosDescription = $NagiosDescription.ToString()
$NagiosStatus = $NagiosStatus.ToString()

if ($NagiosStatus -eq '2') {
Write-Host "CRITICAL: " $NagiosDescription
}
elseif ($NagiosStatus -eq '1') {
Write-Host "WARNING: " $NagiosDescription
}
else {
Write-Host "OK: Log Shipping: Last Copy Was:" $last_copied_date

}

exit $NagiosDescription
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: NRPE Status Appears in the "Status Information" Field

Post by abrist »

Should the exit code be the $NagiosDescription? Looking at the script, you would assume it should be $NagiosStatus.

Code: Select all

exit $NagiosStatus
Additionally, Write-Host instances should really be:

Code: Select all

Write-Output
So that it outputs on stdout instead of interactive out.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
jimmlegs
Posts: 49
Joined: Thu Jun 20, 2013 11:50 am

Re: NRPE Status Appears in the "Status Information" Field

Post by jimmlegs »

I've tries it with $NagiosStatus as well and get the same result. Even if I take out the Exit line it returns the same output. Let me play around with write-output and I will report the results back to this post.

Thanks a bunch!
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: NRPE Status Appears in the "Status Information" Field

Post by abrist »

No problem! I noticed a couple more issues:

Code: Select all

$NagiosDescription = $NagiosDescription + "Log Shipping Stale. Last Copy Was: $last_copied_date"
The above code should really use += as well:

Code: Select all

$NagiosDescription += "Log Shipping Stale. Last Copy Was: $last_copied_date"
The following line seems wrong:

Code: Select all

$NagiosDescription = $NagiosDescription = "Log Shipping: Last Copy Was: $last_copied_date"
Should it not be:

Code: Select all

$NagiosDescription += "Log Shipping: Last Copy Was: $last_copied_date"
?
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
jimmlegs
Posts: 49
Joined: Thu Jun 20, 2013 11:50 am

Re: NRPE Status Appears in the "Status Information" Field

Post by jimmlegs »

You're correct with your code corrections but I'm still returning the status code before the description. I have to run out of the office for now but will try to get back into this later on tonight. I'm wondering if PowerShell is doing something funky with the objects being returned to the script. I'll check out the method of the commands.
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: NRPE Status Appears in the "Status Information" Field

Post by abrist »

Alright, let me know.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
jimmlegs
Posts: 49
Joined: Thu Jun 20, 2013 11:50 am

Re: NRPE Status Appears in the "Status Information" Field

Post by jimmlegs »

So far nada. I've converted it to the function and get the same behavior, I've also tried writing to and then reading from an external file. Something I did notice in the debug log of the client is that NRPE appears to be injecting a CRLF after the 1

2013-11-22 09:52:12: debug:NSClient++.cpp:1106: Injecting: check_log_shipping:
2013-11-22 09:52:13: debug:NSClient++.cpp:1142: Injected Result: OK '1
OK: Log Shipping: Last Copy Was:
11/22/2013 9:45:01 AM'

Whereas another PowerShell script that is functioning properly does not:

2013-11-22 09:31:23: debug:NSClient++.cpp:1106: Injecting: check_replication_health:
2013-11-22 09:31:33: debug:NSClient++.cpp:1142: Injected Result: OK 'OK: All mail queues within limits.'

It does appear to be related to the .net sqlclient data provider, I've been pulling out code and the 1 with the line break only appears when the SQL calls are present.

Regards,

--DG



Regards,

DG
jimmlegs
Posts: 49
Joined: Thu Jun 20, 2013 11:50 am

Re: NRPE Status Appears in the "Status Information" Field

Post by jimmlegs »

It breaks here:

$SqlAdapter.Fill($DataSet)
Locked