Skip to content

Commit

Permalink
Merge pull request #457 from wppzxc/master
Browse files Browse the repository at this point in the history
告警路由中的@人增加按天轮询的功能
  • Loading branch information
feiyu563 authored Nov 18, 2024
2 parents 120f0f3 + 9129da0 commit 761bc64
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
5 changes: 3 additions & 2 deletions controllers/WebAlertRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type AlertRouterJson struct {
RouterTplId string
RouterPurl string
RouterPat string
RouterPatRR bool
RouterSendResolved bool
Rules []LabelMap
}
Expand All @@ -73,11 +74,11 @@ func (c *MainController) AddRouter() {
rules, err := json.Marshal(WebAlertRouterJson.Rules)
if WebAlertRouterJson.RouterId == "" {
tpl_id_int, _ := strconv.Atoi(WebAlertRouterJson.RouterTplId)
err = models.AddAlertRouter(0, tpl_id_int, WebAlertRouterJson.RouterName, string(rules), WebAlertRouterJson.RouterPurl, WebAlertRouterJson.RouterPat, WebAlertRouterJson.RouterSendResolved)
err = models.AddAlertRouter(0, tpl_id_int, WebAlertRouterJson.RouterName, string(rules), WebAlertRouterJson.RouterPurl, WebAlertRouterJson.RouterPat, WebAlertRouterJson.RouterPatRR, WebAlertRouterJson.RouterSendResolved)
} else {
id, _ := strconv.Atoi(WebAlertRouterJson.RouterId)
tpl_id_int, _ := strconv.Atoi(WebAlertRouterJson.RouterTplId)
err = models.UpdateAlertRouter(id, tpl_id_int, WebAlertRouterJson.RouterName, string(rules), WebAlertRouterJson.RouterPurl, WebAlertRouterJson.RouterPat, WebAlertRouterJson.RouterSendResolved)
err = models.UpdateAlertRouter(id, tpl_id_int, WebAlertRouterJson.RouterName, string(rules), WebAlertRouterJson.RouterPurl, WebAlertRouterJson.RouterPat, WebAlertRouterJson.RouterPatRR, WebAlertRouterJson.RouterSendResolved)
}
var resp interface{}
resp = err
Expand Down
18 changes: 15 additions & 3 deletions controllers/prometheusalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,30 @@ func AlertRouterSet(xalert map[string]interface{}, PMsg PrometheusAlertMsg, Tpl
if rules_num == rules_num_match {
PMsg.Type = router_value.Tpl.Tpltype
PMsg.Tpl = router_value.Tpl.Tpl
atSomeOne := router_value.AtSomeOne
if router_value.AtSomeOneRR {
openIds := strings.Split(router_value.AtSomeOne, ",")
if len(openIds) > 1 {
// 用自1970年1月1日以来的天数取余计算
duration := time.Since(time.Unix(0, 0))
days := duration.Hours() / 24
i := int(days) % len(openIds)
atSomeOne = openIds[i]
}
}

switch router_value.Tpl.Tpltype {
case "wx":
PMsg.Wxurl = router_value.UrlOrPhone
PMsg.AtSomeOne = router_value.AtSomeOne
PMsg.AtSomeOne = atSomeOne
//钉钉渠道
case "dd":
PMsg.Ddurl = router_value.UrlOrPhone
PMsg.AtSomeOne = router_value.AtSomeOne
PMsg.AtSomeOne = atSomeOne
//飞书渠道
case "fs":
PMsg.Fsurl = router_value.UrlOrPhone
PMsg.AtSomeOne = router_value.AtSomeOne
PMsg.AtSomeOne = atSomeOne
//Webhook渠道
case "webhook":
PMsg.WebHookUrl = router_value.UrlOrPhone
Expand Down
7 changes: 5 additions & 2 deletions models/AlertRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type AlertRouter struct {
Rules string
UrlOrPhone string
AtSomeOne string
AtSomeOneRR bool
SendResolved bool
Created time.Time
}
Expand All @@ -21,7 +22,7 @@ type AlertRouterQuery struct {
Webhook string
}

func AddAlertRouter(id int, tplid int, name, rules, url_or_phone, at_some_one string, sendResolved bool) error {
func AddAlertRouter(id int, tplid int, name, rules, url_or_phone, at_some_one string, at_some_one_rr bool, sendResolved bool) error {
tpl, _ := GetTpl(tplid)
o := orm.NewOrm()
AlertRouter_table := &AlertRouter{
Expand All @@ -31,6 +32,7 @@ func AddAlertRouter(id int, tplid int, name, rules, url_or_phone, at_some_one st
Rules: rules,
UrlOrPhone: url_or_phone,
AtSomeOne: at_some_one,
AtSomeOneRR: at_some_one_rr,
SendResolved: sendResolved,
Created: time.Now(),
}
Expand All @@ -39,7 +41,7 @@ func AddAlertRouter(id int, tplid int, name, rules, url_or_phone, at_some_one st
return err
}

func UpdateAlertRouter(id int, tplid int, name, rules, url_or_phone, at_some_one string, sendResolved bool) error {
func UpdateAlertRouter(id int, tplid int, name, rules, url_or_phone, at_some_one string, at_some_one_rr bool, sendResolved bool) error {
tpl, _ := GetTpl(tplid)
o := orm.NewOrm()
router_update := &AlertRouter{Id: id}
Expand All @@ -51,6 +53,7 @@ func UpdateAlertRouter(id int, tplid int, name, rules, url_or_phone, at_some_one
router_update.Rules = rules
router_update.UrlOrPhone = url_or_phone
router_update.AtSomeOne = at_some_one
router_update.AtSomeOneRR = at_some_one_rr
router_update.SendResolved = sendResolved
router_update.Created = time.Now()
_, err := o.Update(router_update)
Expand Down
19 changes: 18 additions & 1 deletion views/alertrouter_add.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ <h3 class="card-title"></h3>
</div>
</div>

<div id="Pat" class="col-sm-6">
<div id="Pat" class="col-sm-5">
<!-- text input -->
<div class="form-group">
<label id="typestr">@某人</label>
Expand All @@ -163,6 +163,13 @@ <h3 class="card-title"></h3>
</div>
</div>

<div id="Patrr" class="col-sm-1">
<div class="form-check">
<input id="patrr" class="form-check-input" name="patrr" type="checkbox">
<label class="form-check-label">轮询@某人</label>
</div>
</div>

<div id="Party" class="col-sm-6" style="display:none">
<!-- text input -->
<div class="form-group">
Expand Down Expand Up @@ -236,48 +243,56 @@ <h3 class="card-title"></h3>
typeStr.innerText="钉钉机器人地址(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="";
document.getElementById("Patrr").style.display="";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="wx"){
typeStr.innerText="微信机器人地址(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="";
document.getElementById("Patrr").style.display="";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="fs"){
typeStr.innerText="飞书机器人地址(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="";
document.getElementById("Patrr").style.display="";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="fsapp"){
typeStr.innerText="飞书 用户open_id、user_id、union_ids、部门open_department_id";
document.getElementById("Purl").style.display="none";
document.getElementById("Pat").style.display="";
document.getElementById("Patrr").style.display="";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="voice"){
typeStr.innerText="";
document.getElementById("Purl").style.display="none";
document.getElementById("Pat").style.display="none";
document.getElementById("Patrr").style.display="none";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="webhook"){
typeStr.innerText="webhook地址(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="none";
document.getElementById("Patrr").style.display="none";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="email"){
typeStr.innerText="邮箱(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="none";
document.getElementById("Patrr").style.display="none";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="rl"){
typeStr.innerText="百度Hi(如流)群id";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="none";
document.getElementById("Patrr").style.display="none";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="workwechat"){
Expand All @@ -291,6 +306,7 @@ <h3 class="card-title"></h3>
typeStr.innerText="手机号(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="none";
document.getElementById("Patrr").style.display="none";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
}
Expand Down Expand Up @@ -328,6 +344,7 @@ <h3 class="card-title"></h3>
RouterTplId: $('#tpl_id').val(),
RouterPurl: $('#purl').val(),
RouterPat: $('#pat').val(),
RouterPatRR: $("#patrr").is(":checked"),
RouterSendResolved: send_resolved,
Rules: RouterRules
};
Expand Down
28 changes: 27 additions & 1 deletion views/alertrouter_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ <h3 class="card-title"></h3>
</div>
</div>

<div id="Pat" class="col-sm-6">
<div id="Pat" class="col-sm-5">
<!-- text input -->
<div class="form-group">
<label id="typestr">@某人</label>
Expand All @@ -127,6 +127,13 @@ <h3 class="card-title"></h3>
</div>
</div>

<div id="Patrr" class="col-sm-1">
<div class="form-check">
<input id="patrr" class="form-check-input" name="patrr" type="checkbox">
<label class="form-check-label">轮询@某人</label>
</div>
</div>

<div id="Party" class="col-sm-6" style="display:none">
<!-- text input -->
<div class="form-group">
Expand Down Expand Up @@ -173,6 +180,7 @@ <h3 class="card-title"></h3>
typeChange();
loadRules();
loadSendResolved();
loadAtSomeOneRR();
$('#labeladd').click(function(){
var labeldiv='<div class="row"> <div class="col-3"> <div class="form-group"> <label>Label Name</label> <input type="text" id="rules" class="form-control" name="rules" value="" placeholder="规则如 app=nginx,env=test"> </div> </div> <div class="col-3"> <div class="form-group"> <label>Label Value</label> <input type="text" id="rulesvalue" class="form-control" name="rulesvalue" value="" placeholder="规则如 app=nginx,env=test"> </div> </div> <div class="col-1 d-flex align-items-center"> <div class="form-check"> <input id="rulesregex" class="form-check-input" name="rulesregex" type="checkbox"> <label class="form-check-label">Regex</label> </div> </div> <div class="col-sm-1 d-flex align-items-center"> <div class="form-group"> <button id="labeldel" type="button" class="btn btn-danger btn-flat"><i class="fa fa-trash"></i></button> </div> </div></div> <br />';
$('#labelx').append(labeldiv)
Expand Down Expand Up @@ -212,6 +220,14 @@ <h3 class="card-title"></h3>

}

function loadAtSomeOneRR(){
var checkbox = document.getElementById('patrr');
var atSomeOneRR={{.AlertRouter.AtSomeOneRR}};
if (atSomeOneRR === true) {
checkbox.checked = true
}
}

function typeChange(){
var tpl_id = document.getElementById("tpl_id");
var tpltype = document.getElementById("tpltype");
Expand All @@ -222,48 +238,56 @@ <h3 class="card-title"></h3>
typeStr.innerText="钉钉机器人地址(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="";
document.getElementById("Patrr").style.display="";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="wx"){
typeStr.innerText="微信机器人地址(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="";
document.getElementById("Patrr").style.display="";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="fs"){
typeStr.innerText="飞书机器人地址(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="";
document.getElementById("Patrr").style.display="";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="fsapp"){
typeStr.innerText="飞书 用户open_id、user_id、union_ids、部门open_department_id";
document.getElementById("Purl").style.display="none";
document.getElementById("Pat").style.display="";
document.getElementById("Patrr").style.display="";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="voice"){
typeStr.innerText="";
document.getElementById("Purl").style.display="none";
document.getElementById("Pat").style.display="none";
document.getElementById("Patrr").style.display="none";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="webhook"){
typeStr.innerText="webhook地址(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="none";
document.getElementById("Patrr").style.display="none";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="email"){
typeStr.innerText="邮箱(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="none";
document.getElementById("Patrr").style.display="none";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="rl"){
typeStr.innerText="百度Hi(如流)群id";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="none";
document.getElementById("Patrr").style.display="none";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
} else if(tpltype.value=="workwechat"){
Expand All @@ -277,6 +301,7 @@ <h3 class="card-title"></h3>
typeStr.innerText="手机号(多个请以,隔开)";
document.getElementById("Purl").style.display="";
document.getElementById("Pat").style.display="none";
document.getElementById("Patrr").style.display="none";
document.getElementById("Party").style.display="none";
document.getElementById("Tag").style.display="none";
}
Expand Down Expand Up @@ -314,6 +339,7 @@ <h3 class="card-title"></h3>
RouterTplId: $('#tpl_id').val(),
RouterPurl: $('#purl').val(),
RouterPat: $('#pat').val(),
RouterPatRR: $("#patrr").is(":checked"),
RouterSendResolved: send_resolved,
Rules: RouterRules
};
Expand Down

0 comments on commit 761bc64

Please sign in to comment.