arpakitlib 1.8.74__py3-none-any.whl → 1.8.75__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: arpakitlib
3
- Version: 1.8.74
3
+ Version: 1.8.75
4
4
  Summary: arpakitlib
5
5
  License: Apache-2.0
6
6
  Keywords: arpakitlib,arpakit,arpakit-company,arpakitcompany,arpakit_company
@@ -374,9 +374,8 @@ arpakitlib/ar_ssh_runner_util.py,sha256=yvAwza480MkHKvLkDEsR7JNh2bYNs6P9rCVo4NA8
374
374
  arpakitlib/ar_str_util.py,sha256=NisRtt4xwE7qthkkXzL49jvGKMGlvJ4KKcvpQfmRlIw,4415
375
375
  arpakitlib/ar_type_util.py,sha256=Cs_tef-Fc5xeyAF54KgISCsP11NHyzIsglm4S3Xx7iM,4049
376
376
  arpakitlib/ar_yookassa_api_client_util.py,sha256=VozuZeCJjmLd1zj2BdC9WfiAQ3XYOrIMsdpNK-AUlm0,5347
377
- arpakitlib/ar_zabbix_api_client_util.py,sha256=_lgoDpgM8qI82HYIZylij8wml1NANMKjj_hnyg-JS3E,9449
378
- arpakitlib-1.8.74.dist-info/LICENSE,sha256=GPEDQMam2r7FSTYqM1mm7aKnxLaWcBotH7UvQtea-ec,11355
379
- arpakitlib-1.8.74.dist-info/METADATA,sha256=KOHtJC3KV5wWvlZOq8CXy20EW7vIqUdmFvGDMxBofys,3475
380
- arpakitlib-1.8.74.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
381
- arpakitlib-1.8.74.dist-info/entry_points.txt,sha256=36xqR3PJFT2kuwjkM_EqoIy0qFUDPKSm_mJaI7emewE,87
382
- arpakitlib-1.8.74.dist-info/RECORD,,
377
+ arpakitlib-1.8.75.dist-info/LICENSE,sha256=GPEDQMam2r7FSTYqM1mm7aKnxLaWcBotH7UvQtea-ec,11355
378
+ arpakitlib-1.8.75.dist-info/METADATA,sha256=d-lR8iWLib9zYbbBZ4SQIMsBlXPczbENJIofUa7z-Z4,3475
379
+ arpakitlib-1.8.75.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
380
+ arpakitlib-1.8.75.dist-info/entry_points.txt,sha256=36xqR3PJFT2kuwjkM_EqoIy0qFUDPKSm_mJaI7emewE,87
381
+ arpakitlib-1.8.75.dist-info/RECORD,,
@@ -1,289 +0,0 @@
1
- # arpakit
2
- import asyncio
3
- import logging
4
- import time
5
- from datetime import timedelta, datetime
6
- from typing import Any, Optional, Self, Iterator
7
-
8
- from pyzabbix import ZabbixAPI
9
-
10
- from arpakitlib.ar_list_util import iter_group_list
11
- from arpakitlib.ar_logging_util import setup_normal_logging
12
- from arpakitlib.ar_type_util import raise_for_type
13
-
14
- _ARPAKIT_LIB_MODULE_VERSION = "3.0"
15
-
16
-
17
- class ZabbixApiClient:
18
-
19
- def __init__(
20
- self,
21
- *,
22
- api_url: str,
23
- api_user: str,
24
- api_password: str,
25
- timeout: float | int | timedelta = timedelta(seconds=15).total_seconds()
26
- ):
27
- self._logger = logging.getLogger(self.__class__.__name__)
28
-
29
- raise_for_type(api_url, str)
30
- self.api_url = api_url
31
-
32
- raise_for_type(api_user, str)
33
- self.api_user = api_user
34
-
35
- raise_for_type(api_password, str)
36
- self.api_password = api_password
37
-
38
- if isinstance(timeout, float):
39
- timeout = timedelta(timeout)
40
- elif isinstance(timeout, int):
41
- timeout = timedelta(timeout)
42
- elif isinstance(timeout, timedelta):
43
- pass
44
- else:
45
- raise_for_type(timeout, timedelta)
46
-
47
- self.zabbix_api = ZabbixAPI(
48
- server=self.api_url,
49
- timeout=timeout.total_seconds(),
50
- )
51
- self.zabbix_api.session.verify = False
52
-
53
- self.is_logged_in = False
54
-
55
- def login(self) -> Self:
56
- self.zabbix_api.login(user=self.api_user, password=self.api_password)
57
- self.is_logged_in = True
58
- return self
59
-
60
- def login_if_not_logged_in(self):
61
- if not self.is_logged_in:
62
- self.login()
63
- return self
64
-
65
- def is_login_good(self) -> bool:
66
- try:
67
- self.login()
68
- except Exception as e:
69
- self._logger.error(e)
70
- return False
71
- return True
72
-
73
- def get_host_ids(self) -> list[str]:
74
- kwargs = {"output": ["hostid"]}
75
- self.login_if_not_logged_in()
76
- host_ids = self.zabbix_api.host.get(**kwargs)
77
- kwargs["sortfield"] = "hostid"
78
- kwargs["sortorder"] = "DESC"
79
- return [host_id["hostid"] for host_id in host_ids]
80
-
81
- def get_hosts(self, *, host_ids: Optional[list[str | int]] = None) -> list[dict[str, Any]]:
82
- kwargs = {
83
- "output": "extend",
84
- "selectInterfaces": "extend",
85
- "selectInventory": "extend",
86
- "selectMacros": "extend",
87
- "selectGroups": "extend"
88
- }
89
- if host_ids is not None:
90
- kwargs["hostids"] = host_ids
91
- kwargs["sortfield"] = "hostid"
92
- kwargs["sortorder"] = "DESC"
93
-
94
- self.login_if_not_logged_in()
95
- hosts = self.zabbix_api.host.get(**kwargs)
96
- for d in hosts:
97
- d["hostid_int"] = int(d["hostid"])
98
-
99
- return hosts
100
-
101
- def iter_all_hosts(self) -> Iterator[list[dict[str, Any]]]:
102
- host_ids = self.login_if_not_logged_in().get_host_ids()
103
- for zabbix_api_host_ids in iter_group_list(list_=host_ids, n=100):
104
- hosts = self.get_hosts(host_ids=zabbix_api_host_ids)
105
- yield hosts
106
-
107
- def iter_all_hosts_by_one(self) -> Iterator[dict[str, Any]]:
108
- for hosts in self.iter_all_hosts():
109
- for host in hosts:
110
- yield host
111
-
112
- def get_all_hosts(self) -> list[dict[str, Any]]:
113
- res = []
114
- for hosts in self.iter_all_hosts():
115
- res += hosts
116
- return res
117
-
118
- def get_item_ids(
119
- self,
120
- *,
121
- host_ids: Optional[list[str | int]] = None,
122
- keys: Optional[list[str]] = None,
123
- names: Optional[list[str]] = None,
124
- limit: Optional[int] = None
125
- ) -> list[str]:
126
- kwargs = {"output": ["itemid"]}
127
- if host_ids is not None:
128
- kwargs["hostids"] = host_ids
129
- if keys is not None:
130
- if "filter" not in kwargs.keys():
131
- kwargs["filter"] = {}
132
- if "key_" not in kwargs["filter"].keys():
133
- kwargs["filter"]["key_"] = []
134
- kwargs["filter"]["key_"] = keys
135
- if names is not None:
136
- if "filter" not in kwargs.keys():
137
- kwargs["filter"] = {}
138
- if "name" not in kwargs["filter"].keys():
139
- kwargs["filter"]["name"] = []
140
- kwargs["filter"]["name"] = names
141
- if limit is not None:
142
- kwargs["limit"] = limit
143
- kwargs["sortfield"] = "itemid"
144
- kwargs["sortorder"] = "DESC"
145
- self.login_if_not_logged_in()
146
- itemid_ids = self.zabbix_api.item.get(**kwargs)
147
- res = [d["itemid"] for d in itemid_ids]
148
- return res
149
-
150
- def get_items(
151
- self,
152
- *,
153
- host_ids: Optional[list[str | int]] = None,
154
- item_ids: Optional[list[str | int]] = None,
155
- keys: Optional[list[str]] = None,
156
- names: Optional[list[str]] = None,
157
- limit: Optional[int] = None
158
- ) -> list[dict[str, Any]]:
159
- kwargs = {
160
- "output": "extend",
161
- "selectInterfaces": "extend"
162
- }
163
- if host_ids is not None:
164
- kwargs["hostids"] = host_ids
165
- if item_ids is not None:
166
- kwargs["itemids"] = item_ids
167
- if keys is not None:
168
- if "filter" not in kwargs.keys():
169
- kwargs["filter"] = {}
170
- if "key_" not in kwargs["filter"].keys():
171
- kwargs["filter"]["key_"] = []
172
- kwargs["filter"]["key_"] = keys
173
- if names is not None:
174
- if "filter" not in kwargs.keys():
175
- kwargs["filter"] = {}
176
- if "name" not in kwargs["filter"].keys():
177
- kwargs["filter"]["name"] = []
178
- kwargs["filter"]["name"] = names
179
- if limit is not None:
180
- kwargs["limit"] = limit
181
- kwargs["sortfield"] = "itemid"
182
- kwargs["sortorder"] = "DESC"
183
- self.login_if_not_logged_in()
184
- res = self.zabbix_api.item.get(**kwargs)
185
- for d in res:
186
- d["itemid_int"] = int(d["itemid"])
187
- d["hostid_int"] = int(d["hostid"])
188
- return res
189
-
190
- def iter_all_items(
191
- self,
192
- *,
193
- host_ids: Optional[list[str | int]] = None,
194
- keys: Optional[list[str]] = None,
195
- names: Optional[list[str]] = None
196
- ) -> Iterator[list[dict[str, Any]]]:
197
- item_ids = self.get_item_ids(
198
- host_ids=host_ids,
199
- keys=keys,
200
- names=names
201
- )
202
- for item_ids_ in iter_group_list(item_ids, n=100):
203
- yield self.get_items(item_ids=item_ids_)
204
-
205
- def iter_all_items_by_one(
206
- self,
207
- *,
208
- host_ids: Optional[list[str | int]] = None,
209
- keys: Optional[list[str]] = None,
210
- names: Optional[list[str]] = None
211
- ) -> Iterator[dict[str, Any]]:
212
- for items in self.iter_all_items(host_ids=host_ids, keys=keys, names=names):
213
- for item in items:
214
- yield item
215
-
216
- def get_all_items(
217
- self,
218
- *,
219
- host_ids: list[str | int] | None = None,
220
- keys: list[str] | None = None,
221
- names: list[str] | None = None
222
- ) -> list[dict[str, Any]]:
223
- return [
224
- item
225
- for item in self.iter_all_items_by_one(host_ids=host_ids, keys=keys, names=names)
226
- ]
227
-
228
- def get_histories(
229
- self,
230
- *,
231
- host_ids: Optional[list[str | int]] = None,
232
- item_ids: Optional[list[str | int]] = None,
233
- limit: Optional[int] = None,
234
- history: int | None = None,
235
- time_from: Optional[datetime] = None,
236
- time_till: Optional[datetime] = None
237
- ) -> list[dict[str, Any]]:
238
- kwargs = {
239
- "output": "extend"
240
- }
241
- if host_ids is not None:
242
- kwargs["hostids"] = host_ids
243
- if item_ids is not None:
244
- kwargs["itemids"] = item_ids
245
- if limit is not None:
246
- kwargs["limit"] = limit
247
- if history is not None:
248
- kwargs["history"] = history
249
- if time_from is not None:
250
- kwargs["time_from"] = int(time.mktime((
251
- time_from.year, time_from.month, time_from.day, time_from.hour, time_from.minute, time_from.second, 0,
252
- 0, 0
253
- )))
254
- if time_till is not None:
255
- kwargs["time_till"] = int(time.mktime((
256
- time_till.year, time_till.month, time_till.day, time_till.hour, time_till.minute, time_till.second, 0,
257
- 0, 0
258
- )))
259
-
260
- self.login_if_not_logged_in()
261
- histories: list[dict[str, Any]] = self.zabbix_api.history.get(**kwargs)
262
-
263
- for history in histories:
264
- if "clock" in history.keys():
265
- clock_ns_as_datetime = datetime.fromtimestamp(int(history["clock"]))
266
- if "ns" in history.keys():
267
- clock_ns_as_datetime += timedelta(microseconds=int(history["ns"]) / 1000)
268
- history["dt"] = clock_ns_as_datetime.isoformat()
269
- history["assembled_key"] = (
270
- f"{history["clock"]}_{history["ns"]}_{history["value"]}_{history["itemid"]}"
271
- )
272
-
273
- return histories
274
-
275
-
276
- ZabbixAPIClient = ZabbixApiClient
277
-
278
-
279
- def __example():
280
- setup_normal_logging()
281
-
282
-
283
- async def __async_example():
284
- pass
285
-
286
-
287
- if __name__ == '__main__':
288
- __example()
289
- asyncio.run(__async_example())