pytbox 0.1.9__py3-none-any.whl → 0.2.1__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.
- pytbox/alert/alert_handler.py +6 -4
- pytbox/base.py +4 -3
- pytbox/database/victoriametrics.py +31 -0
- pytbox/excel.py +64 -0
- pytbox/log/logger.py +7 -6
- {pytbox-0.1.9.dist-info → pytbox-0.2.1.dist-info}/METADATA +1 -1
- {pytbox-0.1.9.dist-info → pytbox-0.2.1.dist-info}/RECORD +10 -10
- {pytbox-0.1.9.dist-info → pytbox-0.2.1.dist-info}/WHEEL +0 -0
- {pytbox-0.1.9.dist-info → pytbox-0.2.1.dist-info}/entry_points.txt +0 -0
- {pytbox-0.1.9.dist-info → pytbox-0.2.1.dist-info}/top_level.txt +0 -0
pytbox/alert/alert_handler.py
CHANGED
|
@@ -20,6 +20,7 @@ class AlertHandler:
|
|
|
20
20
|
mail_client: MailClient=None,
|
|
21
21
|
env: Literal['dev', 'prod']='prod'
|
|
22
22
|
):
|
|
23
|
+
|
|
23
24
|
self.config = config
|
|
24
25
|
self.mongo = mongo_client
|
|
25
26
|
self.feishu = feishu_client
|
|
@@ -95,14 +96,15 @@ class AlertHandler:
|
|
|
95
96
|
if event_type == "trigger":
|
|
96
97
|
self.mail.send_mail(
|
|
97
98
|
receiver=[self.config['mail']['mail_address']],
|
|
98
|
-
subject=f
|
|
99
|
-
contents=f
|
|
99
|
+
subject=f"{self.config['mail']['subject_trigger']}, {event_content}",
|
|
100
|
+
contents=f"event_content:{event_content}, alarm_time: {str(event_time)}, event_id: {event_id}, alarm_name: {event_name}, entity_name: {entity_name}, priority: {priority}, automate_ts: {troubleshot}, suggestion: {suggestion}"
|
|
100
101
|
)
|
|
101
102
|
else:
|
|
102
103
|
self.mail.send_mail(
|
|
103
104
|
receiver=[self.config['mail']['mail_address']],
|
|
104
|
-
subject=f
|
|
105
|
-
contents=f
|
|
105
|
+
subject=f"{self.config['mail']['subject_resolved']}, {event_content}",
|
|
106
|
+
contents=f"event_content:{event_content}, alarm_time: {str(TimeUtils.get_now_time_mongo())}, event_id: {event_id}, alarm_name: {event_name}, entity_name: {entity_name}, priority: {priority}, automate_ts: {troubleshot}, suggestion: {suggestion}"
|
|
107
|
+
)
|
|
106
108
|
|
|
107
109
|
if self.config['dida']['enable_alert']:
|
|
108
110
|
if event_type == "trigger":
|
pytbox/base.py
CHANGED
|
@@ -43,6 +43,7 @@ dida = Dida365(
|
|
|
43
43
|
access_token=config['dida']['access_token']
|
|
44
44
|
)
|
|
45
45
|
|
|
46
|
+
|
|
46
47
|
alert_handler = AlertHandler(config=config, mongo_client=get_mongo('alert_test'), feishu_client=feishu, dida_client=dida)
|
|
47
48
|
|
|
48
49
|
def get_logger(app):
|
|
@@ -98,9 +99,9 @@ pyjira = PyJira(
|
|
|
98
99
|
token=config['jira']['token']
|
|
99
100
|
)
|
|
100
101
|
|
|
101
|
-
mail_163 = MailClient(mail_address=config['mail']['163']['mail_address'], password=config['mail']['163']['password'])
|
|
102
|
-
mail_qq = MailClient(mail_address=config['mail']['qq']['mail_address'], password=config['mail']['qq']['password'])
|
|
103
|
-
ali_mail = AliMail(mail_address=config['mail']['aliyun']['mail_address'], client_id=config['mail']['aliyun']['client_id'], client_secret=config['mail']['aliyun']['client_secret'])
|
|
102
|
+
# mail_163 = MailClient(mail_address=config['mail']['163']['mail_address'], password=config['mail']['163']['password'])
|
|
103
|
+
# mail_qq = MailClient(mail_address=config['mail']['qq']['mail_address'], password=config['mail']['qq']['password'])
|
|
104
|
+
# ali_mail = AliMail(mail_address=config['mail']['aliyun']['mail_address'], client_id=config['mail']['aliyun']['client_id'], client_secret=config['mail']['aliyun']['client_secret'])
|
|
104
105
|
|
|
105
106
|
sls = AliCloudSls(
|
|
106
107
|
access_key_id=config['alicloud']['account1']['access_key_id'],
|
|
@@ -230,6 +230,37 @@ class VictoriaMetrics:
|
|
|
230
230
|
rate = r.data[0]['value'][1]
|
|
231
231
|
return int(float(rate))
|
|
232
232
|
|
|
233
|
+
def check_interface_avg_rate(self,
|
|
234
|
+
direction: Literal['in', 'out'],
|
|
235
|
+
sysname: str,
|
|
236
|
+
ifname:str,
|
|
237
|
+
last_hours: Optional[int] = 24,
|
|
238
|
+
last_minutes: Optional[int] = 5,
|
|
239
|
+
) -> ReturnResponse:
|
|
240
|
+
'''
|
|
241
|
+
_summary_
|
|
242
|
+
|
|
243
|
+
Args:
|
|
244
|
+
direction (Literal['in', 'out']): _description_
|
|
245
|
+
sysname (str): _description_
|
|
246
|
+
ifname (str): _description_
|
|
247
|
+
last_hours (Optional[int], optional): _description_. Defaults to 24.
|
|
248
|
+
last_minutes (Optional[int], optional): _description_. Defaults to 5.
|
|
249
|
+
|
|
250
|
+
Returns:
|
|
251
|
+
ReturnResponse: _description_
|
|
252
|
+
'''
|
|
253
|
+
if direction == 'in':
|
|
254
|
+
query = f'avg_over_time(rate(snmp_interface_ifHCInOctets{{sysName="{sysname}", ifName="{ifname}"}}[{last_minutes}m]) * 8 [{last_hours}h:]) / 1e6'
|
|
255
|
+
else:
|
|
256
|
+
query = f'avg_over_time(rate(snmp_interface_ifHCOutOctets{{sysName="{sysname}", ifName="{ifname}"}}[{last_minutes}m]) * 8 [{last_hours}h:]) / 1e6'
|
|
257
|
+
r = self.query(query)
|
|
258
|
+
try:
|
|
259
|
+
rate = r.data[0]['value'][1]
|
|
260
|
+
return ReturnResponse(code=0, msg=f"查询 {sysname} {ifname} 最近 {last_hours} 小时平均速率为 {round(float(rate), 2)} Mbit/s", data=round(float(rate), 2))
|
|
261
|
+
except KeyError:
|
|
262
|
+
return ReturnResponse(code=1, msg=f"查询 {sysname} {ifname} 最近 {last_hours} 小时平均速率为 0 Mbit/s")
|
|
263
|
+
|
|
233
264
|
def check_snmp_port_status(self, sysname: str=None, if_name: str=None, last_minute: int=5, dev_file: str=None) -> ReturnResponse:
|
|
234
265
|
'''
|
|
235
266
|
查询端口状态
|
pytbox/excel.py
CHANGED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+
from openpyxl.styles import Alignment
|
|
4
|
+
from openpyxl.styles import PatternFill
|
|
5
|
+
from openpyxl.styles.differential import DifferentialStyle
|
|
6
|
+
from openpyxl.styles import Font
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ExcelFormat:
|
|
10
|
+
def __init__(self, ws):
|
|
11
|
+
self.ws = ws
|
|
12
|
+
|
|
13
|
+
def set_column(self):
|
|
14
|
+
for column in self.ws.columns:
|
|
15
|
+
max_length = 0
|
|
16
|
+
column = [cell for cell in column]
|
|
17
|
+
for cell in column:
|
|
18
|
+
try:
|
|
19
|
+
if len(str(cell.value)) > max_length:
|
|
20
|
+
max_length = len(cell.value)
|
|
21
|
+
except:
|
|
22
|
+
pass
|
|
23
|
+
adjusted_width = (max_length + 10)
|
|
24
|
+
self.ws.column_dimensions[column[0].column_letter].width = adjusted_width
|
|
25
|
+
|
|
26
|
+
def set_rows_center(self):
|
|
27
|
+
# 将所有单元格的文字居中
|
|
28
|
+
for row in self.ws.iter_rows():
|
|
29
|
+
|
|
30
|
+
for cell in row:
|
|
31
|
+
cell.alignment = Alignment(horizontal='center', vertical='center')
|
|
32
|
+
for row in self.ws.iter_rows():
|
|
33
|
+
self.ws.row_dimensions[row[0].row].height = 24
|
|
34
|
+
|
|
35
|
+
def set_freeze_first_row(self):
|
|
36
|
+
"""设置首行锁定/冻结首行"""
|
|
37
|
+
# 冻结首行,从第二行开始滚动
|
|
38
|
+
self.ws.freeze_panes = 'A2'
|
|
39
|
+
|
|
40
|
+
def set_freeze_first_column(self):
|
|
41
|
+
"""设置首列锁定/冻结首列"""
|
|
42
|
+
# 冻结首列,从第二列开始滚动
|
|
43
|
+
self.ws.freeze_panes = 'B1'
|
|
44
|
+
|
|
45
|
+
def set_first_row_bold_color(self, font_color='FF0000FF'):
|
|
46
|
+
"""设置首行字体加粗并改变颜色
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
font_color (str): 字体颜色的十六进制代码,默认为蓝色(FF0000FF)
|
|
50
|
+
格式:AARRGGBB 或 RRGGBB
|
|
51
|
+
例如:'FF0000FF'(蓝色), 'FFFF0000'(红色), 'FF008000'(绿色)
|
|
52
|
+
"""
|
|
53
|
+
# 遍历首行的所有单元格
|
|
54
|
+
for cell in self.ws[1]:
|
|
55
|
+
if cell.value is not None: # 只对有内容的单元格设置样式
|
|
56
|
+
# 设置字体为粗体并改变颜色
|
|
57
|
+
cell.font = Font(bold=True, color=font_color)
|
|
58
|
+
|
|
59
|
+
def set_freeze_first_row_and_column(self):
|
|
60
|
+
"""同时冻结首行和首列"""
|
|
61
|
+
# 冻结首行首列,从第二行第二列开始滚动
|
|
62
|
+
self.ws.freeze_panes = 'B2'
|
|
63
|
+
|
|
64
|
+
|
pytbox/log/logger.py
CHANGED
|
@@ -152,12 +152,13 @@ class AppLogger:
|
|
|
152
152
|
f"**function_name**: {caller_function}"
|
|
153
153
|
]
|
|
154
154
|
|
|
155
|
-
self.dida
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
155
|
+
if self.dida:
|
|
156
|
+
self.dida.task_create(
|
|
157
|
+
project_id="65e87d2b3e73517c2cdd9d63",
|
|
158
|
+
title=f"自动化脚本告警: {self.app_name}",
|
|
159
|
+
content="\n".join(dida_content_list),
|
|
160
|
+
tags=['L-程序告警', 't-问题处理']
|
|
161
|
+
)
|
|
161
162
|
|
|
162
163
|
def critical(self, message: str):
|
|
163
164
|
"""记录严重错误级别日志"""
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
pytbox/base.py,sha256=
|
|
1
|
+
pytbox/base.py,sha256=VAAaf081rTih9_FjOvAvcihbXp-zwmbGvASnoB2mNZo,4254
|
|
2
2
|
pytbox/cli.py,sha256=N775a0GK80IT2lQC2KRYtkZpIiu9UjavZmaxgNUgJhQ,160
|
|
3
3
|
pytbox/dida365.py,sha256=pUMPB9AyLZpTTbaz2LbtzdEpyjvuGf4YlRrCvM5sbJo,10545
|
|
4
|
-
pytbox/excel.py,sha256=
|
|
4
|
+
pytbox/excel.py,sha256=f5XBLCeJbGgxytoSwVhbk03WLTzz8Q3IJ_RZ2-r_w6A,2334
|
|
5
5
|
pytbox/onepassword_connect.py,sha256=nD3xTl1ykQ4ct_dCRRF138gXCtk-phPfKYXuOn-P7Z8,3064
|
|
6
6
|
pytbox/onepassword_sa.py,sha256=08iUcYud3aEHuQcUsem9bWNxdXKgaxFbMy9yvtr-DZQ,6995
|
|
7
7
|
pytbox/pyjira.py,sha256=TMy34Rtu7OXRA8wpUuLsFeyIQfRNUn2ed2no00L8YSE,22470
|
|
8
8
|
pytbox/vmware.py,sha256=WiH67_3-VCBjXJuh3UueOc31BdZDItiZhkeuPzoRhw4,3975
|
|
9
|
-
pytbox/alert/alert_handler.py,sha256=
|
|
9
|
+
pytbox/alert/alert_handler.py,sha256=WCn4cKahv5G5BFGgmc7dX7BQ38h2kxTgxfRVTwc1O2M,6579
|
|
10
10
|
pytbox/alert/ping.py,sha256=KEnnXdIRJHvR_rEHPWLBt0wz4cGwmA29Lenlak3Z_1Y,778
|
|
11
11
|
pytbox/alicloud/sls.py,sha256=-r6rbCwDUQ4jwAgSVSNu7B2h1MNg20CrRIKnlXVeY9w,4159
|
|
12
12
|
pytbox/categraf/build_config.py,sha256=9G85rLqkz3lchpH7ef0LbvckYHl0nRA6mHVLeUfs9Mw,6308
|
|
@@ -43,13 +43,13 @@ pytbox/cli/formatters/__init__.py,sha256=4o85w4j-A-O1oBLvuE9q8AFiJ2C9rvB3MIKsy5V
|
|
|
43
43
|
pytbox/cli/formatters/output.py,sha256=h5WhZlQk1rjmxEj88Jy5ODLcv6L5zfGUhks_3AWIkKU,5455
|
|
44
44
|
pytbox/common/__init__.py,sha256=3JWfgCQZKZuSH5NCE7OCzKwq82pkyop9l7sH5YSNyfU,122
|
|
45
45
|
pytbox/database/mongo.py,sha256=AhJ9nCAQHKrrcL-ujeonOwEf3x2QkmT2VhoCdglqJmU,3478
|
|
46
|
-
pytbox/database/victoriametrics.py,sha256=
|
|
46
|
+
pytbox/database/victoriametrics.py,sha256=othAjqn7Cmgc-cp-TK-LIX5fGLT6fkgn6RVr4b4e2Mg,13007
|
|
47
47
|
pytbox/feishu/client.py,sha256=kwGLseGT_iQUFmSqpuS2_77WmxtHstD64nXvktuQ3B4,5865
|
|
48
48
|
pytbox/feishu/endpoints.py,sha256=z3nPOZPC2JGDJlO7SusWBpRA33hZZ4Z-GBhI6F8L_u4,40240
|
|
49
49
|
pytbox/feishu/errors.py,sha256=79qFAHZw7jDj3gnWAjI1-W4tB0q1_aSfdjee4xzXeuI,1179
|
|
50
50
|
pytbox/feishu/helpers.py,sha256=jhSkHiUw4822QBXx2Jw8AksogZdakZ-3QqvC3lB3qEI,201
|
|
51
51
|
pytbox/feishu/typing.py,sha256=3hWkJgOi-v2bt9viMxkyvNHsPgrbAa0aZOxsZYg2vdM,122
|
|
52
|
-
pytbox/log/logger.py,sha256=
|
|
52
|
+
pytbox/log/logger.py,sha256=t5LEvuYTDixh67JOyX0ke1ukb9kT5PEaUSsQnDDck90,8921
|
|
53
53
|
pytbox/log/victorialog.py,sha256=gffEiq38adv9sC5oZeMcyKghd3SGfRuqtZOFuqHQF6E,4139
|
|
54
54
|
pytbox/mail/alimail.py,sha256=njKA3PUbIaiKFaxKvUObmklmEEHg2YA-O5rpgsgT5_w,5147
|
|
55
55
|
pytbox/mail/client.py,sha256=RNgWhhTXFTpD43U4p7hbmnfRdmltuZmbm890gaZTzhI,6278
|
|
@@ -63,8 +63,8 @@ pytbox/utils/response.py,sha256=kXjlwt0WVmLRam2eu1shzX2cQ7ux4cCQryaPGYwle5g,1247
|
|
|
63
63
|
pytbox/utils/richutils.py,sha256=OT9_q2Q1bthzB0g1GlhZVvM4ZAepJRKL6a_Vsr6vEqo,487
|
|
64
64
|
pytbox/utils/timeutils.py,sha256=uSKgwt20mVcgIGKLsH2tNum8v3rcpzgmBibPvyPQFgM,20433
|
|
65
65
|
pytbox/win/ad.py,sha256=-3pWfL3dElz-XoO4j4M9lrgu3KJtlhrS9gCWJBpafAU,1147
|
|
66
|
-
pytbox-0.1.
|
|
67
|
-
pytbox-0.1.
|
|
68
|
-
pytbox-0.1.
|
|
69
|
-
pytbox-0.1.
|
|
70
|
-
pytbox-0.1.
|
|
66
|
+
pytbox-0.2.1.dist-info/METADATA,sha256=pkJirfIq1qpvhesBTwKN49MGnAikmQsWoPUHGvswZH0,6319
|
|
67
|
+
pytbox-0.2.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
68
|
+
pytbox-0.2.1.dist-info/entry_points.txt,sha256=YaTOJ2oPjOiv2SZwY0UC-UA9QS2phRH1oMvxGnxO0Js,43
|
|
69
|
+
pytbox-0.2.1.dist-info/top_level.txt,sha256=YADgWue-Oe128ptN3J2hS3GB0Ncc5uZaSUM3e1rwswE,7
|
|
70
|
+
pytbox-0.2.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|