check_docker / NCPA / Python versions

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
aclauss
Posts: 16
Joined: Thu Apr 04, 2019 1:43 pm

check_docker / NCPA / Python versions

Post by aclauss »

Something in our setup recently changed (don't know what, that would be too easy). We have been using this process (https://assets.nagios.com/downloads/nag ... ios-XI.pdf) to monitor docker containers via NCPA and the check_docker.py linked there.

However, we are now getting errors when the check runs:
File "/usr/local/ncpa/plugins/check_docker.py", line 202
print selection_type
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(selection_type)?
I only have limited Python knowledge, but one cause of this is the script was written for Python 2, but is being executed by Python3 at runtime. The machine in question does have both versions installed. Running "python2" gives a 2.7.18 version; "python3" gives 3.8.10. We also have pythonispython2 installed, so running just "python" gives the 2.7.18 environment.

So I guess my question is: How does NCPA decide what version to use when executing the check_docker.py script?

Thanks!
User avatar
pbroste
Posts: 1288
Joined: Tue Jun 01, 2021 1:27 pm

Re: check_docker / NCPA / Python versions

Post by pbroste »

Hello @aclauss

Thanks for reaching out, you are correct that the 'check_docker.py' will only work with python 2. A bug report has been submitted and dev are working on an update to resolve the issue. Here are the Change log sites for plugins: The workaround, for the time being, is to associate the environment path for 'check_docker.py' to use python2x.

Current symbolic path:

Code: Select all

which python
List what ya got:

Code: Select all

ls -la /usr/bin/pyth*
Edit to update 'check_docker.py' with actual directory path to python2....

Code: Select all

vi /usr/local/nagios/libexec/check_docker.py
#!/usr/bin/env python /usr/bin/python2??

# Copyright (c) 2017-2019 Nagios Enterprises, LLC.

# This code uses only standard-library functions available in Python 2.6.6 (the
# default for CentOS 6).........
Thanks,
Perry
aclauss
Posts: 16
Joined: Thu Apr 04, 2019 1:43 pm

Re: check_docker / NCPA / Python versions

Post by aclauss »

Hmm, no immediate luck.

First, output for the commands requested.

Code: Select all

which python
Gives:

Code: Select all

/usr/bin/python

Code: Select all

ls -la /usr/bin/pyth*
Gives:

Code: Select all

lrwxrwxrwx 1 root root       7 Apr 15  2020 /usr/bin/python -> python2
lrwxrwxrwx 1 root root       9 Mar 13  2020 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 3674216 Mar  8  2021 /usr/bin/python2.7
lrwxrwxrwx 1 root root       9 Mar 13  2020 /usr/bin/python3 -> python3.8
-rwxr-xr-x 1 root root 5490488 Nov 26 14:14 /usr/bin/python3.8
-rwxr-xr-x 1 root root    5902 Jan 13  2020 /usr/bin/python3-wsdump
I then edited the check_docker.py. Note - the filepath I edited was the one from the error (/usr/local/ncpa/plugins/check_docker.py), different from the one you had (nagios vs ncpa). The original header line:

Code: Select all

#!/usr/bin/env python
I then tried modifying it with each of the following:

Code: Select all

#!/usr/bin/env python /usr/bin/python
#!/usr/bin/env python /usr/bin/python2
#!/usr/bin/env python /usr/bin/python2.7
However, each gave the same error. Thinking maybe you had a reason for modifying the path at nagios (whcih exists on the nagios server, but not the end-user), I then tried modifying it on the Nagios server as well - no change.

I then tried changing up that syntax altogether and just being:

Code: Select all

#!/usr/bin/python2.7
But unfortunately still no luck there.

Going back to the original commands, I think "which python" indicates it actually SHOULD have been using python v2 by default, even without the explicit path. So maybe it really is something else going on besides the python version?
User avatar
pbroste
Posts: 1288
Joined: Tue Jun 01, 2021 1:27 pm

Re: check_docker / NCPA / Python versions

Post by pbroste »

Hello @aclauss

The header on the 'check_docker.py' should look like this, I accidentally left the extra "python" on there.

Code: Select all

#!/usr/bin/env /usr/bin/python2.7
The command should look like this:

To verify:
/usr/local/nagios/libexec/check_docker.py -V
OK: check_docker.py, Version 1.1.1. See --help for more info.
Thanks,
Perry
aclauss
Posts: 16
Joined: Thu Apr 04, 2019 1:43 pm

Re: check_docker / NCPA / Python versions

Post by aclauss »

Calling it directly from the command line with -V worked exactly as you showed - execution via Nagios/NCPA is still failing though. Some screenshots this time in case I'm missing something dumb.

The top of the file itself:
check_docker - file contents.png
Running that same file from the command line:
check_docker - command line execution.png
Viewing state of the service through NagiosXI:
check_docker - nagios failure.png
You do not have the required permissions to view the files attached to this post.
User avatar
pbroste
Posts: 1288
Joined: Tue Jun 01, 2021 1:27 pm

Re: check_docker / NCPA / Python versions

Post by pbroste »

Hello @aclauss

Looking at the script on the 'check_docker.py' it states that it only works with the standard library in Python2.66.
#!/usr/bin/env python
# Copyright (c) 2017-2019 Nagios Enterprises, LLC.
# This code uses only standard-library functions available in Python 2.6.6 (the
# default for CentOS 6).
Hopefully, dev will get that plugin updated soon.

Thanks,
Perry
aclauss
Posts: 16
Joined: Thu Apr 04, 2019 1:43 pm

Re: check_docker / NCPA / Python versions

Post by aclauss »

If the script in question simply doesn't parse on python 2.7, then I don't think I would have been able to run manually with -V right? I feel like I'm still missing something and NCPA's execution is still using python 3.
aclauss
Posts: 16
Joined: Thu Apr 04, 2019 1:43 pm

Re: check_docker / NCPA / Python versions

Post by aclauss »

To expand on this, it is pretty easily recreatable manually. Invoking with python3 blows up with the same error reported via Nagios, invoking with Python2.7 works.

Code: Select all

aclauss@mattermost:~$ python3 /usr/local/ncpa/plugins/check_docker.py -V
  File "/usr/local/ncpa/plugins/check_docker.py", line 202
    print selection_type
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(selection_type)?
aclauss@mattermost:~$ python2.7 /usr/local/ncpa/plugins/check_docker.py -V
OK: check_docker.py, Version 1.1.1. See --help for more info.
aclauss@mattermost:~$
aclauss
Posts: 16
Joined: Thu Apr 04, 2019 1:43 pm

Re: check_docker / NCPA / Python versions

Post by aclauss »

Bingo. The key ended up being in the NCPA config file (/usr/local/ncpa/etc/ncpa.cfg).

It defines how each script type is handled.

Code: Select all

.py = python3 $plugin_name $plugin_args
So it was bypassing the shebang line at the beginning of the script and explicitly calling the python3 interpreter.

I changed that line to the following.

Code: Select all

.py = $plugin_name $plugin_args
After a restart, NCPA now executes the script itself (not the specific interpreter) which DOES respect the shebang line and now it works.
User avatar
pbroste
Posts: 1288
Joined: Tue Jun 01, 2021 1:27 pm

Re: check_docker / NCPA / Python versions

Post by pbroste »

Hello @aclauss

Excellent find I did not know that the ncpa.cfg defines the exec interpreter path and therefore ignores the shebang line unless you remove it from the config.

Want to go ahead and post the lines in the '/usr/local/ncpa/etc/ncpa.cfg' so others viewing the forum post are aware of the options:
# Extensions for plugins
# ----------------------
# The extension for the plugin denotes how NCPA will try to run the plugin. Use this
# for setting how you want to run the plugin in the command line.
#
# NOTE: Plugins without an extension will be ran in the cmdline as follows:
# $plugin_name $plugin_args
#
# Defaults:
# .sh = /bin/sh $plugin_name $plugin_args
# .py = python $plugin_name $plugin_args
.jar = java -jar $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
#
# Since windows NCPA is 32-bit, if you need to use 64-bit powershell, try the following for
# the powershell plugin definition:
# .ps1 = c:\windows\sysnative\windowspowershell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -File $plugin_name $plugin_args
#

# Linux / Mac OS X
.sh = /bin/sh $plugin_name $plugin_args
.py = python $plugin_name $plugin_args

# Windows
.ps1 = powershell -ExecutionPolicy Bypass -File $plugin_name $plugin_args
.vbs = cscript $plugin_name $plugin_args //NoLogo
.wsf = cscript $plugin_name $plugin_args //NoLogo
.bat = cmd /c $plugin_name $plugin_args
~
See from the defaults (.py = python $plugin_name $plugin_args) it uses the Python that is set in the system PYTHONPATH; the option to set the system path would work as well. But most admins will not want everything set for backward.


Thanks again for posting the follow-up and providing the details on what resolved the issue.
Perry
Locked