Page 1 of 2

NCPA with python3

Posted: Mon Sep 27, 2021 7:58 am
by elade
Hi,

I deployed a new NCPA (2.3.1) agent on my linux machine (ubuntu 20.04) and I'm having trouble to get the agent run python3 scripts.
This is my configuration:
ncpa.cfg

Code: Select all

# Linux / Mac OS X
.sh = /bin/sh $plugin_name $plugin_args
.py = python3 $plugin_name $plugin_args
.sh-sudo = sudo /bin/sh $plugin_name $plugin_args
.py3-sudo = sudo python3 $plugin_name $plugin_args
ncpa.cfg.d/nagios.cfg

Code: Select all

.py3-sudo = sudo python3 /usr/local/ncpa/plugins/myscript_python3.py
sudoers

Code: Select all

nagios ALL = NOPASSWD:/usr/bin/python3 /usr/local/ncpa/plugins/*.py3-sudo
From some reason the check_ncpa.py command is trying to run the script as python2 and not python3.
I tried to change the check_ncpa.py from #!/usr/bin/env python to #!/usr/bin/env python3 didn’t work.

Any idea what's wrong?
Thank you!

Re: NCPA with python3

Posted: Mon Sep 27, 2021 4:11 pm
by gsmith
HI

Please run the following from the CLI and post the results:

Code: Select all

whereis python

ls -l /usr/bin/python*
Thanks

Re: NCPA with python3

Posted: Wed Sep 29, 2021 2:04 am
by elade
whereis python

Code: Select all

python: /usr/bin/python3.5 /usr/bin/python2.7-config /usr/bin/python2.7 /usr/bin/python /usr/bin/python3.5m-config /usr/bin/python3.5-config /usr/bin/python3.hide /usr/bin/python3.5m /usr/lib/python3.5 /usr/lib/python2.7 /etc/python3.5 /etc/python2.7 /etc/python /usr/local/lib/python3.5 /usr/local/lib/python2.7 /usr/include/python3.5 /usr/include/python2.7 /usr/include/python3.5m /usr/share/python /usr/share/man/man1/python.1.gz
ls -l /usr/bin/python*

Code: Select all

lrwxrwxrwx 1 root root       9 Nov 23  2017 /usr/bin/python -> python2.7
lrwxrwxrwx 1 root root       9 Nov 23  2017 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 3492656 Dec  4  2017 /usr/bin/python2.7
lrwxrwxrwx 1 root root      33 Dec  4  2017 /usr/bin/python2.7-config -> x86_64-linux-gnu-python2.7-config
lrwxrwxrwx 1 root root      16 Nov 23  2017 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root      18 Apr  8  2019 /usr/bin/python3 -> /usr/bin/python3.5
-rwxr-xr-x 2 root root 4456208 Jan 26  2021 /usr/bin/python3.5
lrwxrwxrwx 1 root root      33 Jan 26  2021 /usr/bin/python3.5-config -> x86_64-linux-gnu-python3.5-config
-rwxr-xr-x 2 root root 4456208 Jan 26  2021 /usr/bin/python3.5m
lrwxrwxrwx 1 root root      34 Jan 26  2021 /usr/bin/python3.5m-config -> x86_64-linux-gnu-python3.5m-config
lrwxrwxrwx 1 root root      16 Mar 23  2016 /usr/bin/python3-config -> python3.5-config
lrwxrwxrwx 1 root root       9 Apr 29  2018 /usr/bin/python3.hide -> python3.5
lrwxrwxrwx 1 root root      10 Apr 29  2018 /usr/bin/python3m -> python3.5m
lrwxrwxrwx 1 root root      17 Mar 23  2016 /usr/bin/python3m-config -> python3.5m-config
lrwxrwxrwx 1 root root      16 Nov 23  2017 /usr/bin/python-config -> python2.7-config

Re: NCPA with python3

Posted: Wed Sep 29, 2021 5:12 pm
by gsmith
Hi,

On the remote machine (the machine you want to monitor, not the Nagios server)
can you run the following please:

Code: Select all

sudo /usr/bin/python3 /usr/local/ncpa/plugins/myscript_python3.py
sudo /usr/bin/python3.5 /usr/local/ncpa/plugins/myscript_python3.py
Thanks

Re: NCPA with python3

Posted: Sun Oct 03, 2021 1:56 am
by elade
Hi,

Both scripts return OK message.

Re: NCPA with python3

Posted: Mon Oct 04, 2021 10:05 am
by gsmith
Hi
Update your files as follows:

ncpa.cfg

# Linux / Mac OS X
.sh = /bin/sh $plugin_name $plugin_args
.py = python3 $plugin_name $plugin_args
.sh-sudo = sudo /bin/sh $plugin_name $plugin_args REMOVE THIS
.py3-sudo = sudo python3 $plugin_name $plugin_args REMOVE THIS



ncpa.cfg.d/nagios.cfg
.py3-sudo = sudo python3 /usr/local/ncpa/plugins/myscript_python3.py REMOVE THIS


sudoers
change from:
nagios ALL = NOPASSWD:/usr/bin/python3 /usr/local/ncpa/plugins/*.py3-sudo
to:
nagios ALL = NOPASSWD:/usr/bin/python3 /usr/local/ncpa/plugins/*.py3


After you make the changes remember to restart the ncpa service on the remote machine.

Thanks

Re: NCPA with python3

Posted: Tue Oct 05, 2021 2:44 am
by elade
Hi,

This is my update cfg files:

Code: Select all

ncpa.cfg
# Linux / Mac OS X
.sh = /bin/sh $plugin_name $plugin_args
.py = python3 $plugin_name $plugin_args
#.sh-sudo = sudo /bin/sh $plugin_name $plugin_args
#.py3-sudo = sudo python3 $plugin_name $plugin_args

Code: Select all

nagios.cfg
.sh-sudo = sudo /bin/sh /usr/local/ncpa/plugins/check_docker_service
#.py3-sudo = sudo python3 /usr/local/ncpa/plugins/check_docker_status.py
sudoers file

Code: Select all

nagios ALL = NOPASSWD:/bin/sh /usr/local/ncpa/plugins/*.sh-sudo
nagios ALL = NOPASSWD:/usr/bin/python3 /usr/local/ncpa/plugins/*.py3
Still I get the same message after restart ncpa service.

From Nagios GUI:

Code: Select all

/usr/local/nagios/libexec/check_ncpa.py -H X.Y.Z.D -t 'Abcd' -P 5693 -M 'plugins/my_script_p3.py'
Traceback (most recent call last):
File "/usr/local/ncpa/plugins/my_script_p3.py", line 7, in <module>
import docker
ImportError: No module named docker
From remote Ubuntu machine:

Code: Select all

root@ubuntu:/usr/local/ncpa/plugins# /usr/bin/python3 /usr/local/ncpa/plugins/my_script_p3.py
All containers are OK

Re: NCPA with python3

Posted: Tue Oct 05, 2021 4:33 pm
by pbroste
Hello @elade

Thanks for following up, want to update the python path on 'my_script_p3.py' to:

Code: Select all

#!//usr/bin/python3
Please let us know the results,
Perry

Re: NCPA with python3

Posted: Thu Oct 07, 2021 7:25 am
by elade
The script's python path start with #!/usr/bin/python3

Re: NCPA with python3

Posted: Thu Oct 07, 2021 4:52 pm
by ssax
You would need to do this in your ncpa.cfg:

Code: Select all

[plugin directives]
run_with_sudo = my_script_p3.py

Code: Select all

.py = /usr/bin/python3 $plugin_name $plugin_args
Taken from here:

https://github.com/NagiosEnterprises/nc ... cfg.sample

/etc/sudoers:

Code: Select all

nagios ALL=(ALL) NOPASSWD:/usr/local/ncpa/plugins/my_script_p3.py
Then restart the service:

Code: Select all

systemctl restart ncpa_listener
Then test:

Code: Select all

/usr/local/nagios/libexec/check_ncpa.py -H X.X.X.X -t 'yourtoken' -P 5693 -M plugins/my_script_p3.py
Let us know the results.