|
| 1 | +# Zabbix utils library |
| 2 | + |
| 3 | +**zabbix_utils** is a Python library for working with [Zabbix API](https://www.zabbix.com/documentation/current/manual/api/reference) as well as with [Zabbix sender](https://www.zabbix.com/documentation/current/manpages/zabbix_sender) and [Zabbix get](https://www.zabbix.com/documentation/current/manpages/zabbix_get) protocols. |
| 4 | + |
| 5 | +## Get started |
| 6 | +* [Requirements](#requirements) |
| 7 | +* [Installation](#installation) |
| 8 | +* [Zabbix API](#to-work-with-zabbix-api) |
| 9 | +* [Zabbix Sender](#to-work-via-zabbix-sender-protocol) |
| 10 | +* [Zabbix Get](#to-work-via-zabbix-get-protocol) |
| 11 | +* [Debug log](#enabling-debug-log) |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +Supported versions: |
| 16 | + |
| 17 | +* Zabbix 5.0+ |
| 18 | +* Python 3.10+ |
| 19 | + |
| 20 | +Tested on: |
| 21 | + |
| 22 | +* Zabbix 4.0, 5.0, 6.0, 6.4 and 7.0 |
| 23 | +* Python 3.7, 3.8, 3.9, 3.10 and 3.11 |
| 24 | + |
| 25 | +## Documentation |
| 26 | + |
| 27 | +### Installation |
| 28 | + |
| 29 | +Install **zabbix_utils** library using pip: |
| 30 | + |
| 31 | +```bash |
| 32 | +$ pip install zabbix_utils |
| 33 | +``` |
| 34 | + |
| 35 | +### Use cases |
| 36 | + |
| 37 | +##### To work with Zabbix API |
| 38 | + |
| 39 | +To work with Zabbix API you can import and use **zabbix_utils** library as follows: |
| 40 | + |
| 41 | +```python |
| 42 | +from zabbix_utils import ZabbixAPI |
| 43 | + |
| 44 | +api = ZabbixAPI(url="127.0.0.1") |
| 45 | +api.login(user="User", password="zabbix") |
| 46 | + |
| 47 | +users = api.user.get( |
| 48 | + output=['userid', 'alias'] |
| 49 | +) |
| 50 | + |
| 51 | +for user in users: |
| 52 | + print(user['alias']) |
| 53 | + |
| 54 | +api.logout() |
| 55 | +``` |
| 56 | + |
| 57 | +You can also authenticate using an API token (supported since Zabbix 5.4): |
| 58 | + |
| 59 | +```python |
| 60 | +from zabbix_utils import ZabbixAPI |
| 61 | + |
| 62 | +api = ZabbixAPI(url="127.0.0.1") |
| 63 | +api.login(token="xxxxxxxx") |
| 64 | + |
| 65 | +users = api.user.get( |
| 66 | + output=['userid', 'alias'] |
| 67 | +) |
| 68 | + |
| 69 | +for user in users: |
| 70 | + print(user['alias']) |
| 71 | + |
| 72 | +api.logout() |
| 73 | +``` |
| 74 | + |
| 75 | +You can compare Zabbix API version with strings and numbers, for example: |
| 76 | + |
| 77 | +```python |
| 78 | +from zabbix_utils import ZabbixAPI |
| 79 | + |
| 80 | +url = "127.0.0.1" |
| 81 | +user = "User" |
| 82 | +password = "zabbix" |
| 83 | + |
| 84 | +api = ZabbixAPI(url=url, user=user, password=password) |
| 85 | + |
| 86 | +# Method to get version |
| 87 | +ver = api.api_version() |
| 88 | +print(type(ver).__name__, ver) # ZabbixAPIVersion 7.0.0 |
| 89 | + |
| 90 | +# ZabbixAPI prototype with version |
| 91 | +ver = api.version |
| 92 | +print(type(ver).__name__, ver) # ZabbixAPIVersion 7.0.0 |
| 93 | + |
| 94 | +# Comparing versions |
| 95 | +print(ver > 6.0) # True |
| 96 | +print(ver != 7.0) # False |
| 97 | +print(ver <= 7) # True |
| 98 | +print(ver != "7.0.0") # False |
| 99 | + |
| 100 | +# Version additional methods |
| 101 | +print(ver.major) # 7.0 |
| 102 | +print(ver.minor) # 0 |
| 103 | +print(ver.is_lts()) # True |
| 104 | + |
| 105 | +api.logout() |
| 106 | +``` |
| 107 | + |
| 108 | +> Please, refer to the [Zabbix API Documentation](https://www.zabbix.com/documentation/current/manual/api/reference) and the [using examples](https://github.com/zabbix/python-zabbix-utils/tree/master/examples/api) for more information. |
| 109 | +
|
| 110 | +##### To work via Zabbix sender protocol |
| 111 | + |
| 112 | +To send item values to a Zabbix server or a Zabbix proxy you can import and use the library as follows: |
| 113 | + |
| 114 | +```python |
| 115 | +from zabbix_utils import ZabbixSender |
| 116 | + |
| 117 | +sender = ZabbixSender(server='127.0.0.1', port=10051) |
| 118 | +resp = sender.send_value('host', 'item.key', 'value', 1695713666) |
| 119 | + |
| 120 | +print(resp) |
| 121 | +# {"processed": 1, "failed": 0, "total": 1, "time": "0.000338", "chunk": 1} |
| 122 | +``` |
| 123 | + |
| 124 | +Or you can prepare a list of item values and send all at once: |
| 125 | + |
| 126 | +```python |
| 127 | +from zabbix_utils import ZabbixItem, ZabbixSender |
| 128 | + |
| 129 | +items = [ |
| 130 | + ZabbixItem('host1', 'item.key1', 10), |
| 131 | + ZabbixItem('host1', 'item.key2', 'test message'), |
| 132 | + ZabbixItem('host2', 'item.key1', -1, 1695713666), |
| 133 | + ZabbixItem('host3', 'item.key1', '{"msg":"test message"}'), |
| 134 | + ZabbixItem('host2', 'item.key1', 0, 1695713666, 100) |
| 135 | +] |
| 136 | + |
| 137 | +sender = ZabbixSender(server='127.0.0.1', port=10051) |
| 138 | +chunks_resp = sender.send(items) |
| 139 | + |
| 140 | +print(chunks_resp) |
| 141 | +# [{"processed": 5, "failed": 0, "total": 5, "time": "0.001661", "chunk": 1}] |
| 142 | +``` |
| 143 | + |
| 144 | +> Please, refer to the [Zabbix sender protocol](https://www.zabbix.com/documentation/current/manual/appendix/protocols/zabbix_sender) and the [using examples](https://github.com/zabbix/python-zabbix-utils/tree/master/examples/sender) for more information. |
| 145 | +
|
| 146 | +##### To work via Zabbix get protocol |
| 147 | + |
| 148 | +To get a value by item key from a Zabbix agent or agent 2 you can import and use the library as follows: |
| 149 | + |
| 150 | +```python |
| 151 | +from zabbix_utils import ZabbixGet |
| 152 | + |
| 153 | +agent = ZabbixGet(host='127.0.0.1', port=10050) |
| 154 | +resp = agent.get('system.uname') |
| 155 | + |
| 156 | +print(resp) |
| 157 | +# Linux test_server 5.15.0-3.60.5.1.el9uek.x86_64 |
| 158 | +``` |
| 159 | + |
| 160 | +> Please, refer to the [Zabbix agent protocol](https://www.zabbix.com/documentation/current/manual/appendix/protocols/zabbix_agent) and the [using examples](https://github.com/zabbix/python-zabbix-utils/tree/master/examples/get) for more information. |
| 161 | +
|
| 162 | +### Enabling debug log |
| 163 | + |
| 164 | +If it needed to debug some issue with Zabbix API, sender or get you can enable the output of logging. The **zabbix_utils** library uses the default python logging module, but it logs to the `null` by default. You can define logging handler to see records from the library, for example: |
| 165 | + |
| 166 | +```python |
| 167 | +import logging |
| 168 | +from zabbix_utils import ZabbixGet |
| 169 | + |
| 170 | +logging.basicConfig( |
| 171 | + format=u'[%(asctime)s] %(levelname)s %(message)s', |
| 172 | + level=logging.DEBUG |
| 173 | +) |
| 174 | + |
| 175 | +agent = ZabbixGet(host='127.0.0.1', port=10050) |
| 176 | +resp = agent.get('system.uname') |
| 177 | + |
| 178 | +print(resp) |
| 179 | +``` |
| 180 | + |
| 181 | +And then you can see records like the following: |
| 182 | + |
| 183 | +``` |
| 184 | +[2023-10-01 12:00:01,587] DEBUG Content of the packet: b'ZBXD\x01\x0c\x00\x00\x00\x00\x00\x00\x00system.uname' |
| 185 | +[2023-10-01 12:00:01,722] DEBUG Zabbix response header: b'ZBXD\x01C\x00\x00\x00C\x00\x00\x00' |
| 186 | +[2023-10-01 12:00:01,723] DEBUG Zabbix response body: Linux test_server 5.15.0-3.60.5.1.el9uek.x86_64 |
| 187 | +[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 |
| 188 | +Linux test_server 5.15.0-3.60.5.1.el9uek.x86_64 |
| 189 | +
|
| 190 | +``` |
| 191 | + |
| 192 | +## License |
| 193 | +**zabbix_utils** is distributed under MIT License. |
0 commit comments