Execute Python Script on Remote Linux Host with Zabbix Agent
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)
CD into the folder
Create the python script
1 2 3
Save and test
You should see a random integer from 0 and 36
If you get an error that Python is not be installed then run,
Zabbix Agent Configuration
Edit the Zabbix agents configuration file
Depending on the version of your agent, you will need to change different settings. See Notes below.
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,
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
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
Configure Host in Zabbix UI
Now in the Zabbix UI, add a new item to your host configuration,
|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.
You may get the message python cannot execute file [Errno 13] Permission denied
You can disable selinux
Restart zabbix agent if it was not auto started.
Test the item again using Zabbix UI test button in the host item form.