Not able to Run Powershell Script using NRPE

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
gormank
Posts: 1114
Joined: Tue Dec 02, 2014 12:00 pm

Re: Not able to Run Powershell Script using NRPE

Post by gormank »

I suggest you look at the command you're running, and read the output from PS, since the \\ may be incorrect on Windows, and PS is throwing an error on the x86 in the path. It seems that PS doesn't like the whitespace in the "Program Files (x86)" dir even though its quoted.
Even when the script runs it has errors.

Code: Select all

C:\>powershell.exe c:\temp\resourcecheck.ps1
Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\temp\resourcecheck.ps1:11 char:63
+ $TotalRAM = (get-WMIObject win32_operatingsystem -computername <<<<  $servername | Measure-Object TotalVisibleMemorySize -sum).sum / 1024
    + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Not able to Run Powershell Script using NRPE

Post by lmiltchev »

@CloudOps
This didn't work as expected. Hit the "Windows" button on your keyboard, type "powershell", and hit "Enter". In the powershell window, navigate to the directory, in which your script is located, then run it, and show the output. We need to make sure it works locally before we test it with NSClient++.

In the powershell window:

Code: Select all

cd "C:\Program Files\NSClient++\plugins"
./resourcecheck.ps1
@gormank
I don't understand why CloudOps needed to move the "resourcecheck.ps1" script from the "default" NSClient++ scripts directory ("C:\Program Files\NSClient++\scripts") to ""C:\Program Files\NSClient++\plugins"... I've used many powershell scripts with NSClient++ and never had to move them to a different directory to make them work. Am I missing something?
Be sure to check out our Knowledgebase for helpful articles and solutions!
gormank
Posts: 1114
Joined: Tue Dec 02, 2014 12:00 pm

Re: Not able to Run Powershell Script using NRPE

Post by gormank »

The original command used a relative path, which apparently, I mistakenly assumed didn't lead to the right scripts dir.
Feel free to move it wherever.
CloudOps
Posts: 88
Joined: Mon Feb 08, 2016 12:52 am

Re: Not able to Run Powershell Script using NRPE

Post by CloudOps »

lmiltchev wrote:@CloudOps
This didn't work as expected. Hit the "Windows" button on your keyboard, type "powershell", and hit "Enter". In the powershell window, navigate to the directory, in which your script is located, then run it, and show the output. We need to make sure it works locally before we test it with NSClient++.

In the powershell window:

Code: Select all

cd "C:\Program Files\NSClient++\plugins"
./resourcecheck.ps1
@gormank
I don't understand why CloudOps needed to move the "resourcecheck.ps1" script from the "default" NSClient++ scripts directory ("C:\Program Files\NSClient++\scripts") to ""C:\Program Files\NSClient++\plugins"... I've used many powershell scripts with NSClient++ and never had to move them to a different directory to make them work. Am I missing something?
Hi lmiltchev,

i have attached the output, but i can see some error with Output.
You do not have the required permissions to view the files attached to this post.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Not able to Run Powershell Script using NRPE

Post by lmiltchev »

Fixing/troubleshooting PS scripts is out of the scope of Nagios support. You will need to find out why the script produces errors - you may have a RPC service disabled, firewall issues, etc. It's hard to say.

Once you get this script working, you will need to set up a command in nsclient.ini file. As you are actually passing arguments, you will need to have something like this (if your script were in the "default" NSClient++ scripts directory):

Code: Select all

myscript = cmd /c echo scripts\resourcecheck.ps1 $ARG1$; exit $LastExitCode | powershell.exe -command -
From Nagios XI:

Code: Select all

/usr/local/nagios/libexec/check_nrpe -H <client ip> -c myscript -a 'usbankdb -memwar 85 -memcrit 90'
Be sure to check out our Knowledgebase for helpful articles and solutions!
gormank
Posts: 1114
Joined: Tue Dec 02, 2014 12:00 pm

Re: Not able to Run Powershell Script using NRPE

Post by gormank »

The path change was my mistake sorry.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Not able to Run Powershell Script using NRPE

Post by lmiltchev »

No problem, gormank. The main hurdle in my opinion would be to find out why the script is outputting errors. I hope there are some PS gurus on the forum. :)
Once the script works as expected, the rest *should* be a breeze.
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: Not able to Run Powershell Script using NRPE

Post by WillemDH »

Hmm, several remarks, questions come to mind. (sry if I'm saying things already told, I didn't go through the whole thread, only looked at the script and your screenshot)

1) You need to provide computername or it won't work (as you noticed in your screenshot)

2) Is the server you are running the script on also the server where you want to check CPU and memory usage on? So it seems you are using usbankdb as computername argument and running it on 172.26.2.13. Is this the ip of usbankdb?
This might work when you execute it with your admin account, but NSClient runs as system, so it will not be able to connect to another computer. It will work when passing the local computername or when you make the nscp service run as an account with permissions on the host your are checking.

3) When querying WMI remotely, an exception needs to be added to the Windows Firewall. You can find this rule in the advanced settings of your Windows firewall. I think it is explained here in this WMI how to from Nagios: https://assets.nagios.com/downloads/nag ... ios-XI.pdf

4) I just think I can see the problem:

Code: Select all

$CPUQueue = (Get-WmiObject -computername shares Win32_PerfRawData_PerfOS_System).ProcessorQueueLength
-computername shares??? I gues this needs to be -computername $servername

I hope the above helps you forward a bit.

Grtz

Willem
Nagios XI 5.8.1
https://outsideit.net
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Not able to Run Powershell Script using NRPE

Post by lmiltchev »

Thanks, Willem! You got it! :)

After replacing "-computername shares" with "-computername $servername", things are looking better.

Code: Select all

[root@localhost ~]# /usr/local/nagios/libexec/check_nrpe -H x.x.x.x -c myscript -a 'xxxxxx -memwar 85 -memcrit 90'
OK: Memory is 19 % used, CPU % is 7 and CPU Queue is 0 |'Memory'=19%;;;0 'CPU'=7%;;;0 'CPU'=0 'Queue'=0;;;0
Be sure to check out our Knowledgebase for helpful articles and solutions!
CloudOps
Posts: 88
Joined: Mon Feb 08, 2016 12:52 am

Re: Not able to Run Powershell Script using NRPE

Post by CloudOps »

lmiltchev wrote:Thanks, Willem! You got it! :)

After replacing "-computername shares" with "-computername $servername", things are looking better.

Code: Select all

[root@localhost ~]# /usr/local/nagios/libexec/check_nrpe -H x.x.x.x -c myscript -a 'xxxxxx -memwar 85 -memcrit 90'
OK: Memory is 19 % used, CPU % is 7 and CPU Queue is 0 |'Memory'=19%;;;0 'CPU'=7%;;;0 'CPU'=0 'Queue'=0;;;0
I have tested on other server too and if i run the script locally i am getting the output but when i am trying from nagios cli , its showing error :

[root@VM-NAGIOSXI-TEST libexec]# ./check_nrpe -H 192.168.30.64 -c topcpu vm-restore-cy1 memwarn 85 memcrit 95 -t 300
CHECK_NRPE: Received 0 bytes from daemon. Check the remote server logs for error messages.

In the nsclient logs its showing :

2016-03-23 14:14:37: error:c:\source\nscp\include\socket/connection.hpp:149: Failed to send data: The file handle supplied is not valid
2016-03-23 14:15:22: error:c:\source\nscp\include\socket/connection.hpp:149: Failed to send data: The file handle supplied is not valid
2016-03-23 15:02:36: error:c:\source\nscp\include\socket/connection.hpp:149: Failed to send data: The file handle supplied is not valid
Locked