diff --git a/model/alerting/destination.go b/model/alerting/destination.go index 25d6b3ea..dee045ce 100644 --- a/model/alerting/destination.go +++ b/model/alerting/destination.go @@ -4,7 +4,9 @@ package alerting -import "infini.sh/framework/core/orm" +import ( + "infini.sh/framework/core/orm" +) type Channel struct { orm.ORMObjectBase diff --git a/model/alerting/rule_test.go b/model/alerting/rule_test.go index a59c008f..0a6f2ea1 100644 --- a/model/alerting/rule_test.go +++ b/model/alerting/rule_test.go @@ -6,6 +6,7 @@ package alerting import ( "fmt" + "infini.sh/console/model/insight" "infini.sh/framework/core/util" "net/http" "testing" @@ -60,18 +61,21 @@ func TestCreateRule( t *testing.T) { //}, Metrics: Metric{ - PeriodInterval: "1m", - Items: []MetricItem{ - {Name: "a", Field: "payload.elasticsearch.node_stats.fs.total.free_in_bytes", Statistic: "min", Group: []string{"metadata.labels.cluster_id", "metadata.labels.node_id"}}, - {Name: "b", Field: "payload.elasticsearch.node_stats.fs.total.total_in_bytes", Statistic: "max", Group: []string{"metadata.labels.cluster_id", "metadata.labels.node_id"}}, + Metric: insight.Metric{ + Groups: []insight.MetricGroupItem{{"metadata.labels.cluster_id", 10}, {"metadata.labels.node_id", 10}}, + Items: []insight.MetricItem{ + {Name: "a", Field: "payload.elasticsearch.node_stats.fs.total.free_in_bytes", Statistic: "min" }, + {Name: "b", Field: "payload.elasticsearch.node_stats.fs.total.total_in_bytes", Statistic: "max"}, + }, + BucketSize: "1m", + Formula: "a/b*100", }, - Formula: "a/b*100", //Expression: "min(fs.free_in_bytes)/max(fs.total_in_bytes)*100", }, Conditions: Condition{ Operator: "any", Items: []ConditionItem{ - {MinimumPeriodMatch: 1, Operator: "lte", Values: []string{"76"}, Priority: "error", Message: "磁盘可用率小于10%"}, + {MinimumPeriodMatch: 1, Operator: "lte", Values: []string{"76"}, Priority: "error"}, }, }, diff --git a/plugin/api/email/server.go b/plugin/api/email/server.go index 4cee6854..f20c2ee4 100644 --- a/plugin/api/email/server.go +++ b/plugin/api/email/server.go @@ -203,6 +203,7 @@ func (h *EmailAPI) searchEmailServer(w http.ResponseWriter, req *http.Request, p var ( strSize = h.GetParameterOrDefault(req, "size", "20") strFrom = h.GetParameterOrDefault(req, "from", "0") + strEnabled = h.GetParameterOrDefault(req, "enabled", "true") ) size, _ := strconv.Atoi(strSize) if size <= 0 { @@ -217,7 +218,11 @@ func (h *EmailAPI) searchEmailServer(w http.ResponseWriter, req *http.Request, p From: from, Size: size, } - q.Conds = orm.And(orm.Eq("enabled", true)) + if strEnabled == "true" { + q.Conds = orm.And(orm.Eq("enabled", true)) + }else if strEnabled == "false" { + q.Conds = orm.And(orm.Eq("enabled", false)) + } err, res := orm.Search(&model.EmailServer{}, &q) if err != nil { diff --git a/service/alerting/elasticsearch/engine.go b/service/alerting/elasticsearch/engine.go index cf463a5f..b20a5f37 100644 --- a/service/alerting/elasticsearch/engine.go +++ b/service/alerting/elasticsearch/engine.go @@ -999,6 +999,10 @@ func performChannel(channel *alerting.Channel, ctx map[string]interface{}) ([]by message []byte err error ) + channel, err = RetrieveChannel(channel) + if err != nil { + return nil, err, nil + } switch channel.Type { case alerting.ChannelWebhook: diff --git a/service/alerting/elasticsearch/helper.go b/service/alerting/elasticsearch/helper.go new file mode 100644 index 00000000..f19ada63 --- /dev/null +++ b/service/alerting/elasticsearch/helper.go @@ -0,0 +1,24 @@ +/* Copyright © INFINI Ltd. All rights reserved. + * Web: https://infinilabs.com + * Email: hello#infini.ltd */ + +package elasticsearch + +import ( + "fmt" + "infini.sh/console/model/alerting" + "infini.sh/framework/core/orm" +) + +func RetrieveChannel(ch *alerting.Channel) (*alerting.Channel, error) { + if ch == nil { + return nil, fmt.Errorf("empty channel") + } + if ch.ID != "" { + _, err := orm.Get(ch) + if err != nil { + return nil, err + } + } + return ch, nil +}