mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 02:37:24 +08:00
suggestAlert feature:
1.支持由建议列表忽略一类建议信息 2.建议列表和忽略列表,展示名称的处理。同时后端返回资源名称。 3.优化建议规则的修改。后端支持climc修改周期period和时间范围配置 4.优化建议规则返回describe信息 5.去掉了后端自动对忽略列表的init动作 影响范围: 优化建议规则的展示;建议列表的展示
This commit is contained in:
@@ -56,6 +56,7 @@ type SuggestSysAlertDetails struct {
|
||||
Suggest string `json:"suggest"`
|
||||
Brand string `json:"brand"`
|
||||
Account string `json:"account"`
|
||||
ResName string `json:"res_name"`
|
||||
}
|
||||
|
||||
type SuggestSysAlertUpdateInput struct {
|
||||
@@ -76,6 +77,7 @@ type SuggestSysAlertUpdateInput struct {
|
||||
|
||||
type SuggestAlertIngoreInput struct {
|
||||
apis.ScopedResourceCreateInput
|
||||
BatchIgnore bool `json:"batch_ignore"`
|
||||
}
|
||||
|
||||
type SuggestAlertProblem struct {
|
||||
|
||||
@@ -58,27 +58,30 @@ type SuggestSysRuleCreateInput struct {
|
||||
apis.StandaloneResourceCreateInput
|
||||
|
||||
// 查询指标周期
|
||||
Period string `json:"period"`
|
||||
TimeFrom string `json:"time_from"`
|
||||
Type string `json:"type"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
Setting *SSuggestSysAlertSetting `json:"setting"`
|
||||
Period string `json:"period"`
|
||||
TimeFrom string `json:"time_from"`
|
||||
Type string `json:"type"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
Setting *SSuggestSysAlertSetting `json:"setting"`
|
||||
IgnoreTimeFrom *bool `json:"ignore_time_from"`
|
||||
}
|
||||
|
||||
type SuggestSysRuleUpdateInput struct {
|
||||
apis.Meta
|
||||
|
||||
// 查询指标周期
|
||||
Period string `json:"period"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Setting *SSuggestSysAlertSetting `json:"setting"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
ExecTime time.Time `json:"exec_time"`
|
||||
Period string `json:"period"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Setting *SSuggestSysAlertSetting `json:"setting"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
ExecTime time.Time `json:"exec_time"`
|
||||
IgnorePeriod *bool `json:"ignore_period"`
|
||||
}
|
||||
|
||||
type SuggestSysRuleDetails struct {
|
||||
apis.StandaloneResourceDetails
|
||||
CommonAlertMetricDetails []*CommonAlertMetricDetails `json:"common_alert_metric_details"`
|
||||
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
|
||||
@@ -53,6 +53,7 @@ type SuggestSysRuleConfigDetails struct {
|
||||
RuleId string `json:"rule_id"`
|
||||
Rule string `json:"rule"`
|
||||
RuleEnabled bool `json:"rule_enabled"`
|
||||
ResName string `json:"res_name"`
|
||||
}
|
||||
|
||||
type SuggestSysRuleConfigListInput struct {
|
||||
@@ -62,3 +63,7 @@ type SuggestSysRuleConfigListInput struct {
|
||||
ResourceType *MonitorResourceType `json:"resource_type"`
|
||||
IgnoreAlert *bool `json:"ignore_alert"`
|
||||
}
|
||||
|
||||
type SuggestSysRuleConfigTypeInfo struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
@@ -18,20 +18,21 @@ var SnapShotUnusedCreateInput *monitor.SuggestSysRuleCreateInput
|
||||
var InitRuleCreateInputMap = make(map[string]*monitor.SuggestSysRuleCreateInput)
|
||||
|
||||
func init() {
|
||||
ignoreTimeFrom := true
|
||||
diskSetting := new(monitor.SSuggestSysAlertSetting)
|
||||
diskSetting.DiskUnused = new(monitor.DiskUnused)
|
||||
DiskUnusedCreateInput = NewRule("未挂载的云硬盘", "12h", "336h", monitor.DISK_UNUSED, diskSetting)
|
||||
DiskUnusedCreateInput = NewRule("未挂载的云硬盘", "12h", "336h", monitor.DISK_UNUSED, diskSetting, nil)
|
||||
|
||||
eipSetting := new(monitor.SSuggestSysAlertSetting)
|
||||
eipSetting.EIPUnused = new(monitor.EIPUnused)
|
||||
EipUnusedCreateInput = NewRule("未挂载的弹性公网IP", "12h", "336h", monitor.EIP_UNUSED, eipSetting)
|
||||
EipUnusedCreateInput = NewRule("未挂载的弹性公网IP", "12h", "336h", monitor.EIP_UNUSED, eipSetting, nil)
|
||||
|
||||
lbSetting := new(monitor.SSuggestSysAlertSetting)
|
||||
lbSetting.LBUnused = new(monitor.LBUnused)
|
||||
LbUnusedCreateInput = NewRule("未使用的负载均衡实例", "12h", "336h", monitor.LB_UNUSED, lbSetting)
|
||||
LbUnusedCreateInput = NewRule("未使用的负载均衡实例", "12h", "336h", monitor.LB_UNUSED, lbSetting, nil)
|
||||
|
||||
OssSecAclCreateInput = NewRule("对象存储权限为开放读、写的存储桶和文件", "12h", "336h", monitor.OSS_SEC_ACL,
|
||||
new(monitor.SSuggestSysAlertSetting))
|
||||
new(monitor.SSuggestSysAlertSetting), &ignoreTimeFrom)
|
||||
|
||||
redisSetting := new(monitor.SSuggestSysAlertSetting)
|
||||
scaleRule := monitor.Scale{
|
||||
@@ -43,7 +44,7 @@ func init() {
|
||||
Threshold: 100,
|
||||
}
|
||||
redisSetting.ScaleRule = &monitor.ScaleRule{scaleRule}
|
||||
RedisUnReasonableCreateInput = NewRule("空闲的redis", "12h", "336h", monitor.REDIS_UNREASONABLE, redisSetting)
|
||||
RedisUnReasonableCreateInput = NewRule("空闲的redis", "12h", "336h", monitor.REDIS_UNREASONABLE, redisSetting, nil)
|
||||
|
||||
rdsSetting := new(monitor.SSuggestSysAlertSetting)
|
||||
rdsSetting.ScaleRule = &monitor.ScaleRule{monitor.Scale{
|
||||
@@ -54,7 +55,7 @@ func init() {
|
||||
EvalType: "<",
|
||||
Threshold: 5,
|
||||
}}
|
||||
RdsUnReasonableCreateInput = NewRule("空闲的rds", "12h", "336h", monitor.RDS_UNREASONABLE, rdsSetting)
|
||||
RdsUnReasonableCreateInput = NewRule("空闲的rds", "12h", "336h", monitor.RDS_UNREASONABLE, rdsSetting, nil)
|
||||
|
||||
ossSetting := new(monitor.SSuggestSysAlertSetting)
|
||||
ossSetting.ScaleRule = &monitor.ScaleRule{monitor.Scale{
|
||||
@@ -65,7 +66,7 @@ func init() {
|
||||
EvalType: "<",
|
||||
Threshold: 100,
|
||||
}}
|
||||
OssUnReasonableCreateInput = NewRule("空闲的oss", "12h", "336h", monitor.OSS_UNREASONABLE, ossSetting)
|
||||
OssUnReasonableCreateInput = NewRule("空闲的oss", "12h", "336h", monitor.OSS_UNREASONABLE, ossSetting, nil)
|
||||
|
||||
serversetting := new(monitor.SSuggestSysAlertSetting)
|
||||
serversetting.ScaleRule = &monitor.ScaleRule{monitor.Scale{
|
||||
@@ -76,16 +77,17 @@ func init() {
|
||||
EvalType: "<",
|
||||
Threshold: 5,
|
||||
}}
|
||||
ScaleDownCreateInput = NewRule("低负载的虚拟机", "12h", "336h", monitor.SCALE_DOWN, serversetting)
|
||||
ScaleDownCreateInput = NewRule("低负载的虚拟机", "12h", "336h", monitor.SCALE_DOWN, serversetting, nil)
|
||||
|
||||
SecGroupRuleInCreateInput = NewRule("安全组规则的in规则为全开放的主机", "12h", "336h",
|
||||
monitor.SECGROUPRULEINSERVER_ALLIN, &monitor.SSuggestSysAlertSetting{})
|
||||
monitor.SECGROUPRULEINSERVER_ALLIN, &monitor.SSuggestSysAlertSetting{}, &ignoreTimeFrom)
|
||||
|
||||
SnapShotUnusedCreateInput = NewRule("未使用的快照", "12h", "336h", monitor.SNAPSHOT_UNUSED,
|
||||
&monitor.SSuggestSysAlertSetting{})
|
||||
&monitor.SSuggestSysAlertSetting{}, nil)
|
||||
}
|
||||
|
||||
func NewRule(name, period, timeFrom string, typ monitor.SuggestDriverType, setting *monitor.SSuggestSysAlertSetting) *monitor.
|
||||
func NewRule(name, period, timeFrom string, typ monitor.SuggestDriverType, setting *monitor.SSuggestSysAlertSetting,
|
||||
ignore *bool) *monitor.
|
||||
SuggestSysRuleCreateInput {
|
||||
rule := new(monitor.SuggestSysRuleCreateInput)
|
||||
enable := false
|
||||
@@ -95,5 +97,6 @@ func NewRule(name, period, timeFrom string, typ monitor.SuggestDriverType, setti
|
||||
rule.TimeFrom = timeFrom
|
||||
rule.Setting = setting
|
||||
rule.Enabled = &enable
|
||||
rule.IgnoreTimeFrom = ignore
|
||||
return rule
|
||||
}
|
||||
|
||||
@@ -241,17 +241,6 @@ func (self *SSuggestSysAlert) GetType() monitor.SuggestDriverType {
|
||||
return monitor.SuggestDriverType(self.Type)
|
||||
}
|
||||
|
||||
func (self *SSuggestSysAlert) GetShowName() string {
|
||||
rule, _ := SuggestSysRuleManager.GetRules(self.GetType())
|
||||
var showName string
|
||||
if len(rule) != 0 {
|
||||
showName = fmt.Sprintf("%s-%s", self.Name, rule[0].Name)
|
||||
} else {
|
||||
showName = fmt.Sprintf("%s-%s", self.Name, self.Type)
|
||||
}
|
||||
return showName
|
||||
}
|
||||
|
||||
func (self *SSuggestSysAlert) getMoreDetails(out monitor.SuggestSysAlertDetails) monitor.SuggestSysAlertDetails {
|
||||
err := self.ResMeta.Unmarshal(&out)
|
||||
if err != nil {
|
||||
@@ -261,11 +250,20 @@ func (self *SSuggestSysAlert) getMoreDetails(out monitor.SuggestSysAlertDetails)
|
||||
out.Account = self.Cloudaccount
|
||||
out.ResType = string(drv.GetResourceType())
|
||||
out.RuleName = strings.ToLower(string(drv.GetType()))
|
||||
out.ShowName = self.GetShowName()
|
||||
out.ShowName = self.Name
|
||||
out.Suggest = string(drv.GetSuggest())
|
||||
out.ResName = SuggestSysAlertManager.getOriName(self.Name, self.Type)
|
||||
return out
|
||||
}
|
||||
|
||||
func (self *SSuggestSysAlertManager) getOriName(name, typ string) string {
|
||||
lastIndex := strings.LastIndex(name, fmt.Sprintf("-%s", typ))
|
||||
if lastIndex == -1 || lastIndex == 0 {
|
||||
lastIndex = len(name)
|
||||
}
|
||||
return name[:lastIndex]
|
||||
}
|
||||
|
||||
func (manager *SSuggestSysAlertManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
q, err = manager.SVirtualResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
@@ -363,7 +361,8 @@ func (self *SSuggestSysAlert) AllowPerformIgnore(ctx context.Context, userCred m
|
||||
return db.IsProjectAllowPerform(userCred, self, "ignore")
|
||||
}
|
||||
|
||||
func (self *SSuggestSysAlert) GetSuggestConfig(scope rbacutils.TRbacScope, domainId string, projectId string) (*SSuggestSysRuleConfig, error) {
|
||||
func (self *SSuggestSysAlert) GetSuggestConfig(scope rbacutils.TRbacScope, domainId string, projectId string,
|
||||
batchIgnore bool) (*SSuggestSysRuleConfig, error) {
|
||||
if scope == "" {
|
||||
scope = rbacutils.ScopeSystem
|
||||
}
|
||||
@@ -377,7 +376,10 @@ func (self *SSuggestSysAlert) GetSuggestConfig(scope rbacutils.TRbacScope, domai
|
||||
} else if scope == rbacutils.ScopeProject {
|
||||
scopeId = projectId
|
||||
}
|
||||
q := SuggestSysRuleConfigManager.Query().Equals("type", drvType).Equals("resource_id", resId).Equals("resource_type", resType)
|
||||
q := SuggestSysRuleConfigManager.Query().Equals("type", drvType).Equals("resource_type", resType)
|
||||
if !batchIgnore {
|
||||
q = q.Equals("resource_id", resId)
|
||||
}
|
||||
q = SuggestSysRuleConfigManager.FilterByScope(q, scope, scopeId)
|
||||
configs := make([]SSuggestSysRuleConfig, 0)
|
||||
if err := db.FetchModelObjects(SuggestSysRuleConfigManager, q, &configs); err != nil {
|
||||
@@ -394,7 +396,7 @@ func (self *SSuggestSysAlert) PerformIgnore(ctx context.Context, userCred mcclie
|
||||
if data.Scope == "" {
|
||||
data.Scope = string(rbacutils.ScopeSystem)
|
||||
}
|
||||
config, err := self.GetSuggestConfig(rbacutils.TRbacScope(data.Scope), data.ProjectDomainId, data.ProjectId)
|
||||
config, err := self.GetSuggestConfig(rbacutils.TRbacScope(data.Scope), data.ProjectDomainId, data.ProjectId, data.BatchIgnore)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -403,14 +405,20 @@ func (self *SSuggestSysAlert) PerformIgnore(ctx context.Context, userCred mcclie
|
||||
resType := drv.GetResourceType()
|
||||
if config == nil {
|
||||
createData := new(monitor.SuggestSysRuleConfigCreateInput)
|
||||
createData.Name = self.GetShowName()
|
||||
createData.Name = self.Name
|
||||
createData.ScopedResourceCreateInput = data.ScopedResourceCreateInput
|
||||
createData.Type = &drvType
|
||||
createData.ResourceType = &resType
|
||||
createData.ResourceId = &self.ResId
|
||||
if !data.BatchIgnore {
|
||||
createData.ResourceId = &self.ResId
|
||||
}
|
||||
ownerId, err := SuggestSysAlertManager.FetchOwnerId(ctx, jsonutils.Marshal(&data))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SuggestSysAlertManager FetchOwnerId error")
|
||||
}
|
||||
createData.IgnoreAlert = true
|
||||
data := createData.JSON(createData)
|
||||
conf, err := db.DoCreate(SuggestSysRuleConfigManager, ctx, userCred, nil, data, userCred)
|
||||
conf, err := db.DoCreate(SuggestSysRuleConfigManager, ctx, userCred, nil, data, ownerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -40,6 +40,10 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
)
|
||||
|
||||
const (
|
||||
SUGGESTRULE_METADATA_IGNORETIME = "ignore_time_from"
|
||||
)
|
||||
|
||||
var (
|
||||
SuggestSysRuleManager *SSuggestSysRuleManager
|
||||
)
|
||||
@@ -135,7 +139,6 @@ func (man *SSuggestSysRuleManager) ValidateCreateData(
|
||||
ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject,
|
||||
data monitor.SuggestSysRuleCreateInput) (monitor.SuggestSysRuleCreateInput, error) {
|
||||
if data.Period == "" {
|
||||
// default 30s
|
||||
data.Period = "12h"
|
||||
} else {
|
||||
data.Period = parseDuration(data.Period)
|
||||
@@ -244,9 +247,33 @@ func (self *SSuggestSysRule) getMoreDetails(out monitor.SuggestSysRuleDetails) m
|
||||
out.Enabled = self.GetEnabled()
|
||||
self.Period = showDuration(self.Period)
|
||||
self.TimeFrom = showDuration(self.TimeFrom)
|
||||
ignore, _ := strconv.ParseBool(self.getIgnoreTimeFrom())
|
||||
if ignore {
|
||||
self.TimeFrom = ""
|
||||
}
|
||||
self.getMetricDetails(&out)
|
||||
return out
|
||||
}
|
||||
|
||||
func (self *SSuggestSysRule) getMetricDetails(out *monitor.SuggestSysRuleDetails) {
|
||||
if out.Setting.ScaleRule != nil {
|
||||
scaleRule := *out.Setting.ScaleRule
|
||||
commonAlertMetricDetails := make([]*monitor.CommonAlertMetricDetails, len(scaleRule))
|
||||
for i, rule := range scaleRule {
|
||||
metricDetails := monitor.CommonAlertMetricDetails{
|
||||
Comparator: rule.EvalType,
|
||||
Threshold: rule.Threshold,
|
||||
DB: rule.Database,
|
||||
Measurement: rule.Measurement,
|
||||
Field: rule.Field,
|
||||
}
|
||||
getMetricDescriptionDetails(&metricDetails)
|
||||
commonAlertMetricDetails[i] = &metricDetails
|
||||
}
|
||||
out.CommonAlertMetricDetails = commonAlertMetricDetails
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SSuggestSysRule) GetExtraDetails(
|
||||
ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
@@ -259,6 +286,10 @@ func (self *SSuggestSysRule) GetExtraDetails(
|
||||
// after create, update Cronjob's info
|
||||
func (self *SSuggestSysRule) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
self.SStandaloneResourceBase.PostCreate(ctx, userCred, ownerId, query, data)
|
||||
ignore, err := data.GetString("ignore_time_from")
|
||||
if err == nil {
|
||||
self.setIgnoreTimeFrom(ctx, userCred, ignore)
|
||||
}
|
||||
self.updateCronjob()
|
||||
}
|
||||
|
||||
@@ -266,9 +297,22 @@ func (self *SSuggestSysRule) PostCreate(ctx context.Context, userCred mcclient.T
|
||||
func (self *SSuggestSysRule) PostUpdate(
|
||||
ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
ignore, err := data.GetString("ignore_time_from")
|
||||
if err == nil {
|
||||
self.setIgnoreTimeFrom(ctx, userCred, ignore)
|
||||
}
|
||||
self.updateCronjob()
|
||||
}
|
||||
|
||||
func (self *SSuggestSysRule) setIgnoreTimeFrom(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
ignore string) error {
|
||||
return self.SetMetadata(ctx, SUGGESTRULE_METADATA_IGNORETIME, ignore, userCred)
|
||||
}
|
||||
|
||||
func (self *SSuggestSysRule) getIgnoreTimeFrom() string {
|
||||
return self.GetMetadata(SUGGESTRULE_METADATA_IGNORETIME, nil)
|
||||
}
|
||||
|
||||
func (self *SSuggestSysRule) updateCronjob() {
|
||||
cronman.GetCronJobManager().Remove(self.Type)
|
||||
if self.Enabled.Bool() {
|
||||
@@ -529,6 +573,10 @@ func (man *SSuggestSysRuleManager) initUpdateDefaultRule(rule ruleInfo) error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "initUpdateDefaultRule error")
|
||||
}
|
||||
if ruleCreateInput.IgnoreTimeFrom != nil {
|
||||
suggestRule.setIgnoreTimeFrom(context.Background(), auth.AdminCredential(),
|
||||
strconv.FormatBool(*ruleCreateInput.IgnoreTimeFrom))
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -196,7 +196,8 @@ func (man *SSuggestSysRuleConfigManager) AllowGetPropertySupportTypes(ctx contex
|
||||
return true
|
||||
}
|
||||
|
||||
func (man *SSuggestSysRuleConfigManager) GetPropertySupportTypes(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (*monitor.SuggestSysRuleConfigSupportTypes, error) {
|
||||
func (man *SSuggestSysRuleConfigManager) GetPropertySupportTypes(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject) (*monitor.SuggestSysRuleConfigSupportTypes, error) {
|
||||
ret := &monitor.SuggestSysRuleConfigSupportTypes{
|
||||
Types: make([]monitor.SuggestDriverType, 0),
|
||||
ResourceTypes: make([]string, 0),
|
||||
@@ -209,21 +210,70 @@ func (man *SSuggestSysRuleConfigManager) GetPropertySupportTypes(ctx context.Con
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (man *SSuggestSysRuleConfigManager) AllowGetPropertyTypeInfo(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (man *SSuggestSysRuleConfigManager) GetPropertyTypeInfo(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject) (*monitor.SuggestSysRuleConfigTypeInfo, error) {
|
||||
searchInput := new(monitor.SuggestSysRuleConfigListInput)
|
||||
err := query.Unmarshal(searchInput)
|
||||
if err != nil {
|
||||
return nil, errors.Errorf("GetPropertyTypeInfo Unmarshal error:%v", err)
|
||||
}
|
||||
if searchInput.Type == nil {
|
||||
return nil, httperrors.NewInputParameterError("SuggestSysRuleConfig type is empty")
|
||||
}
|
||||
ownerId, err := man.FetchOwnerId(ctx, query)
|
||||
if err != nil {
|
||||
return nil, errors.Errorf("SSuggestSysRuleConfigManager FetchOwnerId error:%v", err)
|
||||
}
|
||||
if ownerId == nil {
|
||||
return nil, httperrors.NewInputParameterError("project or domain is empty")
|
||||
}
|
||||
configs, err := man.getConfigsOfBatchType(rbacutils.TRbacScope(searchInput.Scope), ownerId, string(*searchInput.Type))
|
||||
if err != nil {
|
||||
return nil, errors.Errorf("SSuggestSysRuleConfigManager getConfigsOfBatchType error:%v", err)
|
||||
}
|
||||
typeInfo := new(monitor.SuggestSysRuleConfigTypeInfo)
|
||||
if len(configs) != 0 {
|
||||
typeInfo.Name = configs[0].Name
|
||||
}
|
||||
return typeInfo, nil
|
||||
}
|
||||
|
||||
func (man *SSuggestSysRuleConfigManager) getConfigsQueryByScope(scope rbacutils.TRbacScope,
|
||||
ownerId mcclient.IIdentityProvider) *sqlchemy.SQuery {
|
||||
query := man.Query()
|
||||
switch scope {
|
||||
case rbacutils.ScopeSystem:
|
||||
query = query.IsNullOrEmpty("domain_id").IsNullOrEmpty("tenant_id")
|
||||
case rbacutils.ScopeDomain:
|
||||
query = query.Equals("domain_id", ownerId.GetProjectDomainId()).IsNullOrEmpty("tenant_id")
|
||||
case rbacutils.ScopeProject:
|
||||
query = query.Equals("tenant_id", ownerId.GetProjectId())
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func (man *SSuggestSysRuleConfigManager) getConfigsOfBatchType(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, Typ string) ([]SSuggestSysRuleConfig, error) {
|
||||
query := man.getConfigsQueryByScope(scope, ownerId)
|
||||
query = query.Equals("type", Typ).IsNullOrEmpty("resource_id")
|
||||
configs := make([]SSuggestSysRuleConfig, 0)
|
||||
if err := db.FetchModelObjects(man, query, &configs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
func (man *SSuggestSysRuleConfigManager) NamespaceScope() rbacutils.TRbacScope {
|
||||
return rbacutils.ScopeNone
|
||||
}
|
||||
|
||||
func (man *SSuggestSysRuleConfigManager) GetConfigsByScope(scope rbacutils.TRbacScope, userCred mcclient.TokenCredential, ignoreAlert bool) ([]SSuggestSysRuleConfig, error) {
|
||||
q := man.Query().Equals("ignore_alert", ignoreAlert)
|
||||
q := man.getConfigsQueryByScope(scope, userCred)
|
||||
q = q.Equals("ignore_alert", ignoreAlert)
|
||||
configs := make([]SSuggestSysRuleConfig, 0)
|
||||
switch scope {
|
||||
case rbacutils.ScopeSystem:
|
||||
q = q.IsNullOrEmpty("domain_id").IsNullOrEmpty("tenant_id")
|
||||
case rbacutils.ScopeDomain:
|
||||
q = q.Equals("domain_id", userCred.GetProjectDomainId()).IsNullOrEmpty("tenant_id")
|
||||
case rbacutils.ScopeProject:
|
||||
q = q.Equals("tenant_id", userCred.GetProjectId())
|
||||
}
|
||||
if err := db.FetchModelObjects(man, q, &configs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -351,6 +401,9 @@ func (conf *SSuggestSysRuleConfig) getMoreColumns(out monitor.SuggestSysRuleConf
|
||||
out.RuleId = rule.GetId()
|
||||
out.Rule = rule.GetName()
|
||||
out.RuleEnabled = rule.GetEnabled()
|
||||
if len(conf.ResourceId) != 0 {
|
||||
out.ResName = SuggestSysAlertManager.getOriName(conf.Name, conf.Type)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -65,7 +65,6 @@ func StartService() {
|
||||
|
||||
cron := cronman.InitCronJobManager(true, opts.CronJobWorkerCount)
|
||||
suggestsysdrivers.InitSuggestSysRuleCronjob()
|
||||
cron.AddJobAtIntervalsWithStartRun("InitScopeSuggestConfigs", time.Duration(opts.InitScopeSuggestConfigIntervalSeconds)*time.Second, models.SuggestSysRuleConfigManager.InitScopeConfigs, true)
|
||||
cron.AddJobAtIntervalsWithStartRun("InitAlertResourceAdminRoleUsers", time.Duration(opts.InitAlertResourceAdminRoleUsersIntervalSeconds)*time.Second, models.GetAlertResourceManager().GetAdminRoleUsers, true)
|
||||
cron.Start()
|
||||
defer cron.Stop()
|
||||
|
||||
@@ -77,8 +77,13 @@ func DealAlertData(drvType monitor.SuggestDriverType, oldAlerts []models.SSugges
|
||||
delete(oldMap, res_id)
|
||||
} else {
|
||||
//新增的alert
|
||||
_, err := db.DoCreate(models.SuggestSysAlertManager, context.Background(), adminCredential, nil, newAlert,
|
||||
adminCredential)
|
||||
ownerId, err := models.SuggestSysAlertManager.FetchOwnerId(context.Background(), newAlert)
|
||||
if err != nil {
|
||||
log.Errorf("create SuggestSysAlert FetchOwnerId param:%v. error:%v", newAlert, err)
|
||||
continue
|
||||
}
|
||||
_, err = db.DoCreate(models.SuggestSysAlertManager, context.Background(), adminCredential, nil, newAlert,
|
||||
ownerId)
|
||||
if err != nil {
|
||||
log.Errorf("create new suggest alert %v error: %v", newAlert, err)
|
||||
}
|
||||
@@ -154,6 +159,7 @@ func getSuggestSysAlertFromJson(obj jsonutils.JSONObject, rule models.ISuggestSy
|
||||
suggestSysAlert.Cloudaccount = val
|
||||
}
|
||||
suggestSysAlert.Type = string(rule.GetType())
|
||||
suggestSysAlert.Name = fmt.Sprintf("%s-%s", suggestSysAlert.Name, suggestSysAlert.Type)
|
||||
suggestSysAlert.ResMeta = obj
|
||||
suggestSysAlert.Action = string(rule.GetAction())
|
||||
suggestSysAlert.Status = monitor.SUGGEST_ALERT_READY
|
||||
|
||||
@@ -145,7 +145,7 @@ func (drv *OssSecAcl) getBucketsByAcl() ([]jsonutils.JSONObject, error) {
|
||||
Description: acl,
|
||||
},
|
||||
}
|
||||
suggestSysAlert.Name = GenerateName(suggestSysAlert.Name, string(drv.GetType()))
|
||||
//suggestSysAlert.Name = GenerateName(suggestSysAlert.Name, string(drv.GetType()))
|
||||
suggestSysAlert.Problem = jsonutils.Marshal(&problems)
|
||||
bucketArr = append(bucketArr, jsonutils.Marshal(suggestSysAlert))
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package suggestsysdrivers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
@@ -61,7 +60,7 @@ func (drv *SecGroupRuleInServer) GetLatestAlerts(rule *models.SSuggestSysRule,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
suggestSysAlert.Name = fmt.Sprintf("%s-%s", suggestSysAlert.Name, string(drv.GetType()))
|
||||
//suggestSysAlert.Name = fmt.Sprintf("%s-%s", suggestSysAlert.Name, string(drv.GetType()))
|
||||
suggestSysAlert.Amount = 0
|
||||
secGroupRuleInServerArr = append(secGroupRuleInServerArr, jsonutils.Marshal(suggestSysAlert))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user