pytbox 0.0.3__tar.gz → 0.0.4__tar.gz
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.
Potentially problematic release.
This version of pytbox might be problematic. Click here for more details.
- {pytbox-0.0.3 → pytbox-0.0.4}/PKG-INFO +1 -1
- {pytbox-0.0.3 → pytbox-0.0.4}/pyproject.toml +1 -1
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/database/victoriametrics.py +39 -3
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox.egg-info/PKG-INFO +1 -1
- pytbox-0.0.4/tests/test_victoriametrics.py +18 -0
- pytbox-0.0.3/tests/test_victoriametrics.py +0 -15
- {pytbox-0.0.3 → pytbox-0.0.4}/README.md +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/setup.cfg +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/alert/alert_handler.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/alert/ping.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/alicloud/sls.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/base.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/common/__init__.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/common/base.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/database/mongo.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/dida365.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/feishu/client.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/feishu/endpoints.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/feishu/errors.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/feishu/helpers.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/feishu/typing.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/log/logger.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/log/victorialog.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/onepassword_connect.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/onepassword_sa.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/utils/env.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/utils/load_config.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/utils/ping_checker.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/utils/response.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox/utils/timeutils.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox.egg-info/SOURCES.txt +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox.egg-info/dependency_links.txt +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox.egg-info/requires.txt +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/src/pytbox.egg-info/top_level.txt +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/tests/test_feishu.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/tests/test_logger.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/tests/test_onepassword_connect.py +0 -0
- {pytbox-0.0.3 → pytbox-0.0.4}/tests/test_onepassword_sa.py +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "pytbox"
|
|
7
|
-
version = "0.0.
|
|
7
|
+
version = "0.0.4"
|
|
8
8
|
description = "A collection of Python integrations and utilities (Feishu, Dida365, VictoriaMetrics, ...)"
|
|
9
9
|
authors = [{ name = "mingming hou", email = "houm01@foxmail.com" }]
|
|
10
10
|
license = { text = "MIT" }
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
2
|
|
|
3
|
+
from typing import Literal, Optional
|
|
3
4
|
import requests
|
|
4
5
|
from ..utils.response import ReturnResponse
|
|
5
6
|
|
|
@@ -38,7 +39,16 @@ class VictoriaMetrics:
|
|
|
38
39
|
return ReturnResponse(code=2, msg=f"[{query}] 没有查询到结果", data=r.json())
|
|
39
40
|
else:
|
|
40
41
|
return ReturnResponse(code=1, msg=f"[{query}] 查询失败: {r.json().get('error')}", data=r.json())
|
|
41
|
-
|
|
42
|
+
|
|
43
|
+
def get_labels(self, metric_name: str) -> ReturnResponse:
|
|
44
|
+
url = f"{self.url}/api/v1/series?match[]={metric_name}"
|
|
45
|
+
response = requests.get(url, timeout=self.timeout)
|
|
46
|
+
results = response.json()
|
|
47
|
+
if results['status'] == 'success':
|
|
48
|
+
return ReturnResponse(code=0, msg=f"metric name: {metric_name} 获取到 {len(results['data'])} 条数据", data=results['data'])
|
|
49
|
+
else:
|
|
50
|
+
return ReturnResponse(code=1, msg=f"metric name: {metric_name} 查询失败")
|
|
51
|
+
|
|
42
52
|
def check_ping_result(self, target: str, last_minute: int=10) -> ReturnResponse:
|
|
43
53
|
'''
|
|
44
54
|
检查ping结果
|
|
@@ -60,7 +70,7 @@ class VictoriaMetrics:
|
|
|
60
70
|
msg = f"已检查 {target} 最近 {last_minute} 分钟是正常的!"
|
|
61
71
|
else:
|
|
62
72
|
if all(str(item[1]) == "1" for item in values):
|
|
63
|
-
code =
|
|
73
|
+
code = 1
|
|
64
74
|
msg = f"已检查 {target} 最近 {last_minute} 分钟是异常的!"
|
|
65
75
|
else:
|
|
66
76
|
code = 0
|
|
@@ -74,4 +84,30 @@ class VictoriaMetrics:
|
|
|
74
84
|
except KeyError:
|
|
75
85
|
data = r.data
|
|
76
86
|
|
|
77
|
-
return ReturnResponse(code=code, msg=msg, data=data)
|
|
87
|
+
return ReturnResponse(code=code, msg=msg, data=data)
|
|
88
|
+
|
|
89
|
+
def query_interface_rate(self,
|
|
90
|
+
direction: Literal['in', 'out'],
|
|
91
|
+
sysName: str,
|
|
92
|
+
ifName:str,
|
|
93
|
+
last_minutes: Optional[int] = None
|
|
94
|
+
) -> ReturnResponse:
|
|
95
|
+
"""查询指定设备的入方向总流量速率(bps)。
|
|
96
|
+
|
|
97
|
+
使用 PromQL 对 `snmp_interface_ifHCInOctets` 进行速率计算并聚合到设备级别,
|
|
98
|
+
将结果从字节每秒转换为比特每秒(乘以 8)。
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
sysName: 设备 `sysName` 标签值。
|
|
102
|
+
last_minutes: 计算速率的时间窗口(分钟)。未提供时默认使用 5 分钟窗口。
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
ReturnResponse: 查询结果包装。
|
|
106
|
+
"""
|
|
107
|
+
if direction == 'in':
|
|
108
|
+
query = f'(rate(snmp_interface_ifHCInOctets{{sysName="{sysName}", ifName="{ifName}"}}[{last_minutes}m])) * 8 / 1000000'
|
|
109
|
+
else:
|
|
110
|
+
query = f'(rate(snmp_interface_ifHCOutOctets{{sysName="{sysName}", ifName="{ifName}"}}[{last_minutes}m])) * 8 / 1000000'
|
|
111
|
+
r = self.query(query)
|
|
112
|
+
rate = r.data['values'][1]
|
|
113
|
+
return int(float(rate))
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# from .base import vm
|
|
5
|
+
from base import vm
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def test_query():
|
|
10
|
+
r = vm.query('ping_average_response_ms')
|
|
11
|
+
print(r)
|
|
12
|
+
|
|
13
|
+
def test_get_labels():
|
|
14
|
+
r = vm.get_labels('ping_average_response_ms')
|
|
15
|
+
print(r)
|
|
16
|
+
|
|
17
|
+
if __name__ == "__main__":
|
|
18
|
+
test_get_labels()
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
|
|
3
|
-
import os
|
|
4
|
-
from pytbox.victoriametrics import VictoriaMetrics
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
vm = VictoriaMetrics(url=os.getenv("VICTORIAMETRICS_URL", "http://localhost:8428"))
|
|
8
|
-
|
|
9
|
-
def test_query():
|
|
10
|
-
r = vm.query('ping_average_response_ms')
|
|
11
|
-
print(r)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if __name__ == "__main__":
|
|
15
|
-
test_query()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|