Skip to content

Commit 05d5eaa

Browse files
ymakedaqiSecloud
authored andcommitted
feat(dbm-services): 对导入资源池的机器增加关联的故障单据的检查 #11192
1 parent d24c4c6 commit 05d5eaa

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed

dbm-services/common/db-resource/internal/model/TbRpDetail.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const (
3636
Used = "Used"
3737
// UsedByOther 已被其他业务使用
3838
UsedByOther = "UsedByOther"
39+
// FaultHazard 故障隐患
40+
FaultHazard = "FaultHazard"
3941
)
4042

4143
const (

dbm-services/common/db-resource/internal/svr/dbmapi/dbm_api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ const (
1919
DBMEnvironApi = "/apis/conf/system_settings/environ/"
2020
// DBMSpecApi 查询规格接口
2121
DBMSpecApi = "/apis/dbresource/spec/"
22+
23+
// DBMFaultHostsCheckApi 查询主机关联的故障单据
24+
DBMFaultHostsCheckApi = "/apis/dbresource/resource/check_fault_hosts/"
2225
)
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
3+
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
4+
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
6+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
7+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
8+
* specific language governing permissions and limitations under the License.
9+
*/
10+
11+
package dbmapi
12+
13+
import (
14+
"bytes"
15+
"encoding/json"
16+
"io"
17+
"net/http"
18+
"net/url"
19+
20+
"dbm-services/common/go-pubpkg/logger"
21+
)
22+
23+
// UworkFaultResponse uwork fault response
24+
type UworkFaultResponse struct {
25+
BkHostID int `json:"bk_host_id"` // 主机ID
26+
HasOpenTickets bool `json:"has_open_tickets"` // 是否有未关闭的工单
27+
OpenTicketIDs []string `json:"open_ticket_ids"` // 未关闭的工单ID列表
28+
}
29+
30+
// XworkFaultResponse xwork fault response
31+
type XworkFaultResponse struct {
32+
TaskId int `json:"TaskId"`
33+
VmUuid string `json:"VmUuid"`
34+
TaskStatus int `json:"TaskStatus"`
35+
StartTime string `json:"StartTime"`
36+
EndTime string `json:"EndTime"`
37+
TaskTypeId int `json:"TaskTypeId"`
38+
BsiIp string `json:"BsiIp"`
39+
DeptId int `json:"DeptId"`
40+
SvrOperator string `json:"SvrOperator"`
41+
SvrBakOperator string `json:"SvrBakOperator"`
42+
BsiPath string `json:"BsiPath"`
43+
AssetId string `json:"AssetId"`
44+
TaskStatusCN string `json:"TaskStatusCN"`
45+
TaskTypeCN string `json:"TaskTypeCN"`
46+
TaskDetail string `json:"TaskDetail"`
47+
SvrInstanceType string `json:"SvrInstanceType"`
48+
InstanceId string `json:"InstanceId"`
49+
VmAlias string `json:"VmAlias"`
50+
DeptName string `json:"DeptName"`
51+
AuthTime *string `json:"AuthTime"` // 使用指针类型处理可能的 null 值
52+
HostFaultType string `json:"HostFaultType"`
53+
MainTaskId string `json:"MainTaskId"`
54+
AuthType string `json:"AuthType"`
55+
}
56+
57+
// DbmFaultResponseItem dbm fault response item
58+
type DbmFaultResponseItem struct {
59+
Uwork UworkFaultResponse `json:"uwork"`
60+
Xwork XworkFaultResponse `json:"xwork"`
61+
}
62+
63+
func (u UworkFaultResponse) isOk() bool {
64+
return !u.HasOpenTickets && len(u.OpenTicketIDs) == 0
65+
}
66+
67+
func (x XworkFaultResponse) isOk() bool {
68+
return x.TaskId <= 0
69+
}
70+
71+
// CheckIsOK check if the fault response item is OK
72+
func (x DbmFaultResponseItem) CheckIsOK() bool {
73+
return x.Uwork.isOk() && x.Xwork.isOk()
74+
}
75+
76+
// CheckFaultHostsParamItem check fault hosts param item
77+
type CheckFaultHostsParamItem struct {
78+
IP string `json:"ip"`
79+
BkHostID int `json:"bk_host_id"` // 主机ID
80+
}
81+
82+
// CheckFaultHosts request dbm api to check fault hosts
83+
func CheckFaultHosts(hosts []CheckFaultHostsParamItem) (d map[string]DbmFaultResponseItem, err error) {
84+
var content []byte
85+
cli := NewDbmClient()
86+
u, err := url.JoinPath(cli.EndPoint, DBMFaultHostsCheckApi)
87+
if err != nil {
88+
return nil, err
89+
}
90+
p := map[string]interface{}{
91+
"hosts": hosts,
92+
}
93+
body, err := json.Marshal(p)
94+
if err != nil {
95+
logger.Error("marshal CheckFaultHosts body failed %s ", err.Error())
96+
return nil, err
97+
}
98+
request, err := http.NewRequest(http.MethodPost, u, bytes.NewBuffer(body))
99+
if err != nil {
100+
return nil, err
101+
}
102+
resp, err := cli.Client.Do(request)
103+
if err != nil {
104+
return nil, err
105+
}
106+
defer resp.Body.Close()
107+
content, err = io.ReadAll(resp.Body)
108+
if err != nil {
109+
logger.Error("read respone body failed %s", err.Error())
110+
return nil, err
111+
}
112+
logger.Info("respone %v", string(content))
113+
if err = json.Unmarshal(content, &d); err != nil {
114+
return nil, err
115+
}
116+
return d, nil
117+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
3+
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
4+
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
6+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
7+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
8+
* specific language governing permissions and limitations under the License.
9+
*/
10+
11+
package dbmapi
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
3+
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
4+
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
6+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
7+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
8+
* specific language governing permissions and limitations under the License.
9+
*/
10+
11+
package task
12+
13+
import (
14+
"time"
15+
16+
"github.com/samber/lo"
17+
18+
"dbm-services/common/db-resource/internal/model"
19+
"dbm-services/common/db-resource/internal/svr/dbmapi"
20+
"dbm-services/common/go-pubpkg/logger"
21+
)
22+
23+
// FaultHostCheck TODO
24+
func FaultHostCheck() (err error) {
25+
// 获取空闲机器
26+
var machines []model.TbRpDetail
27+
if err = model.DB.Self.Table(model.TbRpDetailName()).
28+
Where("status = ? ", model.Unused).
29+
Find(&machines).Error; err != nil {
30+
logger.Error("get unused machines failed %s", err.Error())
31+
return err
32+
}
33+
if len(machines) == 0 {
34+
logger.Info("no unused machines found")
35+
return nil
36+
}
37+
for _, mgp := range lo.Chunk(machines, 50) {
38+
var hosts []dbmapi.CheckFaultHostsParamItem
39+
for _, m := range mgp {
40+
hosts = append(hosts, dbmapi.CheckFaultHostsParamItem{
41+
BkHostID: m.BkHostID,
42+
IP: m.IP,
43+
})
44+
}
45+
checkResult, err := dbmapi.CheckFaultHosts(hosts)
46+
if err != nil {
47+
logger.Error("check fault hosts failed %s", err.Error())
48+
continue
49+
}
50+
if len(checkResult) == 0 {
51+
logger.Info("no fault hosts found in this batch")
52+
continue
53+
}
54+
for hostId, item := range checkResult {
55+
if item.CheckIsOK() {
56+
continue
57+
}
58+
logger.Info("host %s fault info %v", hostId, item)
59+
err = model.DB.Self.Table(model.TbRpDetailName()).Where(" bk_host_id = ? ",
60+
hostId).Updates(map[string]interface{}{"status": model.FaultHazard, "update_time": time.Now()}).
61+
Error
62+
if err != nil {
63+
logger.Error("update machine status failed %s", err.Error())
64+
return err
65+
}
66+
}
67+
}
68+
return
69+
}

dbm-services/common/db-resource/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ func registerCrontab(localcron *cron.Cron) {
156156
}
157157
},
158158
},
159+
{
160+
Name: "检查故障主机",
161+
Spec: "@every 12h",
162+
Func: func() {
163+
if err := task.FaultHostCheck(); err != nil {
164+
logger.Error("check fault hosts failed %s", err.Error())
165+
}
166+
},
167+
},
159168
}
160169
for _, cron := range localCrontabs {
161170
if _, err := localcron.AddFunc(cron.Spec, cron.Func); err != nil {

0 commit comments

Comments
 (0)