Ad Widget

Collapse

Zabbix Agent not able to run a python script.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • snetadmin
    Junior Member
    • Aug 2022
    • 12

    #1

    Zabbix Agent not able to run a python script.

    Hi guys,

    We have a ubuntu server that's been updated to 20.04 lts. Since then, I'm unable to execute the python script on the zabbix portal that's stating the following message below.

    Traceback (most recent call last):
    File "/etc/zabbix/test_scripts/ohclientheartbeat.py", line 4, in <module>
    import mysql.connector
    ModuleNotFoundError: No module named 'mysql'


    When I run the python script directly on the server itself, I'm able to see it working.

    Has anyone seen this before?

  • cyber
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Dec 2006
    • 4807

    #2
    If you run locally and everything works, then you have your environment set and modules are found. If you run remotely, then your env is possibly not set properly and your modules are not found. Probably your script should take care of such things, like setting paths, variables etc.
    So if it ran before, but not after upgrade, then try to find what changed for that user during upgrade... shells, environments etc...

    Comment

    • snetadmin
      Junior Member
      • Aug 2022
      • 12

      #3
      Thank you for the information.
      The script stayed the same before and after the upgrade from 16.04 to 20.04 in Ubuntu. The only thing that went missing were the python modules. I had reinstall mysql.connector and boto3 to get the script working on the server directly.
      When I test the script on the zabbix portal, it's saying the module for mysql.connector can't be found which doesn't make sense. Is there something else that I can do to resolve this issue?

      Comment

      • Markku
        Senior Member
        Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
        • Sep 2018
        • 1781

        #4
        What is the actual command that you use to install those extra modules?

        Sounds like you install them as a non-zabbix user and/or they get installed in a place where zabbix user does not find them.

        My standard procedure for creating Python apps to be used in Zabbix agent items:
        1. Create a directory for the app, like /opt/myapp (make that as root, and chown it to your normal user+group)
        2. Create a virtual environment for the app: cd /opt/myapp; python3 -m venv venv
        3. Activate the venv: source venv/bin/activate
        4. Upgrade pip (in the venv!) and install wheel: pip install -U pip wheel
        5. Install the required packages: pip install boto3
        6. Deactivate the venv: deactivate
        7. In the Zabbix agent, use this syntax to run the script using the venv: UserParameter=myapp.key,/opt/myapp/venv/bin/python3 /opt/myapp/myappscript.py
        Just make sure that the zabbix user can access the files and directories as you surely understand. Best practice is not to have zabbix user any write access to the app files.

        That way the extra packages don't interfere with any other Python apps or system-wide packages installed on the same server.

        Markku

        Comment

        • Markku
          Senior Member
          Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
          • Sep 2018
          • 1781

          #5
          And after writing that I realized you are talking about scripts that are run from the GUI. I'm not able to test anything now, but I'd add this to the start of the script in my example:

          #!/opt/myapp/venv/bin/python3

          This ensures that when the script is run directly (with execute bit set) the shell is able to start the correct Python interpreter to run it.

          Markku

          Comment

          • snetadmin
            Junior Member
            • Aug 2022
            • 12

            #6
            Markku,
            I'm going through the steps that you've written but I'm stuck on step 4.
            I'm seeing the following error message below.

            Code:
            (venv) takashi_worrell@tools:/opt/myapp$ sudo pip install -U pip wheel
            Traceback (most recent call last):
              File "/usr/local/bin/pip", line 7, in <module>
                from pip._internal import main
            ModuleNotFoundError: No module named 'pip'
            I tried running pip install pip and apt-get install pip but both did not work.
            Last edited by snetadmin; 09-08-2022, 22:32.

            Comment

            • snetadmin
              Junior Member
              • Aug 2022
              • 12

              #7
              Markku,

              Actually, I was able to figure that issue and completed the steps that you written.

              However, I'm still getting same issue when I tested the script on Zabbix portal in the screenshot shown below.

              Click image for larger version  Name:	image.png Views:	0 Size:	64.7 KB ID:	449530

              Comment

              • snetadmin
                Junior Member
                • Aug 2022
                • 12

                #8
                Markku,

                I went over the steps again and now the python script is working in the Zabbix portal.

                I must have overlooked something the first time and now I'm able to get it to work the second time around.

                Will be adding this to my notes.

                Thank you!

                Comment

                • reekjohns
                  Junior Member
                  • Nov 2019
                  • 4

                  #9
                  Originally posted by snetadmin
                  Markku,
                  Code:
                  ModuleNotFoundError: No module named 'pip'
                  .
                  The error message "ModuleNotFoundError: No module named 'pip'" is usually caused by one of two things:
                  • Pip is not installed: This error can occur if the pip package is not installed on your system. Pip is the package installer for Python, and it is used to install and manage Python packages. To install pip, you can download the appropriate package from the official website: https://pip.pypa.io/en/stable/installation/
                  • The Python path is not set up correctly: If pip is installed but you are still getting this error, it could be because the Python path is not set up correctly on your system. In this case, you can try the following steps:
                  • Check if pip is installed by running the command python -m pip --version. If it is not installed, follow step 1 above.
                  • If pip is installed, try running the command python -m ensurepip --default-pip. This command will reinstall pip and ensure that it is set up correctly.
                  • If the above steps do not work, you can try adding the location of the pip executable to your system's PATH environment variable. On Windows, you can do this by going to Control Panel > System > Advanced System Settings > Environment Variables, and then adding the location of the pip executable to the PATH variable. On Linux or macOS, you can add the following line to your shell startup file (e.g. .bashrc or .zshrc): export PATH=$PATH:/path/to/pip/executable.

                  Comment

                  Working...