| @@ -37,4 +37,8 @@ type ( | |||||
| data interface{} `json:"data"` | data interface{} `json:"data"` | ||||
| msg string `json:"msg"` | msg string `json:"msg"` | ||||
| } | } | ||||
| ) | |||||
| ) | |||||
| type alertListResp { | |||||
| alertMap map[string]interface{} `json:"alertMap"` | |||||
| } | |||||
| @@ -961,4 +961,8 @@ service pcm { | |||||
| @doc "node resource load" | @doc "node resource load" | ||||
| @handler nodesLoadTopHandler | @handler nodesLoadTopHandler | ||||
| get /monitoring/node/top (nodesLoadTopReq) returns (nodesLoadTopResp) | get /monitoring/node/top (nodesLoadTopReq) returns (nodesLoadTopResp) | ||||
| @doc "alert list" | |||||
| @handler alertListHandler | |||||
| get /monitoring/alert/list returns (alertListResp) | |||||
| } | } | ||||
| @@ -0,0 +1,17 @@ | |||||
| package monitoring | |||||
| import ( | |||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | |||||
| "net/http" | |||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/monitoring" | |||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" | |||||
| ) | |||||
| func AlertListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | |||||
| return func(w http.ResponseWriter, r *http.Request) { | |||||
| l := monitoring.NewAlertListLogic(r.Context(), svcCtx) | |||||
| resp, err := l.AlertList() | |||||
| result.HttpResult(r, w, resp, err) | |||||
| } | |||||
| } | |||||
| @@ -1,6 +1,7 @@ | |||||
| package monitoring | package monitoring | ||||
| import ( | import ( | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | |||||
| "net/http" | "net/http" | ||||
| "github.com/zeromicro/go-zero/rest/httpx" | "github.com/zeromicro/go-zero/rest/httpx" | ||||
| @@ -19,10 +20,6 @@ func ClustersLoadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | |||||
| l := monitoring.NewClustersLoadLogic(r.Context(), svcCtx) | l := monitoring.NewClustersLoadLogic(r.Context(), svcCtx) | ||||
| resp, err := l.ClustersLoad(&req) | resp, err := l.ClustersLoad(&req) | ||||
| if err != nil { | |||||
| httpx.ErrorCtx(r.Context(), w, err) | |||||
| } else { | |||||
| httpx.OkJsonCtx(r.Context(), w, resp) | |||||
| } | |||||
| result.HttpResult(r, w, resp, err) | |||||
| } | } | ||||
| } | } | ||||
| @@ -1207,6 +1207,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | |||||
| Path: "/monitoring/node/top", | Path: "/monitoring/node/top", | ||||
| Handler: monitoring.NodesLoadTopHandler(serverCtx), | Handler: monitoring.NodesLoadTopHandler(serverCtx), | ||||
| }, | }, | ||||
| { | |||||
| Method: http.MethodGet, | |||||
| Path: "/monitoring/alert/list", | |||||
| Handler: monitoring.AlertListHandler(serverCtx), | |||||
| }, | |||||
| }, | }, | ||||
| rest.WithPrefix("/pcm/v1"), | rest.WithPrefix("/pcm/v1"), | ||||
| ) | ) | ||||
| @@ -0,0 +1,63 @@ | |||||
| package monitoring | |||||
| import ( | |||||
| "context" | |||||
| "github.com/pkg/errors" | |||||
| v1 "github.com/prometheus/client_golang/api/prometheus/v1" | |||||
| tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" | |||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" | |||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" | |||||
| "github.com/zeromicro/go-zero/core/logx" | |||||
| ) | |||||
| type AlertListLogic struct { | |||||
| logx.Logger | |||||
| ctx context.Context | |||||
| svcCtx *svc.ServiceContext | |||||
| } | |||||
| func NewAlertListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AlertListLogic { | |||||
| return &AlertListLogic{ | |||||
| Logger: logx.WithContext(ctx), | |||||
| ctx: ctx, | |||||
| svcCtx: svcCtx, | |||||
| } | |||||
| } | |||||
| type AlertListResp struct { | |||||
| Mode int `json:"code"` | |||||
| Msg string `json:"msg"` | |||||
| Data map[string][]*v1.Alert `json:"data"` | |||||
| } | |||||
| func (l *AlertListLogic) AlertList() (resp *types.AlertListResp, err error) { | |||||
| // todo: add your logic here and delete this line | |||||
| resp = &types.AlertListResp{} | |||||
| // 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'").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 | |||||
| } | |||||
| } | |||||
| tool.Convert(result, &resp.AlertMap) | |||||
| return resp, nil | |||||
| } | |||||
| @@ -59,6 +59,7 @@ func (l *CreateAlertRuleLogic) CreateAlertRule(req *types.CreateAlertRuleReq) er | |||||
| ForceContentType("application/json"). | ForceContentType("application/json"). | ||||
| Get(server + "/api/v1/monitoring/rule/selector") | Get(server + "/api/v1/monitoring/rule/selector") | ||||
| if err != nil || response.IsError() { | if err != nil || response.IsError() { | ||||
| logx.Error(response) | |||||
| return err | return err | ||||
| } | } | ||||
| // Data Filling | // Data Filling | ||||
| @@ -5468,3 +5468,7 @@ type NodesLoadTopResp struct { | |||||
| Data interface{} `json:"data"` | Data interface{} `json:"data"` | ||||
| Msg string `json:"msg"` | Msg string `json:"msg"` | ||||
| } | } | ||||
| type AlertListResp struct { | |||||
| AlertMap map[string]interface{} `json:"alertMap"` | |||||
| } | |||||