Execute Python Script on Remote Linux Host with Zabbix Agent

Video Lecture

Execute Python Script on Remote Linux Host with Zabbix Agent Execute Python Script on Remote Linux Host with Zabbix Agent Execute Python Script on Remote Linux Host with Zabbix Agent

Description

I create a Python script on my remote Linux host that prints a random number.

I create a new item for the host and select the system.run option for the key and enter the Python command needed to run the new .py file on the remote host.

Create the Python Script

Create a folder on the remote Linux host. (if the folder doesn't already exist)

1
mkdir /home/zabbix/

CD into the folder

1
cd /home/zabbix

Create the python script

1
sudo nano genrand.py

Paste in the following

1
2
3
#!/usr/bin/python
import random
print(random.randint(0,36))

Save and test

1
python genrand.py

You should see a random integer from 0 and 36

If you get an error that Python is not be installed then run,

1
sudo apt install python

Zabbix Agent Configuration

Edit the Zabbix agents configuration file

1
sudo nano /etc/zabbix/zabbix_agentd.conf

Depending on the version of your agent, you will need to change different settings. See Notes below.

Note

  • Zabbix Agents pre 5.02. In the zabbix_agentd.conf for the remote host, add EnableRemoteCommands=1 and then restart the agent process.

  • Zabbix Agents 5.0 and 5.01. Comment out the DenyKey parameter which blocks system.run by default, add EnableRemoteCommands=1 and then restart the agent process.

  • Zabbix Agents 5.02 and later. Either,

    • Comment out the DenyKey and add AllowKey=system.run[*]

    • or Comment out the DenyKey and add EnableRemoteCommands=1 (EnableRemoteCommands is now deprecated so it is no longer recommended and will eventually stop working as versions are updated)

See Restricting agent checks for more info.

To get the version of the agent,

sudo zabbix_agentd -V

I am running zabbix_agentd (daemon) (Zabbix) 5.0.2 so I need to comment out the line for DenyKey and add AllowKey=system.run[*] or EnableRemoteCommands=1(Deprecated)

After making changes to the configuration, restart and check status

1
2
sudo service zabbix-agent restart
sudo service zabbix-agent status

Note

With the AllowKey option, I can also be very explicit on which key is allowed, or even use a wild card (*).

AllowKey=system.run[python /home/zabbix/genrand.py] or

AllowKey=system.run[python * or

AllowKey=system.run[python \home\*

Configure Host in Zabbix UI

Now in the Zabbix UI, add a new item to your host configuration,

Key Value
Name genrand
Type Zabbix agent
Key system.run[python /home/zabbix/genrand.py]
Type of information Numeric (Or Text if you want to see the error message in the latest data)

And then try using the Test button at the bottom of the form.

Centos 7

You may get the message python cannot execute file [Errno 13] Permission denied

You can disable selinux

1
sudo nano /etc/selinux/config

set SELINUX=disabled

Save, reboot

1
sudo reboot now

Check

1
sestatus

Restart zabbix agent if it was not auto started.

1
/bin/systemctl restart zabbix-agent.service

Test the item again using Zabbix UI test button in the host item form.

Zabbix Agent Items

Remote Commands

Restricting Agent Checks