bugfix: return err when unable to get monapi.collectRule (#537)
This commit is contained in:
parent
8fe3d2b0b3
commit
029f0a09ba
|
@ -40,6 +40,7 @@ func (p BaseCollector) TelegrafInput(rule *models.CollectRule) (telegraf.Input,
|
||||||
return r2.TelegrafInput()
|
return r2.TelegrafInput()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// used for ui
|
||||||
func (p BaseCollector) Get(id int64) (interface{}, error) {
|
func (p BaseCollector) Get(id int64) (interface{}, error) {
|
||||||
collect := &models.CollectRule{}
|
collect := &models.CollectRule{}
|
||||||
has, err := models.DB["mon"].Where("id = ?", id).Get(collect)
|
has, err := models.DB["mon"].Where("id = ?", id).Get(collect)
|
||||||
|
@ -49,6 +50,18 @@ func (p BaseCollector) Get(id int64) (interface{}, error) {
|
||||||
return collect, err
|
return collect, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p BaseCollector) mustGetRule(id int64) (*models.CollectRule, error) {
|
||||||
|
collect := &models.CollectRule{}
|
||||||
|
has, err := models.DB["mon"].Where("id = ?", id).Get(collect)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !has {
|
||||||
|
return nil, fmt.Errorf("unable to get the collectRule")
|
||||||
|
}
|
||||||
|
return collect, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p BaseCollector) Gets(nids []int64) (ret []interface{}, err error) {
|
func (p BaseCollector) Gets(nids []int64) (ret []interface{}, err error) {
|
||||||
collects := []models.CollectRule{}
|
collects := []models.CollectRule{}
|
||||||
err = models.DB["mon"].Where("collect_type=?", p.name).In("nid", nids).Find(&collects)
|
err = models.DB["mon"].Where("collect_type=?", p.name).In("nid", nids).Find(&collects)
|
||||||
|
@ -124,16 +137,11 @@ func (p BaseCollector) Update(data []byte, username string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
//校验采集是否存在
|
//校验采集是否存在
|
||||||
obj, err := p.Get(collect.Id) //id找不到的情况
|
obj, err := p.mustGetRule(collect.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("采集不存在 type:%s id:%d", p.name, collect.Id)
|
return fmt.Errorf("采集不存在 type:%s id:%d", p.name, collect.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpId := obj.(*models.CollectRule).Id
|
|
||||||
if tmpId == 0 {
|
|
||||||
return fmt.Errorf("采集不存在 type:%s id:%d", p.name, collect.Id)
|
|
||||||
}
|
|
||||||
|
|
||||||
collect.Updater = username
|
collect.Updater = username
|
||||||
collect.UpdatedAt = time.Now().Unix()
|
collect.UpdatedAt = time.Now().Unix()
|
||||||
|
|
||||||
|
@ -141,7 +149,7 @@ func (p BaseCollector) Update(data []byte, username string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if old != nil && tmpId != old.(*models.CollectRule).Id {
|
if old != nil && obj.Id != old.(*models.CollectRule).Id {
|
||||||
return fmt.Errorf("同节点下策略名称 %s 已存在", collect.Name)
|
return fmt.Errorf("同节点下策略名称 %s 已存在", collect.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,12 +157,11 @@ func (p BaseCollector) Update(data []byte, username string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p BaseCollector) Delete(id int64, username string) error {
|
func (p BaseCollector) Delete(id int64, username string) error {
|
||||||
tmp, err := p.Get(id) //id找不到的情况
|
rule, err := p.mustGetRule(id) //id找不到的情况
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("采集不存在 type:%s id:%d", p.name, id)
|
return fmt.Errorf("采集不存在 type:%s id:%d", p.name, id)
|
||||||
}
|
}
|
||||||
nid := tmp.(*models.CollectRule).Nid
|
can, err := models.UsernameCandoNodeOp(username, "mon_collect_delete", int64(rule.Nid))
|
||||||
can, err := models.UsernameCandoNodeOp(username, "mon_collect_delete", int64(nid))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("models.UsernameCandoNodeOp error %s", err)
|
return fmt.Errorf("models.UsernameCandoNodeOp error %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -142,7 +142,8 @@ func (p *CollectRuleCache) syncCollectRule() error {
|
||||||
collectRuleCount := len(resp.Data)
|
collectRuleCount := len(resp.Data)
|
||||||
stats.Counter.Set("collectrule.count", collectRuleCount)
|
stats.Counter.Set("collectrule.count", collectRuleCount)
|
||||||
if collectRuleCount == 0 { //获取策略数为0,不正常,不更新策略缓存
|
if collectRuleCount == 0 { //获取策略数为0,不正常,不更新策略缓存
|
||||||
return fmt.Errorf("clloect rule count is 0")
|
logger.Debugf("clloect rule count is 0")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rule := range resp.Data {
|
for _, rule := range resp.Data {
|
||||||
|
|
Loading…
Reference in New Issue