How to Monitor Custom Parameters on Zabbix with a Python Script
Monitoring custom parameters on Zabbix can help you keep a closer eye on your systems, ensuring that services are running smoothly. In this guide, we’ll show you how to create a Python script that integrates with Zabbix to monitor service status on a Linux server. This will provide you with a customizable approach to track and report system parameters through Zabbix’s powerful monitoring platform.

Setting Up the Environment for Monitoring Custom Parameters on Zabbix
Before we begin writing the Python script, we need to set up a working directory on our server. Start by creating a folder to store your scripts:
mkdir /home/zabbix
cd /home/zabbix
This will ensure that your Zabbix monitoring scripts are properly organized.
Writing the Python Script for Service Discovery
Next, we’ll create a Python script that collects a list of services on your Linux system and sends this data to Zabbix. Create a new Python file named service_discovery.py:
cat service_discovery.py
Then, add the following Python code to the file:
#!/usr/bin/python
import sys
import os
import json
SERVICES = os.popen('systemctl list-unit-files').read()
ILLEGAL_CHARS = ["\\", "'", "\"", "`", "*", "?", "[", "]", "{", "}", "~", "$", "!", "&", ";", "(", ")", "<", ">", "|", "#", "@", "0x0a"]
DISCOVERY_LIST = {"data": []}
LINES = SERVICES.splitlines()
for l in LINES:
service_unit = l.split(".service")
if len(service_unit) == 2:
if not [ele for ele in ILLEGAL_CHARS if(ele in service_unit[0])]:
status = service_unit[1].split()
if len(status) == 1:
if (status[0] == "enabled" or status[0] == "generated"):
DISCOVERY_LIST["data"].append({"{#SERVICE}": service_unit[0]})
else:
if (status[0] == "enabled" or status[0] == "generated") and status[1] == "enabled":
DISCOVERY_LIST["data"].append({"{#SERVICE}": service_unit[0]})
JSON = json.dumps(DISCOVERY_LIST)
print(JSON)
This script gathers a list of services from the system, filters out illegal characters, and outputs a JSON object that Zabbix can use to monitor service statuses.
Granting Execution Permissions
Once the script is written, make sure to grant execute permissions:
sudo chmod a+x service_discovery.py
This will ensure that the script can be run as a Zabbix user.
Testing the Script
Now, it’s time to test the script from the perspective of the Zabbix user. Run the following command:
sudo -H -u zabbix bash -c '/home/zabbix/service_discovery.py'
This will execute the script and return a JSON object containing the list of services.
Configuring Zabbix to Use the Script
To integrate this script with Zabbix, you need to configure a UserParameter in the Zabbix agent configuration file.
- Open the Zabbix agent configuration file.
- Scroll down to the UserParameters section.
- Add the following lines:
UserParameter=service.discovery,/home/zabbix/service_discovery.py
UserParameter=service.isactive[*],systemctl is-active --quiet '$1' && echo 1 || echo 0
UserParameter=service.activatedtime[*],systemctl show '$1' --property=ActiveEnterTimestampMonotonic | cut -d= -f2
These parameters allow Zabbix to call the Python script and retrieve the status of services.
Restarting Zabbix Agent
After saving your configuration, restart the Zabbix agent to apply the changes:
sudo service zabbix-agent restart
sudo service zabbix-agent status
Check the status of the Zabbix agent to ensure everything is running correctly.
Verifying the Configuration
You can test if the service.discovery parameter is working correctly by running:
zabbix_agentd -t service.discovery
If successful, the command will return a list of available services in a JSON format, like this:
service.discovery [t|{"data": [{"{#SERVICE}": "auditd"}, {"#SERVICE": "chronyd"}, ...]}]
This means your Zabbix server can now discover and monitor the services on your Linux host.
Optimizing Zabbix with ZippyOPS Consulting and Managed Services
If you want to take your Zabbix monitoring setup to the next level, ZippyOPS offers expert DevOps, DevSecOps, and DataOps consulting. We provide both implementation and managed services to help you automate and optimize your operations, ensuring maximum efficiency.
Whether you’re looking to enhance cloud monitoring, streamline your microservices architecture, or implement AIOps and MLOps, ZippyOPS can help. Visit our solutions page to discover more.
Conclusion: Monitoring Custom Parameters with Zabbix Made Easy
By using this Python script, you can efficiently monitor custom parameters in Zabbix and easily retrieve the status of Linux services. This method is just one way to take advantage of Zabbix’s powerful capabilities for custom monitoring.
For more comprehensive support in automating operations, enhancing infrastructure, and securing your systems, reach out to ZippyOPS. Our team of experts is ready to help you scale your business with tailored DevOps and IT operations solutions.
Contact us at sales@zippyops.com for personalized consultations.



