Browse Source

alert list

Former-commit-id: 454efc36db
pull/119/head
zhangwei 1 year ago
parent
commit
79749b3ed7
10 changed files with 120 additions and 29 deletions
  1. +8
    -2
      api/desc/monitoring/pcm-monitoring.api
  2. +4
    -0
      api/desc/pcm.api
  3. +28
    -0
      api/internal/handler/monitoring/syncclusteralerthandler.go
  4. +5
    -0
      api/internal/handler/routes.go
  5. +21
    -25
      api/internal/logic/monitoring/alertlistlogic.go
  6. +1
    -1
      api/internal/logic/monitoring/clustersloadlogic.go
  7. +44
    -0
      api/internal/logic/monitoring/syncclusteralertlogic.go
  8. +1
    -0
      api/internal/svc/servicecontext.go
  9. +7
    -1
      api/internal/types/types.go
  10. +1
    -0
      pkg/tracker/promql.go

+ 8
- 2
api/desc/monitoring/pcm-monitoring.api View File

@@ -1,7 +1,7 @@
syntax = "v1"

type CreateAlertRuleReq {
CLusterId int64 `json:"clusterId"`
CLusterId string `json:"clusterId"`
ClusterName string `json:"clusterName"`
Name string `json:"name"`
PromQL string `json:"promQL"`
@@ -46,8 +46,14 @@ type (
type (
alertListReq {
alertType string `form:"alertType"`
adapterId string `form:"adapterId,optional"`
clusterId string `form:"clusterId,optional"`
}
alertListResp {
alertMap map[string]interface{} `json:"alertMap"`
}
)
)

type SyncClusterAlertReq {
AlertRecordsMap map[string]interface{} `json:"alertRecordsMap"`
}

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

@@ -969,4 +969,8 @@ service pcm {
@doc "alert list"
@handler alertListHandler
get /monitoring/alert/list (alertListReq) returns (alertListResp)

@doc "Synchronize Cluster alert Information"
@handler syncClusterAlertHandler
post /core/syncClusterAlert (SyncClusterAlertReq)
}

+ 28
- 0
api/internal/handler/monitoring/syncclusteralerthandler.go View File

@@ -0,0 +1,28 @@
package monitoring

import (
"net/http"

"github.com/zeromicro/go-zero/rest/httpx"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/monitoring"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
)

func SyncClusterAlertHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.SyncClusterAlertReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}

l := monitoring.NewSyncClusterAlertLogic(r.Context(), svcCtx)
err := l.SyncClusterAlert(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.Ok(w)
}
}
}

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

@@ -1217,6 +1217,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/monitoring/alert/list",
Handler: monitoring.AlertListHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/core/syncClusterAlert",
Handler: monitoring.SyncClusterAlertHandler(serverCtx),
},
},
rest.WithPrefix("/pcm/v1"),
)


+ 21
- 25
api/internal/logic/monitoring/alertlistlogic.go View File

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

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

@@ -27,7 +27,7 @@ func NewClustersLoadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Clus

func (l *ClustersLoadLogic) ClustersLoad(req *types.ClustersLoadReq) (resp *types.ClustersLoadResp, err error) {
resp = &types.ClustersLoadResp{}
metrics := []string{"cluster_cpu_utilisation", "cluster_cpu_avail", "cluster_cpu_total", "cluster_memory_total", "cluster_memory_avail", "cluster_memory_utilisation", "cluster_disk_utilisation", "cluster_disk_avail", "cluster_disk_total"}
metrics := []string{"cluster_cpu_utilisation", "cluster_cpu_avail", "cluster_cpu_total", "cluster_memory_total", "cluster_memory_avail", "cluster_memory_utilisation", "cluster_disk_utilisation", "cluster_disk_avail", "cluster_disk_total", "cluster_pod_utilisation"}
result := l.svcCtx.PromClient.GetNamedMetrics(metrics, time.Now(), tracker.ClusterOption{ClusterName: req.ClusterName})
resp.Data = result
return resp, nil


+ 44
- 0
api/internal/logic/monitoring/syncclusteralertlogic.go View File

@@ -0,0 +1,44 @@
package monitoring

import (
"context"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
"k8s.io/apimachinery/pkg/util/json"
"time"

"github.com/zeromicro/go-zero/core/logx"
)

type SyncClusterAlertLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewSyncClusterAlertLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SyncClusterAlertLogic {
return &SyncClusterAlertLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *SyncClusterAlertLogic) SyncClusterAlert(req *types.SyncClusterAlertReq) error {

if len(req.AlertRecordsMap) != 0 {
for k, v := range req.AlertRecordsMap {
bytes, err := json.Marshal(v)
if err != nil {
return err
}
setCmd := l.svcCtx.RedisClient.Set(l.ctx, k, bytes, 1*time.Minute)
if setCmd.Err() != nil {
logx.Error(setCmd.Err())
}

}
}

return nil
}

+ 1
- 0
api/internal/svc/servicecontext.go View File

@@ -72,6 +72,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
panic("InitSnowflake err")
}
httpClient := resty.New()
httpClient.SetTimeout(1 * time.Second)
alertClient := tracker.NewAlertClient(c.Monitoring.AlertUrl)
if err != nil {
logx.Errorf("InitPrometheus err: %v", err)


+ 7
- 1
api/internal/types/types.go View File

@@ -5453,7 +5453,7 @@ type PushResourceInfoReq struct {
}

type CreateAlertRuleReq struct {
CLusterId int64 `json:"clusterId"`
CLusterId string `json:"clusterId"`
ClusterName string `json:"clusterName"`
Name string `json:"name"`
PromQL string `json:"promQL"`
@@ -5495,8 +5495,14 @@ type NodesLoadTopResp struct {

type AlertListReq struct {
AlertType string `form:"alertType"`
AdapterId string `form:"adapterId,optional"`
ClusterId string `form:"clusterId,optional"`
}

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

type SyncClusterAlertReq struct {
AlertRecordsMap map[string]interface{} `json:"alertRecordsMap"`
}

+ 1
- 0
pkg/tracker/promql.go View File

@@ -30,6 +30,7 @@ var promQLTemplates = map[string]string{
"cluster_cpu_avail": "cluster_cpu_avail{$1}",
"cluster_memory_avail": "cluster_memory_avail{$1}",
"cluster_disk_avail": "cluster_disk_avail{$1}",
"cluster_pod_utilisation": "cluster_pod_utilisation{$1}",

// center
"center_cpu_utilisation": "(sum by (adapter_id)(cluster_cpu_total{$1})-sum by (adapter_id)(cluster_cpu_avail{$1}))/sum by (adapter_id)(cluster_cpu_total{$1})",


Loading…
Cancel
Save