NagiosEnterprises / ncpa

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

500 INTERNAL SERVER ERROR in Windows running plugin with capitalized extension (.PS1) #795

Open h3xx opened 3 years ago

h3xx commented 3 years ago

Steps to reproduce in Windows:

(Default ncpa.cfg)

  1. Create a file called C:\Program Files (x86)\Nagios\NCPA\plugins\test.PS1:
echo "Hello world"
exit 0
  1. Call check_ncpa.py (can be done from the WSL):
$ check_ncpa.py -H localhost -t 'password1' -M 'plugins/test.PS1'
UNKNOWN: An error occured connecting to API. (HTTP error: '500 INTERNAL SERVER ERROR')
ncpa_listener.log with debug logging turned on ``` 2021-07-29 12:46:01,151:DEBUG:handler:Initializing WebSocket 2021-07-29 12:46:01,151:DEBUG:handler:Validating WebSocket request 2021-07-29 12:46:01,209:DEBUG:psapi:Imported windowscounters into the API tree. 2021-07-29 12:46:01,209:DEBUG:psapi:Imported windowslogs into the API tree. 2021-07-29 12:46:01,210:DEBUG:pluginnodes:Running process with command line: `C:\Program Files (x86)\Nagios\NCPA\plugins\test.PS1` 2021-07-29 12:46:01,214:ERROR:app:Exception on /api/plugins/test.PS1/ [GET] Traceback (most recent call last): File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request File "C:\ncpa\agent\listener\server.py", line 292, in token_auth_decoration File "C:\ncpa\agent\listener\server.py", line 1099, in api File "C:\ncpa\agent\listener\nodes.py", line 300, in run_check File "C:\ncpa\agent\listener\pluginnodes.py", line 113, in execute_plugin File "C:\Python27\lib\site-packages\gevent\subprocess.py", line 585, in __init__ File "C:\Python27\lib\site-packages\gevent\subprocess.py", line 554, in __init__ File "C:\Python27\lib\site-packages\gevent\subprocess.py", line 898, in _execute_child WindowsError: [Error 193] %1 is not a valid Win32 application 2021-07-29 12:46:01,216:INFO:handler:127.0.0.1 - - [2021-07-29 12:46:01] "GET /api/plugins/test.PS1/?token=password1&check=1 HTTP/1.1" 500 2362 0.065000 ```

Expected: It should execute test.PS1 using the .ps1 handler defined in ncpa.cfg

ccztux commented 3 years ago

On *nix systems the filenames are case sensitive. I am not sure if this is a good idea.

h3xx commented 3 years ago

On *nix systems the filenames are case sensitive. I am not sure if this is a good idea.

Okay, so you're saying we should still leave it open for OS's using case-sensitive file systems to set up a handler for .FOO that's different from .foo. I agree, that follows the principle of least astonishment -- at least if you're aware of case-sensitive file names.

What should happen, then is -- regardless of OS -- if a file handler isn't set up for it, it should fail with an appropriate message instead of 500 INTERNAL SERVER ERROR. Therein lies the bug -- it doesn't fail that way in Windows.

Or we could follow "just make it work" principles and ignore file case. But that would take away some edge-case functionality you describe.

ccztux commented 3 years ago

What should happen, then is -- regardless of OS -- if a file handler isn't set up for it, it should fail with an appropriate message instead of 500 INTERNAL SERVER ERROR. Therein lies the bug -- it doesn't fail that way in Windows.

I agree that NCPA should throw an appropriate message in this case.

ccztux commented 3 years ago

I have tested this with NCPA 2.3.1 on CentOS 7 and check_ncpa.py, Version 1.2.5:

[root@centos7_ncpa_build client]# ./check_ncpa.py -H 10.0.0.85 -t mytoken -M plugins -l
{
    "plugins": [
        "action_RestartMQIPT_Test.sh", 
        "bg.sh", 
        "check_bg_jobs.sh", 
        "check_docker.py", 
        "check_output.sh", 
        "check_output_err.sh", 
        "test.sh"
    ]
}
[root@centos7_ncpa_build client]# ./check_ncpa.py -H 10.0.0.85 -t mytoken -M plugins/test.SH
UNKNOWN: The plugin (test.SH) requested does not exist.

[root@centos7_ncpa_test ~]# tail -f /usr/local/ncpa/var/log/ncpa_listener.log 2021-09-21 21:09:15,972 22379 ERROR Failed to start in dual stack mode: [Errno 97] Address family not supported by protocol
2021-09-21 21:09:15,973 22379 INFO Falling back to IPv4 only
2021-09-21 21:14:51,477 22379 INFO stopped ncpa_listener, version: 2.4.0
2021-09-22 17:45:19,116 1318 INFO started ncpa_listener, version: 2.4.0
2021-09-22 17:45:19,118 1318 INFO Using SSL version TLSv1_2
2021-09-22 17:45:19,125 1318 ERROR Failed to start in dual stack mode: [Errno 97] Address family not supported by protocol
2021-09-22 17:45:19,126 1318 INFO Falling back to IPv4 only
2021-09-22 17:46:01,943 1318 INFO 10.0.0.88 - - [2021-09-22 17:46:01] "GET /api/plugins/?token=mytoken HTTP/1.1" 200 438 0.018998
2021-09-22 17:46:07,552 1318 INFO 10.0.0.88 - - [2021-09-22 17:46:07] "GET /api/plugins/test.SH/?token=mytoken&check=1 HTTP/1.1" 200 315 0.003340

Which version of NCPA and check_ncpa.py are you using?

I will test this on windows the next time.

ccztux commented 3 years ago

Same result on a Windows system:

09:21:45 [user@localhost]:~/tmp$ ./check_ncpa.py -H localhost -t mytoken -M plugins -l
{
    "plugins": [
        "ncpa_test_env.ps1",
        "test.ps1"
    ]
}

09:21:49 [user@localhost]:~/tmp$ ./check_ncpa.py -H localhost -t mytoken -M plugins/test.PS1
UNKNOWN: The plugin (test.PS1) requested does not exist.

2021-09-23 09:21:23,197:INFO:ncpa_windows:Parsed config from: ['C:\\Program Files (x86)\\Nagios\\NCPA\\etc\\ncpa.cfg', 'C:\\Program Files (x86)\\Nagios\\NCPA\\etc\\ncpa.cfg.d\\example.cfg']
2021-09-23 09:21:23,198:INFO:ncpa_windows:Looking for plugins at: C:\Program Files (x86)\Nagios\NCPA\plugins
2021-09-23 09:21:23,198:INFO:ncpa_windows:Using SSL version TLSv1_2
2021-09-23 09:21:45,727:DEBUG:handler:Initializing WebSocket
2021-09-23 09:21:45,727:DEBUG:handler:Validating WebSocket request
2021-09-23 09:21:45,783:DEBUG:psapi:Imported windowscounters into the API tree.
2021-09-23 09:21:45,783:DEBUG:psapi:Imported windowslogs into the API tree.
2021-09-23 09:21:45,785:INFO:handler:127.0.0.1 - - [2021-09-23 09:21:45] "GET /api/plugins/?token=mytoken&check=1 HTTP/1.1" 200 345 0.068000
2021-09-23 09:21:49,542:DEBUG:handler:Initializing WebSocket
2021-09-23 09:21:49,542:DEBUG:handler:Validating WebSocket request
2021-09-23 09:21:49,582:DEBUG:psapi:Imported windowscounters into the API tree.
2021-09-23 09:21:49,584:DEBUG:psapi:Imported windowslogs into the API tree.
2021-09-23 09:21:49,584:INFO:handler:127.0.0.1 - - [2021-09-23 09:21:49] "GET /api/plugins/?token=mytoken HTTP/1.1" 200 293 0.041000
2021-09-23 09:21:56,940:DEBUG:handler:Initializing WebSocket
2021-09-23 09:21:56,940:DEBUG:handler:Validating WebSocket request
2021-09-23 09:21:56,974:DEBUG:psapi:Imported windowscounters into the API tree.
2021-09-23 09:21:56,974:DEBUG:psapi:Imported windowslogs into the API tree.
2021-09-23 09:21:56,976:INFO:handler:127.0.0.1 - - [2021-09-23 09:21:56] "GET /api/plugins/test.PS1/?token=mytoken&check=1 HTTP/1.1" 200 316 0.037000
jomann09 commented 2 years ago

The real issue seems to be that it's trying to run the .PS1 but it does not have a configuration in the ncpa.cfg file for how to run those extensions, so it's failing - as it's not an exe that can be ran on without context. We can add some error handling to this to return a nicer error, essentially the WindowsError: [Error 193] %1 is not a valid Win32 application portion from above that is explaining it cannot run the plugin.