mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
feat(monitor): add the registered model manger function (#24195)
This commit is contained in:
@@ -23,4 +23,5 @@ func init() {
|
||||
cmd := NewResourceCmd(modules.MonitorResourceManager)
|
||||
cmd.GetProperty(new(options.MonitorResourceJointAlertOptions))
|
||||
cmd.List(new(options.MonitorResourceListOptions))
|
||||
cmd.Perform("do-action", new(options.MonitorResourceDoActionOptions))
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
"yunion.io/x/onecloud/pkg/apis/compute"
|
||||
)
|
||||
@@ -59,3 +61,8 @@ type MonitorResourceDetails struct {
|
||||
AttachAlertCount int64 `json:"attach_alert_count"`
|
||||
Hypervisor string `json:"hypervisor"`
|
||||
}
|
||||
|
||||
type MonitorResourceDoActionInput struct {
|
||||
Action string `json:"action"`
|
||||
Data jsonutils.JSONObject `json:"data"`
|
||||
}
|
||||
|
||||
@@ -55,14 +55,15 @@ type MonitorResourceJointCreateInput struct {
|
||||
}
|
||||
|
||||
type MonitorResourceJointDetails struct {
|
||||
ResName string `json:"res_name"`
|
||||
ResId string `json:"res_id"`
|
||||
ResType string `json:"res_type"`
|
||||
AlertName string `json:"alert_name"`
|
||||
AlertRule jsonutils.JSONObject `json:"alert_rule"`
|
||||
Level string `json:"level"`
|
||||
SendState string `json:"send_state"`
|
||||
State string `json:"state"`
|
||||
IsSetShield bool `json:"is_set_shield"`
|
||||
AlertCount int `json:"alert_count"`
|
||||
MonitorResourceObjectId string `json:"monitor_resource_object_id"`
|
||||
ResName string `json:"res_name"`
|
||||
ResId string `json:"res_id"`
|
||||
ResType string `json:"res_type"`
|
||||
AlertName string `json:"alert_name"`
|
||||
AlertRule jsonutils.JSONObject `json:"alert_rule"`
|
||||
Level string `json:"level"`
|
||||
SendState string `json:"send_state"`
|
||||
State string `json:"state"`
|
||||
IsSetShield bool `json:"is_set_shield"`
|
||||
AlertCount int `json:"alert_count"`
|
||||
}
|
||||
|
||||
@@ -18,7 +18,9 @@ import (
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis/monitor"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/options"
|
||||
)
|
||||
|
||||
@@ -47,3 +49,29 @@ type MonitorResourceListOptions struct {
|
||||
func (o *MonitorResourceListOptions) Params() (jsonutils.JSONObject, error) {
|
||||
return options.ListStructToParams(o)
|
||||
}
|
||||
|
||||
type MonitorResourceDoActionOptions struct {
|
||||
ID string `help:"ID of the resource"`
|
||||
ACTION string `help:"Action name"`
|
||||
Data string `json:"json string data"`
|
||||
}
|
||||
|
||||
func (o *MonitorResourceDoActionOptions) GetId() string {
|
||||
return o.ID
|
||||
}
|
||||
|
||||
func (o *MonitorResourceDoActionOptions) Params() (jsonutils.JSONObject, error) {
|
||||
var data jsonutils.JSONObject = jsonutils.NewDict()
|
||||
if len(o.Data) != 0 {
|
||||
d, err := jsonutils.ParseString(o.Data)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "parse string to data: %s", o.Data)
|
||||
}
|
||||
data = d
|
||||
}
|
||||
input := monitor.MonitorResourceDoActionInput{
|
||||
Action: o.ACTION,
|
||||
Data: data,
|
||||
}
|
||||
return jsonutils.Marshal(input), nil
|
||||
}
|
||||
|
||||
@@ -800,3 +800,24 @@ func newMonitorResourceCreateInput(input jsonutils.JSONObject, typ string) jsonu
|
||||
|
||||
return monitorResource
|
||||
}
|
||||
|
||||
type MonitorResourceDoActionF func(obj *SMonitorResource, ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input *monitor.MonitorResourceDoActionInput) (jsonutils.JSONObject, error)
|
||||
|
||||
var (
|
||||
monitorResourceDoActionMap = make(map[string]MonitorResourceDoActionF)
|
||||
)
|
||||
|
||||
func RegisterMonitorResourceDoAction(action string, f MonitorResourceDoActionF) {
|
||||
if _, ok := monitorResourceDoActionMap[action]; ok {
|
||||
log.Fatalf("action %s already registered for monitor resource do action", action)
|
||||
}
|
||||
monitorResourceDoActionMap[action] = f
|
||||
}
|
||||
|
||||
func (res *SMonitorResource) PerformDoAction(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input *monitor.MonitorResourceDoActionInput) (jsonutils.JSONObject, error) {
|
||||
f, ok := monitorResourceDoActionMap[input.Action]
|
||||
if !ok {
|
||||
return nil, errors.Errorf("action %q not found for monitor resource do action", input.Action)
|
||||
}
|
||||
return f(res, ctx, userCred, query, input)
|
||||
}
|
||||
|
||||
@@ -204,6 +204,18 @@ func (m *SMonitorResourceAlertManager) GetNowAlertingAlerts(ctx context.Context,
|
||||
return alertRess, nil
|
||||
}
|
||||
|
||||
func (m *SMonitorResourceAlertManager) FilterByParams(q *sqlchemy.SQuery, params jsonutils.JSONObject) *sqlchemy.SQuery {
|
||||
input := &monitor.MonitorResourceJointListInput{}
|
||||
if err := params.Unmarshal(input); err != nil {
|
||||
log.Errorf("FilterByParams unmarshal params error: %v", err)
|
||||
return q
|
||||
}
|
||||
if input.Metric != "" {
|
||||
q = q.Equals("metric", input.Metric)
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
func (m *SMonitorResourceAlertManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, input *monitor.MonitorResourceJointListInput) (*sqlchemy.SQuery, error) {
|
||||
// 如果指定了时间段、top 和 alert_id 参数,执行特殊的 top 查询
|
||||
if input.Top != nil {
|
||||
@@ -299,6 +311,16 @@ func (m *SMonitorResourceAlertManager) CustomizeFilterList(ctx context.Context,
|
||||
return filters, nil
|
||||
}
|
||||
|
||||
func (m *SMonitorResourceAlertManager) GetMonitorResourceAlert(resId, alertId, metric string) (*SMonitorResourceAlert, error) {
|
||||
joint, err := db.FetchJointByIds(m, resId, alertId, jsonutils.Marshal(&monitor.MonitorResourceJointListInput{
|
||||
Metric: metric,
|
||||
}))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "FetchJointByIds with master id %s and slave id %s and metric %s", resId, alertId, metric)
|
||||
}
|
||||
return joint.(*SMonitorResourceAlert), nil
|
||||
}
|
||||
|
||||
// getTopResourcesByMetricAndAlertCount 查询指定时间段内,某个监控策略下各监控指标报警资源最多的 top N 资源
|
||||
func (m *SMonitorResourceAlertManager) getTopResourcesByMetricAndAlertCount(
|
||||
ctx context.Context,
|
||||
@@ -572,6 +594,7 @@ func (man *SMonitorResourceAlertManager) FetchCustomizeColumns(
|
||||
if res, ok := resources[obj.MonitorResourceId]; ok {
|
||||
rows[i].ResName = res.Name
|
||||
rows[i].ResType = res.ResType
|
||||
rows[i].MonitorResourceObjectId = res.GetId()
|
||||
}
|
||||
if _, ok := shieldsMap[fmt.Sprintf("%s-%s", obj.MonitorResourceId, obj.AlertId)]; ok {
|
||||
rows[i].IsSetShield = true
|
||||
|
||||
@@ -33,6 +33,19 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/monitor/options"
|
||||
)
|
||||
|
||||
var (
|
||||
modelManagers = []db.IModelManager{}
|
||||
jointModelManagers = []db.IJointModelManager{}
|
||||
)
|
||||
|
||||
type GetRegisteredModelManagersF func() []db.IModelManager
|
||||
type GetRegisteredJointModelManagersF func() []db.IJointModelManager
|
||||
|
||||
var (
|
||||
GetRegisteredModelManagers GetRegisteredModelManagersF = nil
|
||||
GetRegisteredJointModelManagers GetRegisteredJointModelManagersF = nil
|
||||
)
|
||||
|
||||
func InitHandlers(app *appsrv.Application, isSlave bool) {
|
||||
db.InitAllManagers()
|
||||
|
||||
@@ -52,25 +65,31 @@ func InitHandlers(app *appsrv.Application, isSlave bool) {
|
||||
db.RegisterModelManager(manager)
|
||||
}
|
||||
|
||||
for _, manager := range []db.IModelManager{
|
||||
db.OpsLog,
|
||||
db.Metadata,
|
||||
models.DataSourceManager,
|
||||
models.AlertManager,
|
||||
models.NodeAlertManager,
|
||||
models.MeterAlertManager,
|
||||
models.NotificationManager,
|
||||
models.CommonAlertManager,
|
||||
models.MetricMeasurementManager,
|
||||
models.MetricFieldManager,
|
||||
models.AlertRecordManager,
|
||||
models.AlertDashBoardManager,
|
||||
models.GetAlertResourceManager(),
|
||||
models.AlertPanelManager,
|
||||
models.MonitorResourceManager,
|
||||
models.AlertRecordShieldManager,
|
||||
models.GetMigrationAlertManager(),
|
||||
} {
|
||||
if GetRegisteredModelManagers == nil {
|
||||
GetRegisteredModelManagers = func() []db.IModelManager {
|
||||
return []db.IModelManager{
|
||||
db.OpsLog,
|
||||
db.Metadata,
|
||||
models.DataSourceManager,
|
||||
models.AlertManager,
|
||||
models.NodeAlertManager,
|
||||
models.MeterAlertManager,
|
||||
models.NotificationManager,
|
||||
models.CommonAlertManager,
|
||||
models.MetricMeasurementManager,
|
||||
models.MetricFieldManager,
|
||||
models.AlertRecordManager,
|
||||
models.AlertDashBoardManager,
|
||||
models.GetAlertResourceManager(),
|
||||
models.AlertPanelManager,
|
||||
models.MonitorResourceManager,
|
||||
models.AlertRecordShieldManager,
|
||||
models.GetMigrationAlertManager(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, manager := range GetRegisteredModelManagers() {
|
||||
db.RegisterModelManager(manager)
|
||||
handler := db.NewModelHandler(manager)
|
||||
dispatcher.AddModelDispatcher("", app, handler, isSlave)
|
||||
@@ -83,13 +102,18 @@ func InitHandlers(app *appsrv.Application, isSlave bool) {
|
||||
dispatcher.AddModelDispatcher("", app, handler, isSlave)
|
||||
}
|
||||
|
||||
for _, manager := range []db.IJointModelManager{
|
||||
models.AlertNotificationManager,
|
||||
models.MetricManager,
|
||||
models.GetAlertResourceAlertManager(),
|
||||
models.AlertDashBoardPanelManager,
|
||||
models.MonitorResourceAlertManager,
|
||||
} {
|
||||
if GetRegisteredJointModelManagers == nil {
|
||||
GetRegisteredJointModelManagers = func() []db.IJointModelManager {
|
||||
return []db.IJointModelManager{
|
||||
models.AlertNotificationManager,
|
||||
models.MetricManager,
|
||||
models.GetAlertResourceAlertManager(),
|
||||
models.AlertDashBoardPanelManager,
|
||||
models.MonitorResourceAlertManager,
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, manager := range GetRegisteredJointModelManagers() {
|
||||
db.RegisterModelManager(manager)
|
||||
handler := db.NewJointModelHandler(manager)
|
||||
dispatcher.AddJointModelDispatcher("", app, handler, isSlave)
|
||||
|
||||
Reference in New Issue
Block a user