| @@ -1,7 +1,7 @@ | |||||
| syntax = "v1" | syntax = "v1" | ||||
| type CreateAlertRuleReq { | type CreateAlertRuleReq { | ||||
| CLusterId int64 `json:"clusterId"` | |||||
| CLusterId string `json:"clusterId"` | |||||
| ClusterName string `json:"clusterName"` | ClusterName string `json:"clusterName"` | ||||
| Name string `json:"name"` | Name string `json:"name"` | ||||
| PromQL string `json:"promQL"` | PromQL string `json:"promQL"` | ||||
| @@ -46,8 +46,14 @@ type ( | |||||
| type ( | type ( | ||||
| alertListReq { | alertListReq { | ||||
| alertType string `form:"alertType"` | alertType string `form:"alertType"` | ||||
| adapterId string `form:"adapterId,optional"` | |||||
| clusterId string `form:"clusterId,optional"` | |||||
| } | } | ||||
| alertListResp { | alertListResp { | ||||
| alertMap map[string]interface{} `json:"alertMap"` | alertMap map[string]interface{} `json:"alertMap"` | ||||
| } | } | ||||
| ) | |||||
| ) | |||||
| type SyncClusterAlertReq { | |||||
| AlertRecordsMap map[string]interface{} `json:"alertRecordsMap"` | |||||
| } | |||||
| @@ -969,4 +969,8 @@ service pcm { | |||||
| @doc "alert list" | @doc "alert list" | ||||
| @handler alertListHandler | @handler alertListHandler | ||||
| get /monitoring/alert/list (alertListReq) returns (alertListResp) | get /monitoring/alert/list (alertListReq) returns (alertListResp) | ||||
| @doc "Synchronize Cluster alert Information" | |||||
| @handler syncClusterAlertHandler | |||||
| post /core/syncClusterAlert (SyncClusterAlertReq) | |||||
| } | } | ||||
| @@ -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) | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -1217,6 +1217,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | |||||
| Path: "/monitoring/alert/list", | Path: "/monitoring/alert/list", | ||||
| Handler: monitoring.AlertListHandler(serverCtx), | Handler: monitoring.AlertListHandler(serverCtx), | ||||
| }, | }, | ||||
| { | |||||
| Method: http.MethodPost, | |||||
| Path: "/core/syncClusterAlert", | |||||
| Handler: monitoring.SyncClusterAlertHandler(serverCtx), | |||||
| }, | |||||
| }, | }, | ||||
| rest.WithPrefix("/pcm/v1"), | rest.WithPrefix("/pcm/v1"), | ||||
| ) | ) | ||||
| @@ -2,9 +2,9 @@ package monitoring | |||||
| import ( | import ( | ||||
| "context" | "context" | ||||
| "github.com/pkg/errors" | |||||
| "fmt" | |||||
| v1 "github.com/prometheus/client_golang/api/prometheus/v1" | 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/svc" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" | "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) { | 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. | // 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 | return resp, nil | ||||
| } | } | ||||
| @@ -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) { | func (l *ClustersLoadLogic) ClustersLoad(req *types.ClustersLoadReq) (resp *types.ClustersLoadResp, err error) { | ||||
| resp = &types.ClustersLoadResp{} | 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}) | result := l.svcCtx.PromClient.GetNamedMetrics(metrics, time.Now(), tracker.ClusterOption{ClusterName: req.ClusterName}) | ||||
| resp.Data = result | resp.Data = result | ||||
| return resp, nil | return resp, nil | ||||
| @@ -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 | |||||
| } | |||||
| @@ -72,6 +72,7 @@ func NewServiceContext(c config.Config) *ServiceContext { | |||||
| panic("InitSnowflake err") | panic("InitSnowflake err") | ||||
| } | } | ||||
| httpClient := resty.New() | httpClient := resty.New() | ||||
| httpClient.SetTimeout(1 * time.Second) | |||||
| alertClient := tracker.NewAlertClient(c.Monitoring.AlertUrl) | alertClient := tracker.NewAlertClient(c.Monitoring.AlertUrl) | ||||
| if err != nil { | if err != nil { | ||||
| logx.Errorf("InitPrometheus err: %v", err) | logx.Errorf("InitPrometheus err: %v", err) | ||||
| @@ -5453,7 +5453,7 @@ type PushResourceInfoReq struct { | |||||
| } | } | ||||
| type CreateAlertRuleReq struct { | type CreateAlertRuleReq struct { | ||||
| CLusterId int64 `json:"clusterId"` | |||||
| CLusterId string `json:"clusterId"` | |||||
| ClusterName string `json:"clusterName"` | ClusterName string `json:"clusterName"` | ||||
| Name string `json:"name"` | Name string `json:"name"` | ||||
| PromQL string `json:"promQL"` | PromQL string `json:"promQL"` | ||||
| @@ -5495,8 +5495,14 @@ type NodesLoadTopResp struct { | |||||
| type AlertListReq struct { | type AlertListReq struct { | ||||
| AlertType string `form:"alertType"` | AlertType string `form:"alertType"` | ||||
| AdapterId string `form:"adapterId,optional"` | |||||
| ClusterId string `form:"clusterId,optional"` | |||||
| } | } | ||||
| type AlertListResp struct { | type AlertListResp struct { | ||||
| AlertMap map[string]interface{} `json:"alertMap"` | AlertMap map[string]interface{} `json:"alertMap"` | ||||
| } | } | ||||
| type SyncClusterAlertReq struct { | |||||
| AlertRecordsMap map[string]interface{} `json:"alertRecordsMap"` | |||||
| } | |||||
| @@ -30,6 +30,7 @@ var promQLTemplates = map[string]string{ | |||||
| "cluster_cpu_avail": "cluster_cpu_avail{$1}", | "cluster_cpu_avail": "cluster_cpu_avail{$1}", | ||||
| "cluster_memory_avail": "cluster_memory_avail{$1}", | "cluster_memory_avail": "cluster_memory_avail{$1}", | ||||
| "cluster_disk_avail": "cluster_disk_avail{$1}", | "cluster_disk_avail": "cluster_disk_avail{$1}", | ||||
| "cluster_pod_utilisation": "cluster_pod_utilisation{$1}", | |||||
| // center | // 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})", | "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})", | ||||