Ad Widget

Collapse

Python script not executing when trigger fires, but Bash script executes OK.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JKS
    Junior Member
    • Feb 2019
    • 4

    #1

    Python script not executing when trigger fires, but Bash script executes OK.

    Hello.

    Trying to run a python script directly from 'Commands' field in 'Actions > Operations', seems the script never runs.

    Have hard coded path:
    Click image for larger version  Name:	1.png Views:	0 Size:	22.5 KB ID:	395152

    HTML Code:
    root@mon-a-tron:/home# ls -l | grep zabbix
    drwxr-xr-x 5 zabbix zabbix 4096 Feb  8 02:05 zabbix
    
    root@mon-a-tron:/home/zabbix# ls -l | grep p.py
    -rwxrwxr-x 1 zabbix zabbix   344 Feb  8 01:55 p.py
    I tested running a shell (.sh) script which works fine, if I try running the same python script from within the shell script it still does not run.

    Maybe it suggests there is an issue with Python? I am just writing a simple string to file for testing.

    If I 'su - zabbix' and run the Python script is executes OK, just doesn't run from Zabbix.

    I have enabled 'Remote Commands' in agent conf file (Not that it is set to execute on the agent anyway).

    There are no errors in the server or agent log.

    I'm not to sure how to diagnose any further.

    Thanks for your help!

    Other possible useful info:

    OS
    HTML Code:
    root@mon-a-tron:/home/zabbix# cat /etc/lsb-release
    DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=18.04
    DISTRIB_CODENAME=bionic
    DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"
    /etc/passwd
    HTML Code:
    root@mon-a-tron:/home# cat /etc/passwd | grep zabbix/
    zabbix:x:113:115::/home/zabbix/:/bin/bash
    /etc/sudoers
    HTML Code:
    root@mon-a-tron:/etc# cat /etc/sudoers
    
    Defaults !requiretty
    
    zabbix   ALL=(ALL:ALL) NOPASSWD: ALL
    Python version
    HTML Code:
    root@mon-a-tron:/home/zabbix# python -V
    Python 2.7.17
    Top of Python script
    HTML Code:
    root@mon-a-tron:/home/zabbix# cat p.py
    #!/usr/bin/env python
    HTML Code:
    Slight progress, not sure why I have not seen this error before below. It is in Zabbix server log, so look like the issue is within Python or access to Python from the zabbix user account.
    
    1521:20200208:034557.331 Failed to execute command "/home/zabbix/p.py": Traceback (most recent call last):
      File "/home/zabbix/p.py", line 14, in <module>
        f= open("test99.txt","w+")
    IOError: [Errno 13] Permission denied: 'test99.txt'
    OK. My bad, that was me not hard coding the path to home/zabbix. So I can get a Python script to run, but there is still an issue somewhere, below is log entry from the real script am trying to run. The script updates, a macro field in Zabbix, this is the same script that works fine when run manually under the zabbix account.
    I'm a little bit of my depth here.

    HTML Code:
    1521:20200208:040118.043 Failed to execute command "/home/zabbix/zhostupdater.py DEATH-STAR -M SSH.USERNAME.SET=1
    
    /home/zabbix/zhostupdater.py DEATH-STAR -M SSH.USERNAME=abc123": Traceback (most recent call last):
      File "/home/zabbix/zhostupdater.py", line 30, in <module>
        defconf = os.getenv("HOME") + "/.zbx.conf"
    TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
    Traceback (most recent call last):
      File "/home/zabbix/zhostupdater.py", line 30, in <module>
        defconf = os.getenv("HOME") + "/.zbx.conf"
    TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
    Last edited by JKS; 08-02-2020, 06:07.
  • JKS
    Junior Member
    • Feb 2019
    • 4

    #2
    OK, fixed it. Was working on it for ages, seems just posting in the forums was enough to scare it into working.

    If anyone else the same issue, its something to so with this line in the python script:
    HTML Code:
    defconf = os.getenv("HOME") + "/.zbx.conf"
    I'm guessing it does not resolve the path correctly due to.... something. Hard coding the path fixed it.

    Comment

    Working...