func (opsgenie *OpsGenieNotifier) closeAlert(alertCli *ogcli.OpsGenieAlertClient, alias string) bool {
	log.Debug(fmt.Sprintf("OpsGenieAlertClient.CloseAlert alias: %s", alias))
	req := alerts.CloseAlertRequest{
		Alias:  alias,
		Source: "consul",
	}
	response, alertErr := alertCli.Close(req)

	if alertErr != nil {
		if response == nil {
			log.Warn("Opsgenie notification trouble. ", alertErr)
		} else {
			log.Warn("Opsgenie notification trouble. ", response.Status)
		}
		return false
	}

	log.Println("Opsgenie close alert sent.")
	return true
}
func (opsgenie *OpsGenieNotifier) createAlert(alertCli *ogcli.OpsGenieAlertClient, message string, content string, alias string) bool {
	log.Debug(fmt.Sprintf("OpsGenieAlertClient.CreateAlert alias: %s", alias))

	req := alerts.CreateAlertRequest{
		Message:     message,
		Description: content,
		Alias:       alias,
		Source:      "consul",
		Entity:      opsgenie.ClusterName,
	}
	response, alertErr := alertCli.Create(req)

	if alertErr != nil {
		if response == nil {
			log.Warn("Opsgenie notification trouble. ", alertErr)
		} else {
			log.Warn("Opsgenie notification trouble. ", response.Status)
		}
		return false
	}

	log.Println("Opsgenie notification sent.")
	return true
}
//Notify sends messages to the endpoint notifier
func (opsgenie *OpsGenieNotifier) Notify(messages Messages) bool {

	overallStatus, pass, warn, fail := messages.Summary()

	client := new(ogcli.OpsGenieClient)
	client.SetApiKey(opsgenie.ApiKey)

	alertCli, cliErr := client.Alert()

	if cliErr != nil {
		log.Println("Opsgenie notification trouble with client")
		return false
	}

	ok := true
	for _, message := range messages {
		title := fmt.Sprintf("\n%s:%s:%s is %s.", message.Node, message.Service, message.Check, message.Status)
		alias := opsgenie.createAlias(message)
		content := fmt.Sprintf(header, opsgenie.ClusterName, overallStatus, fail, warn, pass)
		content += fmt.Sprintf("\n%s:%s:%s is %s.", message.Node, message.Service, message.Check, message.Status)
		content += fmt.Sprintf("\n%s", message.Output)

		// create the alert
		switch {
		case message.IsCritical():
			ok = opsgenie.createAlert(alertCli, title, content, alias) && ok
		case message.IsWarning():
			ok = opsgenie.createAlert(alertCli, title, content, alias) && ok
		case message.IsPassing():
			ok = opsgenie.closeAlert(alertCli, alias) && ok
		default:
			ok = false
			log.Warn("Message was not either IsCritical, IsWarning or IsPasssing. No notification was sent for ", alias)
		}
	}
	return ok
}
// Notify sends messages to the endpoint notifier
func (vo *VictorOpsNotifier) Notify(messages Messages) bool {
	endpoint := fmt.Sprintf(apiEndpointTemplate, vo.APIKey, vo.RoutingKey)

	ok := true

	for _, message := range messages {
		entityID := fmt.Sprintf("%s:", message.Node)
		entityDisplayName := entityID

		// This might be a node level check without an explicit service
		if message.ServiceId == "" {
			entityID += message.CheckId
			entityDisplayName += message.Check
		} else {
			entityID += message.ServiceId
			entityDisplayName += message.Service
		}

		var messageType string

		switch {
		case message.IsCritical():
			messageType = "CRITICAL"
		case message.IsWarning():
			messageType = "WARNING"
		case message.IsPassing():
			messageType = "RECOVERY"
		default:
			log.Warn(fmt.Sprintf("Message with status %s was neither critical, warning, nor passing, reporting to VictorOps as INFO", message.Status))
			messageType = "INFO"
		}

		// VictorOps automatically displays the entity display name in notifications and page SMSs / emails,
		// so for brevity we don't repeat it in the "StateMessage" field
		stateMessage := fmt.Sprintf("%s: %s\n%s", messageType, message.Notes, message.Output)

		event := VictorOpsEvent{
			MessageType:       messageType,
			EntityID:          entityID,
			Timestamp:         uint32(message.Timestamp.Unix()),
			StateMessage:      stateMessage,
			MonitoringTool:    monitoringToolName,
			EntityDisplayName: entityDisplayName,

			HostName:    message.Node,
			MonitorName: message.Check,

			ConsulNode:      message.Node,
			ConsulService:   message.Service,
			ConsulServiceID: message.ServiceId,
			ConsulCheck:     message.Check,
			ConsulCheckID:   message.CheckId,
		}

		eventJSON, jsonError := json.Marshal(event)

		if jsonError != nil {
			ok = false
			log.Error("Error JSON-ifying VictorOps alert. ", jsonError)
			continue
		}

		response, httpError := http.Post(endpoint, "application/json", bytes.NewBuffer(eventJSON))

		if httpError != nil {
			ok = false
			log.Error("Error hitting VictorOps API. ", httpError)
			continue
		}

		if response.StatusCode != 200 {
			ok = false
			log.Error(fmt.Sprintf("Expected VictorOps endpoint to return 200, but it returned %d"), response.StatusCode)
			continue
		}
	}

	log.Println("VictorOps notification sent.")
	return ok
}