mirror of
https://hubproxy.babadafafafafa.cn/https://github.com/yunionio/cloudpods.git
synced 2026-09-21 00:24:07 +08:00
1.suggestsys_const.go 中定义规则和资源列表中涉及到一些常量 2.suggestsysalart.go 1)新增一些列来匹配重用过滤;2) 进行删除相关操作时,通过task处理; 3.driverconfig.go 中包含目前driver中可以共用的函数 4.suggestsysruledrivers.go 调整driver interface 涉及的函数
161 lines
4.7 KiB
Go
161 lines
4.7 KiB
Go
// Copyright 2019 Yunion
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package suggestsysdrivers
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"yunion.io/x/jsonutils"
|
|
"yunion.io/x/log"
|
|
"yunion.io/x/pkg/errors"
|
|
|
|
"yunion.io/x/onecloud/pkg/apis/monitor"
|
|
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
|
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
|
"yunion.io/x/onecloud/pkg/mcclient"
|
|
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
|
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
|
"yunion.io/x/onecloud/pkg/monitor/models"
|
|
)
|
|
|
|
type EIPUnused struct {
|
|
monitor.EIPUnused
|
|
}
|
|
|
|
func (_ *EIPUnused) GetType() string {
|
|
return monitor.EIP_UN_USED
|
|
}
|
|
|
|
func (_ *EIPUnused) GetResourceType() string {
|
|
return string(monitor.EIP_MONITOR_RES_TYPE)
|
|
}
|
|
|
|
func NewEIPUsedDriver() models.ISuggestSysRuleDriver {
|
|
return &EIPUnused{
|
|
monitor.EIPUnused{
|
|
//Status: "",
|
|
},
|
|
}
|
|
}
|
|
|
|
func (dri *EIPUnused) ValidateSetting(input *monitor.SSuggestSysAlertSetting) error {
|
|
obj := new(monitor.EIPUnused)
|
|
input.EIPUnused = obj
|
|
return nil
|
|
}
|
|
|
|
func (rule *EIPUnused) Run(instance *monitor.SSuggestSysAlertSetting) {
|
|
oldAlert := make([]models.SSuggestSysAlert, 0)
|
|
q := models.SuggestSysAlertManager.Query()
|
|
q.Equals("type", monitor.EIP_UN_USED)
|
|
err := db.FetchModelObjects(models.SuggestSysAlertManager, q, &oldAlert)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
log.Errorln(errors.Wrap(err, "db.FetchModelObjects"))
|
|
return
|
|
}
|
|
newAlert, err := rule.getEIPUnused(instance)
|
|
if err != nil {
|
|
log.Errorln(errors.Wrap(err, "getEIPUnused error"))
|
|
return
|
|
}
|
|
|
|
DealAlertData(oldAlert, newAlert.Value())
|
|
}
|
|
|
|
func (rule *EIPUnused) getEIPUnused(instance *monitor.SSuggestSysAlertSetting) (*jsonutils.JSONArray, error) {
|
|
//处理逻辑
|
|
session := auth.GetAdminSession(context.Background(), "", "")
|
|
query := jsonutils.NewDict()
|
|
query.Add(jsonutils.NewString("0"), "limit")
|
|
query.Add(jsonutils.NewString("system"), "scope")
|
|
rtn, err := modules.Elasticips.List(session, query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
EIPUnsedArr := jsonutils.NewArray()
|
|
for _, row := range rtn.Data {
|
|
if row.ContainsIgnoreCases("associate_type") || row.ContainsIgnoreCases("associate_id") {
|
|
continue
|
|
}
|
|
suggestSysAlert := new(models.SSuggestSysAlert)
|
|
alertData := jsonutils.DeepCopy(row).(*jsonutils.JSONDict)
|
|
id, _ := alertData.GetString("id")
|
|
alertData.Add(jsonutils.NewString(id), "res_id")
|
|
alertData.Remove("id")
|
|
|
|
err := alertData.Unmarshal(suggestSysAlert)
|
|
if err != nil {
|
|
return EIPUnsedArr, errors.Wrap(err, "getEIPUnused's alertData Unmarshal error")
|
|
}
|
|
if val, err := alertData.GetString("account"); err == nil {
|
|
suggestSysAlert.Cloudaccount = val
|
|
}
|
|
|
|
input := &monitor.SSuggestSysAlertSetting{
|
|
EIPUnused: &monitor.EIPUnused{},
|
|
}
|
|
suggestSysAlert.MonitorConfig = jsonutils.Marshal(input)
|
|
if instance != nil {
|
|
suggestSysAlert.MonitorConfig = jsonutils.Marshal(instance)
|
|
}
|
|
|
|
problem := jsonutils.NewDict()
|
|
problem.Add(jsonutils.NewString(rule.GetType()), "eip")
|
|
suggestSysAlert.Problem = problem
|
|
|
|
suggestSysAlert.Type = rule.GetType()
|
|
|
|
suggestSysAlert.Action = monitor.DRIVER_ACTION
|
|
suggestSysAlert.ResMeta = row
|
|
EIPUnsedArr.Add(jsonutils.Marshal(suggestSysAlert))
|
|
}
|
|
return EIPUnsedArr, nil
|
|
}
|
|
|
|
func (rule *EIPUnused) DoSuggestSysRule(ctx context.Context, userCred mcclient.TokenCredential,
|
|
isStart bool) {
|
|
var instance *monitor.SSuggestSysAlertSetting
|
|
suggestSysSettingMap, err := models.SuggestSysRuleManager.FetchSuggestSysAlartSettings(rule.GetType())
|
|
if err != nil {
|
|
log.Errorln("DoSuggestSysRule error :", err)
|
|
return
|
|
}
|
|
if details, ok := suggestSysSettingMap[rule.GetType()]; ok {
|
|
instance = details.Setting
|
|
}
|
|
rule.Run(instance)
|
|
}
|
|
|
|
func (rule *EIPUnused) Resolve(data *models.SSuggestSysAlert) error {
|
|
session := auth.GetAdminSession(context.Background(), "", "")
|
|
_, err := modules.Elasticips.Delete(session, data.ResId, jsonutils.NewDict())
|
|
if err != nil {
|
|
log.Errorln("delete unused eip error", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (rule *EIPUnused) StartResolveTask(ctx context.Context, userCred mcclient.TokenCredential,
|
|
suggestSysAlert *models.SSuggestSysAlert, params *jsonutils.JSONDict) error {
|
|
task, err := taskman.TaskManager.NewTask(ctx, "ResolveUnusedTask", suggestSysAlert, userCred, params, "", "", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
task.ScheduleRun(nil)
|
|
return nil
|
|
}
|