Package: zabbix-utils
Zabbix utils libraryzabbix_utilsis a Python library for working withZabbix APIas well as withZabbix senderandZabbix getprotocols.RequirementsSupported versions:Zabbix 5.0+Python 3.8+Tested on:Zabbix 5.0, 6.0, 6.4 and pre-7.0Python 3.8, 3.9, 3.10, 3.11 and 3.12DocumentationInstallationInstallzabbix_utilslibrary using pip:$pipinstallzabbix_utilsUse casesTo work with Zabbix APITo work with Zabbix API you can import and usezabbix_utilslibrary as follows:fromzabbix_utilsimportZabbixAPIapi=ZabbixAPI(url="127.0.0.1")api.login(user="User",password="zabbix")users=api.user.get(output=['userid','name'])foruserinusers:print(user['name'])api.logout()You can also authenticate using an API token (supported since Zabbix 5.4):fromzabbix_utilsimportZabbixAPIapi=ZabbixAPI(url="127.0.0.1")api.login(token="xxxxxxxx")users=api.user.get(output=['userid','name'])foruserinusers:print(user['name'])When token is used, callingapi.logout()is not necessary.It is possible to specify authentication fields by the following environment variables:ZABBIX_URL,ZABBIX_TOKEN,ZABBIX_USER,ZABBIX_PASSWORDYou can compare Zabbix API version with strings and numbers, for example:fromzabbix_utilsimportZabbixAPIurl="127.0.0.1"user="User"password="zabbix"api=ZabbixAPI(url=url,user=user,password=password)# Method to get versionver=api.api_version()print(type(ver).__name__,ver)# APIVersion 7.0.0# ZabbixAPI prototype with versionver=api.versionprint(type(ver).__name__,ver)# APIVersion 7.0.0# Comparing versionsprint(ver>6.0)# Trueprint(ver!=7.0)# Falseprint(ver!="7.0.0")# False# Version additional methodsprint(ver.major)# 7.0print(ver.minor)# 0print(ver.is_lts())# Trueapi.logout()In case the API object or method name matches one of Python keywords, you can use the suffix_in their name to execute correctly:fromzabbix_utilsimportZabbixAPIapi=ZabbixAPI(url="127.0.0.1")api.login(token="xxxxxxxx")template_source=''withopen('template_example.xml',mode='r',encoding='utf-8')asf:template_source=f.read()response=api.configuration.import_(source=template_source,format="xml",rules={...})ifresponse:print("Template imported successfully")Please, refer to theZabbix API Documentationand theusing examplesfor more information.To work via Zabbix sender protocolTo send item values to a Zabbix server or a Zabbix proxy you can import and use the library as follows:fromzabbix_utilsimportSendersender=Sender(server='127.0.0.1',port=10051)response=sender.send_value('host','item.key','value',1695713666)print(response)# {"processed": 1, "failed": 0, "total": 1, "time": "0.000338", "chunk": 1}Or you can prepare a list of item values and send all at once:fromzabbix_utilsimportItemValue,Senderitems=[ItemValue('host1','item.key1',10),ItemValue('host1','item.key2','test message'),ItemValue('host2','item.key1',-1,1695713666),ItemValue('host3','item.key1','{"msg":"test message"}'),ItemValue('host2','item.key1',0,1695713666,100)]sender=Sender(server='127.0.0.1',port=10051)response=sender.send(items)print(response)# {"processed": 5, "failed": 0, "total": 5, "time": "0.001661", "chunk": 1}If you need to send values to several Zabbix clusters at once, you can do this by passing a list of Zabbix clusters:fromzabbix_utilsimportSenderzabbix_clusters=[['zabbix.cluster1.node1','zabbix.cluster1.node2:10051'],['zabbix.cluster2.node1:10051','zabbix.cluster2.node2:20051','zabbix.cluster2.node3']]sender=Sender(clusters=zabbix_clusters)response=sender.send_value('host','item.key','value',1695713666)print(response)# {"processed": 2, "failed": 0, "total": 2, "time": "0.000103", "chunk": 2}print(response.details)# {#     zabbix.cluster1.node1:10051: [{"processed": 1, "failed": 0, "total": 1, "time": "0.000050", "chunk": 1}],#     zabbix.cluster2.node2:20051: [{"processed": 1, "failed": 0, "total": 1, "time": "0.000053", "chunk": 1}]# }In such case, the value will be sent to the first available node of each cluster.Please, refer to theZabbix sender protocoland theusing examplesfor more information.To work via Zabbix get protocolTo get a value by item key from a Zabbix agent or agent 2 you can import and use the library as follows:fromzabbix_utilsimportGetteragent=Getter(host='127.0.0.1',port=10050)resp=agent.get('system.uname')print(resp.value)# Linux test_server 5.15.0-3.60.5.1.el9uek.x86_64Please, refer to theZabbix agent protocoland theusing examplesfor more information.Enabling debug logIf it needed to debug some issue with Zabbix API, sender or get you can enable the output of logging. Thezabbix_utilslibrary uses the default python logging module, but it doesn't log by default. You can define logging handler to see records from the library, for example:importloggingfromzabbix_utilsimportGetterlogging.basicConfig(format=u'[%(asctime)s]%(levelname)s%(message)s',level=logging.DEBUG)agent=Getter(host='127.0.0.1',port=10050)resp=agent.get('system.uname')print(resp.value)And then you can see records like the following:[2023-10-01 12:00:01,587] DEBUG Content of the packet: b'ZBXD\x01\x0c\x00\x00\x00\x00\x00\x00\x00system.uname'
[2023-10-01 12:00:01,722] DEBUG Zabbix response header: b'ZBXD\x01C\x00\x00\x00C\x00\x00\x00'
[2023-10-01 12:00:01,723] DEBUG Zabbix response body: Linux test_server 5.15.0-3.60.5.1.el9uek.x86_64
[2023-10-01 12:00:01,724] DEBUG Response from [127.0.0.1:10050]: Linux test_server 5.15.0-3.60.5.1.el9uek.x86_64
Linux test_server 5.15.0-3.60.5.1.el9uek.x86_64Licensezabbix_utilsis distributed under MIT License.