|
| 1 | +# Copyright (C) 2001-2023 Zabbix SIA |
| 2 | +# |
| 3 | +# Zabbix SIA licenses this file to you under the MIT License. |
| 4 | +# See the LICENSE file in the project root for more information. |
| 5 | + |
| 6 | +import ssl |
| 7 | +import asyncio |
| 8 | +from zabbix_utils import AsyncSender |
| 9 | + |
| 10 | +# !!! IMPORTANT |
| 11 | +# The code example below is supported only from Python version 3.13 onwards. |
| 12 | + |
| 13 | +# Zabbix server details |
| 14 | +ZABBIX_SERVER = "zabbix-server.example.com" |
| 15 | +ZABBIX_PORT = 10051 |
| 16 | + |
| 17 | + |
| 18 | +# Create and configure an SSL context for secure communication with the Zabbix server. |
| 19 | +def custom_context(config) -> ssl.SSLContext: |
| 20 | + psk = None |
| 21 | + |
| 22 | + # Try to get PSK key and identity |
| 23 | + psk_identity = config.get('tlspskidentity').encode('utf-8') |
| 24 | + psk_file = config.get('tlspskfile') |
| 25 | + |
| 26 | + # Read PSK from file if specified |
| 27 | + if psk_file: |
| 28 | + with open(psk_file, encoding='utf-8') as f: |
| 29 | + psk = bytes.fromhex(f.read()) |
| 30 | + |
| 31 | + # Create an SSL context for TLS client |
| 32 | + context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 33 | + |
| 34 | + # Disable hostname verification |
| 35 | + context.check_hostname = False |
| 36 | + |
| 37 | + # Set the verification mode to require a valid certificate |
| 38 | + context.verify_mode = ssl.CERT_NONE |
| 39 | + |
| 40 | + # Set the maximum allowed version of the TLS protocol to TLS 1.2 |
| 41 | + context.maximum_version = ssl.TLSVersion.TLSv1_2 |
| 42 | + |
| 43 | + # Set the ciphers to use for the connection |
| 44 | + context.set_ciphers('PSK') |
| 45 | + |
| 46 | + # Set up the callback function to provide the PSK and identity when requested |
| 47 | + context.set_psk_client_callback(lambda hint: (psk_identity, psk)) |
| 48 | + |
| 49 | + # Return the customized SSL context |
| 50 | + return context |
| 51 | + |
| 52 | + |
| 53 | +async def main(): |
| 54 | + """ |
| 55 | + The main function to perform asynchronous tasks. |
| 56 | + """ |
| 57 | + |
| 58 | + # Create an instance of AsyncSender with a custom SSL context |
| 59 | + sender = AsyncSender( |
| 60 | + server=ZABBIX_SERVER, |
| 61 | + port=ZABBIX_PORT, |
| 62 | + use_config=True, |
| 63 | + ssl_context=custom_context |
| 64 | + ) |
| 65 | + |
| 66 | + # Send a value to a Zabbix server/proxy with specified parameters |
| 67 | + # Parameters: (host, key, value, clock, ns) |
| 68 | + response = await sender.send_value('host', 'item.key', 'value', 1695713666, 30) |
| 69 | + |
| 70 | + # Check if the value sending was successful |
| 71 | + if response.failed == 0: |
| 72 | + # Print a success message along with the response time |
| 73 | + print(f"Value sent successfully in {response.time}") |
| 74 | + else: |
| 75 | + # Print a failure message |
| 76 | + print("Failed to send value") |
| 77 | + |
| 78 | +# Run the main coroutine |
| 79 | +asyncio.run(main()) |
0 commit comments