pytbox 0.0.3__py3-none-any.whl → 0.0.4__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.

Potentially problematic release.


This version of pytbox might be problematic. Click here for more details.

@@ -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 = 2
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))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytbox
3
- Version: 0.0.3
3
+ Version: 0.0.4
4
4
  Summary: A collection of Python integrations and utilities (Feishu, Dida365, VictoriaMetrics, ...)
5
5
  Author-email: mingming hou <houm01@foxmail.com>
6
6
  License: MIT
@@ -8,7 +8,7 @@ pytbox/alicloud/sls.py,sha256=UR4GdI86dCKAFI2xt_1DELu7q743dpd3xrYtuNpfC5A,4065
8
8
  pytbox/common/__init__.py,sha256=3JWfgCQZKZuSH5NCE7OCzKwq82pkyop9l7sH5YSNyfU,122
9
9
  pytbox/common/base.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  pytbox/database/mongo.py,sha256=QyCeSoyWoJdIFXxjDmbV5t-xU7wN3x-Ig0I8pvRmBwg,1864
11
- pytbox/database/victoriametrics.py,sha256=qs3IQZbcKGSIObQcwUujZEyPshq2yOIVvnc4yfTkXJ0,2642
11
+ pytbox/database/victoriametrics.py,sha256=fh6wghcWLhQxizVljC6X22gqXr2CioB1L4qixSi33Ms,4388
12
12
  pytbox/feishu/client.py,sha256=kwGLseGT_iQUFmSqpuS2_77WmxtHstD64nXvktuQ3B4,5865
13
13
  pytbox/feishu/endpoints.py,sha256=z_nHGXAjlIyYdFnbAWY4hfzzHrENJvoNOqY9sA6-FLk,40009
14
14
  pytbox/feishu/errors.py,sha256=79qFAHZw7jDj3gnWAjI1-W4tB0q1_aSfdjee4xzXeuI,1179
@@ -21,7 +21,7 @@ pytbox/utils/load_config.py,sha256=wNCDPLH7xet5b9pUlTz6VsBejRsJZ7LP85wWMaITBYg,3
21
21
  pytbox/utils/ping_checker.py,sha256=Nqnn8clbgv-5l0PgxcTOldg8mkMKrFn4TvPL-rYUUGg,1
22
22
  pytbox/utils/response.py,sha256=kXjlwt0WVmLRam2eu1shzX2cQ7ux4cCQryaPGYwle5g,1247
23
23
  pytbox/utils/timeutils.py,sha256=7ZI-TU2FCUV0Gyc_QUjhSreto8SVIs54e2CZgGix5pI,1628
24
- pytbox-0.0.3.dist-info/METADATA,sha256=WWQcYi7uThTfbjMuhEsV9vKgqInlBZWn1VJr-0llPDQ,6006
25
- pytbox-0.0.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
26
- pytbox-0.0.3.dist-info/top_level.txt,sha256=YADgWue-Oe128ptN3J2hS3GB0Ncc5uZaSUM3e1rwswE,7
27
- pytbox-0.0.3.dist-info/RECORD,,
24
+ pytbox-0.0.4.dist-info/METADATA,sha256=bgImPZ1wSfkX-ARmOiiBDaRwrrdHqf9sgzcrs5uGUcw,6006
25
+ pytbox-0.0.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
26
+ pytbox-0.0.4.dist-info/top_level.txt,sha256=YADgWue-Oe128ptN3J2hS3GB0Ncc5uZaSUM3e1rwswE,7
27
+ pytbox-0.0.4.dist-info/RECORD,,
File without changes