From 79749b3ed71ee15db3f5d4e87724d6ba870bfd4c Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Thu, 18 Apr 2024 15:54:32 +0800 Subject: [PATCH] alert list Former-commit-id: 454efc36db52ba10f611d5624aa651015801ed28 --- api/desc/monitoring/pcm-monitoring.api | 10 +++- api/desc/pcm.api | 4 ++ .../monitoring/syncclusteralerthandler.go | 28 +++++++++++ api/internal/handler/routes.go | 5 ++ .../logic/monitoring/alertlistlogic.go | 46 +++++++++---------- .../logic/monitoring/clustersloadlogic.go | 2 +- .../logic/monitoring/syncclusteralertlogic.go | 44 ++++++++++++++++++ api/internal/svc/servicecontext.go | 1 + api/internal/types/types.go | 8 +++- pkg/tracker/promql.go | 1 + 10 files changed, 120 insertions(+), 29 deletions(-) create mode 100644 api/internal/handler/monitoring/syncclusteralerthandler.go create mode 100644 api/internal/logic/monitoring/syncclusteralertlogic.go diff --git a/api/desc/monitoring/pcm-monitoring.api b/api/desc/monitoring/pcm-monitoring.api index bcb2d982..a70f3bf5 100644 --- a/api/desc/monitoring/pcm-monitoring.api +++ b/api/desc/monitoring/pcm-monitoring.api @@ -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"` } -) \ No newline at end of file +) + +type SyncClusterAlertReq { + AlertRecordsMap map[string]interface{} `json:"alertRecordsMap"` +} \ No newline at end of file diff --git a/api/desc/pcm.api b/api/desc/pcm.api index 84956825..9180680e 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -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) } \ No newline at end of file diff --git a/api/internal/handler/monitoring/syncclusteralerthandler.go b/api/internal/handler/monitoring/syncclusteralerthandler.go new file mode 100644 index 00000000..8aac80f6 --- /dev/null +++ b/api/internal/handler/monitoring/syncclusteralerthandler.go @@ -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) + } + } +} diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index 56d6c5ec..810a8957 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -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"), ) diff --git a/api/internal/logic/monitoring/alertlistlogic.go b/api/internal/logic/monitoring/alertlistlogic.go index fed39a58..e7d7a665 100644 --- a/api/internal/logic/monitoring/alertlistlogic.go +++ b/api/internal/logic/monitoring/alertlistlogic.go @@ -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 } diff --git a/api/internal/logic/monitoring/clustersloadlogic.go b/api/internal/logic/monitoring/clustersloadlogic.go index ed2a4d07..184cd5ed 100644 --- a/api/internal/logic/monitoring/clustersloadlogic.go +++ b/api/internal/logic/monitoring/clustersloadlogic.go @@ -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 diff --git a/api/internal/logic/monitoring/syncclusteralertlogic.go b/api/internal/logic/monitoring/syncclusteralertlogic.go new file mode 100644 index 00000000..11e8eef3 --- /dev/null +++ b/api/internal/logic/monitoring/syncclusteralertlogic.go @@ -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 +} diff --git a/api/internal/svc/servicecontext.go b/api/internal/svc/servicecontext.go index ee6fc50f..dfd3f102 100644 --- a/api/internal/svc/servicecontext.go +++ b/api/internal/svc/servicecontext.go @@ -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) diff --git a/api/internal/types/types.go b/api/internal/types/types.go index da9a25ac..727b1eaf 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -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"` +} diff --git a/pkg/tracker/promql.go b/pkg/tracker/promql.go index 6d8e5662..ce84be43 100644 --- a/pkg/tracker/promql.go +++ b/pkg/tracker/promql.go @@ -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})",