Issues while running Powershell script in Nagios xi

This support forum board is for support questions relating to Nagios xi, our flagship commercial network monitoring solution.
RIDS_I2MP
Posts: 751
Joined: Thu Mar 13, 2014 9:25 am

Issues while running Powershell script in Nagios xi

Post by RIDS_I2MP »

Hi Team,

We have written a Powershell script which has a parameter to be passed while running it as we have declared param($list) in the first line of the script.

While we run it through Powershell manually we run it like this path\Untitled2.ps1 @('xxxx').

Now we have recorded its entry in the Nagios.cfg file as

check_users=cmd /c echo scripts\Untitled2.ps1 $ARG1$; exit($lastexitcode) | powershell.exe -command - and have restarted nsc client ++ service as well.

The problem is the parameter which we are passing that is @('xxx') or @('xx','yyy') is not getting passed in Nagios.

we are running the script in Nagios as below:
/usr/local/nagios/libexec- ./check_nrpe -2 -H -xxxxx -c check_users - a '@('xxx','yyy')' but not luck.

Can you suggest a solution please for this.
Thanks & Regards,
I2MP Team.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Issues while running Powershell script in Nagios xi

Post by ssax »

I'd call it like this, works from the Backend (after apply)/CCM/CLI on mine:

Code: Select all

/usr/local/nagios/libexec/check_nrpe -H X.X.X.X -t 30 -c check_test -a '@("xxxx")'
This is the script I tested with:

check_test.ps1

Code: Select all

$array = $args[0]
for ($i=0; $i -lt $array.length; $i++) {
    write-host $array[$i]
}
exit(0)
And here is the command:

Code: Select all

[/settings/external scripts]
allow arguments = true
allow nasty characters=true

; External scripts - A list of scripts available to run from the CheckExternalScripts module. Syntax is: `command=script arguments`
[/settings/external scripts/scripts]
check_test=cmd /c echo scripts\check_test.ps1 $ARGS$; exit($lastexitcode) | powershell.exe -executionpolicy bypass /noprofile -command -
RIDS_I2MP
Posts: 751
Joined: Thu Mar 13, 2014 9:25 am

Re: Issues while running Powershell script in Nagios xi

Post by RIDS_I2MP »

Hi Team,

I have tried the way you suggested but it says Unknown : Could not construct return packet in NRPE Handler check client side(nsclient.logs)

But instead if instead of double quotes if i use single quotes it gives me the output but with wrong results that is the argument which I am passing is not taken into account.

Kindly suggest another possible solution please.

Same script if I run manually in powershell it works well with the argument. What wrong am i doing ?
Thanks & Regards,
I2MP Team.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Issues while running Powershell script in Nagios xi

Post by ssax »

Please attach your nsclient.ini, your nsclient.log file, and your powershell script so I can take a look.

Additionally, what version of nsclient are you running? You can see it next to the program in add/remove programs in the control panel.
RIDS_I2MP
Posts: 751
Joined: Thu Mar 13, 2014 9:25 am

Re: Issues while running Powershell script in Nagios xi

Post by RIDS_I2MP »

Hello Team,

We are using the below attached version of NSC Client 0.3.9 and PFA requested data. I have named script as Untitled2 and NSC.ini file.

Please let me know where to find nsclient.log file.
You do not have the required permissions to view the files attached to this post.
Thanks & Regards,
I2MP Team.
RIDS_I2MP
Posts: 751
Joined: Thu Mar 13, 2014 9:25 am

Re: Issues while running Powershell script in Nagios xi

Post by RIDS_I2MP »

Attaching nscclient.log file
You do not have the required permissions to view the files attached to this post.
Thanks & Regards,
I2MP Team.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Issues while running Powershell script in Nagios xi

Post by ssax »

This is an issue with your script but it's very hard to read what it's doing, try this one instead:

Code: Select all

param($list)
$logged_on_users = quser | Select-Object -Skip 1 | ForEach-Object {
	$CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s'
	if ($list.Contains($CurrentLine[0])) {
		$unauthorized_list += $CurrentLine[0]
	}
}

if ($unauthorized_list.Count -gt 0) {
	$unauthorized_users = [string]::join(",", $unauthorized_list)
	Write-Host "CRITICAL: Unauthorized users detected: ($unauthorized_users) | unauthorized_users=$($unauthorized_list.Count)"
	exit 2
} else {
	Write-Host "OK: No unauthorized users detected. | unauthorized_users=$($unauthorized_list.Count)"
	exit 0
}
/usr/local/nagios/libexec/check_nrpe -2 -H 192.168.X.X -c check_test -a '@("administrator", "test")'

Code: Select all

[root@xid libexec]# /usr/local/nagios/libexec/check_nrpe -2 -H 192.168.X.X -c check_test -a '@("administrator")'
CRITICAL: Unauthorized users detected: (administrator) |'unauthorized_users'=1

Code: Select all

[root@xid libexec]# /usr/local/nagios/libexec/check_nrpe -2 -H 192.168.X.X -c check_test -a '@("administrator-nomatch", "anotheruser")'
OK: No unauthorized users detected. |'unauthorized_users'=0
RIDS_I2MP
Posts: 751
Joined: Thu Mar 13, 2014 9:25 am

Re: Issues while running Powershell script in Nagios xi

Post by RIDS_I2MP »

Hi Team,

I have tried using the script via method which you suggested but no luck , we are still getting the same error.

Instead of double quotes for the arguments after @ I tried giving single quotes so after this the error vanished and we got the output but the parameter didn't pass through the script.

This is happening for your script as well as my script. So naturally the script doesn't have any problem the problem lies somewhere else.
Perhaps instead of Nagios if I use Powershell ISE it works well.(both the scripts)

Kindly suggest us further steps to resolve this issue.

JFYI....The Nagios xi Instance which we are using has a Standard License and not Enterprise License does this make an impact ?
Thanks & Regards,
I2MP Team.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Issues while running Powershell script in Nagios xi

Post by ssax »

JFYI....The Nagios xi Instance which we are using has a Standard License and not Enterprise License does this make an impact ?
It has no impact, see here for the differences:

https://www.nagios.com/products/nagios- ... omparison/

Please edit your NSC.ini and change this:

Code: Select all

[NRPE]
;allow_nasty_meta_chars=1
To this:

Code: Select all

[NRPE]
allow_nasty_meta_chars=1
Then restart the nsclient++ service and try again.
RIDS_I2MP
Posts: 751
Joined: Thu Mar 13, 2014 9:25 am

Re: Issues while running Powershell script in Nagios xi

Post by RIDS_I2MP »

Thanks a lot after it worked after making those changes suggested by you and also modifying my nsc.ini file in the definition by writing $ARG1$ $ARG2$ instead of $ARGS$.

But now it is not working in another server where I am doing all those similar things as what i did on that server where I am getting the required output.

Can you please tell me what wrong I am doing ?
Attaching all the required things for you.
You do not have the required permissions to view the files attached to this post.
Thanks & Regards,
I2MP Team.