Files
Zabbix-Python-Exception-Mon…/example_usage.py
Holger Sielaff 35b6ac3fbc initial
2025-09-29 18:26:33 +02:00

62 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""
Example usage of Zabbix Python Exception Handler
This demonstrates how to use the Zabbix exception monitoring in your Python applications.
"""
import sys
import os
# Add the Zabbix module path
sys.path.insert(0, '/usr/lib/python3.12')
# Method 1: Import the main handler (auto-initializes)
import zabbix_exception_handler
# Method 2: Alternative simple import (also auto-initializes)
# import zabbix_monitor
# Method 3: Manual initialization (if you want more control)
# from zabbix_exception_handler import ZabbixPythonExceptionHandler
# ZabbixPythonExceptionHandler.init()
def demonstrate_exception():
"""Function that will cause an exception for testing"""
print("About to cause an exception for demonstration...")
# This will cause a ZeroDivisionError
result = 10 / 0
return result
def demonstrate_file_error():
"""Function that will cause a file not found error"""
print("About to cause a file error for demonstration...")
# This will cause a FileNotFoundError
with open('/nonexistent/file.txt', 'r') as f:
content = f.read()
return content
if __name__ == "__main__":
print("Zabbix Python Exception Handler Example")
print("=" * 50)
print()
print("Exception handler is now active!")
print("Any unhandled exceptions will be logged to: /var/log/zabbix/python.exceptions.log")
print()
# Choose which type of exception to demonstrate
choice = input("Choose exception type (1=ZeroDivision, 2=FileNotFound, 3=None): ")
if choice == "1":
demonstrate_exception()
elif choice == "2":
demonstrate_file_error()
else:
print("No exception generated. Handler is still active for any future exceptions.")
print("Your application can continue normally...")