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/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/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/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}") 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() diff --git a/zabbix_utils/api.py b/zabbix_utils/api.py index c3ae9ad..439b6f0 100644 --- a/zabbix_utils/api.py +++ b/zabbix_utils/api.py @@ -347,7 +347,6 @@ 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 +359,7 @@ def send_api_request(self, method: str, params: Optional[dict] = None, ctx = None try: - 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 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