Custom Script and Python Version

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Post Reply
ronhow
Posts: 5
Joined: Mon May 25, 2020 4:21 am

Custom Script and Python Version

Post by ronhow »

Hello,

I am attempting to use a custom command to perform a Service Check.
I am added a Python script to /usr/local/nagios/libexec.
I have created the command itself:

Code: Select all

/usr/bin/python3 $USER1$/my_script.py $ARG1$ $ARG2$ $ARG3$

Within my script:

Code: Select all

#!/usr/bin/python3

import requests
However, I will get error:

Code: Select all

module 'requests' has no attribute 'request'
I have installed requests library with pip3 and pip3 list shows I have

Code: Select all

Package            Version
------------------ -----------
requests           2.27.1
I am only suspecting that the script is being run with python2.7 despite me trying to have 3.6 run it.
Can anyone share some pointers, please?
User avatar
lgute
Posts: 420
Joined: Mon Apr 06, 2020 2:49 pm

Re: Custom Script and Python Version

Post by lgute »

Hi @ronhow, thanks for reaching out.

Disclaimer: Not a Python expert.

Did you ever have a python file named requests.py in /usr/local/nagios/libexec? Python Attribute Error

I can simulate your error by doing the following...
  1. Create a file named /usr/local/nagios/libexec/my_script.py, with the following...

    Code: Select all

    #!/usr/bin/env python3
    import requests
    firstRequest = requests.request('https://api.github.com/events')
    
  2. # touch /usr/local/nagios/libexec/requests.py
  3. # python3 my_script.py

    Code: Select all

    Traceback (most recent call last):
      File "my_script.py", line 3, in <module>
        firstRequest = requests.request('https://api.github.com/events')
    AttributeError: module 'requests' has no attribute 'request'
    
  4. # python2 my_script.py

    Code: Select all

    Traceback (most recent call last):
      File "my_script.py", line 3, in <module>
        firstRequest = requests.request('https://api.github.com/events')
    AttributeError: 'module' object has no attribute 'request'
    
It actually creates quite a mess, because removing /usr/local/nagios/libexec/requests.py just gave me a different error...

# python3 my_script.py

Code: Select all

Traceback (most recent call last):
  File "my_script.py", line 2, in <module>
    import requests
ImportError: bad magic number in 'requests': b'\x03\xf3\r\n'
Which means the library module has been damaged.

After a little digging...

Code: Select all

# find / -name "*.pyc" 2> /dev/null | grep "requests\..*.pyc"
/usr/local/nagios/libexec/__pycache__/requests.cpython-37.pyc

# ls -l /usr/local/nagios/libexec/__pycache__/requests.cpython-37.pyc 
-rw-r--r-- 1 root nagios 124 Aug 30 12:04 /usr/local/nagios/libexec/__pycache__/requests.cpython-37.pyc

# cat /usr/local/nagios/libexec/__pycache__/requests.cpython-37.pyc 
B
�v�d�@sdS)N�rrr�%/usr/local/nagios/libexec/requests.py<module>�root@debian:/usr/local/nagios/libexec# 
This issue was resolved by removing the "new" /usr/local/nagios/libexec/__pycache__/requests*.pyc file.
Please let us know if you have any other questions or concerns.

-Laura
ronhow
Posts: 5
Joined: Mon May 25, 2020 4:21 am

Re: Custom Script and Python Version

Post by ronhow »

Hi Laura,

Thanks very much for the feedback.

I've never created any such file and I am not aware of anyone on my team that has either.
I've checked for presence of any requests (ending in.py or .pyc) anywhere on the system - but don't find any result so that doesn't appear to be it.

If I run the script from the command line, without specifying the interpreter in the command line but specifying

Code: Select all

#!/usr/bin/python3
at the top of my script, it works as expected.

If I omit

Code: Select all

#!/usr/bin/python3
from the script heading, and also do not specify in the command definition to use that interpreter, the command does not appear to run at all (no error).

So for some reason, Nagios itself when running the command I have created appears to be using perhaps 2.7 and that is where the module is missing an attribute etc. - I am not sure.
Yet trying to enforce the 3.6 interpreter at command definition doesn't make a difference, but setting the interpreter within the script and running at command line does work.
Last edited by ronhow on Thu Aug 31, 2023 7:54 am, edited 1 time in total.
ronhow
Posts: 5
Joined: Mon May 25, 2020 4:21 am

Re: Custom Script and Python Version

Post by ronhow »

If I add

Code: Select all

print(requests.__file__)
print(platform.python_version())
to my script and run it from command line it runs as expected and the print line gives output:

Code: Select all

/usr/local/lib/python3.6/site-packages/requests/__init__.py
3.6.8

However if I run same script from Nagios itself, I get no output.

If I comment the 'requests.__file__' line, I get same attribute error:

Code: Select all

3.6.8
WARNING failed to retrieve data
module 'requests' has no attribute 'get'
Post Reply