Browse Source

告警消息查询

Former-commit-id: aba3c3cff2
pull/119/head
zhangwei 1 year ago
parent
commit
b3aa276ff7
8 changed files with 101 additions and 6 deletions
  1. +5
    -1
      api/desc/monitoring/pcm-monitoring.api
  2. +4
    -0
      api/desc/pcm.api
  3. +17
    -0
      api/internal/handler/monitoring/alertlisthandler.go
  4. +2
    -5
      api/internal/handler/monitoring/clustersloadhandler.go
  5. +5
    -0
      api/internal/handler/routes.go
  6. +63
    -0
      api/internal/logic/monitoring/alertlistlogic.go
  7. +1
    -0
      api/internal/logic/monitoring/createalertrulelogic.go
  8. +4
    -0
      api/internal/types/types.go

+ 5
- 1
api/desc/monitoring/pcm-monitoring.api View File

@@ -37,4 +37,8 @@ type (
data interface{} `json:"data"`
msg string `json:"msg"`
}
)
)

type alertListResp {
alertMap map[string]interface{} `json:"alertMap"`
}

+ 4
- 0
api/desc/pcm.api View File

@@ -961,4 +961,8 @@ service pcm {
@doc "node resource load"
@handler nodesLoadTopHandler
get /monitoring/node/top (nodesLoadTopReq) returns (nodesLoadTopResp)

@doc "alert list"
@handler alertListHandler
get /monitoring/alert/list returns (alertListResp)
}

+ 17
- 0
api/internal/handler/monitoring/alertlisthandler.go View File

@@ -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)
}
}

+ 2
- 5
api/internal/handler/monitoring/clustersloadhandler.go View File

@@ -1,6 +1,7 @@
package monitoring

import (
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
"net/http"

"github.com/zeromicro/go-zero/rest/httpx"
@@ -19,10 +20,6 @@ func ClustersLoadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {

l := monitoring.NewClustersLoadLogic(r.Context(), svcCtx)
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)
}
}

+ 5
- 0
api/internal/handler/routes.go View File

@@ -1207,6 +1207,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/monitoring/node/top",
Handler: monitoring.NodesLoadTopHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/monitoring/alert/list",
Handler: monitoring.AlertListHandler(serverCtx),
},
},
rest.WithPrefix("/pcm/v1"),
)


+ 63
- 0
api/internal/logic/monitoring/alertlistlogic.go View File

@@ -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
}

+ 1
- 0
api/internal/logic/monitoring/createalertrulelogic.go View File

@@ -59,6 +59,7 @@ func (l *CreateAlertRuleLogic) CreateAlertRule(req *types.CreateAlertRuleReq) er
ForceContentType("application/json").
Get(server + "/api/v1/monitoring/rule/selector")
if err != nil || response.IsError() {
logx.Error(response)
return err
}
// Data Filling


+ 4
- 0
api/internal/types/types.go View File

@@ -5468,3 +5468,7 @@ type NodesLoadTopResp struct {
Data interface{} `json:"data"`
Msg string `json:"msg"`
}

type AlertListResp struct {
AlertMap map[string]interface{} `json:"alertMap"`
}

Loading…
Cancel
Save