From 245e0a16c9bc9ea6e806b83b007f1c6d838d4b03 Mon Sep 17 00:00:00 2001 From: Ulrich Moshammer-Mischkof Date: Thu, 4 Sep 2025 20:00:51 +1000 Subject: [PATCH 1/5] #34: updated api.py send_api_request to pass the timeout parameter to the urlopen function instead of the Request object. --- zabbix_utils/api.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/zabbix_utils/api.py b/zabbix_utils/api.py index c3ae9ad..aa28b0b 100644 --- a/zabbix_utils/api.py +++ b/zabbix_utils/api.py @@ -347,7 +347,7 @@ def send_api_request(self, method: str, params: Optional[dict] = None, headers=headers, method='POST' ) - req.timeout = self.timeout + # Disable SSL certificate validation if needed. if not self.validate_certs: @@ -360,7 +360,10 @@ def send_api_request(self, method: str, params: Optional[dict] = None, ctx = None try: - resp = ul.urlopen(req, context=ctx) + if self.timeout: + resp = ul.urlopen(req, context=ctx, timeout=self.timeout) + else: + resp = ul.urlopen(req, context=ctx) resp_json = json.loads(resp.read().decode('utf-8')) except URLError as err: raise ProcessingError(f"Unable to connect to {self.url}:", err) from None From ec2d7d8c6ab4c6a0b3bd4677a6eb5e276695ce03 Mon Sep 17 00:00:00 2001 From: Aleksandr Iantsen Date: Tue, 16 Dec 2025 12:05:09 +0200 Subject: [PATCH 2/5] fixed ZabbixAPI timeout issue #34 --- zabbix_utils/api.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/zabbix_utils/api.py b/zabbix_utils/api.py index aa28b0b..439b6f0 100644 --- a/zabbix_utils/api.py +++ b/zabbix_utils/api.py @@ -348,7 +348,6 @@ def send_api_request(self, method: str, params: Optional[dict] = None, method='POST' ) - # Disable SSL certificate validation if needed. if not self.validate_certs: ctx = ssl.create_default_context() @@ -360,10 +359,7 @@ def send_api_request(self, method: str, params: Optional[dict] = None, ctx = None try: - if self.timeout: - resp = ul.urlopen(req, context=ctx, timeout=self.timeout) - else: - resp = ul.urlopen(req, context=ctx) + resp = ul.urlopen(req, context=ctx, timeout=self.timeout) resp_json = json.loads(resp.read().decode('utf-8')) except URLError as err: raise ProcessingError(f"Unable to connect to {self.url}:", err) from None From ff4f4dc7a2cf70ef37fe2ddc1d9f35628fd5f2a5 Mon Sep 17 00:00:00 2001 From: Aleksandr Iantsen Date: Tue, 16 Dec 2025 12:28:22 +0200 Subject: [PATCH 3/5] added examples of how to push item history --- examples/api/asynchronous/push_history.py | 62 +++++++++++++++++++++++ examples/api/synchronous/push_history.py | 47 +++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 examples/api/asynchronous/push_history.py create mode 100644 examples/api/synchronous/push_history.py diff --git a/examples/api/asynchronous/push_history.py b/examples/api/asynchronous/push_history.py new file mode 100644 index 0000000..4431095 --- /dev/null +++ b/examples/api/asynchronous/push_history.py @@ -0,0 +1,62 @@ +# Copyright (C) 2001-2023 Zabbix SIA +# +# Zabbix SIA licenses this file to you under the MIT License. +# See the LICENSE file in the project root for more information. + +import asyncio +from zabbix_utils import AsyncZabbixAPI, APIRequestError + +# Zabbix server URL or IP address +ZABBIX_SERVER = "127.0.0.1" + +# Zabbix server authentication credentials +ZABBIX_AUTH = { + "user": "Admin", # Zabbix user name for authentication + "password": "zabbix" # Zabbix user password for authentication +} + +# IDs and values of items to push +ITEM_VALUES = [ + { + "itemid": 70060, + "value": 55 + }, + { + "itemid": 70061, + "value": 1.8, + "clock": 1690891294, + "ns": 45440940 + }, + { + "itemid": 70062, + "value": 123, + "clock": 1690891295 + } +] + + +async def main(): + """ + The main function to perform asynchronous tasks. + """ + + # Create an instance of the AsyncZabbixAPI class + api = AsyncZabbixAPI(ZABBIX_SERVER) + + # Authenticating with Zabbix API using the provided username and password. + await api.login(**ZABBIX_AUTH) + + # Clear history for items with specified IDs + try: + await api.history.push(ITEM_VALUES) + + # A way to do the same for versions prior to v2.0.2: + # await api.history.push(*ITEM_VALUES) + except APIRequestError as e: + print(f"An error occurred when attempting to delete items: {e}") + else: + # Logout to release the Zabbix API session + await api.logout() + +# Run the main coroutine +asyncio.run(main()) diff --git a/examples/api/synchronous/push_history.py b/examples/api/synchronous/push_history.py new file mode 100644 index 0000000..2f920bc --- /dev/null +++ b/examples/api/synchronous/push_history.py @@ -0,0 +1,47 @@ +# Copyright (C) 2001-2023 Zabbix SIA +# +# Zabbix SIA licenses this file to you under the MIT License. +# See the LICENSE file in the project root for more information. + +from zabbix_utils import ZabbixAPI, APIRequestError + +# Zabbix server details and authentication credentials +ZABBIX_AUTH = { + "url": "127.0.0.1", # Zabbix server URL or IP address + "user": "Admin", # Zabbix user name for authentication + "password": "zabbix" # Zabbix user password for authentication +} + +# IDs and values of items to push +ITEM_VALUES = [ + { + "itemid": 70060, + "value": 55 + }, + { + "itemid": 70061, + "value": 1.8, + "clock": 1690891294, + "ns": 45440940 + }, + { + "itemid": 70062, + "value": 123, + "clock": 1690891295 + } +] + +# Create an instance of the ZabbixAPI class with the specified authentication details +api = ZabbixAPI(**ZABBIX_AUTH) + +# Push history for the list of item IDs and values +try: + api.history.push(ITEM_VALUES) + + # A way to do the same for versions prior to v2.0.2: + # api.history.push(*ITEM_VALUES) +except APIRequestError as e: + print(f"An error occurred when attempting to clear items' history: {e}") + +# Logout to release the Zabbix API session +api.logout() From df0d9fcbd8658505fce150b6b73b6493324ad7bb Mon Sep 17 00:00:00 2001 From: Aleksandr Iantsen Date: Tue, 16 Dec 2025 12:31:29 +0200 Subject: [PATCH 4/5] modified the examples of clearing history --- examples/api/asynchronous/clear_history.py | 8 ++++---- examples/api/synchronous/clear_history.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/api/asynchronous/clear_history.py b/examples/api/asynchronous/clear_history.py index c973d6e..e53a5fd 100644 --- a/examples/api/asynchronous/clear_history.py +++ b/examples/api/asynchronous/clear_history.py @@ -16,7 +16,7 @@ } # IDs of items for which the history should be cleared -ITEM_IDS = [70060] +ITEM_IDS = [70060, 70061, 70062] async def main(): @@ -32,10 +32,10 @@ async def main(): # Clear history for items with specified IDs try: - await api.history.clear(*ITEM_IDS) + await api.history.clear(ITEM_IDS) - # Alternative way to do the same (since v2.0.2): - # await api.history.clear(ITEM_IDS) + # A way to do the same for versions prior to v2.0.2: + # await api.history.clear(*ITEM_IDS) except APIRequestError as e: print(f"An error occurred when attempting to delete items: {e}") else: diff --git a/examples/api/synchronous/clear_history.py b/examples/api/synchronous/clear_history.py index abaa54b..cd7c2a0 100644 --- a/examples/api/synchronous/clear_history.py +++ b/examples/api/synchronous/clear_history.py @@ -13,16 +13,16 @@ } # IDs of items for which the history should be cleared -ITEM_IDS = [70060] +ITEM_IDS = [70060, 70061, 70062] # Create an instance of the ZabbixAPI class with the specified authentication details api = ZabbixAPI(**ZABBIX_AUTH) # Clear history for items with specified IDs try: - api.history.clear(*ITEM_IDS) + api.history.clear(ITEM_IDS) - # Alternative way to do the same (since v2.0.2): + # A way to do the same for versions prior to v2.0.2: # api.history.clear(*ITEM_IDS) except APIRequestError as e: print(f"An error occurred when attempting to clear items' history: {e}") From 036b0c3ba9d0656bbe04ee85efd85d6b4fcb6149 Mon Sep 17 00:00:00 2001 From: Aleksandr Iantsen Date: Wed, 17 Dec 2025 10:46:57 +0200 Subject: [PATCH 5/5] updated CHANGELOG and version number --- CHANGELOG.md | 12 ++++++++++++ zabbix_utils/version.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7c7138..e8036f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +## [2.0.4](https://github.com/zabbix/python-zabbix-utils/compare/v2.0.3...v2.0.4) (2025-12-17) + +### Changes: + +- added examples of how to push item history +- updated examples of how to clear item history + +### Bug fixes: + +- fixed issue [#34](https://github.com/zabbix/python-zabbix-utils/issues/34) with timeout ignorance +- fixed small bugs and flaws + ## [2.0.3](https://github.com/zabbix/python-zabbix-utils/compare/v2.0.2...v2.0.3) (2025-07-03) ### Features: diff --git a/zabbix_utils/version.py b/zabbix_utils/version.py index 2d1f19d..58092dd 100644 --- a/zabbix_utils/version.py +++ b/zabbix_utils/version.py @@ -22,7 +22,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. -__version__ = "2.0.3" +__version__ = "2.0.4" __min_supported__ = 6.0 __max_supported__ = 7.4