/ notifier / opsgenie-notifier.go
opsgenie-notifier.go
  1  package notifier
  2  
  3  import (
  4  	"fmt"
  5  
  6  	"github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/opsgenie/opsgenie-go-sdk/alertsv2"
  7  	ogcli "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/opsgenie/opsgenie-go-sdk/client"
  8  
  9  	log "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
 10  )
 11  
 12  type OpsGenieNotifier struct {
 13  	Enabled     bool
 14  	ClusterName string `json:"cluster-name"`
 15  	ApiKey      string `json:"api-key"`
 16  }
 17  
 18  // NotifierName provides name for notifier selection
 19  func (opsgenie *OpsGenieNotifier) NotifierName() string {
 20  	return "opsgenie"
 21  }
 22  
 23  func (opsgenie *OpsGenieNotifier) Copy() Notifier {
 24  	notifier := *opsgenie
 25  	return &notifier
 26  }
 27  
 28  //Notify sends messages to the endpoint notifier
 29  func (opsgenie *OpsGenieNotifier) Notify(messages Messages) bool {
 30  
 31  	overallStatus, pass, warn, fail := messages.Summary()
 32  
 33  	client := new(ogcli.OpsGenieClient)
 34  	client.SetAPIKey(opsgenie.ApiKey)
 35  
 36  	alertCli, cliErr := client.AlertV2()
 37  
 38  	if cliErr != nil {
 39  		log.Println("Opsgenie notification trouble with client")
 40  		return false
 41  	}
 42  
 43  	ok := true
 44  	for _, message := range messages {
 45  		title := fmt.Sprintf("\n%s:%s:%s is %s.", message.Node, message.Service, message.Check, message.Status)
 46  		alias := opsgenie.createAlias(message)
 47  		content := fmt.Sprintf(header, opsgenie.ClusterName, overallStatus, fail, warn, pass)
 48  		content += fmt.Sprintf("\n%s:%s:%s is %s.", message.Node, message.Service, message.Check, message.Status)
 49  		content += fmt.Sprintf("\n%s", message.Output)
 50  
 51  		// create the alert
 52  		switch {
 53  		case message.IsCritical():
 54  			ok = opsgenie.createAlert(alertCli, title, content, alias) && ok
 55  		case message.IsWarning():
 56  			ok = opsgenie.createAlert(alertCli, title, content, alias) && ok
 57  		case message.IsPassing():
 58  			ok = opsgenie.closeAlert(alertCli, alias) && ok
 59  		default:
 60  			ok = false
 61  			log.Warn("Message was not either IsCritical, IsWarning or IsPasssing. No notification was sent for ", alias)
 62  		}
 63  	}
 64  	return ok
 65  }
 66  
 67  func (opsgenie OpsGenieNotifier) createAlias(message Message) string {
 68  	incidentKey := message.Node
 69  	if message.ServiceId != "" {
 70  		incidentKey += ":" + message.ServiceId
 71  	}
 72  
 73  	return incidentKey
 74  }
 75  
 76  func (opsgenie *OpsGenieNotifier) createAlert(alertCli *ogcli.OpsGenieAlertV2Client, message string, content string, alias string) bool {
 77  	log.Debug(fmt.Sprintf("OpsGenieAlertClient.CreateAlert alias: %s", alias))
 78  
 79  	req := alertsv2.CreateAlertRequest{
 80  		Message:     message,
 81  		Description: content,
 82  		Alias:       alias,
 83  		Source:      "consul",
 84  		Entity:      opsgenie.ClusterName,
 85  	}
 86  	response, alertErr := alertCli.Create(req)
 87  
 88  	if alertErr != nil {
 89  		if response == nil {
 90  			log.Warn("Opsgenie notification trouble. ", alertErr)
 91  		} else {
 92  			log.Warn("Opsgenie notification trouble. ", response.RequestID)
 93  		}
 94  		return false
 95  	}
 96  
 97  	log.Println("Opsgenie notification sent.")
 98  	return true
 99  }
100  
101  func (opsgenie *OpsGenieNotifier) closeAlert(alertCli *ogcli.OpsGenieAlertV2Client, alias string) bool {
102  	log.Debug(fmt.Sprintf("OpsGenieAlertClient.CloseAlert alias: %s", alias))
103  
104  	identifier := alertsv2.Identifier{
105  		Alias: alias,
106  	}
107  
108  	req := alertsv2.CloseRequest{
109  		Identifier: &identifier,
110  		Source:     "consul",
111  	}
112  	response, alertErr := alertCli.Close(req)
113  
114  	if alertErr != nil {
115  		if response == nil {
116  			log.Warn("Opsgenie notification trouble. ", alertErr)
117  		} else {
118  			log.Warn("Opsgenie notification trouble. ", response.RequestID)
119  		}
120  		return false
121  	}
122  
123  	log.Println("Opsgenie close alert sent.")
124  	return true
125  }