Passing arguments to Powershell

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Passing arguments to Powershell

Post by WillemDH »

Hello,

I've made a Powershell script to get the folder size of a DFS directory. As this directory is on a storage appliance we can't monitor for now, I made a Powershell script to do this, but I have some issues passing arguments to Powershell from Nagios.
This is the script:

Code: Select all

Param(
[Parameter(Mandatory=$true)][string]$dfspad,
[int]$warning=20
#[int]$errorlevel
)

$status = 0
echo "level: $warning"


$folderlist = (Get-ChildItem $dfspad -Recurse | Measure-Object -property length -sum)


$sumMB = "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

if (($folderlist.sum / 1MB) -ge $warning) {
	$status = 2
}

if ($status -eq 0) {
	echo "$dfspad is op dit moment $sumMB" 
}
else {
	echo "$dfspad heeft zijn quota overschreden: $sumMB / $warning"
}
exit $status
When calling the script locally from Powershell and giving two arguments, it works. When testing from Nagios XI however, the test keeps running untill I manually close or a timeout occurs. When I try test this, the CCM hangs, I can't click anything in CCM untill I reboot the server... Restarting the service doesn't seem to help.
I made a command check_dfsfoldersize => $USER1$/check_nrpe -H $HOSTADDRESS$ -p 5666 -t 120 -c check_dfs_foldersize -a "$ARG1$" "$ARG2$"
In Nsclient, I configured the external script like this:
check_dfs_foldersize=cmd /c echo scripts\check_dfs_foldersize.ps1 $ARG1$ $ARG2$; exit $LastExitCode | powershell.exe -command -

Allow arguments and allow external commands is on.

When I test, trying to give the DFS path as $ARG1$ and the quota soft limit as $ARG2$, I get no result. Any help is welcome :)
Nagios XI 5.8.1
https://outsideit.net
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

Re: Passing arguments to Powershell

Post by sreinhardt »

What happens if you run the command as the nagios user from the cli? Also about how long does this take to check when running on the local machine? Since dfs is likely a remote system, I just wanted to be sure that it is not taking an exordinarily long time to compute the check. Also I should ask, is the extended amount of time this takes, within the 120 second timeout or beyond, that you have specified?

Code: Select all

/usr/local/nagios/libexec/check_nrpe -H [Hostname\IP] -p 5666 -t 120 -c check_dfs_foldersize -a dfspad (I listed this with only one argument as it seems you have 20 set by default for warning)
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.
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: Passing arguments to Powershell

Post by WillemDH »

hello,

On the local machine the script takes 5 seconds. Trying it with user naios on cli results in nrpe socket timeout (120 sec)

I expect the script to take max 60 seconds or so.

The strange thing is that I had it working a certain moment, but it only passed one argument. After testing a bit with the second argument (warning), I never got it to work again.. I just don't know where I'm going wrong. I think it has something to do with single or double quotes...


Grtz
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: Passing arguments to Powershell

Post by lmiltchev »

What happens, if you change your command definition in NSClient++ from this:

Code: Select all

check_dfs_foldersize=cmd /c echo scripts\check_dfs_foldersize.ps1 $ARG1$ $ARG2$; exit $LastExitCode | powershell.exe -command -
to this:

Code: Select all

check_dfs_foldersize=cmd /c echo scripts\check_dfs_foldersize.ps1 $ARG1$; exit $LastExitCode | powershell.exe -command -
Restart the NSClient++ service, so that the changes can take effect, than test your check from the CLI in nagios:

Code: Select all

/usr/local/nagios/libexec/check_nrpe -H [Hostname\IP] -p 5666 -t 120 -c check_dfs_foldersize -a 'Put your args here, wrapped in single quotes'
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: Passing arguments to Powershell

Post by WillemDH »

Hello,

Thanks for your answer. I managed to get it working from the command line. Testing from the CCM also seems to work. Scheduling a check from Nagios XI however still gives me some errors. Apparently some backslahes are not rendered correctly.

I get the following error:

Cannot find path 'C:\\stad\\namespace\\share' because it does not exist.
In CCM $ARG1$ is '\\\stad\\namespace\\share'

When I put in CCM $ARG1$ as '\\\stad\namespace\share', I get
Get-ChildItem : Cannot find path 'C:\\stadnamespaceshare' because it does not exist.

In CLI however:

/usr/local/nagios/libexec/check_nrpe -H serverhostname -p 5666 -t 120 -c check_dfs_foldersize -a '\\stad\namespace\share 10'
level: 10
dfspad: \\stad\namespace\share
Sum: 9,78 MB
\\stad\namespace\share is op dit moment 9,78 MB

So in CLI all works ok and arguments are passed.. How should I configure CCM or XI so I get the same result?
Nagios XI 5.8.1
https://outsideit.net
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

Re: Passing arguments to Powershell

Post by sreinhardt »

Well first thing I noticed is that in your CCM stuff, it has 3 \'s where as your cli has 2. If you are using the test command functionality for this, I would suggest just letting it run via applying config instead. The test command function has some wacky issues with striping characters that does not directly relate to how the command should look in nagios it's self.
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.
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: Passing arguments to Powershell

Post by WillemDH »

hello,

I tried running the apply config scheduling a check, but still can't get him to accept the correct dfs path.

Get-ChildItem : Cannot find path 'C:\\stadnamespaceshare' because it does not exist.

nagios XI seems to just not see the \'s or see 2 \'s at once...

I've been trying so many different options.. :roll: Nothing seems to work... While the cli still works fine. Should I put the dfs path between single quotes in the service configuration? (Already tried, but doesn't seem to work)

Running out of options...

Ok, to proove what I said about the no \ or 2 \:
I edited the script to echo the variable dfspad
When I use '\\stad\namespace\\share' (using one slash between stad and namespace and two between namespace and share)
this is the result : dfspad: \\stadnamespace\\share

So the big question is how I'm able to submit one slash...
Last edited by slansing on Wed Aug 21, 2013 11:43 am, edited 1 time in total.
Reason: Please edit your previous post to add information/questions if you are the last poster.
Nagios XI 5.8.1
https://outsideit.net
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

Re: Passing arguments to Powershell

Post by sreinhardt »

This is interesting, and really shouldn't be happening, at least adding a \. It does seem to be stripping one from the front of the text, however it seems to leave the middle ones alone. I would again note, that in your example, both parts show double slashes everywhere, but that may just be a copy paste issue. For this particular host, I would suggest trying to simply directly add this path into the nsclient config for this check. Then it shouldn't need any additional escaping and ideally should return exactly what you are looking for.
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.
User avatar
WillemDH
Posts: 2320
Joined: Wed Mar 20, 2013 5:49 am
Location: Ghent
Contact:

Re: Passing arguments to Powershell

Post by WillemDH »

Yes I know this shouldn't be happening. I also can't add the path directly in the nsclient config as we distribute this nsclient.ini automatically and only got two versions for now. As we have several dfs paths to be checked, and these paths will have to be entered by other users as me, I'll have to give the path as a parameter through Nagios.

About my example, it is correct.
'\\stad\namespace\\share' => \\stadnamespace\\share while I expect it to be \\stad\namespace\\share
In relaity of course I need \\stad\namespace\share, but it was just an example..

Double slashes seem to work but single slashes are impossible to send as a parameter... :(
I was hoping someone could give me a quick fix... ;) Should I file a bug report for this?
An option would be to parse the parameter in Powershell and make it what it needs to be, but this just didn't seem a proper solution..
Nagios XI 5.8.1
https://outsideit.net
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

Re: Passing arguments to Powershell

Post by sreinhardt »

OH, I didn't catch before that \\stad\namespace was getting merged to \\stadnamespace. I am not sure how I missed that, as you did point it out at least once. Since I don't see it as having been asked, are your tests via the test command function in the ccm? If so, I would highly suggest putting in the command the exact way that it works in cli, and let nagios run the actual check.
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