|
| 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 | +} |
0 commit comments