From 05aba257f01fadb19c81dcd62481d7892cc62569 Mon Sep 17 00:00:00 2001 From: uliian Date: Fri, 1 Sep 2023 16:07:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/WebAlertRouter.go | 15 +- controllers/WebTemplate.go | 10 +- controllers/prometheusalert.go | 5 +- models/AlertRouter.go | 13 +- views/alertrouter.html | 293 ++++++++++++++++++--------------- 5 files changed, 190 insertions(+), 146 deletions(-) diff --git a/controllers/WebAlertRouter.go b/controllers/WebAlertRouter.go index ab49c2a1..17b07a0d 100644 --- a/controllers/WebAlertRouter.go +++ b/controllers/WebAlertRouter.go @@ -7,7 +7,7 @@ import ( "strconv" ) -//router +// router func (c *MainController) AlertRouter() { if !CheckAccount(c.Ctx) { c.Redirect("/login", 302) @@ -16,14 +16,19 @@ func (c *MainController) AlertRouter() { c.Data["IsAlertRouter"] = true c.Data["IsAlertManageMenu"] = true c.TplName = "alertrouter.html" - - GlobalAlertRouter, _ = models.GetAllAlertRouter() + query := models.AlertRouterQuery{} + query.Name = c.GetString("name", "") + query.Webhook = c.GetString("webhook", "") + //刷新告警路由AlertRouter + GlobalAlertRouter, _ = models.GetAllAlertRouter(query) c.Data["AlertRouter"] = GlobalAlertRouter c.Data["IsLogin"] = CheckAccount(c.Ctx) + c.Data["SearchName"] = query.Name + c.Data["SearchWebhook"] = query.Webhook } -//router add +// router add func (c *MainController) RouterAdd() { if !CheckAccount(c.Ctx) { c.Redirect("/login", 302) @@ -83,7 +88,7 @@ func (c *MainController) AddRouter() { c.ServeJSON() } -//router edit +// router edit func (c *MainController) RouterEdit() { if !CheckAccount(c.Ctx) { c.Redirect("/login", 302) diff --git a/controllers/WebTemplate.go b/controllers/WebTemplate.go index a729c831..de36312f 100644 --- a/controllers/WebTemplate.go +++ b/controllers/WebTemplate.go @@ -3,13 +3,13 @@ package controllers import ( "PrometheusAlert/models" "encoding/json" + "github.com/astaxie/beego" "github.com/astaxie/beego/logs" - "github.com/astaxie/beego" "strconv" "strings" ) -//template page +// template page func (c *MainController) Template() { if !CheckAccount(c.Ctx) { c.Redirect("/login", 302) @@ -26,7 +26,7 @@ func (c *MainController) Template() { c.Data["IsLogin"] = CheckAccount(c.Ctx) } -//template add +// template add func (c *MainController) TemplateAdd() { if !CheckAccount(c.Ctx) { c.Redirect("/login", 302) @@ -65,7 +65,7 @@ func (c *MainController) AddTpl() { resp = err GlobalPrometheusAlertTpl, _ = models.GetAllTpl() } - GlobalAlertRouter, _ = models.GetAllAlertRouter() + GlobalAlertRouter, _ = models.GetAllAlertRouter(models.AlertRouterQuery{}) c.Data["json"] = resp c.ServeJSON() } @@ -88,7 +88,7 @@ func (c *MainController) ImportTpl() { } GlobalPrometheusAlertTpl, _ = models.GetAllTpl() - GlobalAlertRouter, _ = models.GetAllAlertRouter() + GlobalAlertRouter, _ = models.GetAllAlertRouter(models.AlertRouterQuery{}) c.Data["json"] = resp c.ServeJSON() } else { diff --git a/controllers/prometheusalert.go b/controllers/prometheusalert.go index 1983cd7f..a4b30994 100644 --- a/controllers/prometheusalert.go +++ b/controllers/prometheusalert.go @@ -165,8 +165,11 @@ func (c *PrometheusAlertController) PrometheusAlert() { if pMsg.Split != "false" && PrometheusAlertTpl.Tpluse == "Prometheus" { //判断告警路由AlertRouter列表是否为空 if GlobalAlertRouter == nil { + query := models.AlertRouterQuery{} + query.Name = c.GetString("name", "") + query.Webhook = c.GetString("webhook", "") //刷新告警路由AlertRouter - GlobalAlertRouter, _ = models.GetAllAlertRouter() + GlobalAlertRouter, _ = models.GetAllAlertRouter(query) } Alerts_Value, _ := p_alertmanager_json["alerts"].([]interface{}) //拆分告警消息 diff --git a/models/AlertRouter.go b/models/AlertRouter.go index 879e7511..e804a319 100644 --- a/models/AlertRouter.go +++ b/models/AlertRouter.go @@ -16,6 +16,11 @@ type AlertRouter struct { Created time.Time } +type AlertRouterQuery struct { + Name string + Webhook string +} + func AddAlertRouter(id int, tplid int, name, rules, url_or_phone, at_some_one string, sendResolved bool) error { tpl, _ := GetTpl(tplid) o := orm.NewOrm() @@ -61,10 +66,16 @@ func DelAlertRouter(id int) error { return err } -func GetAllAlertRouter() ([]*AlertRouter, error) { +func GetAllAlertRouter(query AlertRouterQuery) ([]*AlertRouter, error) { o := orm.NewOrm() Tpl_all := make([]*AlertRouter, 0) qs := o.QueryTable("AlertRouter") + if len(query.Name) > 0 { + qs = qs.Filter("name__icontains", query.Name) + } + if len(query.Webhook) > 0 { + qs = qs.Filter("url_or_phone__istartswith", query.Webhook) + } _, err := qs.RelatedSel().All(&Tpl_all) return Tpl_all, err } diff --git a/views/alertrouter.html b/views/alertrouter.html index 71822e37..72ce2ef4 100644 --- a/views/alertrouter.html +++ b/views/alertrouter.html @@ -1,160 +1,185 @@ {{template "main" .}} - - + - -
+ +
-
-
-
-

告警路由

-
- -
-
+
+
+
+

告警路由

+
+
+
+ 路由名称 + +
+ +
+
+
+ webhook + + +
+ +
+ +
+
- -
-
-

** 告警路由目前仅支持 Prometheus

- -
- - -
-
-
- - - - - - - - - - - - - - - - - - {{range $k,$v:= .AlertRouter}} - - - - - + + + + + + {{end}} + +
Id路由名称生效模版模版类型模版用途路由规则创建时间编辑删除
{{.Id}}{{$v.Name}}{{$v.Tpl.Tplname}} - {{if eq $v.Tpl.Tpltype "dd"}}钉钉{{end}} - {{if eq $v.Tpl.Tpltype "wx"}}企业微信{{end}} - {{if eq $v.Tpl.Tpltype "workwechat"}}企业微信应用{{end}} - {{if eq $v.Tpl.Tpltype "fs"}}飞书{{end}} - {{if eq $v.Tpl.Tpltype "webhook"}}WebHook{{end}} - {{if eq $v.Tpl.Tpltype "txdx"}}腾讯云短信{{end}} - {{if eq $v.Tpl.Tpltype "txdh"}}腾讯云电话{{end}} - {{if eq $v.Tpl.Tpltype "alydx"}}阿里云短信{{end}} - {{if eq $v.Tpl.Tpltype "alydh"}}阿里云电话{{end}} - {{if eq $v.Tpl.Tpltype "hwdx"}}华为云短信{{end}} - {{if eq $v.Tpl.Tpltype "bddx"}}百度云短信{{end}} - {{if eq $v.Tpl.Tpltype "rlydh"}}容联云电话{{end}} - {{if eq $v.Tpl.Tpltype "7moordx"}}七陌短信{{end}} - {{if eq $v.Tpl.Tpltype "7moordh"}}七陌语音电话{{end}} - {{if eq $v.Tpl.Tpltype "email"}}Email{{end}} - {{if eq $v.Tpl.Tpltype "tg"}}Telegram{{end}} - {{if eq $v.Tpl.Tpltype "rl"}}百度Hi(如流){{end}} - {{if eq $v.Tpl.Tpltype "bark"}}Bark(iPhone推送){{end}} - {{if eq $v.Tpl.Tpltype "voice"}}语音播报{{end}} - {{if eq $v.Tpl.Tpltype "fsapp"}}飞书机器人应用{{end}} + +
+
+

** 告警路由目前仅支持 Prometheus

+ +
+ + +
+
+
+ + + + + + + + + + + + + + + + + + {{range $k,$v:= .AlertRouter}} + + + + + - - - - - - - - - {{end}} - -
Id路由名称生效模版模版类型模版用途路由规则创建时间编辑删除
{{.Id}}{{$v.Name}}{{$v.Tpl.Tplname}} + {{if eq $v.Tpl.Tpltype "dd"}}钉钉{{end}} + {{if eq $v.Tpl.Tpltype "wx"}}企业微信{{end}} + {{if eq $v.Tpl.Tpltype "workwechat"}}企业微信应用{{end}} + {{if eq $v.Tpl.Tpltype "fs"}}飞书{{end}} + {{if eq $v.Tpl.Tpltype "webhook"}}WebHook{{end}} + {{if eq $v.Tpl.Tpltype "txdx"}}腾讯云短信{{end}} + {{if eq $v.Tpl.Tpltype "txdh"}}腾讯云电话{{end}} + {{if eq $v.Tpl.Tpltype "alydx"}}阿里云短信{{end}} + {{if eq $v.Tpl.Tpltype "alydh"}}阿里云电话{{end}} + {{if eq $v.Tpl.Tpltype "hwdx"}}华为云短信{{end}} + {{if eq $v.Tpl.Tpltype "bddx"}}百度云短信{{end}} + {{if eq $v.Tpl.Tpltype "rlydh"}}容联云电话{{end}} + {{if eq $v.Tpl.Tpltype "7moordx"}}七陌短信{{end}} + {{if eq $v.Tpl.Tpltype "7moordh"}}七陌语音电话{{end}} + {{if eq $v.Tpl.Tpltype "email"}}Email{{end}} + {{if eq $v.Tpl.Tpltype "tg"}}Telegram{{end}} + {{if eq $v.Tpl.Tpltype "rl"}}百度Hi(如流){{end}} + {{if eq $v.Tpl.Tpltype "bark"}}Bark(iPhone推送){{end}} + {{if eq $v.Tpl.Tpltype "voice"}}语音播报{{end}} + {{if eq $v.Tpl.Tpltype "fsapp"}}飞书机器人应用{{end}} {{$v.Tpl.Tpluse}} - {{$v.Rules}} - {{$v.Created.Format "2006-01-02 15:04:05 UTC"}} - - Edit - - - - Delete - -
+
+ {{$v.Rules}} + {{$v.Created.Format "2006-01-02 15:04:05 UTC"}} + + Edit + + + + Delete + +
+ +
+ + +
- - - -
- + -
- + + {{template "endhtml"}} {{template "jshtml"}} \ No newline at end of file