|
| 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 asyncio |
| 7 | +from zabbix_utils import AsyncZabbixAPI |
| 8 | + |
| 9 | +# Zabbix server URL or IP address |
| 10 | +ZABBIX_SERVER = "127.0.0.1" |
| 11 | + |
| 12 | +# Zabbix server authentication credentials |
| 13 | +ZABBIX_AUTH = { |
| 14 | + "user": "Admin", # Zabbix user name for authentication |
| 15 | + "password": "zabbix" # Zabbix user password for authentication |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +async def main(): |
| 20 | + """ |
| 21 | + The main function to perform asynchronous tasks. |
| 22 | + """ |
| 23 | + |
| 24 | + # Create an instance of the AsyncZabbixAPI class |
| 25 | + api = AsyncZabbixAPI(ZABBIX_SERVER) |
| 26 | + |
| 27 | + # Authenticating with Zabbix API using the provided username and password. |
| 28 | + await api.login(**ZABBIX_AUTH) |
| 29 | + |
| 30 | + # There are only three ways to pass parameters of type dictionary: |
| 31 | + # |
| 32 | + # 1. Specifying values directly with their keys: |
| 33 | + problems = await api.problem.get(tags=[{"tag": "scope", "value": "notice", "operator": "0"}]) |
| 34 | + # |
| 35 | + # 2. Unpacking dictionary keys and values using `**`: |
| 36 | + # request_params = {"tags": [{"tag": "scope", "value": "notice", "operator": "0"}]} |
| 37 | + # problems = await api.problem.get(**request_params) |
| 38 | + # |
| 39 | + # 3. Passing the dictionary directly as an argument (since v2.0.2): |
| 40 | + # request_params = {"tags": [{"tag": "scope", "value": "notice", "operator": "0"}]} |
| 41 | + # problems = await api.problem.get(request_params) |
| 42 | + |
| 43 | + # Print the names of the retrieved users |
| 44 | + for problem in problems: |
| 45 | + print(problem['name']) |
| 46 | + |
| 47 | + # Logout to release the Zabbix API session |
| 48 | + await api.logout() |
| 49 | + |
| 50 | +# Run the main coroutine |
| 51 | +asyncio.run(main()) |
0 commit comments