Page 1 of 3
NRPE Status Appears in the "Status Information" Field
Posted: Thu Nov 21, 2013 12:36 pm
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
Re: NRPE Status Appears in the "Status Information" Field
Posted: Thu Nov 21, 2013 1:48 pm
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?
Re: NRPE Status Appears in the "Status Information" Field
Posted: Thu Nov 21, 2013 2:30 pm
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
Re: NRPE Status Appears in the "Status Information" Field
Posted: Thu Nov 21, 2013 3:21 pm
by abrist
Should the exit code be the $NagiosDescription? Looking at the script, you would assume it should be $NagiosStatus.
Additionally, Write-Host instances should really be:
So that it outputs on stdout instead of interactive out.
Re: NRPE Status Appears in the "Status Information" Field
Posted: Thu Nov 21, 2013 3:28 pm
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!
Re: NRPE Status Appears in the "Status Information" Field
Posted: Thu Nov 21, 2013 4:03 pm
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"
?
Re: NRPE Status Appears in the "Status Information" Field
Posted: Thu Nov 21, 2013 5:12 pm
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.
Re: NRPE Status Appears in the "Status Information" Field
Posted: Thu Nov 21, 2013 5:40 pm
by abrist
Alright, let me know.
Re: NRPE Status Appears in the "Status Information" Field
Posted: Fri Nov 22, 2013 10:57 am
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

Injecting: check_log_shipping:
2013-11-22 09:52:13: debug:NSClient++.cpp

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

Injecting: check_replication_health:
2013-11-22 09:31:33: debug:NSClient++.cpp

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
Re: NRPE Status Appears in the "Status Information" Field
Posted: Fri Nov 22, 2013 10:59 am
by jimmlegs
It breaks here:
$SqlAdapter.Fill($DataSet)