Page 1 of 1

Powershell script result is not completed display on nagios

Posted: Mon Dec 09, 2019 11:39 am
by kunalv30

Code: Select all

clear-host

Add-PSSnapin -Name sqlserverprovidersnapin100 -ErrorAction SilentlyContinue
Add-PSSnapin -Name sqlservercmdletsnapin100 -ErrorAction SilentlyContinue
try
{

$DBservers = ('ServerFQDN')
#Clear-Variable -name Alert -Scope Global
#$DBservers = ('ServerFQDN','ServerFQDN\SQL2017')
foreach ($DBServer in $DBservers)
{

$QueryText1 = "select name from sys.databases where state_desc = 'online'"

$DBname = Invoke-SQLCMD $QueryText1 -ServerInstance $DBServer
if ($DBname -eq $null)
{
$QueryText4 = "select '$DBServer' as SQLInstance, name as DB_name, state_desc from sys.databases where state_desc <> 'online' and state_desc <> 'RESTORING' "
$DB = Invoke-SQLCMD $QueryText4 -ServerInstance $DBServer

if ($DB.name -eq $null)
{

}
else
{
$R3 = "Issue found, below are the details" 
Write-Host "Issue found, below are the details" 
$DB | Select * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors | Format-Table | Out-File $output -append
$Alert  = '1'
}

}
else
{
$databases = $DBname.Name
$R3 = "Issue found, below are the details" 
foreach ($database in $databases)
{
$QueryText2 = "select '$DBServer' as SQLInstance, name as DB_name, state_desc from sys.databases where state_desc = 'online' and  name = '$database' "

$Result = Invoke-SQLCMD $QueryText2 -ServerInstance $DBServer 

If ($Result -eq $null) 
{
exit 3
}
Else
{

$Result | Select * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors 
$Result
#| Out-File -FilePath $output -append

#| Format-Table 
$Alert  = '1'
}
}

}
}
If ($Alert -eq '1')
{
$R2 = "Problem occured" 
exit 2
}
else
{

$R4 = "No Issue:All Databases Are Online" 
exit 0
}
}
catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Host "Error:$ErrorMessage $FailedItem" 

break
}
Hello Folks,
I'm struggling to print the complete result of powershell output on nagios, i made a powershell script to show the databases status but result displayed on nagios of only 13 lines, actually result have 21 lines.
Note: I have already set the timeout of 180 sec on both nsclinet.ini and check_nrpe side.
command alias:
check_db_status = cmd //T:180 /c echo scripts\DB_Status.ps1; exit $LastExitCode | powershell.exe -command -

Powershell script as shared below, Please suggest

Re: Powershell script result is not completed display on nag

Posted: Mon Dec 09, 2019 12:10 pm
by lmiltchev
How many characters total are shown in the output? Perhaps you would need to update the db so that it could store more information. See our KB article on the topic below:

https://support.nagios.com/kb/article/n ... s-478.html

Re: Powershell script result is not completed display on nag

Posted: Tue Dec 10, 2019 7:45 am
by kunalv30
@lmiltchev;

I have applied the changes which you have shared and same has been affected too, but i'm getting the same result. No improvement.
Changes :-
Before Database update:-
field,type
output;varchar(255)
long_output;varchar(8192)
perfdata;varchar(255)

After:-
field,type
output; mediumtext
long_output; text
perfdata;text

Re: Powershell script result is not completed display on nag

Posted: Tue Dec 10, 2019 10:00 am
by lmiltchev
When you said:
...but result displayed on nagios of only 13 lines, actually result have 21 lines.
you meant the output you see under the "Service Status Detail", correct? Can you show us a screenshot?

Do you see all 21 lines, when you run the check from the command line?

Example:

Code: Select all

/usr/local/nagios/libexec/check_nrpe -H <ip address> -t 60 -c check_db_status

Re: Powershell script result is not completed display on nag

Posted: Wed Dec 11, 2019 8:35 am
by kunalv30
Powershellresult.PNG
CLIresult.PNG
Hello lmiltchev,

I'm still trying on command line and once it'll show the completed output, i'll go on GUI. Facing issue at Command line.
PFA both the screenshots, 1. Command Line 2. Windows server Powershell script output.

Re: Powershell script result is not completed display on nag

Posted: Wed Dec 11, 2019 8:47 am
by lmiltchev
Try the following:

1. Modify your nsclient.ini file by setting the "payload length" value under the [/settings/default] and [/settings/NRPE/client/targets/default] sections to 8192.

Code: Select all

payload length = 8192
Restart the nsclient++ service, so that changes can take effect.

2. Test your check again from the CLI on the Nagios XI server by using the "-P" flag (--payload-size=SIZE).

Example:

Code: Select all

/usr/local/nagios/libexec/check_nrpe -H <ip address> -t 180 -P 8192 -c check_db_status
Did you get larger output this time?

Re: Powershell script result is not completed display on nag

Posted: Fri Dec 13, 2019 7:35 am
by kunalv30
Hello lmiltchev,

I have done the following changes, but no luck.
Output which i gets, attachments for reference

Re: Powershell script result is not completed display on nag

Posted: Fri Dec 13, 2019 9:16 am
by lmiltchev
Can you post your nsclient.ini file on the forum? Obfuscate sensitive info. Thanks!