notify.go raw

   1  package monitor
   2  
   3  // NotifyList wraps notifications.
   4  type NotifyList struct {
   5  	ID            string          `json:"id,omitempty"`
   6  	Name          string          `json:"name,omitempty"`
   7  	Notifications []*Notification `json:"notify_list,omitempty"`
   8  }
   9  
  10  // Notification represents endpoint to alert to.
  11  type Notification struct {
  12  	Type   string `json:"type,omitempty"`
  13  	Config Config `json:"config,omitempty"`
  14  }
  15  
  16  // NewNotifyList returns a notify list that alerts via the given notifications.
  17  func NewNotifyList(name string, nl ...*Notification) *NotifyList {
  18  	if nl == nil {
  19  		nl = []*Notification{}
  20  	}
  21  
  22  	return &NotifyList{Name: name, Notifications: nl}
  23  }
  24  
  25  // NewUserNotification returns a notification that alerts via user.
  26  func NewUserNotification(username string) *Notification {
  27  	return &Notification{
  28  		Type:   "user",
  29  		Config: Config{"user": username}}
  30  }
  31  
  32  // NewEmailNotification returns a notification that alerts via email.
  33  func NewEmailNotification(email string) *Notification {
  34  	return &Notification{
  35  		Type:   "email",
  36  		Config: Config{"email": email}}
  37  }
  38  
  39  // NewFeedNotification returns a notification that alerts via datafeed.
  40  func NewFeedNotification(sourceID string) *Notification {
  41  	return &Notification{
  42  		Type:   "datafeed",
  43  		Config: Config{"sourceid": sourceID}}
  44  }
  45  
  46  // NewWebNotification returns a notification that alerts via webhook.
  47  func NewWebNotification(url string, headers map[string]string) *Notification {
  48  	return &Notification{
  49  		Type:   "webhook",
  50  		Config: Config{"url": url, "headers": headers}}
  51  }
  52  
  53  // NewPagerDutyNotification returns a notification that alerts via pagerduty.
  54  func NewPagerDutyNotification(key string) *Notification {
  55  	return &Notification{
  56  		Type:   "pagerduty",
  57  		Config: Config{"service_key": key}}
  58  }
  59  
  60  // NewHipChatNotification returns a notification that alerts via hipchat.
  61  func NewHipChatNotification(token, room string) *Notification {
  62  	return &Notification{
  63  		Type:   "hipchat",
  64  		Config: Config{"token": token, "room": room}}
  65  }
  66  
  67  // NewSlackNotification returns a notification that alerts via slack.
  68  func NewSlackNotification(url, username, channel string) *Notification {
  69  	return &Notification{
  70  		Type:   "slack",
  71  		Config: Config{"url": url, "username": username, "channel": channel}}
  72  }
  73