bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/models/silence.go (about)

     1  package models
     2  
     3  import (
     4  	"crypto/sha1"
     5  	"fmt"
     6  	"time"
     7  
     8  	"bosun.org/opentsdb"
     9  	"bosun.org/util"
    10  )
    11  
    12  type Silence struct {
    13  	Start, End time.Time
    14  	Alert      string
    15  	Tags       opentsdb.TagSet
    16  	TagString  string
    17  	Forget     bool
    18  	User       string
    19  	Message    string
    20  }
    21  
    22  func (s *Silence) Silenced(now time.Time, alert string, tags opentsdb.TagSet) bool {
    23  	if !s.ActiveAt(now) {
    24  		return false
    25  	}
    26  	return s.Matches(alert, tags)
    27  }
    28  
    29  func (s *Silence) ActiveAt(now time.Time) bool {
    30  	if now.Before(s.Start) || now.After(s.End) {
    31  		return false
    32  	}
    33  	return true
    34  }
    35  
    36  func (s *Silence) Matches(alert string, tags opentsdb.TagSet) bool {
    37  	if s.Alert != "" && s.Alert != alert {
    38  		return false
    39  	}
    40  	for k, pattern := range s.Tags {
    41  		tagv, ok := tags[k]
    42  		if !ok {
    43  			return false
    44  		}
    45  		matched, _ := util.Match(pattern, tagv)
    46  		if !matched {
    47  			return false
    48  		}
    49  	}
    50  	return true
    51  }
    52  
    53  func (s Silence) ID() string {
    54  	h := sha1.New()
    55  	fmt.Fprintf(h, "%s|%s|%s%s", s.Start, s.End, s.Alert, s.Tags)
    56  	return fmt.Sprintf("%x", h.Sum(nil))
    57  }