|
|
|
@@ -2,9 +2,9 @@ package monitoring |
|
|
|
|
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"github.com/pkg/errors" |
|
|
|
"fmt" |
|
|
|
v1 "github.com/prometheus/client_golang/api/prometheus/v1" |
|
|
|
tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" |
|
|
|
"k8s.io/apimachinery/pkg/util/json" |
|
|
|
|
|
|
|
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" |
|
|
|
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" |
|
|
|
@@ -33,31 +33,27 @@ type AlertListResp struct { |
|
|
|
} |
|
|
|
|
|
|
|
func (l *AlertListLogic) AlertList(req *types.AlertListReq) (resp *types.AlertListResp, err error) { |
|
|
|
// todo: add your logic here and delete this line |
|
|
|
resp = &types.AlertListResp{} |
|
|
|
resp = &types.AlertListResp{ |
|
|
|
AlertMap: make(map[string]interface{}), |
|
|
|
} |
|
|
|
|
|
|
|
// query server http url. |
|
|
|
var serverArray []string |
|
|
|
l.svcCtx.DbEngin.Raw("select ta.server from t_adapter ta,t_cluster tc where ta.id = tc.adapter_id and label = 'kubernetes' and ta.type = ?", req.AlertType).Scan(&serverArray) |
|
|
|
|
|
|
|
result := make(map[string][]*v1.Alert) |
|
|
|
for _, server := range serverArray { |
|
|
|
alertListResp := AlertListResp{} |
|
|
|
response, err := l.svcCtx.HttpClient.R(). |
|
|
|
SetResult(&alertListResp). |
|
|
|
ForceContentType("application/json"). |
|
|
|
Get(server + "/api/v1/alert/rule/list") |
|
|
|
if err != nil { |
|
|
|
logx.Error(response) |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
if response.IsError() { |
|
|
|
return nil, errors.New(response.String()) |
|
|
|
} |
|
|
|
for k, v := range alertListResp.Data { |
|
|
|
result[k] = v |
|
|
|
} |
|
|
|
var clusterArray []string |
|
|
|
|
|
|
|
sql := "select distinct tc.name from t_adapter ta,t_cluster tc where ta.id = tc.adapter_id and label = 'kubernetes' and ta.type = ? and ta.id = ?" |
|
|
|
if len(req.AdapterId) > 0 { |
|
|
|
sql = fmt.Sprintf("select distinct tc.name from t_adapter ta,t_cluster tc where ta.id = tc.adapter_id and label = 'kubernetes' and ta.type = ? and ta.id = %s", req.AdapterId) |
|
|
|
} |
|
|
|
if len(req.ClusterId) > 0 { |
|
|
|
sql = fmt.Sprintf("select distinct tc.name from t_adapter ta,t_cluster tc where ta.id = tc.adapter_id and label = 'kubernetes' and ta.type = ? and tc.id = %s", req.ClusterId) |
|
|
|
} |
|
|
|
l.svcCtx.DbEngin.Raw(sql, req.AlertType).Scan(&clusterArray) |
|
|
|
|
|
|
|
for _, clusterName := range clusterArray { |
|
|
|
getResult := l.svcCtx.RedisClient.Get(l.ctx, clusterName) |
|
|
|
var alerts []v1.Alert |
|
|
|
json.Unmarshal([]byte(getResult.Val()), &alerts) |
|
|
|
resp.AlertMap[clusterName] = alerts |
|
|
|
} |
|
|
|
tool.Convert(result, &resp.AlertMap) |
|
|
|
return resp, nil |
|
|
|
} |