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.
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 »

Interesting. Could you post the current version of 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 »

Here it is with the function (the text file output is no longer in my buffer).

$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()

function runStatus {
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 += "Log Shipping: Last Copy Was: $last_copied_date"
$NagiosStatus = '0'
}

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

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

}

exit $NagiosStatus
}
runStatus

EDIT: Wrapping in code tabs and "prettying" it up - tmcdonald

Code: Select all

$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()

function runStatus {
	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 += "Log Shipping: Last Copy Was: $last_copied_date"
		$NagiosStatus = '0'
	}

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

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

	exit $NagiosStatus
}
runStatus
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

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

Post by sreinhardt »

I'm seeing some pretty big things that need to be cleaned up. give me a chance to work on this tonight and I will get back to you with something that works properly and an explanation of what I changed.
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
jimmlegs
Posts: 49
Joined: Thu Jun 20, 2013 11:50 am

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

Post by jimmlegs »

Thanks a mil! I really appreciate it.
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

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

Post by sreinhardt »

Code: Select all

[int]$NagiosStatus = 0
[string]$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()
$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 += ", "
        } # end if nagDesc
    $NagiosDescription += "CRITICAL: Log Shipping Stale. Last Copy Was: $last_copied_date"
    # Set the status to failed.
    $NagiosStatus = 2
} # end if date compare

elseif ($date.AddMinutes(-15) -gt $last_copied_date) {
    if ($NagiosDescription -ne "") {
        $NagiosDescription += ", "
    } #end if nagDesc
    $NagiosDescription += "WARNING: Log Shipping Stale. Last Copy Was: $last_copied_date"
    # Set the status to failed.
    $NagiosStatus = 1
} # end elseif date compare

else {
    if ($NagiosDescription -ne "") {
        $NagiosDescription += ", "
    } #end if nagDesc
    $NagiosDescription += "OK: Log Shipping: Last Copy Was: $last_copied_date"
    $NagiosStatus = 0
} # end else no compare

Write-Output $NagiosDescription
exit $NagiosStatus
cast nagiosdescription to type string, and nagiosstatus to type int.
removed function, functions are great when reusing code, in this case its not helpful as its only called once.
first if was not closed prior to the elseif statement for additional date comparison, i think this was how you intended it.
removed ' or " around areas where nagiosstatus is set, this could set it as a string instead of int, same for around -eq checks as it is technically checking a string not numerically.
added the critical\warning\ok statements within your normal string additions

removed the if\else statements for warnings, considering this is only called once there is no reason not to include it in your date checking logic and avoid possible other issues. This would also be useful if you are going to add this to a function that is called repeatedly, in the case that one or more items are failing while the others are fine.

I left them in, but i see no need for checking if the nagiosdescription variable is empty, you would only ever hit one if\elseif\else statement that would add to it, but I left it looking towards the future that you might want to use this on several databases\tables in the same plugin.

some notes for powershell:
numbers never need to be compared with quotes
when adding to the end of a string, += is always easiest such as $string += "text to add" this avoids the need to do $string = $string+"text to add"
there is no need to call .toString() on something that is already a string :)
it helps to cast variables as the type you want when creating them so that you can quickly reference what they are.
as with all languages, comment, comment, comment, while I could understand most of what you are doing, I had to make a few assumptions based on how the rest of the code works without comments.
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

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

Post by sreinhardt »

To separate code corrections from suggestions, if you are having issues around "$SqlAdapter.Fill($DataSet)" I would suggest opening a standard powershell cli, running ALL commands up to and including that line, then play around with it from there. Specifically try

Code: Select all

$DataSet | get-members
$DataSet.getType()
write-host $DataSet
This way you can see what objects and methods the dataset object contains, what type of object it is(mainly for googling around alternatives for working with it), and what just outputting dataset contains. I find these three pretty insightful when I'm having issues with PS objects.

edit: BTW, you're welcome, I hope it helps! powershell is a great language for windows, but still has its drawbacks.
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
jimmlegs
Posts: 49
Joined: Thu Jun 20, 2013 11:50 am

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

Post by jimmlegs »

Thanks for all of the guidance! I'll bang around on them and get as deep into the analysis as possible. I would like to get more in-depth with PowerShell but alas, the life of a generalist with no time to specialize :)

I really do appreciate the assistance.

Regards,

--DG
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

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

Post by slansing »

Great! Let us know what you come up with.
jimmlegs
Posts: 49
Joined: Thu Jun 20, 2013 11:50 am

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

Post by jimmlegs »

It requires piping the dataset to out-null:

$SqlAdapter.Fill($DataSet) | Out-Null

Argh!

Thanks a bunch for the pointers.

--Jimmlegs
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

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

Post by sreinhardt »

OH so thats whats causing that extra line up top of your nagios results?
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
Locked