Skip to content

Execute Python Script on Remote Linux Host with Zabbix Agent

Video Lecture

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)

mkdir /home/zabbix/

CD into the folder

cd /home/zabbix

Create the Python script

sudo nano genrand.py

Paste in the following

1
2
import random
print(random.randint(0,36))

Save and test

python3 genrand.py

You should see a random integer from 0 to 36.

Configure Item in Zabbix UI

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

Key Value
Name Generate Random Number
Type Zabbix agent
Key system.run[python3 /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.

It is unlikely to work until you've configured the DenyKey and AllowKey in the Zabbix agents configuration.

Zabbix Agent Configuration

Edit the Zabbix agents configuration file

sudo nano /etc/zabbix/zabbix_agentd.conf

I have explicitly allowed this key by adding,

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

After making changes to the configuration, restart and check status

sudo service zabbix-agent restart
sudo service zabbix-agent status

No go back into the Zabbix UI and test the new item again.

Zabbix Agent Items

Remote Commands

Restricting Agent Checks

Comments