NCPA Powershell Plugin trouble passing arguments

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Post Reply
rfaulkner99
Posts: 5
Joined: Tue Aug 18, 2015 1:01 pm

NCPA Powershell Plugin trouble passing arguments

Post by rfaulkner99 »

I am having trouble passing parameters using a custom powershell plugin from a Windows machine running the NCPA 3.0.0.client.

To help with testing, I wrote the following PS script:
param (
[string]$F='John',
[string]$L='Doe',
[string]$M='E'
)

write-host "Success! $($F) $($M) modified $($L)."
Exit 0


Then, I try passing parameters, initially with Powershell named parameters like -F Joe -L Donuts, but that didn't work at all. Eventually I settled on using positional parameters, but have had issues getting that to work as well.

To test, I am modifying the nrdp.cfg on the Windows machine, restarting the service and then going to http://localhost:5693/gui/checks.

I have tried lots of combinations with varying results. For example:
%HOSTNAME%|TestArgs = plugins/testargs.ps1 --args Joe Donuts S"
returns Joe E Doe

Here are some of my notes, sorry they are a little disorganized. Keep in mind F used to be First, M - Middle, L - Last
-a did not work
--args 'G K B' not working at all
--args 'G,K,B' treats as first argument only
--args '-First G' not working
--args '--First G' not working
--args "-First G" not working
--args "-First 'G'"
--args -First G - specify a system string
--args -First "G" - Same
--args -First 'G' - Same
--args 'G','k' recognized first two params, but included quotes
--args Gar,k combined into 1 parameter.
--args "Gar ak","k" not working at all - Same with single quotes - doesn't seem to like spaces.
Gar,F,Eild - combined
"Gar","F","Eild" - correclty set first to "Gar" but combimed "F","Eild" together for last
"Gar,F,Eild" - combined into first
bbahn
Posts: 244
Joined: Thu Jan 12, 2023 5:42 pm

Re: NCPA Powershell Plugin trouble passing arguments

Post by bbahn »

Hello @rfaulkner99,

For your PowerShell script to process the arguments correctly, you want the CLI input to be

Code: Select all

plugins/plugin.ps1 "arg1" "arg2" "arg3"
To get this behavior as a passive check, you can use

Code: Select all

%HOSTNAME%|TEST = plugins/test.ps1 --args "arg1" --args "arg2" --args "arg3"
You can more intuitively figure out how to properly configure your plugin inputs by first going through the NCPA UI at https://your_NCPA_ip_address:5693/gui/api/ and selecting plugins and pluginname.ps1 and then entering your arguments into the argument box and adding arguments as necessary until you get your desired result.
Actively advancing awesome answers with ardent alliteration, aptly addressing all ambiguities. Amplify your acumen and avail our amicable assistance. Eagerly awaiting your astute assessments of our advice.
rfaulkner99
Posts: 5
Joined: Tue Aug 18, 2015 1:01 pm

Re: NCPA Powershell Plugin trouble passing arguments

Post by rfaulkner99 »

Thanks! That got me further, and the API tip makes testing much easier.

The only thing I am stuck on now is getting it to pass a path with a space. Powershell expects it to be wrapped in quotes, but I have tried double-quoting and using \ as an escape character and neither work.
bbahn
Posts: 244
Joined: Thu Jan 12, 2023 5:42 pm

Re: NCPA Powershell Plugin trouble passing arguments

Post by bbahn »

That is a bug that should be fixed in NCPA 3.1.0.
Actively advancing awesome answers with ardent alliteration, aptly addressing all ambiguities. Amplify your acumen and avail our amicable assistance. Eagerly awaiting your astute assessments of our advice.
rfaulkner99
Posts: 5
Joined: Tue Aug 18, 2015 1:01 pm

Re: NCPA Powershell Plugin trouble passing arguments

Post by rfaulkner99 »

OK, thanks for the information. Also, passing multiple arguments in a passive check using:

Code: Select all

%HOSTNAME%|TEST = plugins/test.ps1 --args "arg1" --args "arg2" --args "arg3"
did not work for me either. When I do that, it only passes the last argument (arg3).

Is that also a bug?
bbahn
Posts: 244
Joined: Thu Jan 12, 2023 5:42 pm

Re: NCPA Powershell Plugin trouble passing arguments

Post by bbahn »

This will work correctly in NCPA 3.1.0, which is scheduled to release very soon.
Actively advancing awesome answers with ardent alliteration, aptly addressing all ambiguities. Amplify your acumen and avail our amicable assistance. Eagerly awaiting your astute assessments of our advice.
rfaulkner99
Posts: 5
Joined: Tue Aug 18, 2015 1:01 pm

Re: NCPA Powershell Plugin trouble passing arguments

Post by rfaulkner99 »

Almost everything seems to be working now with 3.1 except passing double quotes to arguments. I need to pass double quotes to a powershell -path argument - the path has a space and powershell requires it to be wrapped in double quotes - when I pass the argument it passes it with escape characters as show below. This is using the local API in the web gui.

Code: Select all

powershell -ExecutionPolicy Bypass -File C:\\Program Files\\Nagios\\NCPA\\plugins\\check_release_args.ps1 -path '\"F:\\My Folder\\Backup\\Logs\\Release\"' -maxage 24 -filter *.*
Please let me know if there is another way to pass double quotes to the command line argument.
User avatar
jmichaelson
Posts: 241
Joined: Wed Aug 23, 2023 1:02 pm

Re: NCPA Powershell Plugin trouble passing arguments

Post by jmichaelson »

Where did you get the check_release_args script from? is it possible to modify it to handle the escaped quote? As a workaround, it may be possible to use the legacy 8.3 file name (as shown by doing a dir /X in a command prompt) for the directory and bypass the quotes, until we can come up with a better solution.
Please let us know if you have any other questions or concerns.

-Jason
rfaulkner99
Posts: 5
Joined: Tue Aug 18, 2015 1:01 pm

Re: NCPA Powershell Plugin trouble passing arguments

Post by rfaulkner99 »

I wrote the script. I just tried adding some code to mitigate the issue, but it is not working. Here is the code I added, which works from powershell if I feed it a similar looking escaped path. I commented above the line that I added

Code: Select all

$path = '\"F:\\Special Apps\\Backup\\Logs\\Release\"'
#below is the line I added
$path=($path -replace '\\"','')
$filter="*.*"
If ($Path -like '*\') {$LogFilePath = $Path + $Filter}
Else {$LogFilePath = $Path + "\" + $Filter}
$Files=gci $logfilepath
And here is the error I get when running from the web API.

Code: Select all

{
    "returncode": 2,
    "stdout": "\"F:\\Special Apps\\Backup\\Logs\\Release\"\n\"F:\\Special Apps\\Backup\\Logs\\Release\"\ngci : Cannot find drive. A drive with the name '\"F' does not exist.\nAt C:\\Program Files\\Nagios\\NCPA\\plugins\\check_release_args.ps1:17 char:8\n+ $Files=gci $logfilepath\n+        ~~~~~~~~~~~~~~~~\n    + CategoryInfo          : ObjectNotFound: (\"F:String) [Get-ChildItem], Dri \n   veNotFoundException\n    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetC \n   hildItemCommand\n \ngci : Cannot find drive. A drive with the name '\"F' does not exist.\nAt C:\\Program Files\\Nagios\\NCPA\\plugins\\check_release_args.ps1:19 char:11\n+ $LastFile=gci $logfilepath| sort lastwritetime | select -last 1\n+           ~~~~~~~~~~~~~~~~\n    + CategoryInfo          : ObjectNotFound: (\"F:String) [Get-ChildItem], Dri \n   veNotFoundException\n    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetC \n   hildItemCommand\n \nError! 0 files considered, no files match *.*.",
    "cmd": "powershell -ExecutionPolicy Bypass -File C:\\Program Files\\Nagios\\NCPA\\plugins\\check_release_args.ps1 \"F:\\Special Apps\\Backup\\Logs\\Release\""
}
bbahn
Posts: 244
Joined: Thu Jan 12, 2023 5:42 pm

Re: NCPA Powershell Plugin trouble passing arguments

Post by bbahn »

@rfaulkner, it seems your issue is that your wrapping of the path in quotes is breaking your plugin.

I see a few methods to handle this:
  1. Use single quotes
  2. Have your script convert the escaped double quotes to unescaped double quotes (($path -replace '\"', '"'))
Actively advancing awesome answers with ardent alliteration, aptly addressing all ambiguities. Amplify your acumen and avail our amicable assistance. Eagerly awaiting your astute assessments of our advice.
Post Reply