Execute Python Script on Remote Linux Host with Zabbix Agent
Video Lecture
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 |
|
CD into the folder
1 |
|
Create the python script
1 |
|
1 2 3 |
|
Save and test
1 |
|
You should see a random integer from 0 and 36
If you get an error that Python is not be installed then run,
1 |
|
Zabbix Agent Configuration
Edit the Zabbix agents configuration file
1 |
|
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,
1 |
|
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 |
|
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 |
|
SELINUX=disabled
Save, reboot
1 |
|
Check
1 |
|
Restart zabbix agent if it was not auto started.
1 |
|
Test the item again using Zabbix UI test button in the host item form.