NagiosEnterprises / ncpa

Nagios Cross-Platform Agent
Other
177 stars 95 forks source link

how do i call plugin scripts with extensions? #932

Closed tonyguadagno closed 1 year ago

tonyguadagno commented 1 year ago

hi, on this page https://support.nagios.com/kb/article/nagios-xi-using-scripts-plugins-with-ncpa-722.html the nagios docs give this example:

Test PowerShell Script From Nagios XI
The last step is to test the script from your Nagios XI server. Open an SSH session to Nagios XI and execute the following:

/usr/local/nagios/libexec/check_ncpa.py -H your_windows_server_ip_address -t your_windows_server_ncpa_token -P 5693 -M 'plugins/check_dummy.ps1' -q "args=2 Something_is_CRITICAL"

note that the plugin script has a .ps1 extension.

when i try running a script with any extension, i get this error:

[nagios@nagios ~]$ /usr/local/nagios/libexec/check_ncpa.py -H ip -t token -P 5693 -M 'plugins/phpstatus.ps1'
UNKNOWN: An error occured connecting to API. (HTTP error: '500 INTERNAL SERVER ERROR')

if i take the extension off, it executes...... to be clear, I have this issue on any type of script...so script.sh does not work but script does.

so, how do i run a plugin script that has an extension?

thanks

MrPippin66 commented 1 year ago

I assume this is on Windows. Is the extension upper or lower case. There should be a known issue if you use lower case, where for now the extension needs to be in upper case.

tonyguadagno commented 1 year ago

sorry, this is not windows. it is rhel linux and it is not just powershell scripts. any script with an extension will not work. this seems to be contradicted by the documentation.

thx

ericloyd commented 1 year ago

Have you installed the PowerShell release for your Linux distribution?

What happens if you log into the machine in question as the user running the NCPA Listener (which is likely nagios) and execute the command you're trying to run through the agent:

/usr/local/ncpa/plugins/check_dummy.ps1 2 Something_is_CRITICAL

If it doesn't work at the command line, it won't work through the agent.

MrPippin66 commented 1 year ago

PowerShell core is cross platform.

@tonyguadagno You said ANY extension doesn't work, and I'm not sure what you mean. The default configuration for Linux only supports by default the following:

Defaults:

.sh = /bin/sh $plugin_name $plugin_args

.py = python $plugin_name $plugin_args

.ps1 = powershell -ExecutionPolicy Bypass -File $plugin_name $plugin_args

.vbs = cscript $plugin_name $plugin_args //NoLogo

.bat = cmd /c $plugin_name $plugin_args

I've validated .sh works, for instance.

But...'.ps1' would be specific to Windows and you'd need to edit the config to correctly invoke powershell scripts on Linux.

tonyguadagno commented 1 year ago

hi, powershell is on linux and works really well (https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-linux?view=powershell-7.3#red-hat-enterprise-linux-rhel) I am able to run the script on the command line. also, ncpa uses a user called 'nagios'. that user can also run the script.

again, to be clear, this is not a powershell issue. if i try to run a bash script like xyz.bash or xyz.sh that will also fail. anytime the script has an extension, it fails.

tonyguadagno commented 1 year ago

ok, i appologize, i see my mistake. this is a problem specific to powershell, i am able to run scripts with extensions for bash etc. however, my original statement is the same....the documentation clearly states that you can run powershell scripts that way...and i get a 500 error when i try: image

ericloyd commented 1 year ago

When you log in to the target box, su to nagios, and run:

powershell -ExecutionPolicy Bypass -File /usr/local/ncpa/plugins/check_dummy.ps1 2 CRITICAL

What happens? I'm fairly certain your PowerShell is trying to interact in a way that causes the HTTP protocol to break.

tonyguadagno commented 1 year ago

no, that works fine. it is only when called via the api

 sudo su nagios
[sudo] password for tonyg:
[nagios@server tonyg@]$ pwsh -executionpolicy bypass -file /usr/local/ncpa/plugins/phpstatus.ps1
OK: Pool Name: www, ProcMGR: static, Total Procs: 100, IdleProcs: 48, MaxChildReached: 0, SlowReq: 0, ActiveProcs: 52 |ProcessUtilization=52%
[nagios@server tonyg@]$
MrPippin66 commented 1 year ago

You didn't run it the way NCPA is configured to run it. You need to alter the rule for how the Linux version is being invoked

This is my default config.

Windows

.ps1 = powershell -ExecutionPolicy Bypass -File $plugin_name $plugin_args

That's not how you called it above, so it would need to be altered to match (and ncpa_listener recycled)

tonyguadagno commented 1 year ago

hi, i am not sure which difference you are pointing to....first...there is no "powershell" command in linux...on linux, it is "pwsh"..is that what you mean? or are you talking about plugin_args? i have no arguments

MrPippin66 commented 1 year ago

I'm well aware. That's because the entry in question is intended for Windows systems.

That's why it needs to be modified if you intend to run this on Linux systems.

Or..you can create an alias to "pwsh" called "powershell" in the system PATH.

But...probably best way is just change "powershell" to "pwsh" in the ncpa.cfg file for this entry.

tonyguadagno commented 1 year ago

ok, i see now...i never realized that ncpa had definitions for the extensions in its config....sorry for my ignorance. i made this change in /usr/local/ncpa/etc/ncpa.cfg: .ps1 = pwsh -ExecutionPolicy Bypass -File $plugin_name $plugin_args

and the extensions work now. thanks for pointing me in the right direction!