pytbox 0.1.7__py3-none-any.whl → 0.1.9__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.
- pytbox/alert/alert_handler.py +25 -7
- pytbox/base.py +0 -2
- pytbox/database/mongo.py +1 -1
- pytbox/database/victoriametrics.py +6 -3
- pytbox/mail/client.py +4 -0
- {pytbox-0.1.7.dist-info → pytbox-0.1.9.dist-info}/METADATA +1 -1
- {pytbox-0.1.7.dist-info → pytbox-0.1.9.dist-info}/RECORD +10 -10
- {pytbox-0.1.7.dist-info → pytbox-0.1.9.dist-info}/WHEEL +0 -0
- {pytbox-0.1.7.dist-info → pytbox-0.1.9.dist-info}/entry_points.txt +0 -0
- {pytbox-0.1.7.dist-info → pytbox-0.1.9.dist-info}/top_level.txt +0 -0
pytbox/alert/alert_handler.py
CHANGED
|
@@ -7,6 +7,7 @@ from ..database.mongo import Mongo
|
|
|
7
7
|
from ..feishu.client import Client as FeishuClient
|
|
8
8
|
from ..dida365 import Dida365
|
|
9
9
|
from ..utils.timeutils import TimeUtils
|
|
10
|
+
from ..mail.client import MailClient
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
class AlertHandler:
|
|
@@ -15,12 +16,16 @@ class AlertHandler:
|
|
|
15
16
|
config: dict=None,
|
|
16
17
|
mongo_client: Mongo=None,
|
|
17
18
|
feishu_client: FeishuClient=None,
|
|
18
|
-
dida_client: Dida365=None
|
|
19
|
+
dida_client: Dida365=None,
|
|
20
|
+
mail_client: MailClient=None,
|
|
21
|
+
env: Literal['dev', 'prod']='prod'
|
|
19
22
|
):
|
|
20
23
|
self.config = config
|
|
21
24
|
self.mongo = mongo_client
|
|
22
25
|
self.feishu = feishu_client
|
|
23
26
|
self.dida = dida_client
|
|
27
|
+
self.mail = mail_client
|
|
28
|
+
self.env = env
|
|
24
29
|
|
|
25
30
|
def send_alert(self,
|
|
26
31
|
event_id: str=None,
|
|
@@ -34,7 +39,7 @@ class AlertHandler:
|
|
|
34
39
|
suggestion: str='',
|
|
35
40
|
troubleshot: str='暂无',
|
|
36
41
|
mongo_id: str=None
|
|
37
|
-
|
|
42
|
+
):
|
|
38
43
|
|
|
39
44
|
if not event_id:
|
|
40
45
|
event_id = str(uuid.uuid4())
|
|
@@ -62,11 +67,11 @@ class AlertHandler:
|
|
|
62
67
|
update = {"$set": { "resolved_time": event_time}}
|
|
63
68
|
self.mongo.collection.update_one(filter_doc, update)
|
|
64
69
|
alarm_time = self.mongo.collection.find_one(filter_doc, {'event_time': 1})['event_time']
|
|
65
|
-
|
|
70
|
+
|
|
66
71
|
content = [
|
|
67
72
|
f'**事件名称**: {event_name}',
|
|
68
73
|
f'**告警时间**: {TimeUtils.convert_timeobj_to_str(timeobj=event_time, timezone_offset=0) if event_type == "trigger" else TimeUtils.convert_timeobj_to_str(timeobj=alarm_time, timezone_offset=8)}',
|
|
69
|
-
f'**事件内容**: {event_content}',
|
|
74
|
+
f'**事件内容**: {event_content + " 已恢复" if event_type == "resolved" else event_content}',
|
|
70
75
|
f'**告警实例**: {entity_name}',
|
|
71
76
|
f'**建议**: {suggestion}',
|
|
72
77
|
f'**故障排查**: {troubleshot}',
|
|
@@ -78,17 +83,30 @@ class AlertHandler:
|
|
|
78
83
|
|
|
79
84
|
if self.config['feishu']['enable_alert']:
|
|
80
85
|
self.feishu.extensions.send_message_notify(
|
|
86
|
+
receive_id=self.config['feishu']['receive_id'],
|
|
81
87
|
color='red' if event_type == "trigger" else 'green',
|
|
82
|
-
title=event_content,
|
|
88
|
+
title=event_content + " 已恢复" if event_type == "resolved" else event_content,
|
|
83
89
|
priority=priority,
|
|
84
|
-
sub_title=
|
|
90
|
+
sub_title='测试告警, 无需处理' if self.env == 'dev' else '',
|
|
85
91
|
content='\n'.join(content)
|
|
86
92
|
)
|
|
87
93
|
|
|
94
|
+
if self.config['mail']['enable_mail']:
|
|
95
|
+
if event_type == "trigger":
|
|
96
|
+
self.mail.send_mail(
|
|
97
|
+
receiver=[self.config['mail']['mail_address']],
|
|
98
|
+
subject=f'{self.config['mail']['subject_trigger']}, {event_content}',
|
|
99
|
+
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
|
+
else:
|
|
102
|
+
self.mail.send_mail(
|
|
103
|
+
receiver=[self.config['mail']['mail_address']],
|
|
104
|
+
subject=f'{self.config['mail']['subject_resolved']}, {event_content}',
|
|
105
|
+
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}')
|
|
106
|
+
|
|
88
107
|
if self.config['dida']['enable_alert']:
|
|
89
108
|
if event_type == "trigger":
|
|
90
109
|
res = self.dida.task_create(
|
|
91
|
-
|
|
92
110
|
project_id=self.config['dida']['alert_project_id'],
|
|
93
111
|
title=event_content,
|
|
94
112
|
content='\n'.join(content),
|
pytbox/base.py
CHANGED
|
@@ -8,7 +8,6 @@ from pytbox.feishu.client import Client as FeishuClient
|
|
|
8
8
|
from pytbox.dida365 import Dida365
|
|
9
9
|
from pytbox.alert.alert_handler import AlertHandler
|
|
10
10
|
from pytbox.log.logger import AppLogger
|
|
11
|
-
from pytbox.win.ad import ADClient
|
|
12
11
|
from pytbox.network.meraki import Meraki
|
|
13
12
|
from pytbox.utils.env import get_env_by_os_environment
|
|
14
13
|
from pytbox.vmware import VMwareClient
|
|
@@ -110,6 +109,5 @@ sls = AliCloudSls(
|
|
|
110
109
|
logstore=config['alicloud']['account1']['logstore']
|
|
111
110
|
)
|
|
112
111
|
|
|
113
|
-
|
|
114
112
|
def get_cronjob_counter(app_type='', app='', comment=None, schedule_interval=None, schedule_cron=None):
|
|
115
113
|
return cronjob_counter(vm=vm, log=get_logger('cronjob_counter'), app_type=app_type, app=app, comment=comment, schedule_interval=schedule_interval, schedule_cron=schedule_cron)
|
pytbox/database/mongo.py
CHANGED
|
@@ -80,7 +80,7 @@ class Mongo:
|
|
|
80
80
|
alarm_list = []
|
|
81
81
|
for result in results:
|
|
82
82
|
duration_minute = '持续 ' + str(int((result['resolved_time'] - result['event_time']).total_seconds() / 60)) + ' 分钟'
|
|
83
|
-
alarm_list.append('
|
|
83
|
+
alarm_list.append('触发时间: ' + TimeUtils.convert_timeobj_to_str(timeobj=result['event_time']) + ' ' + duration_minute)
|
|
84
84
|
|
|
85
85
|
alarm_str = '\n'.join(alarm_list)
|
|
86
86
|
|
|
@@ -230,7 +230,7 @@ class VictoriaMetrics:
|
|
|
230
230
|
rate = r.data[0]['value'][1]
|
|
231
231
|
return int(float(rate))
|
|
232
232
|
|
|
233
|
-
def check_snmp_port_status(self, sysname: str=None, if_name: str=None, last_minute: int=5) -> ReturnResponse:
|
|
233
|
+
def check_snmp_port_status(self, sysname: str=None, if_name: str=None, last_minute: int=5, dev_file: str=None) -> ReturnResponse:
|
|
234
234
|
'''
|
|
235
235
|
查询端口状态
|
|
236
236
|
status code 可参考 SNMP 文件 https://mibbrowser.online/mibdb_search.php?mib=IF-MIB
|
|
@@ -245,9 +245,12 @@ class VictoriaMetrics:
|
|
|
245
245
|
code: 0, msg: , data: up,down
|
|
246
246
|
'''
|
|
247
247
|
q = f"""avg_over_time(snmp_interface_ifOperStatus{{sysName="{sysname}", ifName="{if_name}"}}[{last_minute}m])"""
|
|
248
|
-
|
|
248
|
+
if self.env == 'dev':
|
|
249
|
+
r = load_dev_file(dev_file)
|
|
250
|
+
else:
|
|
251
|
+
r = self.query(query=q)
|
|
249
252
|
if r.code == 0:
|
|
250
|
-
status_code = r.data[0]['value'][1]
|
|
253
|
+
status_code = int(r.data[0]['value'][1])
|
|
251
254
|
if status_code == 1:
|
|
252
255
|
status = 'up'
|
|
253
256
|
else:
|
pytbox/mail/client.py
CHANGED
|
@@ -27,6 +27,10 @@ class MailClient:
|
|
|
27
27
|
elif 'foxmail.com' in mail_address:
|
|
28
28
|
self.smtp_address = 'smtp.qq.com'
|
|
29
29
|
self.imap_address = 'imap.qq.com'
|
|
30
|
+
|
|
31
|
+
elif 'mail' in mail_address and 'cn' in mail_address:
|
|
32
|
+
self.smtp_address = "smtpdm.aliyun.com"
|
|
33
|
+
self.imap_address = ""
|
|
30
34
|
|
|
31
35
|
else:
|
|
32
36
|
raise ValueError(f'不支持的邮箱地址: {mail_address}')
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
pytbox/base.py,sha256=
|
|
1
|
+
pytbox/base.py,sha256=wc2CTbICGBNvS0mBU3pRIHrIGaFdjf3Y5DIkux-_CmU,4247
|
|
2
2
|
pytbox/cli.py,sha256=N775a0GK80IT2lQC2KRYtkZpIiu9UjavZmaxgNUgJhQ,160
|
|
3
3
|
pytbox/dida365.py,sha256=pUMPB9AyLZpTTbaz2LbtzdEpyjvuGf4YlRrCvM5sbJo,10545
|
|
4
4
|
pytbox/excel.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -6,7 +6,7 @@ pytbox/onepassword_connect.py,sha256=nD3xTl1ykQ4ct_dCRRF138gXCtk-phPfKYXuOn-P7Z8
|
|
|
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=mDg1bv9F9XOH0NNNZTlw_KaN_P4G3w8rswmDW_N_3gw,6553
|
|
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
|
|
@@ -42,8 +42,8 @@ pytbox/cli/common/utils.py,sha256=0uQ5L1l5cySFheixB91B9ptq0p6tGsBhqvvm3ouRGbI,90
|
|
|
42
42
|
pytbox/cli/formatters/__init__.py,sha256=4o85w4j-A-O1oBLvuE9q8AFiJ2C9rvB3MIKsy5VvdfQ,101
|
|
43
43
|
pytbox/cli/formatters/output.py,sha256=h5WhZlQk1rjmxEj88Jy5ODLcv6L5zfGUhks_3AWIkKU,5455
|
|
44
44
|
pytbox/common/__init__.py,sha256=3JWfgCQZKZuSH5NCE7OCzKwq82pkyop9l7sH5YSNyfU,122
|
|
45
|
-
pytbox/database/mongo.py,sha256=
|
|
46
|
-
pytbox/database/victoriametrics.py,sha256=
|
|
45
|
+
pytbox/database/mongo.py,sha256=AhJ9nCAQHKrrcL-ujeonOwEf3x2QkmT2VhoCdglqJmU,3478
|
|
46
|
+
pytbox/database/victoriametrics.py,sha256=k6D6SGi7ky5pugM8iHa4ZjzTIlcnxwFw178VA6Wjbyk,11408
|
|
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
|
|
@@ -52,7 +52,7 @@ pytbox/feishu/typing.py,sha256=3hWkJgOi-v2bt9viMxkyvNHsPgrbAa0aZOxsZYg2vdM,122
|
|
|
52
52
|
pytbox/log/logger.py,sha256=tfqRc7VcDyI_U-NiiK4euFe8O2ro9yeXVohPmzAC_hY,8867
|
|
53
53
|
pytbox/log/victorialog.py,sha256=gffEiq38adv9sC5oZeMcyKghd3SGfRuqtZOFuqHQF6E,4139
|
|
54
54
|
pytbox/mail/alimail.py,sha256=njKA3PUbIaiKFaxKvUObmklmEEHg2YA-O5rpgsgT5_w,5147
|
|
55
|
-
pytbox/mail/client.py,sha256=
|
|
55
|
+
pytbox/mail/client.py,sha256=RNgWhhTXFTpD43U4p7hbmnfRdmltuZmbm890gaZTzhI,6278
|
|
56
56
|
pytbox/mail/mail_detail.py,sha256=6u8DK-7WzYPSuX6TdicSCh2Os_9Ou6Rn9xc6WRvv85M,699
|
|
57
57
|
pytbox/network/meraki.py,sha256=054E3C5KzAuXs9aPalvdAOUo6Hc5aOKZSWUaVbPquy4,6112
|
|
58
58
|
pytbox/utils/cronjob.py,sha256=b17CY1fmaFTdQojicXAXHliov_JZdmT7cZhtO4v5sCo,3080
|
|
@@ -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.1.9.dist-info/METADATA,sha256=BrT45GpS_cUh5Xz1ClDKgHl58mcsFulmXfVzgMCAsrc,6319
|
|
67
|
+
pytbox-0.1.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
68
|
+
pytbox-0.1.9.dist-info/entry_points.txt,sha256=YaTOJ2oPjOiv2SZwY0UC-UA9QS2phRH1oMvxGnxO0Js,43
|
|
69
|
+
pytbox-0.1.9.dist-info/top_level.txt,sha256=YADgWue-Oe128ptN3J2hS3GB0Ncc5uZaSUM3e1rwswE,7
|
|
70
|
+
pytbox-0.1.9.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|