Issue with python-requests

Hello,
I have a check_mk instance (2.0) deployed in Docker. In there, I have a custom notification sripts that’s supposed to open SNOW tickets. Unfortunately it fails with below error:

2021-07-28 10:29:57,777 [20] [cmk.base.notify]      Output: Traceback (most recent call last):
2021-07-28 10:29:57,777 [20] [cmk.base.notify]      Output:   File "/omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin", line 293, in <module>
2021-07-28 10:29:57,778 [20] [cmk.base.notify]      Output:     Plugin().notify()
2021-07-28 10:29:57,778 [20] [cmk.base.notify]      Output:   File "/omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin", line 290, in notify
2021-07-28 10:29:57,778 [20] [cmk.base.notify]      Output:     self.adapter.notify()
2021-07-28 10:29:57,778 [20] [cmk.base.notify]      Output:   File "/omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin", line 186, in notify
2021-07-28 10:29:57,778 [20] [cmk.base.notify]      Output:     import requests
2021-07-28 10:29:57,778 [20] [cmk.base.notify]      Output: ImportError: No module named requests

I had installed pip to install the requests module but according to it, it was already installed. I changed the header of the script to #!/usr/bin/python3 and then when I forced notification logs showed something different:

2021-07-27 15:37:11,696 [20] [cmk.base.notify]   * notifying cmkadmin via sr_servicenow_plugin, parameters: --debug, bulk: no
2021-07-27 15:37:11,696 [20] [cmk.base.notify]      executing /omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin
2021-07-27 15:37:11,703 [40] [cmk.base.notify]     ERROR:
Traceback (most recent call last):
  File "/omd/sites/cmk/lib/python3/cmk/base/notify.py", line 633, in _process_notifications
    call_notification_script(plugin_name, context)
  File "/omd/sites/cmk/lib/python3/cmk/base/notify.py", line 1490, in call_notification_script
    p = subprocess.Popen([path],
  File "/omd/sites/cmk/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/omd/sites/cmk/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin'

But the mentioned file is under the path of the error? Under the OMD I see 3 folders with python:

OMD[cmk]:~/lib$ ls -l |grep pyth
lrwxrwxrwx   1 root root      11 May 26 07:06 check_mk -> python3/cmk/
lrwxrwxrwx   1 root root      19 May 26 07:06 libpython3.8.so -> libpython3.8.so.1.0
-rw-r--r--   1 root root 3424224 May 26 07:06 libpython3.8.so.1.0
-rw-r--r--   1 root root   14112 May 26 07:06 libpython3.so
drwxr-xr-x   2 root root    4096 May 26 07:06 python/
drwxr-xr-x 192 root root   12288 May 26 15:48 python3/
drwxr-xr-x  34 root root    4096 May 26 15:48 python3.8/
OMD[cmk]:~/lib$ pwd
/omd/sites/cmk/lib

python3.8 contains the requests module. How should I proceed with this?

You are sure that your script is using the python3 and no the old python2?
Can you show the script how it looks like.

What I see is that it tries to execute sr_servicenow_plugin in local but its not there:

FileNotFoundError: [Errno 2] No such file or directory: ‘/omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin’

You should change the first line to

#!/usr/bin/env python3

This way it uses the Python 3 installation from checkmk.

And make sure it is a Python 3 script.

1 Like

This is what I did. Result was the second error I pasted above:

FileNotFoundError: [Errno 2] No such file or directory: '/omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin'

While the file is there. One thing though - when I did a “ls -l” on that path the file had an * after its name. I don’t know what that means.

-rwxrwxr-x 1 cmk cmk 10747 Jul 27 15:39 sr_servicenow_plugin*
-rwxrwxr-x 1 cmk cmk 10502 Jun 23 09:58 sr_servicenow_plugin_high*

No, you wrote that you put #!/usr/bin/python3 as first line in your script. This is not the same. I bet there is no Python binary at this path. This is why you get the file not found error.

This is because ls is aliased to ls -F and will output markers for filetypes. The * denotes executable files.

1 Like

Thanks, I corrected that and now that error is gone. Right now it seems I have a problem with indentation in my script. Working on fixing that.

Syntax seems to be ok, now I have different error

2021-07-29 13:27:19,442 [20] [cmk.base.notify]      Output: Traceback (most recent call last):
2021-07-29 13:27:19,442 [20] [cmk.base.notify]      Output:   File "/omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin", line 292, in <module>
2021-07-29 13:27:19,442 [20] [cmk.base.notify]      Output:     Plugin().notify()
2021-07-29 13:27:19,442 [20] [cmk.base.notify]      Output:   File "/omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin", line 288, in notify
2021-07-29 13:27:19,443 [20] [cmk.base.notify]      Output:     self.__parse__()
2021-07-29 13:27:19,443 [20] [cmk.base.notify]      Output:   File "/omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin", line 279, in __parse__
2021-07-29 13:27:19,443 [20] [cmk.base.notify]      Output:     self.data = self.__parse_rulebased__() # data is dict
2021-07-29 13:27:19,443 [20] [cmk.base.notify]      Output:   File "/omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin", line 247, in __parse_rulebased__
2021-07-29 13:27:19,443 [20] [cmk.base.notify]      Output:     context = dict([
2021-07-29 13:27:19,443 [20] [cmk.base.notify]      Output:   File "/omd/sites/cmk/local/share/check_mk/notifications/sr_servicenow_plugin", line 248, in <listcomp>
2021-07-29 13:27:19,443 [20] [cmk.base.notify]      Output:     (var[7:], value.decode('utf-8'))
2021-07-29 13:27:19,443 [20] [cmk.base.notify]      Output: AttributeError: 'str' object has no attribute 'decode'

It’s for this part of code:

        self.logger.debug("Parsing rulebased data from env variables")
        context = dict([
            (var[7:], value.decode('utf-8'))
            for (var, value) in os.environ.items()
            if var.startswith("NOTIFY_")])

I’m not sure if I can share the full script.

Python 3 does Unicode differently. You will have to properly port that script to the new Python version.

2 Likes

Thank you all for assistance. I was able to get the script working. What had to be done is what @r.sander mentioned. Change

#!/usr/bin/python

to

#!/usr/bin/env python3

Then update

(var[7:], value.decode('utf-8'))

to

(var[7:], value)

as decode is not necessary in py3.

2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.