4
4
"encoding/json"
5
5
"fmt"
6
6
"net/http"
7
+ "regexp"
7
8
"strconv"
8
9
"strings"
9
10
"time"
@@ -13,6 +14,7 @@ import (
13
14
"github.com/ccfos/nightingale/v6/pushgw/writer"
14
15
15
16
"github.com/gin-gonic/gin"
17
+ "github.com/jinzhu/copier"
16
18
"github.com/prometheus/prometheus/prompb"
17
19
"github.com/toolkits/pkg/ginx"
18
20
"github.com/toolkits/pkg/i18n"
@@ -295,6 +297,17 @@ func (rt *Router) alertRulePutFields(c *gin.Context) {
295
297
continue
296
298
}
297
299
300
+ if f .Action == "update_triggers" {
301
+ if triggers , has := f .Fields ["triggers" ]; has {
302
+ originRule := ar .RuleConfigJson .(map [string ]interface {})
303
+ originRule ["triggers" ] = triggers
304
+ b , err := json .Marshal (originRule )
305
+ ginx .Dangerous (err )
306
+ ginx .Dangerous (ar .UpdateFieldsMap (rt .Ctx , map [string ]interface {}{"rule_config" : string (b )}))
307
+ continue
308
+ }
309
+ }
310
+
298
311
if f .Action == "annotations_add" {
299
312
if annotations , has := f .Fields ["annotations" ]; has {
300
313
annotationsMap := annotations .(map [string ]interface {})
@@ -499,3 +512,84 @@ func (rt *Router) relabelTest(c *gin.Context) {
499
512
500
513
ginx .NewRender (c ).Data (tags , nil )
501
514
}
515
+
516
+ type identListForm struct {
517
+ Ids []int64 `json:"ids"`
518
+ IdentList []string `json:"ident_list"`
519
+ }
520
+
521
+ func (rt * Router ) cloneToMachine (c * gin.Context ) {
522
+ var f identListForm
523
+ ginx .BindJSON (c , & f )
524
+
525
+ if len (f .IdentList ) == 0 {
526
+ ginx .Bomb (http .StatusBadRequest , "ident_list is empty" )
527
+ }
528
+
529
+ alertRules , err := models .AlertRuleGetsByIds (rt .Ctx , f .Ids )
530
+ ginx .Dangerous (err )
531
+
532
+ re := regexp .MustCompile ("ident = \" .*?\" " )
533
+
534
+ user := c .MustGet ("username" ).(string )
535
+ now := time .Now ().Unix ()
536
+
537
+ newRules := make ([]* models.AlertRule , 0 )
538
+
539
+ reterr := make (map [string ]map [string ]string )
540
+
541
+ for i := range alertRules {
542
+ reterr [alertRules [i ].Name ] = make (map [string ]string )
543
+
544
+ if alertRules [i ].Cate != "prometheus" {
545
+ reterr [alertRules [i ].Name ]["all" ] = "Only Prometheus rules can be cloned to machines"
546
+ continue
547
+ }
548
+ var ruleConfig * models.PromRuleConfig
549
+ if err := json .Unmarshal ([]byte (alertRules [i ].RuleConfig ), & ruleConfig ); err != nil {
550
+ reterr [alertRules [i ].Name ]["all" ] = "invalid rule, fail to unmarshal rule config"
551
+ continue
552
+ }
553
+
554
+ for j := range f .IdentList {
555
+
556
+ for q := range ruleConfig .Queries {
557
+ ruleConfig .Queries [q ].PromQl = re .ReplaceAllString (ruleConfig .Queries [q ].PromQl , fmt .Sprintf ("ident = \" %s\" " , f .IdentList [j ]))
558
+ }
559
+
560
+ configJson , err := json .Marshal (ruleConfig )
561
+ if err != nil {
562
+ reterr [alertRules [i ].Name ][f .IdentList [j ]] = fmt .Sprintf ("invalid rule, fail to marshal rule config, err: %s" , err )
563
+ continue
564
+ }
565
+
566
+ newRule := & models.AlertRule {}
567
+ if err := copier .Copy (newRule , alertRules [i ]); err != nil {
568
+ reterr [alertRules [i ].Name ][f .IdentList [j ]] = fmt .Sprintf ("fail to clone rule, err: %s" , err )
569
+ continue
570
+ }
571
+ newRule .Id = 0
572
+ newRule .Name = alertRules [i ].Name + "_" + f .IdentList [j ]
573
+ newRule .CreateBy = user
574
+ newRule .UpdateBy = user
575
+ newRule .UpdateAt = now
576
+ newRule .CreateAt = now
577
+ newRule .RuleConfig = string (configJson )
578
+
579
+ exist , err := models .AlertRuleExists (rt .Ctx , 0 , newRule .GroupId , newRule .DatasourceIdsJson , newRule .Name )
580
+ if err != nil {
581
+ reterr [alertRules [i ].Name ][f .IdentList [j ]] = err .Error ()
582
+ continue
583
+ }
584
+
585
+ if exist {
586
+ reterr [alertRules [i ].Name ][f .IdentList [j ]] = fmt .Sprintf ("rule already exists, ruleName: %s" , newRule .Name )
587
+ continue
588
+ }
589
+
590
+ newRules = append (newRules , newRule )
591
+ }
592
+ }
593
+
594
+ ginx .NewRender (c ).Data (reterr , models .InsertAlertRule (rt .Ctx , newRules ))
595
+ }
0 commit comments